-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.ino
284 lines (239 loc) · 5.4 KB
/
lcd.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
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
#define TFT_DC 2
#define TFT_CS 15
#define BUFFPIXEL 60 // must be a divisor of 240
#define BUFFPIXEL_X3 BUFFPIXEL*3 // BUFFPIXELx3
uint16_t idx,row,j,n;
int state;
uint8_t pxbuffer[BUFFPIXEL_X3]; // 3 * pixels to buffer
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define MADCTL_MY 0x80
#define MADCTL_MX 0x40
#define MADCTL_MV 0x20
#define MADCTL_ML 0x10
#define MADCTL_RGB 0x00
#define MADCTL_BGR 0x08
#define MADCTL_MH 0x04
void lcd_init()
{
tft.begin();
tft.setRotation(2);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setTextWrap(true);
}
void bmpPUTinit()
{
state = 0;
row = 319;
idx = 0;
j=0;
n=0;
}
bool bmpPUT(HTTPUpload& upload)
{
if(upload.currentSize == 0)
{
debugPrintln("Empty PUT",ILI9341_RED);
state = 0;
return false;
}
switch(state)
{
case 0:
if(bmpCheckHeader(upload.buf))
{
bmpDraw(upload.buf,upload.currentSize);
state++;
}
else
state = -1;
break;
case -1:
return false;
default:
idx=0;
bmpDraw(upload.buf,upload.currentSize);
break;
}
return true;
}
void bmpDraw(uint8_t* data,uint32_t size)
{
for (; row>= 0; row--)
{
for(; j<(240/BUFFPIXEL); j++)
{
for(;n<BUFFPIXEL_X3;n++)
{
pxbuffer[n] = data[idx++];
if(idx>size)
return;
}
n=0;
uint8_t buffidx = 0;
int offset_x = j*BUFFPIXEL;
for(int k=0; k<BUFFPIXEL; k++)
{
tft.drawPixel(j*BUFFPIXEL+k,row,tft.color565(pxbuffer[buffidx+2],pxbuffer[buffidx+1],pxbuffer[buffidx+0]));
buffidx += 3;
}
}
j=0;
}
}
void bmpDraw(WiFiClient f)
{
uint32_t time = millis();
for (int i=319; i>= 0; i--)
{
for(j=0; j<240; j++)
{
while(f.available() < 3)
yield();
uint8_t b = f.read();
uint8_t g = f.read();
uint8_t r = f.read();
tft.drawPixel(j,i,tft.color565(r,g,b));
}
}
Serial.print(millis() - time, DEC);
Serial.println(" ms");
}
boolean bmpCheckHeader(uint8_t* f)
{
// read header
uint32_t tmp;
uint8_t bmpDepth;
if (read16(f) != 0x4D42) {
Serial.println("Magic bytes missing");
return false;
}
// read file size
tmp = read32(f);
Serial.print("size 0x");
Serial.println(tmp, HEX);
// read and ignore creator bytes
read32(f);
uint32_t offset = read32(f);
Serial.print("offset ");
Serial.println(offset, DEC);
// read DIB header
tmp = read32(f);
Serial.print("header size ");
Serial.println(tmp, DEC);
int bmp_width = read32(f);
int bmp_height = read32(f);
if(bmp_width != 240 || bmp_height != 320) // if image is not 320x240, return false
{
return false;
}
if (read16(f) != 1)
return false;
bmpDepth = read16(f);
Serial.print("bitdepth ");
Serial.println(bmpDepth, DEC);
if (read32(f) != 0) {
Serial.println("Compression not supported!");
return false;
}
read32(f); //imagesizes
read32(f);//horizontal resolution
read32(f);//vertical resolution
read32(f);//number of colors
read32(f);//number of important colors
tft.setCursor(0, 0);
return true;
}
boolean bmpReadHeader(WiFiClient f)
{
// read header
uint32_t tmp;
uint8_t bmpDepth;
if (read16(f) != 0x4D42) {
Serial.println("Magic bytes missing");
return false;
}
// read file size
tmp = read32(f);
Serial.print("size 0x");
Serial.println(tmp, HEX);
// read and ignore creator bytes
read32(f);
uint32_t offset = read32(f);
Serial.print("offset ");
Serial.println(offset, DEC);
// read DIB header
tmp = read32(f);
Serial.print("header size ");
Serial.println(tmp, DEC);
int bmp_width = read32(f);
int bmp_height = read32(f);
if(bmp_width != 240 || bmp_height != 320) // if image is not 320x240, return false
{
Serial.println("Wrong dimensions");
return false;
}
if (read16(f) != 1)
{
Serial.println("Wrong # color planes");
return false;
}
bmpDepth = read16(f);
Serial.print("bitdepth ");
Serial.println(bmpDepth, DEC);
if (read32(f) != 0) {
Serial.println("Compression not supported!");
return false;
}
read32(f); //imagesizes
read32(f);//horizontal resolution
read32(f);//vertical resolution
read32(f);//number of colors
read32(f);//number of important colors
tft.setCursor(0, 0);
return true;
}
// LITTLE ENDIAN!
uint16_t read16(WiFiClient f)
{
uint16_t d;
uint8_t b;
b = f.read();
d = f.read();
d <<= 8;
d |= b;
return d;
}
// LITTLE ENDIAN!
uint32_t read32(WiFiClient f)
{
uint32_t d;
uint16_t b;
b = read16(f);
d = read16(f);
d <<= 16;
d |= b;
return d;
}
// LITTLE ENDIAN!
uint16_t read16(uint8_t* data)
{
uint16_t d;
uint8_t b;
b = data[idx++];
d = data[idx++];
d <<= 8;
d |= b;
return d;
}
// LITTLE ENDIAN!
uint32_t read32(uint8_t* data)
{
uint32_t d;
uint16_t b;
b = read16(data);
d = read16(data);
d <<= 16;
d |= b;
return d;
}