8#include "st7735s_drv.h"
9#include "pixel_stream_const.h"
15 128, 128, 132, 132, 2, 1, st7735s_drv::BGR_COLOR_FILTER);
25#define CMD_RDDMADCTL 0x0B
26#define CMD_RDDCOLMOD 0x0C
29#define CMD_RDDSDR 0x0F
38#define CMD_SWRESET 0x01
40#define CMD_SLPOUT 0x11
43#define CMD_INVOFF 0x20
45#define CMD_GAMSET 0x26
46#define CMD_DISPOFF 0x28
47#define CMD_DISPON 0x29
51#define CMD_RGBSET 0x2D
53#define CMD_SCRLAR 0x33
56#define CMD_MADCTL 0x36
57#define CMD_VSCSAD 0x37
58#define CMD_IDMOFF 0x38
60#define CMD_COLMOD 0x3A
64#define CMD_FRMCTR1 0xB1
65#define CMD_FRMCTR2 0xB2
66#define CMD_FRMCTR3 0xB3
67#define CMD_INVCTR 0xB4
68#define CMD_PWCTR1 0xC0
69#define CMD_PWCTR2 0xC1
70#define CMD_PWCTR3 0xC2
71#define CMD_PWCTR4 0xC3
72#define CMD_PWCTR5 0xC4
73#define CMD_VMCTR1 0xC5
74#define CMD_VMOFCTR 0xC7
77#define CMD_NVCTR1 0xD9
78#define CMD_NVCTR2 0xDE
79#define CMD_NVCTR3 0xDF
80#define CMD_GAMCTRP1 0xE0
81#define CMD_GAMCTRN1 0xE1
87 : _spi(spi), _rst_pin(rst_pin), _dc_pin(dc_pin), _lcd(lcd), _mutex(
mutex)
91 _rst_pin.gpioMode(GPIO::OUTPUT | GPIO::INIT_HIGH);
92 _dc_pin. gpioMode(GPIO::OUTPUT | GPIO::INIT_HIGH);
95 _rst_pin.gpioWrite(LOW);
97 _rst_pin.gpioWrite(HIGH);
102 writeCommand(CMD_SLPOUT);
105 writeCommand(CMD_GAMSET);
108 writeCommand(CMD_COLMOD);
111 writeCommand(CMD_SCRLAR);
113 writeData(_lcd.offsetY);
115 writeData(_lcd.sizeY);
117 writeData(_lcd.sizeRamY - _lcd.sizeY - _lcd.offsetY);
123 writeCommand(CMD_DISPON);
128st7735s_drv::~st7735s_drv()
132uint16_t st7735s_drv::getSizeX()
134 if ((_orientation == UP) || (_orientation == DOWN))
140uint16_t st7735s_drv::getSizeY()
142 if ((_orientation == UP) || (_orientation == DOWN))
148void st7735s_drv::setOrientation(Orientation o)
151 uint8_t madctl = (_lcd.flags & BGR_COLOR_FILTER) ? 0x8 : 0;
154 writeCommand(CMD_MADCTL);
158 writeData(madctl | 0xC0);
164 writeData(madctl | 0xA0);
167 writeData(madctl | 0x60);
174void st7735s_drv::drawPixel(uint16_t x, uint16_t y, color_t c)
176 color_t color = convertColor(c, LCD::COLORTYPE_RGB565);
180 setFrame(x, y, x, y);
181 writeCommand(CMD_RAMWR);
182 _tx_buffer[0] = color >> 8;
183 _tx_buffer[1] = color & 0xff;
189void st7735s_drv::drawHLine(uint16_t xs, uint16_t y, uint16_t xe, color_t c)
191 color_t color = convertColor(c, LCD::COLORTYPE_RGB565);
197 setFrame(xs, y, xe, y);
198 writeCommand(CMD_RAMWR);
200 for (
int i = xs; i <= xe; ++i)
202 _tx_buffer[buf_index++] = color >> 8;
203 _tx_buffer[buf_index++] = color & 0xff;
205 writeDataBuffer(buf_index);
210void st7735s_drv::drawVLine(uint16_t x, uint16_t ys, uint16_t ye, color_t c)
212 color_t color = convertColor(c, LCD::COLORTYPE_RGB565);
218 setFrame(x, ys, x, ye);
219 writeCommand(CMD_RAMWR);
221 for (
int i = ys; i <= ye; ++i)
223 _tx_buffer[buf_index++] = color >> 8;
224 _tx_buffer[buf_index++] = color & 0xff;
226 writeDataBuffer(buf_index);
231void st7735s_drv::drawArea(uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye,
234 bool color_conversion = (ps.getColorType() != LCD::COLORTYPE_RGB565);
240 setFrame(xs, ys, xe, ye);
241 int16_t pixels = (xe - xs + 1) * (ye - ys + 1);
242 writeCommand(CMD_RAMWR);
244 for (
int i = 0; i < pixels; i++)
246 color_t color = ps.getNext();
247 if (color_conversion)
249 color = convertColor(color, LCD::COLORTYPE_RGB565);
251 _tx_buffer[index++] = color >> 8;
252 _tx_buffer[index++] = color & 0xff;
253 if (index == BUF_LEN) {
254 writeDataBuffer(index);
259 writeDataBuffer(index);
265void st7735s_drv::fillArea(uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye,
269 drawArea(xs, ys, xe, ye, ps);
272void st7735s_drv::scroll(int16_t rows)
276 _first_row += _lcd.sizeY;
277 else if (_first_row >= _lcd.sizeY)
278 _first_row -= _lcd.sizeY;
281 writeCommand(CMD_VSCSAD);
283 writeData(_lcd.offsetY + _first_row);
288void st7735s_drv::clearScreen(color_t c)
290 color_t color = convertColor(c, LCD::COLORTYPE_RGB565);
294 setFrame(0, 0, _lcd.sizeX - 1, _lcd.sizeY - 1);
295 int16_t pixels = _lcd.sizeX * _lcd.sizeY;
296 writeCommand(CMD_RAMWR);
297 uint8_t msb = color >> 8;
298 uint8_t lsb = color & 0xff;
300 for (
int i = 0; i <= pixels; i++) {
301 _tx_buffer[index++] = msb;
302 _tx_buffer[index++] = lsb;
303 if (index == BUF_LEN) {
304 writeDataBuffer(index);
309 writeDataBuffer(index);
315void st7735s_drv::inverseColors(
bool b)
320 writeCommand(CMD_INVON);
322 writeCommand(CMD_INVOFF);
329void st7735s_drv::change(uint16_t & x, uint16_t & y)
331 switch (_orientation)
334 y += _lcd.sizeY + _lcd.offsetY - _first_row - 1;
340 x += _lcd.sizeY + _lcd.offsetY - _first_row - 1;
352void st7735s_drv::setFrame(uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye)
354 switch (_orientation)
357 xs += (_lcd.sizeRamX - _lcd.sizeX - _lcd.offsetX);
358 ys += (_lcd.sizeRamY - _lcd.sizeY - _lcd.offsetY);
359 xe += (_lcd.sizeRamX - _lcd.sizeX - _lcd.offsetX);
360 ye += (_lcd.sizeRamY - _lcd.sizeY - _lcd.offsetY);
369 xs += (_lcd.sizeRamY - _lcd.sizeY - _lcd.offsetY);
371 xe += (_lcd.sizeRamY - _lcd.sizeY - _lcd.offsetY);
376 ys += (_lcd.sizeRamX - _lcd.sizeX - _lcd.offsetX);
378 ye += (_lcd.sizeRamX - _lcd.sizeX - _lcd.offsetX);
381 writeCommand(CMD_CASET);
388 writeCommand(CMD_RASET);
396void st7735s_drv::writeData(uint8_t data)
398 _spi.spiTx(&data, 1);
401void st7735s_drv::writeDataBuffer(
int len)
403 _spi.spiTx(_tx_buffer, len);
406void st7735s_drv::writeCommand(uint8_t cmd)
408 _dc_pin.gpioWrite(LOW);
410 _spi.spiTxRx(&cmd, &rcv_data, 1);
411 _dc_pin.gpioWrite(HIGH);