-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.c
More file actions
310 lines (256 loc) · 10 KB
/
Copy pathdisplay.c
File metadata and controls
310 lines (256 loc) · 10 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// display.c — see display.h for the module overview.
#include "display.h"
#include "font.h"
#include <avr/io.h>
#include <util/delay.h>
// ST7735 command subset needed for basic framebuffer-less drawing.
#define ST7735_SWRESET 0x01
#define ST7735_SLPOUT 0x11
#define ST7735_COLMOD 0x3A
#define ST7735_MADCTL 0x36
#define ST7735_DISPON 0x29
#define ST7735_CASET 0x2A
#define ST7735_RASET 0x2B
#define ST7735_RAMWR 0x2C
static void SPI_Init(void) {
// Set MOSI and SCK output, all others input
DDRB = (1 << PB3) | (1 << PB5);
// Enable SPI, Master, set clock rate fck/2
SPCR = (1 << SPE) | (1 << MSTR);
SPSR = (1 << SPI2X);
}
static inline void SPI_Send(uint8_t data) {
SPDR = data;
while (!(SPSR & (1 << SPIF))) {}
}
static inline void displayCSLow(void) { DISPLAY_CS_PORT &= (uint8_t)~(1 << DISPLAY_CS_PIN); }
static inline void displayCSHigh(void) { DISPLAY_CS_PORT |= (uint8_t)(1 << DISPLAY_CS_PIN); }
static inline void displayDCCmd(void) { DISPLAY_DC_PORT &= (uint8_t)~(1 << DISPLAY_DC_PIN); }
static inline void displayDCData(void) { DISPLAY_DC_PORT |= (uint8_t)(1 << DISPLAY_DC_PIN); }
static void displayWriteCommand(uint8_t cmd) {
displayCSHigh(); // End any previous transaction
displayDCCmd();
displayCSLow(); // Start new transaction
SPI_Send(cmd);
}
static void displayWriteData(uint8_t data) {
displayDCData();
SPI_Send(data);
}
static void displayWriteData16(uint16_t data) {
displayDCData();
SPI_Send((uint8_t)(data >> 8));
SPI_Send((uint8_t)(data & 0xFF));
}
void displayInit(void) {
// Disable SD Card so it ignores the SPI bus (Active LOW)
DDRB |= (1 << PB0);
PORTB |= (1 << PB0);
displayCSHigh(); // Ensure CS is HIGH before setting it as output
DISPLAY_CS_DDR |= (1 << DISPLAY_CS_PIN);
DISPLAY_DC_DDR |= (1 << DISPLAY_DC_PIN);
DISPLAY_RST_DDR |= (1 << DISPLAY_RST_PIN);
SPI_Init();
// Hardware reset pulse -- see the blocking-delay note in display.h.
DISPLAY_RST_PORT |= (1 << DISPLAY_RST_PIN);
_delay_ms(5);
DISPLAY_RST_PORT &= (uint8_t)~(1 << DISPLAY_RST_PIN);
_delay_ms(20);
DISPLAY_RST_PORT |= (1 << DISPLAY_RST_PIN);
_delay_ms(150);
displayWriteCommand(ST7735_SWRESET);
_delay_ms(150);
displayWriteCommand(ST7735_SLPOUT);
_delay_ms(255);
// ST7789 specific initialization
displayWriteCommand(0x3A); // COLMOD
displayWriteData(0x05); // 16 bits/pixel (RGB565)
displayWriteCommand(0x36); // MADCTL
displayWriteData(0xC0); // Upside-down landscape, RGB color order
displayWriteCommand(0xB2); // PORCTRL
displayWriteData(0x0C);
displayWriteData(0x0C);
displayWriteData(0x00);
displayWriteData(0x33);
displayWriteData(0x33);
displayWriteCommand(0xB7); // GCTRL
displayWriteData(0x35);
displayWriteCommand(0xBB); // VCOMS
displayWriteData(0x19);
displayWriteCommand(0xC0); // LCMCTRL
displayWriteData(0x2C);
displayWriteCommand(0xC2); // VDVVRHEN
displayWriteData(0x01);
displayWriteCommand(0xC3); // VRHS
displayWriteData(0x12);
displayWriteCommand(0xC4); // VDVS
displayWriteData(0x20);
displayWriteCommand(0xC6); // FRCTRL2
displayWriteData(0x0F);
displayWriteCommand(0xD0); // PWCTRL1
displayWriteData(0xA4);
displayWriteData(0xA1);
displayWriteCommand(0x20); // INVOFF (disable negative image mode)
displayWriteCommand(0x13); // NORON
_delay_ms(10);
displayWriteCommand(ST7735_DISPON); // 0x29
_delay_ms(100);
displayFillScreen(COLOR_BLACK);
}
void displaySetAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
displayWriteCommand(ST7735_CASET);
displayWriteData16(x0);
displayWriteData16(x1);
displayWriteCommand(ST7735_RASET);
displayWriteData16(y0);
displayWriteData16(y1);
displayWriteCommand(ST7735_RAMWR);
}
void displayDrawPixel(uint8_t x, uint8_t y, uint16_t color) {
if (x >= DISPLAY_WIDTH || y >= DISPLAY_HEIGHT) return;
displaySetAddrWindow(x, y, x, y);
displayWriteData16(color);
displayCSHigh();
}
void displayFillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color) {
if (x >= DISPLAY_WIDTH || y >= DISPLAY_HEIGHT) return;
if ((uint16_t)x + w > DISPLAY_WIDTH) w = (uint8_t)(DISPLAY_WIDTH - x);
if ((uint16_t)y + h > DISPLAY_HEIGHT) h = (uint8_t)(DISPLAY_HEIGHT - y);
displaySetAddrWindow(x, y, (uint8_t)(x + w - 1), (uint8_t)(y + h - 1));
displayDCData();
displayCSLow();
uint16_t total = (uint16_t)w * (uint16_t)h;
for (uint16_t i = 0; i < total; i++) {
SPI_Send((uint8_t)(color >> 8));
SPI_Send((uint8_t)(color & 0xFF));
}
displayCSHigh();
}
void displayFillScreen(uint16_t color) {
displayFillRect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, color);
}
void displayDrawText(uint8_t x, uint8_t y, const char *text, uint16_t color, uint16_t bg) {
uint8_t start_x = x;
while (*text) {
if (*text == '\n') {
y += 8;
x = start_x;
} else {
uint8_t c = *text;
if (c < 32 || c > 127) c = 32;
c -= 32;
uint8_t y_offset = 0;
char orig_c = c + 32;
if (orig_c == 'g' || orig_c == 'j' || orig_c == 'p' || orig_c == 'q' || orig_c == 'y') {
y_offset = 2;
}
// Draw background above the shifted character if needed
if (y_offset > 0 && bg != COLOR_BLACK) {
displayFillRect(x, y, 6, y_offset, bg);
}
displaySetAddrWindow(x, y + y_offset, x + 5, y + y_offset + 7);
displayDCData();
for (uint8_t py = 0; py < 8; py++) {
for (uint8_t px = 0; px < 6; px++) {
uint16_t pcolor = bg;
if (px < 5) {
uint8_t col = pgm_read_byte(&font[c * 5 + px]);
if (col & (1 << py)) pcolor = color;
}
SPI_Send((uint8_t)(pcolor >> 8));
SPI_Send((uint8_t)(pcolor & 0xFF));
}
}
displayCSHigh();
x += 6;
}
text++;
}
}
void displayDrawTextScaled(uint8_t x, uint8_t y, const char *text, uint16_t color, uint16_t bg, uint8_t scale) {
uint8_t start_x = x;
while (*text) {
if (*text == '\n') {
y += (uint8_t)(8 * scale);
x = start_x;
} else {
uint8_t c = *text;
if (c < 32 || c > 127) c = 32;
c -= 32;
uint8_t y_offset = 0;
char orig_c = c + 32;
if (orig_c == 'g' || orig_c == 'j' || orig_c == 'p' || orig_c == 'q' || orig_c == 'y') {
y_offset = 2 * scale;
}
uint8_t w = (uint8_t)(6 * scale);
uint8_t h = (uint8_t)(8 * scale);
if (y_offset > 0 && bg != COLOR_BLACK) {
displayFillRect(x, y, w, y_offset, bg);
}
displaySetAddrWindow(x, y + y_offset, x + w - 1, y + y_offset + h - 1);
displayDCData();
for (uint8_t sy = 0; sy < h; sy++) {
uint8_t py = sy / scale;
for (uint8_t sx = 0; sx < w; sx++) {
uint8_t px = sx / scale;
uint16_t pcolor = bg;
if (px < 5) {
uint8_t col = pgm_read_byte(&font[c * 5 + px]);
if (col & (1 << py)) pcolor = color;
}
SPI_Send((uint8_t)(pcolor >> 8));
SPI_Send((uint8_t)(pcolor & 0xFF));
}
}
displayCSHigh();
x += w;
}
text++;
}
}
void displayDrawThemedText(uint8_t x, uint8_t y, const char *text, const uint16_t *colors, uint8_t num_colors, uint16_t bg, uint8_t scale) {
uint8_t start_x = x;
uint8_t color_idx = 0;
while (*text) {
if (*text == '\n') {
y += (uint8_t)(8 * scale);
x = start_x;
} else {
uint8_t c = *text;
if (c != ' ') { // Only advance color if not a space (optional, but looks better)
uint16_t color = colors[color_idx % num_colors];
if (c < 32 || c > 127) c = 32;
c -= 32;
uint8_t y_offset = 0;
char orig_c = c + 32;
if (orig_c == 'g' || orig_c == 'j' || orig_c == 'p' || orig_c == 'q' || orig_c == 'y') {
y_offset = 2 * scale;
}
uint8_t w = (uint8_t)(6 * scale);
uint8_t h = (uint8_t)(8 * scale);
if (y_offset > 0 && bg != COLOR_BLACK) {
displayFillRect(x, y, w, y_offset, bg);
}
displaySetAddrWindow(x, y + y_offset, x + w - 1, y + y_offset + h - 1);
displayDCData();
for (uint8_t sy = 0; sy < h; sy++) {
uint8_t py = sy / scale;
for (uint8_t sx = 0; sx < w; sx++) {
uint8_t px = sx / scale;
uint16_t pcolor = bg;
if (px < 5) {
uint8_t col = pgm_read_byte(&font[c * 5 + px]);
if (col & (1 << py)) pcolor = color;
}
SPI_Send((uint8_t)(pcolor >> 8));
SPI_Send((uint8_t)(pcolor & 0xFF));
}
}
displayCSHigh();
color_idx++;
}
x += (uint8_t)(6 * scale);
}
text++;
}
}