-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtft.c
268 lines (223 loc) · 4.78 KB
/
tft.c
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
/*
* File: tft.c
* Author: [email protected]
*
* Created on 6. November 2023, 18:45
*/
#include "tft.h"
/**
* Does a hardware reset.
*/
static void hwReset(void) {
PORT_DISP &= ~(1 << PIN_DRST);
_delay_ms(10);
PORT_DISP |= (1 << PIN_DRST);
}
static void displaySel(void) {
PORT_DISP &= ~(1 << PIN_DCS);
}
static void displayDes(void) {
PORT_DISP |= (1 << PIN_DCS);
}
/**
* Sets display to send a command.
*/
static void displaySetCmd(void) {
PORT_DISP &= ~(1 << PIN_DDC);
}
/**
* Sets display to send data.
*/
static void displaySetData(void) {
PORT_DISP |= (1 << PIN_DDC);
}
/**
* Sends the given command to the display.
*
* @param cmd
*/
static void displayCmd(uint8_t cmd) {
displaySetCmd();
transmit(cmd);
}
/**
* Sends the given data to the display.
*
* @param data
*/
static void displayData(uint8_t data) {
displaySetData();
transmit(data);
}
/**
* Sets horizontal and/or vertical flip.
*
* @param hflip
* @param vflip
*/
static void madctl(bool hflip, bool vflip) {
// Memory data access control
uint8_t madctl = 0b00110110;
madctl |= (HFLIP << 7);
madctl |= (VFLIP << 6);
madctl |= (BGR << 3);
if (hflip) {
// Row Address Order (MY)
madctl ^= (1 << 7);
}
if (vflip) {
// Column Address Order (MX)
madctl ^= (1 << 6);
}
displaySel();
displayCmd(MADCTL);
displayData(madctl);
displayDes();
}
/**
* Sets the given column start and end address.
*
* @param xs start address
* @param xe end address
*/
static void caset(x_t xs, x_t xe) {
displaySel();
displayCmd(CASET);
displayData(xs >> 8);
displayData(xs);
displayData(xe >> 8);
displayData(xe);
displayDes();
}
/**
* Sets the given row start and end address.
*
* @param ys start address
* @param ye end address
*/
static void raset(y_t ys, y_t ye) {
displaySel();
displayCmd(RASET);
displayData(ys >> 8);
displayData(ys);
displayData(ye >> 8);
displayData(ye);
displayDes();
}
void initDisplay(void) {
_delay_ms(100);
// Hardware reset
hwReset();
// TODO necessary?
_delay_ms(10);
// Software reset
displaySel();
displayCmd(SWRESET);
displayDes();
// TODO necessary?
_delay_ms(10);
// Sleep out & booster on
displaySel();
displayCmd(SLPOUT);
displayDes();
// Partial off (Normal)
displaySel();
displayCmd(NORON);
displayDes();
// Display Inversion on/off
displaySel();
displayCmd(INVOFF + INVERT); // INVOFF + 1 = INVON
displayDes();
// Interface pixel format
displaySel();
displayCmd(COLMOD);
displayData(0b00111101);
displayDes();
// Display on
displaySel();
displayCmd(DISPON);
displayDes();
// Sleep in & booster off
// displaySel();
// displayCmd(SLPIN);
// displayDes();
printString("Display init done\r\n");
}
void demoDisplay(void) {
// TODO
}
void drawPixel(x_t x, y_t y, uint16_t color) {
// TODO
}
void drawCircle(x_t x, y_t y, uint16_t radius, uint16_t color) {
// TODO
}
void drawRectangle(x_t x, y_t y, width_t width, height_t height,
uint16_t color) {
// TODO
}
void writeStart(void) {
// Memory write
displaySel();
displayCmd(RAMWR);
displaySetData();
}
void writeRestart(void) {
displaySel();
}
void writeByte(uint8_t byte) {
// Memory write
transmit(byte);
}
void writeEnd(void) {
// Memory write
displayDes();
}
void fillArea(x_t x, x_t y,
width_t width, height_t height,
uint16_t color) {
madctl(false, false);
// X address start/end
uint16_t xs = x;
uint16_t xe = x + width - 1;
caset(xs, xe);
// Y address start/end
uint16_t ys = y;
uint16_t ye = y + height - 1;
raset(ys, ye);
writeStart();
bytes_t pixels = (bytes_t)width * (bytes_t)height;
for (bytes_t i = 0; i < pixels; i++) {
transmit(color >> 8);
transmit(color);
}
writeEnd();
}
void setArea(x_t x, y_t y,
width_t width, height_t height,
bool hflip, bool vflip) {
madctl(hflip, vflip);
// X address start/end
uint16_t xs = x;
uint16_t xe = x + width - 1;
if (hflip) {
xs = DISPLAY_WIDTH - x - width;
xe = DISPLAY_WIDTH - x - 1;
}
caset(xs, xe);
// Y address start/end
uint16_t ys = y;
uint16_t ye = y + height - 1;
if (vflip) {
ys = DISPLAY_HEIGHT - y - height;
ye = DISPLAY_HEIGHT - y - 1;
}
raset(ys, ye);
}
void writeData(const __flash uint8_t *bitmap,
width_t width, height_t height,
space_t space, uint16_t bg, uint16_t fg) {
writeStart();
writeSpace(bitmap, width, height, space, bg, fg);
writeEnd();
}