ESPHome  2024.10.2
ssd1331_base.cpp
Go to the documentation of this file.
1 #include "ssd1331_base.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 namespace ssd1331_base {
7 
8 static const char *const TAG = "ssd1331";
9 
10 static const uint16_t SSD1331_COLORMASK = 0xffff;
11 static const uint8_t SSD1331_MAX_CONTRASTA = 0x91;
12 static const uint8_t SSD1331_MAX_CONTRASTB = 0x50;
13 static const uint8_t SSD1331_MAX_CONTRASTC = 0x7D;
14 static const uint8_t SSD1331_BYTESPERPIXEL = 2;
15 // SSD1331 Commands
16 static const uint8_t SSD1331_DRAWLINE = 0x21; // Draw line
17 static const uint8_t SSD1331_DRAWRECT = 0x22; // Draw rectangle
18 static const uint8_t SSD1331_FILL = 0x26; // Fill enable/disable
19 static const uint8_t SSD1331_SETCOLUMN = 0x15; // Set column address
20 static const uint8_t SSD1331_SETROW = 0x75; // Set row address
21 static const uint8_t SSD1331_CONTRASTA = 0x81; // Set contrast for color A
22 static const uint8_t SSD1331_CONTRASTB = 0x82; // Set contrast for color B
23 static const uint8_t SSD1331_CONTRASTC = 0x83; // Set contrast for color C
24 static const uint8_t SSD1331_MASTERCURRENT = 0x87; // Master current control
25 static const uint8_t SSD1331_SETREMAP = 0xA0; // Set re-map & data format
26 static const uint8_t SSD1331_STARTLINE = 0xA1; // Set display start line
27 static const uint8_t SSD1331_DISPLAYOFFSET = 0xA2; // Set display offset
28 static const uint8_t SSD1331_NORMALDISPLAY = 0xA4; // Set display to normal mode
29 static const uint8_t SSD1331_DISPLAYALLON = 0xA5; // Set entire display ON
30 static const uint8_t SSD1331_DISPLAYALLOFF = 0xA6; // Set entire display OFF
31 static const uint8_t SSD1331_INVERTDISPLAY = 0xA7; // Invert display
32 static const uint8_t SSD1331_SETMULTIPLEX = 0xA8; // Set multiplex ratio
33 static const uint8_t SSD1331_SETMASTER = 0xAD; // Set master configuration
34 static const uint8_t SSD1331_DISPLAYOFF = 0xAE; // Display OFF (sleep mode)
35 static const uint8_t SSD1331_DISPLAYON = 0xAF; // Normal Brightness Display ON
36 static const uint8_t SSD1331_POWERMODE = 0xB0; // Power save mode
37 static const uint8_t SSD1331_PRECHARGE = 0xB1; // Phase 1 and 2 period adjustment
38 static const uint8_t SSD1331_CLOCKDIV = 0xB3; // Set display clock divide ratio/oscillator frequency
39 static const uint8_t SSD1331_PRECHARGEA = 0x8A; // Set second pre-charge speed for color A
40 static const uint8_t SSD1331_PRECHARGEB = 0x8B; // Set second pre-charge speed for color B
41 static const uint8_t SSD1331_PRECHARGEC = 0x8C; // Set second pre-charge speed for color C
42 static const uint8_t SSD1331_PRECHARGELEVEL = 0xBB; // Set pre-charge voltage
43 static const uint8_t SSD1331_VCOMH = 0xBE; // Set Vcomh voltge
44 
46  this->init_internal_(this->get_buffer_length_());
47 
48  this->command(SSD1331_DISPLAYOFF); // 0xAE
49  this->command(SSD1331_SETREMAP); // 0xA0
50  this->command(0x72); // RGB Color
51  this->command(SSD1331_STARTLINE); // 0xA1
52  this->command(0x0);
53  this->command(SSD1331_DISPLAYOFFSET); // 0xA2
54  this->command(0x0);
55  this->command(SSD1331_NORMALDISPLAY); // 0xA4
56  this->command(SSD1331_SETMULTIPLEX); // 0xA8
57  this->command(0x3F); // 0x3F 1/64 duty
58  this->command(SSD1331_SETMASTER); // 0xAD
59  this->command(0x8E);
60  this->command(SSD1331_POWERMODE); // 0xB0
61  this->command(0x0B);
62  this->command(SSD1331_PRECHARGE); // 0xB1
63  this->command(0x31);
64  this->command(SSD1331_CLOCKDIV); // 0xB3
65  this->command(0xF0); // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio, (A[3:0]+1 = 1..16)
66  this->command(SSD1331_PRECHARGEA); // 0x8A
67  this->command(0x64);
68  this->command(SSD1331_PRECHARGEB); // 0x8B
69  this->command(0x78);
70  this->command(SSD1331_PRECHARGEC); // 0x8C
71  this->command(0x64);
72  this->command(SSD1331_PRECHARGELEVEL); // 0xBB
73  this->command(0x3A);
74  this->command(SSD1331_VCOMH); // 0xBE
75  this->command(0x3E);
76  this->command(SSD1331_MASTERCURRENT); // 0x87
77  this->command(0x06);
79  this->fill(Color::BLACK); // clear display - ensures we do not see garbage at power-on
80  this->display(); // ...write buffer, which actually clears the display's memory
81  this->turn_on(); // display ON
82 }
84  this->command(SSD1331_SETCOLUMN); // set column address
85  this->command(0x00); // set column start address
86  this->command(0x5F); // set column end address
87  this->command(SSD1331_SETROW); // set row address
88  this->command(0x00); // set row start address
89  this->command(0x3F); // set last row
90  this->write_display_data();
91 }
93  this->do_update_();
94  this->display();
95 }
96 void SSD1331::set_brightness(float brightness) {
97  // validation
98  this->brightness_ = clamp(brightness, 0.0F, 1.0F);
99  // now write the new brightness level to the display
100  this->command(SSD1331_CONTRASTA); // 0x81
101  this->command(int(SSD1331_MAX_CONTRASTA * (this->brightness_)));
102  this->command(SSD1331_CONTRASTB); // 0x82
103  this->command(int(SSD1331_MAX_CONTRASTB * (this->brightness_)));
104  this->command(SSD1331_CONTRASTC); // 0x83
105  this->command(int(SSD1331_MAX_CONTRASTC * (this->brightness_)));
106 }
107 bool SSD1331::is_on() { return this->is_on_; }
109  this->command(SSD1331_DISPLAYON);
110  this->is_on_ = true;
111 }
113  this->command(SSD1331_DISPLAYOFF);
114  this->is_on_ = false;
115 }
116 int SSD1331::get_height_internal() { return 64; }
117 int SSD1331::get_width_internal() { return 96; }
119  return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * size_t(SSD1331_BYTESPERPIXEL);
120 }
121 void HOT SSD1331::draw_absolute_pixel_internal(int x, int y, Color color) {
122  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
123  return;
124  const uint32_t color565 = display::ColorUtil::color_to_565(color);
125  // where should the bits go in the big buffer array? math...
126  uint16_t pos = (x + y * this->get_width_internal()) * SSD1331_BYTESPERPIXEL;
127  this->buffer_[pos++] = (color565 >> 8) & 0xff;
128  this->buffer_[pos] = color565 & 0xff;
129 }
130 void SSD1331::fill(Color color) {
131  const uint32_t color565 = display::ColorUtil::color_to_565(color);
132  for (uint32_t i = 0; i < this->get_buffer_length_(); i++) {
133  if (i & 1) {
134  this->buffer_[i] = color565 & 0xff;
135  } else {
136  this->buffer_[i] = (color565 >> 8) & 0xff;
137  }
138  }
139 }
141  if (this->reset_pin_ != nullptr) {
142  this->reset_pin_->setup();
143  this->reset_pin_->digital_write(true);
144  delay(1);
145  // Trigger Reset
146  this->reset_pin_->digital_write(false);
147  delay(10);
148  // Wake up
149  this->reset_pin_->digital_write(true);
150  }
151 }
152 
153 } // namespace ssd1331_base
154 } // namespace esphome
void set_brightness(float brightness)
virtual void digital_write(bool value)=0
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void draw_absolute_pixel_internal(int x, int y, Color color) override
uint16_t x
Definition: tt21100.cpp:17
virtual void setup()=0
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:92
virtual void command(uint8_t value)=0
uint16_t y
Definition: tt21100.cpp:18
void init_internal_(uint32_t buffer_length)
virtual void write_display_data()=0
static const Color BLACK
Definition: color.h:168
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void fill(Color color) override
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26