-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoTFT28.ino
371 lines (323 loc) · 9.66 KB
/
ArduinoTFT28.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
*
* Copyright (c) 2019 Matrix Orbital, Corp. https://www.matrixorbital.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <stdint.h> // Find types like "uint8_t"
#include <stdbool.h> // Find type "bool"
#include <SPI.h> // Arduino SPI library
#include "ILI9341_HAL.h"
#include "graphic.h"
#include "calibrate.h"
#include "touch.h"
#include "ArduinoTFT28.h"
color_type LineColor, BlankingColor;
uint64_t Time2Draw = 0;
uint64_t Time2ReadTouch = 0;
char LogBuf[WorkBuffSz];
int16_t SS_x1, SS_y1, SS_x2, SS_y2, dx1, dy1, dx2, dy2, stride;
uint16_t ChangeInterval = 0;
bool RandomLines = false;
bool greenTouched = false;
bool redTouched = false;
bool middleTouched = false; // middle square starts the random lines
bool touched = false;
void DrawSquares(void);
void UpdateSquares(void);
void setup()
{
MyDelay(5000);
Serial.begin(115200);
InitPin(GPIO_19, PIN_OUTPUT); // this is BL (A4 on schematic)
InitPin(GPIO_7, PIN_OUTPUT); // TFT_CS (D7 on schematic)
InitPin(GPIO_3, PIN_OUTPUT); // TFT_DC (D3 on schematic)
InitPin(GPIO_5, PIN_INPUT); // TFT_DC (D3 on schematic)
SetPin(GPIO_6, true); // TPR_CS - make sure touch driver is unselected
SetPin(GPIO_19, true);
SPI.begin();
SPI.beginTransaction(SPISettings(6000000, MSBFIRST, SPI_MODE0));
HAL_ScreenInit();
ClearScreen(0,0,0);
Touch_Init();
// perform calibration, try as many times if calibration fails
char* goodCal = "Calibrated"; // xy = 3,150
char* badCal = "NOT"; // xy = 2, 130
char* restartCal = "restarting"; // xy = 2, 130
bool res;
do
{
ClearScreen(0,0,0);
res = Calibrate_REStp();
if (res == false)
{
Log("not successful\n");
ClearScreen(0,0,0);
writeString(6, 120, badCal);
writeString(3, 150, goodCal);
MyDelay(1500);
ClearScreen(0,0,0);
writeString(3, 150, restartCal);
MyDelay(1500);
}
else
{
Log("successful\n");
ClearScreen(0,0,0);
writeString(3, 150, goodCal);
}
} while (res == false);
MyDelay(1000);
ClearScreen(0,0,0);
DrawSquares();
stride = 3;
dx1 = stride;
dx2 = stride;
dy1 = stride;
dy2 = stride;
SS_x1 = 0; SS_x2 = 45; SS_y1 = 0; SS_y2 = 25;
BlankingColor.Red = 0xFF;
BlankingColor.Green = 0;
BlankingColor.Blue = 0;
LineColor.Red = 0;
LineColor.Green = 0xFF;
LineColor.Blue = 0xFF;
Log("Startup Done\n");
}
void loop()
{
if ( MyMillis() >= Time2ReadTouch )
{
Time2ReadTouch = MyMillis() + 100;
// check if there is something to read on the touch controller TSC2046
if (digitalRead(GPIO_5) == 0)
{
if (ReadTouch())
UpdateSquares();
}
}
if (RandomLines == true)
{
if ( MyMillis() >= Time2Draw )
{
Time2Draw = MyMillis() + 3;
if (ChangeInterval++ == 800)
{
BlankingColor.Red += (45 + (8 * random(16)));
BlankingColor.Green -= 55;
BlankingColor.Blue += (25 + (8 * random(16)));
ChangeInterval = 0;
ClearScreen(0,0,0);
}
line(SS_x1, SS_y1, SS_x2, SS_y2, BlankingColor);
if ((SS_x1 + dx1) < DISPLAY_PWIDTH)
SS_x1 += dx1;
else
dx1 = -(2 + ((stride * random(16)) >> 4)); // "random" number between 0 and 15 then divided by 16
if ((SS_x1 + dx1) >= 0)
SS_x1 += dx1;
else
dx1 = (2 + ((stride * random(16)) >> 4)); // "random" number between 0 and 15 then divided by 16
if ((SS_x2 + dx2) < DISPLAY_PWIDTH)
SS_x2 += dx2;
else
dx2 = -(2 + ((stride * random(16)) >> 4)); // "random" number between 0 and 15 then divided by 16
if ((SS_x2 + dx2) >= 0)
SS_x2 += dx2;
else
dx2 = (2 + ((stride * random(16)) >> 4)); // "random" number between 0 and 15 then divided by 16
if ((SS_y1 + dy1) < DISPLAY_PHEIGHT)
SS_y1 += dy1;
else
dy1 = -(2 + ((stride * random(16)) >> 4)); // "random" number between 0 and 15 then divided by 16
if ((SS_y1 + dy1) >= 0)
SS_y1 += dy1;
else
dy1 = (2 + ((stride * random(16)) >> 4)); // "random" number between 0 and 15 then divided by 16
if ((SS_y2 + dy2) < DISPLAY_PHEIGHT)
SS_y2 += dy2;
else
dy2 = -(2 + ((stride * random(16)) >> 4)); // "random" number between 0 and 15 then divided by 16
if ((SS_y2 + dy2) >= 0)
SS_y2 += dy2;
else
dy2 = (2 + ((stride * random(16)) >> 4)); // "random" number between 0 and 15 then divided by 16
// Check check check
if (SS_x1 < 0)
SS_x1 = 0;
if (SS_x1 >= DISPLAY_PWIDTH)
SS_x1 = DISPLAY_PWIDTH - 1;
if (SS_x2 < 0)
SS_x2 = 0;
if (SS_x2 >= DISPLAY_PWIDTH)
SS_x2 = DISPLAY_PWIDTH - 1;
if (SS_y1 < 0)
SS_y1 = 0;
if (SS_y1 >= DISPLAY_PHEIGHT)
SS_y1 = DISPLAY_PHEIGHT - 1;
if (SS_y2 < 0)
SS_y2 = 0;
if (SS_y2 >= DISPLAY_PHEIGHT)
SS_y2 = DISPLAY_PHEIGHT - 1;
line(SS_x1, SS_y1, SS_x2, SS_y2, LineColor);
}
}
}
static void writeString(uint16_t x, uint16_t y, char* data)
{
for (uint8_t i = 0; i < strlen(data); i++)
displayChar((x+i)*16, y, data[i]); // 16 spaces in between start of new char
}
void UpdateSquares(void)
{
static uint8_t lastUpdate = 2; // 0-green 1-red 2-white
color_type redBtn, grnBtn, clearBtn;
clearBtn.Red = 0x00;
clearBtn.Green = 0x00;
clearBtn.Blue = 0x00;
grnBtn.Red = 0x00;
grnBtn.Green = 0xFF;
grnBtn.Blue = 0x00;
redBtn.Red = 0xFF;
redBtn.Green = 0x00;
redBtn.Blue = 0x00;
if (RandomLines)
{
if (touched)
{
RandomLines = false;
touched = false;
DrawSquares();
lastUpdate = 2; // back to white middle square
}
}
else
{
if (greenTouched)
{
if (lastUpdate != 0) // if not yet green
{
filledrect(89, 43, 146, 106, clearBtn);
filledrect(89, 43, 146, 106, grnBtn);
Log("Green drawn\n");
lastUpdate = 0;
}
greenTouched = false;
}
else if (redTouched)
{
if (lastUpdate != 1) // if not yet red
{
filledrect(89, 43, 146, 106, clearBtn);
filledrect(89, 43, 146, 106, redBtn);
Log("Red drawn\n");
lastUpdate = 1;
}
redTouched = false;
}
else if (middleTouched)
{
middleTouched = false;
RandomLines = true;
ClearScreen(0,0,0);
}
}
}
void DrawSquares(void)
{
ClearScreen(0,0,0);
color_type redBtn, grnBtn, waitBtn;
waitBtn.Red = 0xFF;
waitBtn.Green = 0xFF;
waitBtn.Blue = 0xFF;
rect(89, 43, 146, 106, waitBtn);
grnBtn.Red = 0x00;
grnBtn.Green = 0xFF;
grnBtn.Blue = 0x00;
rect(35, 186, 91, 250, grnBtn);
redBtn.Red = 0xFF;
redBtn.Green = 0x00;
redBtn.Blue = 0x00;
rect(141, 186, 198, 250, redBtn);
}
void DebugPrint(char *str)
{
Serial.print(str);
}
// A millisecond delay wrapper for the Arduino function
void MyDelay(uint32_t DLY)
{
uint32_t wait;
wait = millis() + DLY; while (millis() < wait);
}
// Externally accessible abstraction for millis()
uint32_t MyMillis(void)
{
return millis();
}
// Interrupt Related Wrappers
void DisableInt(void)
{
noInterrupts();
}
void EnableInt(void)
{
interrupts();
}
// GPIO Related Wrappers
void InitPin(uint8_t PinNum, uint8_t PinMode)
{
pinMode(PinNum, PinMode);
}
// An abstracted pin write that may be called from outside this file.
void SetPin(uint8_t pin, bool state)
{
digitalWrite(pin, state);
}
// An abstracted pin read that may be called from outside this file.
uint8_t ReadPin(uint8_t pin)
{
return (digitalRead(pin));
}
//====================================== SPI FUNCTION ABSTRACTIONS ============================================
// Send a byte through SPI as part of a larger transmission. Does not enable/disable SPI CS
void SPI_Write(uint8_t data)
{
// Log("W-0x%02X\n", data);
SPI.transfer(data);
}
uint8_t SPI_Read(void)
{
return (SPI.transfer(0x00));
}
uint8_t SPI_RW(uint8_t data)
{
return (SPI.transfer(data));
}
// Read a series of bytes from SPI and store them in a buffer
void SPI_ReadBuffer(uint8_t *Buffer, uint32_t Length)
{
// uint8_t a = SPI.transfer(0x00); // dummy read
while (Length--)
{
*(Buffer++) = SPI.transfer(0x00);
}
}