-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32-st7789.ino
149 lines (122 loc) · 3.23 KB
/
esp32-st7789.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#define TFT_DC 3 // DC pin
#define TFT_RST 1 // RES pin
#include <SPI.h>
#define ST7789_SWRESET 0x01
#define ST7789_SLPIN 0x10 // sleep on
#define ST7789_SLPOUT 0x11 // sleep off
#define ST7789_PTLON 0x12 // partial on
#define ST7789_NORON 0x13 // partial off
#define ST7789_INVOFF 0x20 // invert off
#define ST7789_INVON 0x21 // invert on
#define ST7789_DISPOFF 0x28 // display off
#define ST7789_DISPON 0x29 // display on
#define ST7789_IDMOFF 0x38 // idle off
#define ST7789_IDMON 0x39 // idle on
#define ST7789_CASET 0x2A
#define ST7789_RASET 0x2B
#define ST7789_RAMWR 0x2C
#define ST7789_RAMRD 0x2E
#define ST7789_COLMOD 0x3A
#define ST7789_MADCTL 0x36
#define ST7789_POWSAVE 0xbc
#define ST7789_DLPOFFSAVE 0xbd
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
uint8_t _width = 240;
uint8_t _height = 240;
SPIClass * spi = NULL;
static SPISettings spiSettings;
#define DC_DATA digitalWrite(TFT_DC, HIGH)
#define DC_COMMAND digitalWrite(TFT_DC, LOW)
void writeSPI(uint8_t c) {
spi->beginTransaction(spiSettings);
spi->transfer(c);
spi->endTransaction();
}
void writeMulti(uint16_t color, uint16_t num) {
spi->beginTransaction(spiSettings);
while(num--) {
spi->transfer(color>>8);
spi->transfer(color);
}
spi->endTransaction();
}
void writeCmd(uint8_t c) {
DC_COMMAND;
writeSPI(c);
}
void writeData(uint8_t d8) {
DC_DATA;
writeSPI(d8);
}
void writeData16(uint16_t d16) {
DC_DATA;
writeMulti(d16, 1);
}
void fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color);
}
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
if(x+w-1>=_width) w=_width -x;
if(y+h-1>=_height) h=_height-y;
setAddrWindow(x, y, x+w-1, y+h-1);
writeMulti(color, w*h);
}
void drawPixel(int16_t x, int16_t y, uint16_t color) {
setAddrWindow(x,y,x+1,y+1);
writeSPI(color>>8); writeSPI(color);
}
void setAddrWindow(uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye){
writeCmd(ST7789_CASET);
writeData16(xs);
writeData16(xe);
writeCmd(ST7789_RASET);
writeData16(ys);
writeData16(ye);
writeCmd(ST7789_RAMWR);
DC_DATA;
}
void displayInit() {
writeCmd(ST7789_SWRESET);
delay(150);
writeCmd(ST7789_SLPOUT);
delay(500);
writeCmd(ST7789_COLMOD); writeData(0x55);
delay(10);
writeCmd(ST7789_MADCTL); writeData(0);
writeCmd(ST7789_CASET); writeData16(0); writeData16(240);
writeCmd(ST7789_RASET); writeData16(0); writeData16(240);
writeCmd(ST7789_INVON);
delay(10);
writeCmd(ST7789_NORON);
delay(10);
writeCmd(ST7789_DISPON);
delay(20);
}
void wiresInit() {
pinMode(TFT_DC, OUTPUT);
spi = new SPIClass(VSPI);
spi->begin(); // default VSPI pins
spiSettings = SPISettings(16000000, MSBFIRST, SPI_MODE3);
// reset display (optional)
pinMode(TFT_RST, OUTPUT);
digitalWrite(TFT_RST, HIGH);
delay(50);
digitalWrite(TFT_RST, LOW);
delay(50);
digitalWrite(TFT_RST, HIGH);
delay(50);
}
void setup(void) {
wiresInit();
displayInit();
fillScreen(RED); // clear screen
//drawPixel(120, 120, RED);
//drawPixel(130, 120, BLUE);
}
void loop(){
// display colored squares in a random place on the screen
fillRect(random(230), random(230), 10, 10, random(65000));
}