ESPHome  2024.12.2
ssd1322_base.cpp
Go to the documentation of this file.
1 #include "ssd1322_base.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 namespace ssd1322_base {
7 
8 static const char *const TAG = "ssd1322";
9 
10 static const uint8_t SSD1322_MAX_CONTRAST = 255;
11 static const uint8_t SSD1322_COLORMASK = 0x0f;
12 static const uint8_t SSD1322_COLORSHIFT = 4;
13 static const uint8_t SSD1322_PIXELSPERBYTE = 2;
14 
15 static const uint8_t SSD1322_ENABLEGRAYSCALETABLE = 0x00;
16 static const uint8_t SSD1322_SETCOLUMNADDRESS = 0x15;
17 static const uint8_t SSD1322_WRITERAM = 0x5C;
18 static const uint8_t SSD1322_READRAM = 0x5D;
19 static const uint8_t SSD1322_SETROWADDRESS = 0x75;
20 static const uint8_t SSD1322_SETREMAP = 0xA0;
21 static const uint8_t SSD1322_SETSTARTLINE = 0xA1;
22 static const uint8_t SSD1322_SETOFFSET = 0xA2;
23 static const uint8_t SSD1322_SETMODEALLOFF = 0xA4;
24 static const uint8_t SSD1322_SETMODEALLON = 0xA5;
25 static const uint8_t SSD1322_SETMODENORMAL = 0xA6;
26 static const uint8_t SSD1322_SETMODEINVERTED = 0xA7;
27 static const uint8_t SSD1322_ENABLEPARTIALDISPLAY = 0xA8;
28 static const uint8_t SSD1322_EXITPARTIALDISPLAY = 0xA9;
29 static const uint8_t SSD1322_SETFUNCTIONSELECTION = 0xAB;
30 static const uint8_t SSD1322_SETDISPLAYOFF = 0xAE;
31 static const uint8_t SSD1322_SETDISPLAYON = 0xAF;
32 static const uint8_t SSD1322_SETPHASELENGTH = 0xB1;
33 static const uint8_t SSD1322_SETFRONTCLOCKDIVIDER = 0xB3;
34 static const uint8_t SSD1322_DISPLAYENHANCEMENTA = 0xB4;
35 static const uint8_t SSD1322_SETGPIO = 0xB5;
36 static const uint8_t SSD1322_SETSECONDPRECHARGEPERIOD = 0xB6;
37 static const uint8_t SSD1322_SETGRAYSCALETABLE = 0xB8;
38 static const uint8_t SSD1322_SELECTDEFAULTLINEARGRAYSCALETABLE = 0xB9;
39 static const uint8_t SSD1322_SETPRECHARGEVOLTAGE = 0xBB;
40 static const uint8_t SSD1322_SETVCOMHVOLTAGE = 0xBE;
41 static const uint8_t SSD1322_SETCONTRAST = 0xC1;
42 static const uint8_t SSD1322_MASTERCURRENTCONTROL = 0xC7;
43 static const uint8_t SSD1322_SETMULTIPLEXRATIO = 0xCA;
44 static const uint8_t SSD1322_DISPLAYENHANCEMENTB = 0xD1;
45 static const uint8_t SSD1322_SETCOMMANDLOCK = 0xFD;
46 
47 static const uint8_t SSD1322_SETCOMMANDLOCK_UNLOCK = 0x12;
48 static const uint8_t SSD1322_SETCOMMANDLOCK_LOCK = 0x16;
49 
51  this->init_internal_(this->get_buffer_length_());
52 
53  this->command(SSD1322_SETCOMMANDLOCK);
54  this->data(SSD1322_SETCOMMANDLOCK_UNLOCK);
55  this->turn_off();
56  this->command(SSD1322_SETFRONTCLOCKDIVIDER);
57  this->data(0x91);
58  this->command(SSD1322_SETMULTIPLEXRATIO);
59  this->data(0x3F);
60  this->command(SSD1322_SETOFFSET);
61  this->data(0x00);
62  this->command(SSD1322_SETSTARTLINE);
63  this->data(0x00);
64  this->command(SSD1322_SETREMAP);
65  this->data(0x14);
66  this->data(0x11);
67  this->command(SSD1322_SETGPIO);
68  this->data(0x00);
69  this->command(SSD1322_SETFUNCTIONSELECTION);
70  this->data(0x01);
71  this->command(SSD1322_DISPLAYENHANCEMENTA);
72  this->data(0xA0);
73  this->data(0xFD);
74  this->command(SSD1322_MASTERCURRENTCONTROL);
75  this->data(0x0F);
76  this->command(SSD1322_SETPHASELENGTH);
77  this->data(0xE2);
78  this->command(SSD1322_DISPLAYENHANCEMENTB);
79  this->data(0x82);
80  this->data(0x20);
81  this->command(SSD1322_SETPRECHARGEVOLTAGE);
82  this->data(0x1F);
83  this->command(SSD1322_SETSECONDPRECHARGEPERIOD);
84  this->data(0x08);
85  this->command(SSD1322_SETVCOMHVOLTAGE);
86  this->data(0x07);
87  this->command(SSD1322_SETMODENORMAL);
88  this->command(SSD1322_EXITPARTIALDISPLAY);
89  // this->command(SSD1322_SELECTDEFAULTLINEARGRAYSCALETABLE);
90  this->command(SSD1322_SETGRAYSCALETABLE);
91  // gamma ~2.2
92  this->data(24);
93  this->data(29);
94  this->data(36);
95  this->data(43);
96  this->data(51);
97  this->data(60);
98  this->data(70);
99  this->data(81);
100  this->data(93);
101  this->data(105);
102  this->data(118);
103  this->data(132);
104  this->data(147);
105  this->data(163);
106  this->data(180);
107  this->command(SSD1322_ENABLEGRAYSCALETABLE);
109  this->fill(Color::BLACK); // clear display - ensures we do not see garbage at power-on
110  this->display(); // ...write buffer, which actually clears the display's memory
111  this->turn_on(); // display ON
112 }
114  this->command(SSD1322_SETCOLUMNADDRESS); // set column address
115  this->data(0x1C); // set column start address
116  this->data(0x5B); // set column end address
117  this->command(SSD1322_SETROWADDRESS); // set row address
118  this->data(0x00); // set row start address
119  this->data(0x3F); // set last row
120  this->command(SSD1322_WRITERAM); // write
121 
122  this->write_display_data();
123 }
125  this->do_update_();
126  this->display();
127 }
128 void SSD1322::set_brightness(float brightness) {
129  this->brightness_ = clamp(brightness, 0.0F, 1.0F);
130  // now write the new brightness level to the display
131  this->command(SSD1322_SETCONTRAST);
132  this->data(int(SSD1322_MAX_CONTRAST * (this->brightness_)));
133 }
134 bool SSD1322::is_on() { return this->is_on_; }
136  this->command(SSD1322_SETDISPLAYON);
137  this->is_on_ = true;
138 }
140  this->command(SSD1322_SETDISPLAYOFF);
141  this->is_on_ = false;
142 }
144  switch (this->model_) {
146  return 64;
147  default:
148  return 0;
149  }
150 }
152  switch (this->model_) {
154  return 256;
155  default:
156  return 0;
157  }
158 }
160  return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / SSD1322_PIXELSPERBYTE;
161 }
162 void HOT SSD1322::draw_absolute_pixel_internal(int x, int y, Color color) {
163  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
164  return;
165  uint32_t color4 = display::ColorUtil::color_to_grayscale4(color);
166  // where should the bits go in the big buffer array? math...
167  uint16_t pos = (x / SSD1322_PIXELSPERBYTE) + (y * this->get_width_internal() / SSD1322_PIXELSPERBYTE);
168  uint8_t shift = (1u - (x % SSD1322_PIXELSPERBYTE)) * SSD1322_COLORSHIFT;
169  // ensure 'color4' is valid (only 4 bits aka 1 nibble) and shift the bits left when necessary
170  color4 = (color4 & SSD1322_COLORMASK) << shift;
171  // first mask off the nibble we must change...
172  this->buffer_[pos] &= (~SSD1322_COLORMASK >> shift);
173  // ...then lay the new nibble back on top. done!
174  this->buffer_[pos] |= color4;
175 }
176 void SSD1322::fill(Color color) {
177  const uint32_t color4 = display::ColorUtil::color_to_grayscale4(color);
178  uint8_t fill = (color4 & SSD1322_COLORMASK) | ((color4 & SSD1322_COLORMASK) << SSD1322_COLORSHIFT);
179  for (uint32_t i = 0; i < this->get_buffer_length_(); i++)
180  this->buffer_[i] = fill;
181 }
183  if (this->reset_pin_ != nullptr) {
184  this->reset_pin_->setup();
185  this->reset_pin_->digital_write(true);
186  delay(1);
187  // Trigger Reset
188  this->reset_pin_->digital_write(false);
189  delay(10);
190  // Wake up
191  this->reset_pin_->digital_write(true);
192  }
193 }
194 const char *SSD1322::model_str_() {
195  switch (this->model_) {
197  return "SSD1322 256x64";
198  default:
199  return "Unknown";
200  }
201 }
202 
203 } // namespace ssd1322_base
204 } // namespace esphome
virtual void digital_write(bool value)=0
uint16_t x
Definition: tt21100.cpp:17
virtual void data(uint8_t value)=0
virtual void setup()=0
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:93
uint16_t y
Definition: tt21100.cpp:18
void init_internal_(uint32_t buffer_length)
static uint32_t color_to_grayscale4(Color color)
virtual void command(uint8_t value)=0
void draw_absolute_pixel_internal(int x, int y, Color color) override
virtual void write_display_data()=0
static const Color BLACK
Definition: color.h:168
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_brightness(float brightness)
void fill(Color color) override
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26