ESPHome  2024.11.1
ssd1325_base.cpp
Go to the documentation of this file.
1 #include "ssd1325_base.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 namespace ssd1325_base {
7 
8 static const char *const TAG = "ssd1325";
9 
10 static const uint8_t SSD1325_MAX_CONTRAST = 127;
11 static const uint8_t SSD1325_COLORMASK = 0x0f;
12 static const uint8_t SSD1325_COLORSHIFT = 4;
13 static const uint8_t SSD1325_PIXELSPERBYTE = 2;
14 
15 static const uint8_t SSD1325_SETCOLADDR = 0x15;
16 static const uint8_t SSD1325_SETROWADDR = 0x75;
17 static const uint8_t SSD1325_SETCONTRAST = 0x81;
18 static const uint8_t SSD1325_SETCURRENT = 0x84;
19 
20 static const uint8_t SSD1325_SETREMAP = 0xA0;
21 static const uint8_t SSD1325_SETSTARTLINE = 0xA1;
22 static const uint8_t SSD1325_SETOFFSET = 0xA2;
23 static const uint8_t SSD1325_NORMALDISPLAY = 0xA4;
24 static const uint8_t SSD1325_DISPLAYALLON = 0xA5;
25 static const uint8_t SSD1325_DISPLAYALLOFF = 0xA6;
26 static const uint8_t SSD1325_INVERTDISPLAY = 0xA7;
27 static const uint8_t SSD1325_SETMULTIPLEX = 0xA8;
28 static const uint8_t SSD1325_MASTERCONFIG = 0xAD;
29 static const uint8_t SSD1325_DISPLAYOFF = 0xAE;
30 static const uint8_t SSD1325_DISPLAYON = 0xAF;
31 
32 static const uint8_t SSD1325_SETPRECHARGECOMPENABLE = 0xB0;
33 static const uint8_t SSD1325_SETPHASELEN = 0xB1;
34 static const uint8_t SSD1325_SETROWPERIOD = 0xB2;
35 static const uint8_t SSD1325_SETCLOCK = 0xB3;
36 static const uint8_t SSD1325_SETPRECHARGECOMP = 0xB4;
37 static const uint8_t SSD1325_SETGRAYTABLE = 0xB8;
38 static const uint8_t SSD1325_SETDEFAULTGRAYTABLE = 0xB9;
39 static const uint8_t SSD1325_SETPRECHARGEVOLTAGE = 0xBC;
40 static const uint8_t SSD1325_SETVCOMLEVEL = 0xBE;
41 static const uint8_t SSD1325_SETVSL = 0xBF;
42 
43 static const uint8_t SSD1325_GFXACCEL = 0x23;
44 static const uint8_t SSD1325_DRAWRECT = 0x24;
45 static const uint8_t SSD1325_COPY = 0x25;
46 
48  this->init_internal_(this->get_buffer_length_());
49 
50  this->command(SSD1325_DISPLAYOFF); // display off
51  this->command(SSD1325_SETCLOCK); // set osc division
52  this->command(0xF1); // 145
53  this->command(SSD1325_SETMULTIPLEX); // multiplex ratio
54  if (this->model_ == SSD1327_MODEL_128_128) {
55  this->command(0x7f); // duty = height - 1
56  } else {
57  this->command(0x3f); // duty = 1/64
58  }
59  this->command(SSD1325_SETOFFSET); // set display offset
60  if (this->model_ == SSD1327_MODEL_128_128) {
61  this->command(0x00); // 0
62  } else {
63  this->command(0x4C); // 76
64  }
65  this->command(SSD1325_SETSTARTLINE); // set start line
66  this->command(0x00); // ...
67  this->command(SSD1325_MASTERCONFIG); // Set Master Config DC/DC Converter
68  this->command(0x02);
69  this->command(SSD1325_SETREMAP); // set segment remapping
70  if (this->model_ == SSD1327_MODEL_128_128) {
71  this->command(0x53); // COM bottom-up, split odd/even, enable column and nibble remapping
72  } else {
73  this->command(0x50); // COM bottom-up, split odd/even
74  }
75  this->command(SSD1325_SETCURRENT + 0x2); // Set Full Current Range
76  this->command(SSD1325_SETGRAYTABLE);
77  // gamma ~2.2
78  if (this->model_ == SSD1327_MODEL_128_128) {
79  this->command(0);
80  this->command(1);
81  this->command(2);
82  this->command(3);
83  this->command(6);
84  this->command(8);
85  this->command(12);
86  this->command(16);
87  this->command(20);
88  this->command(26);
89  this->command(32);
90  this->command(39);
91  this->command(46);
92  this->command(54);
93  this->command(63);
94  } else {
95  this->command(0x01);
96  this->command(0x11);
97  this->command(0x22);
98  this->command(0x32);
99  this->command(0x43);
100  this->command(0x54);
101  this->command(0x65);
102  this->command(0x76);
103  }
104  this->command(SSD1325_SETROWPERIOD);
105  this->command(0x51);
106  this->command(SSD1325_SETPHASELEN);
107  this->command(0x55);
108  this->command(SSD1325_SETPRECHARGECOMP);
109  this->command(0x02);
110  this->command(SSD1325_SETPRECHARGECOMPENABLE);
111  this->command(0x28);
112  this->command(SSD1325_SETVCOMLEVEL); // Set High Voltage Level of COM Pin
113  this->command(0x1C);
114  this->command(SSD1325_SETVSL); // set Low Voltage Level of SEG Pin
115  this->command(0x0D | 0x02);
116  this->command(SSD1325_NORMALDISPLAY); // set display mode
118  this->fill(Color::BLACK); // clear display - ensures we do not see garbage at power-on
119  this->display(); // ...write buffer, which actually clears the display's memory
120  this->turn_on(); // display ON
121 }
123  this->command(SSD1325_SETCOLADDR); // set column address
124  this->command(0x00); // set column start address
125  this->command(0x3F); // set column end address
126  this->command(SSD1325_SETROWADDR); // set row address
127  this->command(0x00); // set row start address
128  if (this->model_ == SSD1327_MODEL_128_128) {
129  this->command(127); // set last row
130  } else {
131  this->command(63); // set last row
132  }
133 
134  this->write_display_data();
135 }
137  this->do_update_();
138  this->display();
139 }
140 void SSD1325::set_brightness(float brightness) {
141  // validation
142  if (brightness > 1) {
143  this->brightness_ = 1.0;
144  } else if (brightness < 0) {
145  this->brightness_ = 0;
146  } else {
147  this->brightness_ = brightness;
148  }
149  // now write the new brightness level to the display
150  this->command(SSD1325_SETCONTRAST);
151  this->command(int(SSD1325_MAX_CONTRAST * (this->brightness_)));
152 }
153 bool SSD1325::is_on() { return this->is_on_; }
155  this->command(SSD1325_DISPLAYON);
156  this->is_on_ = true;
157 }
159  this->command(SSD1325_DISPLAYOFF);
160  this->is_on_ = false;
161 }
163  switch (this->model_) {
165  return 32;
167  return 64;
168  case SSD1325_MODEL_96_16:
169  return 16;
170  case SSD1325_MODEL_64_48:
171  return 48;
173  return 128;
174  default:
175  return 0;
176  }
177 }
179  switch (this->model_) {
183  return 128;
184  case SSD1325_MODEL_96_16:
185  return 96;
186  case SSD1325_MODEL_64_48:
187  return 64;
188  default:
189  return 0;
190  }
191 }
193  return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / SSD1325_PIXELSPERBYTE;
194 }
195 void HOT SSD1325::draw_absolute_pixel_internal(int x, int y, Color color) {
196  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
197  return;
198  uint32_t color4 = display::ColorUtil::color_to_grayscale4(color);
199  // where should the bits go in the big buffer array? math...
200  uint16_t pos = (x / SSD1325_PIXELSPERBYTE) + (y * this->get_width_internal() / SSD1325_PIXELSPERBYTE);
201  uint8_t shift = (x % SSD1325_PIXELSPERBYTE) * SSD1325_COLORSHIFT;
202  // ensure 'color4' is valid (only 4 bits aka 1 nibble) and shift the bits left when necessary
203  color4 = (color4 & SSD1325_COLORMASK) << shift;
204  // first mask off the nibble we must change...
205  this->buffer_[pos] &= (~SSD1325_COLORMASK >> shift);
206  // ...then lay the new nibble back on top. done!
207  this->buffer_[pos] |= color4;
208 }
209 void SSD1325::fill(Color color) {
210  const uint32_t color4 = display::ColorUtil::color_to_grayscale4(color);
211  uint8_t fill = (color4 & SSD1325_COLORMASK) | ((color4 & SSD1325_COLORMASK) << SSD1325_COLORSHIFT);
212  for (uint32_t i = 0; i < this->get_buffer_length_(); i++)
213  this->buffer_[i] = fill;
214 }
216  if (this->reset_pin_ != nullptr) {
217  this->reset_pin_->setup();
218  this->reset_pin_->digital_write(true);
219  delay(1);
220  // Trigger Reset
221  this->reset_pin_->digital_write(false);
222  delay(10);
223  // Wake up
224  this->reset_pin_->digital_write(true);
225  }
226 }
227 const char *SSD1325::model_str_() {
228  switch (this->model_) {
230  return "SSD1325 128x32";
232  return "SSD1325 128x64";
233  case SSD1325_MODEL_96_16:
234  return "SSD1325 96x16";
235  case SSD1325_MODEL_64_48:
236  return "SSD1325 64x48";
238  return "SSD1327 128x128";
239  default:
240  return "Unknown";
241  }
242 }
243 
244 } // namespace ssd1325_base
245 } // namespace esphome
virtual void digital_write(bool value)=0
virtual void command(uint8_t value)=0
void draw_absolute_pixel_internal(int x, int y, Color color) override
uint16_t x
Definition: tt21100.cpp:17
virtual void write_display_data()=0
void fill(Color color) override
virtual void setup()=0
uint16_t y
Definition: tt21100.cpp:18
void init_internal_(uint32_t buffer_length)
static uint32_t color_to_grayscale4(Color color)
static const Color BLACK
Definition: color.h:168
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_brightness(float brightness)
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26