LCDGFX LCD display driver  1.2.0
Lightweight graphics library for SSD1306, SSD1325, SSD1327, SSD1331, SSD1351, SH1106, SH1107, IL9163, ST7735, ST7789, ILI9341, PCD8544 displays over I2C/SPI
lcd_st7789.inl
1 /*
2  MIT License
3 
4  Copyright 2019-2022 (C) Alexey Dynda
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23 */
24 /* !!! THIS FILE IS AUTO GENERATED !!! */
25 #include "lcd_hal/io.h"
26 #ifdef SDL_EMULATION
27 #include "sdl_core.h"
28 #endif
29 #include "nano_gfx_types.h"
30 
31 #ifndef CMD_ARG
32 #define CMD_ARG 0xFF
33 #endif
34 
36 {
37  lcduint_t rx = x + (w ? w : m_base.width()) - 1;
38  commandStart();
39  this->send(0x2A);
40  setDataMode(1); // According to datasheet all args must be passed in data mode
41  lcduint_t pos = x + m_offset_x;
42  this->send(pos >> 8);
43  this->send(pos & 0xFF);
44  pos = rx + m_offset_x;
45  this->send(pos >> 8);
46  this->send(pos & 0xFF);
47  setDataMode(0);
48  this->send(0x2B);
49  setDataMode(1); // According to datasheet all args must be passed in data mode
50  pos = y + m_offset_y;
51  this->send(pos >> 8);
52  this->send(pos & 0xFF);
53  pos = m_base.height() - 1 + m_offset_y;
54  this->send(pos >> 8);
55  this->send(pos & 0xFF);
56  setDataMode(0);
57  this->send(0x2C);
58  if ( m_dc >= 0 )
59  {
60  setDataMode(1);
61  }
62  else
63  {
64  this->stop();
65  this->start();
66  this->send(0x40);
67  }
68 }
69 
70 template <class I> void InterfaceST7789<I>::nextBlock()
71 {
72 
73 }
74 
75 template <class I> void InterfaceST7789<I>::endBlock()
76 {
77  this->stop();
78 }
79 
80 template <class I> void InterfaceST7789<I>::setDataMode(uint8_t mode)
81 {
82  if ( m_dc >= 0 )
83  {
84  lcd_gpioWrite(m_dc, mode ? LCD_HIGH : LCD_LOW);
85  }
86 }
87 
88 template <class I> void InterfaceST7789<I>::commandStart()
89 {
90  this->start();
91  if ( m_dc >= 0 )
92  setDataMode(0);
93  else
94  this->send(0x00);
95 }
96 
97 template <class I> void InterfaceST7789<I>::setRotation(uint8_t rotation)
98 {
99  uint8_t ram_mode;
100  if ( (rotation ^ m_rotation) & 0x01 )
101  {
102  m_base.swapDimensions();
103  lcduint_t temp = m_offset_x;
104  m_offset_x = m_offset_y;
105  m_offset_y = temp;
106  }
107  if ( (rotation ^ m_rotation) == 0x01 || (rotation ^ m_rotation) == 0x11 )
108  {
109  // if one of the dimensions is odd
110  if ( (m_base.width() & 0x01) || (m_base.height() & 0x01) )
111  {
112  switch (rotation)
113  {
114  case 0:
115  m_offset_x--;
116  break;
117  case 1:
118  m_offset_y++;
119  break;
120  case 2:
121  m_offset_x++;
122  break;
123  case 3:
124  m_offset_y--;
125  break;
126  default:
127  break;
128  }
129  }
130  }
131  m_rotation = (rotation & 0x03);
132  this->start();
133  setDataMode(0);
134  this->send(0x28);
135  this->send(0x36);
136  setDataMode(1);
137  switch ( m_rotation )
138  {
139  case 0: // 0 degree CW
140  ram_mode = 0b00000000;
141  break;
142  case 1: // 90 degree CW
143  ram_mode = 0b01100000;
144  break;
145  case 2: // 180 degree CW
146  ram_mode = 0b11000000;
147  break;
148  default: // 270 degree CW
149  ram_mode = 0b10100000;
150  break;
151  }
152  this->send(ram_mode | m_rgb_bit);
153  setDataMode(0);
154  this->send(0x29);
155  this->stop();
156 }
157 
158 template <class I> void InterfaceST7789<I>::setOffset(lcdint_t ox, lcdint_t oy)
159 {
160  if (m_rotation & 0x01)
161  {
162  m_offset_x = oy;
163  m_offset_y = ox;
164  }
165  else
166  {
167  m_offset_x = ox;
168  m_offset_y = oy;
169  }
170 }
171 
172 template <class I> void InterfaceST7789<I>::normalMode()
173 {
174  this->start();
175  setDataMode(0);
176  this->send(0x20);
177  this->stop();
178 }
179 
180 template <class I> void InterfaceST7789<I>::invertMode()
181 {
182  this->start();
183  setDataMode(0);
184  this->send(0x21);
185  this->stop();
186 }
187 
188 
190 // ST7789 basic 16-bit implementation
192 
193 template <class I> void DisplayST7789x16<I>::beginDisplay()
194 {
195 }
196 
197 template <class I> void DisplayST7789x16<I>::endDisplay()
198 {
199 }
200 
201 static const PROGMEM uint8_t s_ST7789_lcd135x240x16_initData[] = {
202 #ifdef SDL_EMULATION
203  SDL_LCD_ST7789, 0x00,
204  0b00010000, 0x00,
205 #endif
206  0x01, CMD_DELAY, 150, // SWRESET sw reset. not needed, we do hardware reset
207  0x11, CMD_DELAY, 20, // SLPOUT exit sleep mode
208  0x13, CMD_DELAY, 10, // NORON
209  0x3A, 0x01, 0x55, // COLMOD set 16-bit pixel format 0x55
210  0x36, 0x01, 0b00001000, // MADCTL 08 Adafruit
211  0xB6, 0x02, 0x0A, 0x82, // DISSET5
212  0xB2, 0x05, 0x0C, 0x0C, 0x00, 0x33, 0x33, // FRMCTR2 / PORCTL, Frame Rate Control (In Idle mode/ 8-colors)
213  0xB7, 0x01, 0x35, // VGH / VGL
214  0xBB, 0x01, 0x28, // VCOM
215  0xC0, 0x01, 0x0C, // LCM / PWCTR1 power control 1
216  0xC2, 0x02, 0x01, 0xFF, // VDV PWCTR3 power control 3
217  0xC3, 0x01, 0x10, // VRH
218  0xC4, 0x01, 0x20, // VDV
219  0xC6, 0x01, 0x0F, // FRCTR2
220  0xD0, 0x02, 0xA4, 0xA1, // PWCTRL1
221  0xE0, 0x0E, // GMCTRP1 positive gamma correction
222  0xD0, 0x00, 0x02, 0x07,
223  0x0A, 0x28, 0x32, 0x44,
224  0x42, 0x06, 0x0E, 0x12,
225  0x14, 0x17,
226  0xE1, 0x0E, // GMCTRN1 negative gamma correction
227  0xD0, 0x00, 0x02, 0x07,
228  0x0A, 0x28, 0x31, 0x54,
229  0x47, 0x0E, 0x1C, 0x17,
230  0x1B, 0x1E,
231 // 0x2A, 0x04, 0x00, 0x00, 0x00, 0x7F, // set column address, not needed. set by direct API
232 // 0x2B, 0x04, 0x00, 0x00, 0x00, 0x9F, // set page address, not needed. set by direct API
233  0x21, CMD_DELAY, 10, // INVON (21h): Display Inversion On
234  0x29, CMD_DELAY, 120, // DISPON display on
235 };
236 
238 // ST7789 basic 16-bit implementation
240 
242 {
243  ssd1306_resetController2(this->m_rstPin, 20);
244  this->m_w = 135;
245  this->m_h = 240;
246  // Give LCD some time to initialize. Refer to ST7789 datasheet
247  lcd_delay(120);
248  _configureSpiDisplay<I>(this->m_intf,
249  s_ST7789_lcd135x240x16_initData,
250  sizeof(s_ST7789_lcd135x240x16_initData));
251 
252 }
253 
255 {
256 }
257 
258 static const PROGMEM uint8_t s_ST7789_lcd240x240x16_initData[] = {
259 #ifdef SDL_EMULATION
260  SDL_LCD_ST7789, 0x00,
261  0b00010000, 0x00,
262 #endif
263  0x01, CMD_DELAY, 150, // SWRESET sw reset. not needed, we do hardware reset
264  0x11, CMD_DELAY, 20, // SLPOUT exit sleep mode
265  0x13, CMD_DELAY, 10, // NORON
266  0x3A, 0x01, 0x55, // COLMOD set 16-bit pixel format 0x55
267  0x36, 0x01, 0b00001000, // MADCTL 08 Adafruit
268  0xB6, 0x02, 0x0A, 0x82, // DISSET5
269  0xB2, 0x05, 0x0C, 0x0C, 0x00, 0x33, 0x33, // FRMCTR2 / PORCTL, Frame Rate Control (In Idle mode/ 8-colors)
270  0xB7, 0x01, 0x35, // VGH / VGL
271  0xBB, 0x01, 0x28, // VCOM
272  0xC0, 0x01, 0x0C, // LCM / PWCTR1 power control 1
273  0xC2, 0x02, 0x01, 0xFF, // VDV PWCTR3 power control 3
274  0xC3, 0x01, 0x10, // VRH
275  0xC4, 0x01, 0x20, // VDV
276  0xC6, 0x01, 0x0F, // FRCTR2
277  0xD0, 0x02, 0xA4, 0xA1, // PWCTRL1
278  0xE0, 0x0E, // GMCTRP1 positive gamma correction
279  0xD0, 0x00, 0x02, 0x07,
280  0x0A, 0x28, 0x32, 0x44,
281  0x42, 0x06, 0x0E, 0x12,
282  0x14, 0x17,
283  0xE1, 0x0E, // GMCTRN1 negative gamma correction
284  0xD0, 0x00, 0x02, 0x07,
285  0x0A, 0x28, 0x31, 0x54,
286  0x47, 0x0E, 0x1C, 0x17,
287  0x1B, 0x1E,
288 // 0x2A, 0x04, 0x00, 0x00, 0x00, 0x7F, // set column address, not needed. set by direct API
289 // 0x2B, 0x04, 0x00, 0x00, 0x00, 0x9F, // set page address, not needed. set by direct API
290  0x21, CMD_DELAY, 10, // INVON (21h): Display Inversion On
291  0x29, CMD_DELAY, 120, // DISPON display on
292 };
293 
295 // ST7789 basic 16-bit implementation
297 
299 {
300  ssd1306_resetController2(this->m_rstPin, 20);
301  this->m_w = 240;
302  this->m_h = 240;
303  // Give LCD some time to initialize. Refer to ST7789 datasheet
304  lcd_delay(120);
305  _configureSpiDisplay<I>(this->m_intf,
306  s_ST7789_lcd240x240x16_initData,
307  sizeof(s_ST7789_lcd240x240x16_initData));
308  this->m_intf.setOffset( 0, 0 );
309 }
310 
312 {
313 }
314 
315 static const PROGMEM uint8_t s_ST7789_lcd170x320x16_initData[] = {
316 #ifdef SDL_EMULATION
317  SDL_LCD_ST7789, 0x00,
318  0b00010000, 0x00,
319 #endif
320  0x01, CMD_DELAY, 150, // SWRESET sw reset. not needed, we do hardware reset
321  0x11, CMD_DELAY, 20, // SLPOUT exit sleep mode
322  0x13, CMD_DELAY, 10, // NORON
323  0x3A, 0x01, 0x55, // COLMOD set 16-bit pixel format 0x55
324  0x36, 0x01, 0b00001000, // MADCTL 08 Adafruit
325  0xB6, 0x02, 0x0A, 0x82, // DISSET5
326  0xB2, 0x05, 0x0C, 0x0C, 0x00, 0x33, 0x33, // FRMCTR2 / PORCTL, Frame Rate Control (In Idle mode/ 8-colors)
327  0xB7, 0x01, 0x35, // VGH / VGL
328  0xBB, 0x01, 0x28, // VCOM
329  0xC0, 0x01, 0x0C, // LCM / PWCTR1 power control 1
330  0xC2, 0x02, 0x01, 0xFF, // VDV PWCTR3 power control 3
331  0xC3, 0x01, 0x10, // VRH
332  0xC4, 0x01, 0x20, // VDV
333  0xC6, 0x01, 0x0F, // FRCTR2
334  0xD0, 0x02, 0xA4, 0xA1, // PWCTRL1
335  0xE0, 0x0E, // GMCTRP1 positive gamma correction
336  0xD0, 0x00, 0x02, 0x07,
337  0x0A, 0x28, 0x32, 0x44,
338  0x42, 0x06, 0x0E, 0x12,
339  0x14, 0x17,
340  0xE1, 0x0E, // GMCTRN1 negative gamma correction
341  0xD0, 0x00, 0x02, 0x07,
342  0x0A, 0x28, 0x31, 0x54,
343  0x47, 0x0E, 0x1C, 0x17,
344  0x1B, 0x1E,
345 // 0x2A, 0x04, 0x00, 0x00, 0x00, 0x7F, // set column address, not needed. set by direct API
346 // 0x2B, 0x04, 0x00, 0x00, 0x00, 0x9F, // set page address, not needed. set by direct API
347  0x21, CMD_DELAY, 10, // INVON (21h): Display Inversion On
348  0x29, CMD_DELAY, 120, // DISPON display on
349 };
350 
352 // ST7789 basic 16-bit implementation
354 
356 {
357  ssd1306_resetController2(this->m_rstPin, 20);
358  this->m_w = 170;
359  this->m_h = 320;
360  // Give LCD some time to initialize. Refer to ST7789 datasheet
361  lcd_delay(120);
362  _configureSpiDisplay<I>(this->m_intf,
363  s_ST7789_lcd170x320x16_initData,
364  sizeof(s_ST7789_lcd170x320x16_initData));
365  this->m_intf.setOffset( 35, 0 );
366 }
367 
369 {
370 }
void nextBlock()
Switches to the start of next RAM page for the block, specified by startBlock().
Definition: lcd_st7789.inl:70
void startBlock(lcduint_t x, lcduint_t y, lcduint_t w)
Sets block in RAM of lcd display controller to write data to.
Definition: lcd_st7789.inl:35
void beginController()
Basic ST7789 240x240x16 initialization.
Definition: lcd_st7789.inl:298
uint8_t lcduint_t
internal int type, used by the library.
Definition: canvas_types.h:79
void lcd_gpioWrite(int pin, int level)
Writes value to gpio.
void setRotation(uint8_t rotation)
Sets screen orientation (rotation)
Definition: lcd_st7789.inl:97
void setDataMode(uint8_t mode)
Enables either data or command mode on SPI bus.
Definition: lcd_st7789.inl:80
int8_t lcdint_t
internal int type, used by the library.
Definition: canvas_types.h:77
void endController()
Basic ST7789 deinitialization.
Definition: lcd_st7789.inl:254
SSD1306 HAL IO communication functions.
void ssd1306_resetController2(int8_t rstPin, uint8_t delayMs)
Does hardware reset for oled controller.
void beginController()
Basic ST7789 135x240x16 initialization.
Definition: lcd_st7789.inl:241
#define LCD_LOW
Constant corresponds to low level of gpio pin.
Definition: io.h:175
Basic structures of nano gfx library.
void endController()
Basic ST7789 deinitialization.
Definition: lcd_st7789.inl:311
void beginDisplay()
Basic ST7789 initialization.
Definition: lcd_st7789.inl:193
void endBlock()
Closes data send operation to lcd display.
Definition: lcd_st7789.inl:75
void endController()
Basic ST7789 deinitialization.
Definition: lcd_st7789.inl:368
void invertMode()
Switches display to inverted mode (sends INVON command).
Definition: lcd_st7789.inl:180
void setOffset(lcdint_t ox, lcdint_t oy)
Sets display offset in GDRAM memory.
Definition: lcd_st7789.inl:158
void endDisplay()
Basic ST7789 deinitialization.
Definition: lcd_st7789.inl:197
void beginController()
Basic ST7789 170x320x16 initialization.
Definition: lcd_st7789.inl:355
void normalMode()
Switches display to normal mode (sends INVOFF command).
Definition: lcd_st7789.inl:172
#define LCD_HIGH
Constant corresponds to high level of gpio pin.
Definition: io.h:177
void commandStart()
Starts communication with LCD display in command mode.
Definition: lcd_st7789.inl:88
void lcd_delay(unsigned long ms)
Forces current thread to sleeps for specified number of milliseconds.