ESPHome  2022.6.3
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_NORMALDISPLAY); // set display mode
81  this->fill(Color::BLACK); // clear display - ensures we do not see garbage at power-on
82  this->display(); // ...write buffer, which actually clears the display's memory
83  this->turn_on(); // display ON
84 }
86  this->command(SSD1327_SETCOLUMNADDRESS); // set column address
87  this->command(0x00); // set column start address
88  this->command(0x3F); // set column end address
89  this->command(SSD1327_SETROWADDRESS); // set row address
90  this->command(0x00); // set row start address
91  this->command(127); // set last row
92 
93  this->write_display_data();
94 }
96  if (!this->is_failed()) {
97  this->do_update_();
98  this->display();
99  }
100 }
101 void SSD1327::set_brightness(float brightness) {
102  // validation
103  this->brightness_ = clamp(brightness, 0.0F, 1.0F);
104  // now write the new brightness level to the display
105  this->command(SSD1327_SETCONTRAST);
106  this->command(int(SSD1327_MAX_CONTRAST * (this->brightness_)));
107 }
108 bool SSD1327::is_on() { return this->is_on_; }
110  this->command(SSD1327_DISPLAYON);
111  this->is_on_ = true;
112 }
114  this->command(SSD1327_DISPLAYOFF);
115  this->is_on_ = false;
116 }
118  switch (this->model_) {
120  return 128;
121  default:
122  return 0;
123  }
124 }
126  switch (this->model_) {
128  return 128;
129  default:
130  return 0;
131  }
132 }
134  return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / SSD1327_PIXELSPERBYTE;
135 }
136 void HOT SSD1327::draw_absolute_pixel_internal(int x, int y, Color color) {
137  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
138  return;
139  uint32_t color4 = display::ColorUtil::color_to_grayscale4(color);
140  // where should the bits go in the big buffer array? math...
141  uint16_t pos = (x / SSD1327_PIXELSPERBYTE) + (y * this->get_width_internal() / SSD1327_PIXELSPERBYTE);
142  uint8_t shift = (x % SSD1327_PIXELSPERBYTE) * SSD1327_COLORSHIFT;
143  // ensure 'color4' is valid (only 4 bits aka 1 nibble) and shift the bits left when necessary
144  color4 = (color4 & SSD1327_COLORMASK) << shift;
145  // first mask off the nibble we must change...
146  this->buffer_[pos] &= (~SSD1327_COLORMASK >> shift);
147  // ...then lay the new nibble back on top. done!
148  this->buffer_[pos] |= color4;
149 }
150 void SSD1327::fill(Color color) {
151  const uint32_t color4 = display::ColorUtil::color_to_grayscale4(color);
152  uint8_t fill = (color4 & SSD1327_COLORMASK) | ((color4 & SSD1327_COLORMASK) << SSD1327_COLORSHIFT);
153  for (uint32_t i = 0; i < this->get_buffer_length_(); i++)
154  this->buffer_[i] = fill;
155 }
157  if (this->reset_pin_ != nullptr) {
158  this->reset_pin_->setup();
159  this->reset_pin_->digital_write(true);
160  delay(1);
161  // Trigger Reset
162  this->reset_pin_->digital_write(false);
163  delay(10);
164  // Wake up
165  this->reset_pin_->digital_write(true);
166  }
167 }
168 const char *SSD1327::model_str_() {
169  switch (this->model_) {
171  return "SSD1327 128x128";
172  default:
173  return "Unknown";
174  }
175 }
176 
177 } // namespace ssd1327_base
178 } // namespace esphome
virtual void digital_write(bool value)=0
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:84
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:147
Definition: a4988.cpp:4
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27