ESPHome  2024.10.2
ssd1327_base.cpp
Go to the documentation of this file.
1 #include "ssd1327_base.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 namespace ssd1327_base {
7 
8 static const char *const TAG = "ssd1327";
9 
10 static const uint8_t SSD1327_MAX_CONTRAST = 127;
11 static const uint8_t SSD1327_COLORMASK = 0x0f;
12 static const uint8_t SSD1327_COLORSHIFT = 4;
13 static const uint8_t SSD1327_PIXELSPERBYTE = 2;
14 
15 static const uint8_t SSD1327_SETCOLUMNADDRESS = 0x15;
16 static const uint8_t SSD1327_SETROWADDRESS = 0x75;
17 static const uint8_t SSD1327_SETCONTRAST = 0x81;
18 static const uint8_t SSD1327_SETREMAP = 0xA0;
19 static const uint8_t SSD1327_SETSTARTLINE = 0xA1;
20 static const uint8_t SSD1327_SETOFFSET = 0xA2;
21 static const uint8_t SSD1327_NORMALDISPLAY = 0xA4;
22 static const uint8_t SSD1327_DISPLAYALLON = 0xA5;
23 static const uint8_t SSD1327_DISPLAYALLOFF = 0xA6;
24 static const uint8_t SSD1327_INVERTDISPLAY = 0xA7;
25 static const uint8_t SSD1327_SETMULTIPLEX = 0xA8;
26 static const uint8_t SSD1327_FUNCTIONSELECTIONA = 0xAB;
27 static const uint8_t SSD1327_DISPLAYOFF = 0xAE;
28 static const uint8_t SSD1327_DISPLAYON = 0xAF;
29 static const uint8_t SSD1327_SETPHASELENGTH = 0xB1;
30 static const uint8_t SSD1327_SETFRONTCLOCKDIVIDER = 0xB3;
31 static const uint8_t SSD1327_SETGPIO = 0xB5;
32 static const uint8_t SSD1327_SETSECONDPRECHARGEPERIOD = 0xB6;
33 static const uint8_t SSD1327_SETGRAYSCALETABLE = 0xB8;
34 static const uint8_t SSD1327_SELECTDEFAULTLINEARGRAYSCALETABLE = 0xB9;
35 static const uint8_t SSD1327_SETPRECHARGEVOLTAGE = 0xBC;
36 static const uint8_t SSD1327_SETVCOMHVOLTAGE = 0xBE;
37 static const uint8_t SSD1327_FUNCTIONSELECTIONB = 0xD5;
38 static const uint8_t SSD1327_SETCOMMANDLOCK = 0xFD;
39 static const uint8_t SSD1327_HORIZONTALSCROLLRIGHTSETUP = 0x26;
40 static const uint8_t SSD1327_HORIZONTALSCROLLLEFTSETUP = 0x27;
41 static const uint8_t SSD1327_DEACTIVATESCROLL = 0x2E;
42 static const uint8_t SSD1327_ACTIVATESCROLL = 0x2F;
43 
45  this->init_internal_(this->get_buffer_length_());
46 
47  this->turn_off(); // display OFF
48  this->command(SSD1327_SETFRONTCLOCKDIVIDER); // set osc division
49  this->command(0xF1); // 145
50  this->command(SSD1327_SETMULTIPLEX); // multiplex ratio
51  this->command(0x7f); // duty = height - 1
52  this->command(SSD1327_SETOFFSET); // set display offset
53  this->command(0x00); // 0
54  this->command(SSD1327_SETSTARTLINE); // set start line
55  this->command(0x00); // ...
56  this->command(SSD1327_SETREMAP); // set segment remapping
57  this->command(0x53); // COM bottom-up, split odd/even, enable column and nibble remapping
58  this->command(SSD1327_SETGRAYSCALETABLE);
59  // gamma ~2.2
60  this->command(0);
61  this->command(1);
62  this->command(2);
63  this->command(3);
64  this->command(6);
65  this->command(8);
66  this->command(12);
67  this->command(16);
68  this->command(20);
69  this->command(26);
70  this->command(32);
71  this->command(39);
72  this->command(46);
73  this->command(54);
74  this->command(63);
75  this->command(SSD1327_SETPHASELENGTH);
76  this->command(0x55);
77  this->command(SSD1327_SETVCOMHVOLTAGE); // Set High Voltage Level of COM Pin
78  this->command(0x1C);
79  this->command(SSD1327_SETGPIO); // Switch voltage converter on (for Aliexpress display)
80  this->command(0x03);
81  this->command(SSD1327_NORMALDISPLAY); // set display mode
83  this->fill(Color::BLACK); // clear display - ensures we do not see garbage at power-on
84  this->display(); // ...write buffer, which actually clears the display's memory
85  this->turn_on(); // display ON
86 }
88  this->command(SSD1327_SETCOLUMNADDRESS); // set column address
89  this->command(0x00); // set column start address
90  this->command(0x3F); // set column end address
91  this->command(SSD1327_SETROWADDRESS); // set row address
92  this->command(0x00); // set row start address
93  this->command(127); // set last row
94 
95  this->write_display_data();
96 }
98  if (!this->is_failed()) {
99  this->do_update_();
100  this->display();
101  }
102 }
103 void SSD1327::set_brightness(float brightness) {
104  // validation
105  this->brightness_ = clamp(brightness, 0.0F, 1.0F);
106  // now write the new brightness level to the display
107  this->command(SSD1327_SETCONTRAST);
108  this->command(int(SSD1327_MAX_CONTRAST * (this->brightness_)));
109 }
110 bool SSD1327::is_on() { return this->is_on_; }
112  this->command(SSD1327_DISPLAYON);
113  this->is_on_ = true;
114 }
116  this->command(SSD1327_DISPLAYOFF);
117  this->is_on_ = false;
118 }
120  switch (this->model_) {
122  return 128;
123  default:
124  return 0;
125  }
126 }
128  switch (this->model_) {
130  return 128;
131  default:
132  return 0;
133  }
134 }
136  return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / SSD1327_PIXELSPERBYTE;
137 }
138 void HOT SSD1327::draw_absolute_pixel_internal(int x, int y, Color color) {
139  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
140  return;
141  uint32_t color4 = display::ColorUtil::color_to_grayscale4(color);
142  // where should the bits go in the big buffer array? math...
143  uint16_t pos = (x / SSD1327_PIXELSPERBYTE) + (y * this->get_width_internal() / SSD1327_PIXELSPERBYTE);
144  uint8_t shift = (x % SSD1327_PIXELSPERBYTE) * SSD1327_COLORSHIFT;
145  // ensure 'color4' is valid (only 4 bits aka 1 nibble) and shift the bits left when necessary
146  color4 = (color4 & SSD1327_COLORMASK) << shift;
147  // first mask off the nibble we must change...
148  this->buffer_[pos] &= (~SSD1327_COLORMASK >> shift);
149  // ...then lay the new nibble back on top. done!
150  this->buffer_[pos] |= color4;
151 }
152 void SSD1327::fill(Color color) {
153  const uint32_t color4 = display::ColorUtil::color_to_grayscale4(color);
154  uint8_t fill = (color4 & SSD1327_COLORMASK) | ((color4 & SSD1327_COLORMASK) << SSD1327_COLORSHIFT);
155  for (uint32_t i = 0; i < this->get_buffer_length_(); i++)
156  this->buffer_[i] = fill;
157 }
159  if (this->reset_pin_ != nullptr) {
160  this->reset_pin_->setup();
161  this->reset_pin_->digital_write(true);
162  delay(1);
163  // Trigger Reset
164  this->reset_pin_->digital_write(false);
165  delay(10);
166  // Wake up
167  this->reset_pin_->digital_write(true);
168  }
169 }
170 const char *SSD1327::model_str_() {
171  switch (this->model_) {
173  return "SSD1327 128x128";
174  default:
175  return "Unknown";
176  }
177 }
178 
179 } // namespace ssd1327_base
180 } // namespace esphome
virtual void digital_write(bool value)=0
bool is_failed() const
Definition: component.cpp:143
uint16_t x
Definition: tt21100.cpp:17
void draw_absolute_pixel_internal(int x, int y, Color color) override
virtual void setup()=0
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:92
uint16_t y
Definition: tt21100.cpp:18
void set_brightness(float brightness)
void init_internal_(uint32_t buffer_length)
static uint32_t color_to_grayscale4(Color color)
virtual void write_display_data()=0
virtual void command(uint8_t value)=0
void fill(Color color) override
static const Color BLACK
Definition: color.h:168
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26