-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackbone.ino
More file actions
417 lines (348 loc) · 13.3 KB
/
Backbone.ino
File metadata and controls
417 lines (348 loc) · 13.3 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* Backbone.ino - this file is the core of the program. It contains functions and variables
* pertaining to Backbone, the underlying graphics and OS functions that run the program
* itself. Variables and functionsthat need to passed or used between program states must
* be declared here in order to be used.
*/
#include <StandardCplusplus.h>
#include <system_configuration.h>
#include <unwind-cxx.h>
#include <utility.h>
#include <vector>
#include <Elegoo_GFX.h> // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h> // touch screen library
using namespace std;
/*
* #########################################################################
* ### ###
* ### UI Vars ###
* ### ###
* #########################################################################
*/
// COLORS
#define BLACK 0x0000
#define LIGHTGRAY 0xCE79
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define DARKGRAY 0x9CD3
#define LIGHTBLUE 0xAD5F
#define ORANGE 0xECA0
#define DARKGREEN 0x0B80
#define BROWN 0x7260
#define PALE 0xE637
#define GOLD 0xD566
#define BOXSIZE 30
#define spriteTileW 48
/* button struct */
struct Button {
int x;
int y;
int w;
int h;
unsigned backgroundColor;
unsigned frameColor;
unsigned textColor;
char* text;
void (*callFunc)(int par);
int p;
};
const unsigned colors[16] = {BLACK, DARKGRAY, RED, YELLOW, GOLD, GREEN, CYAN, MAGENTA,
WHITE, LIGHTGRAY, PALE, ORANGE, BROWN, DARKGREEN, BLUE, LIGHTBLUE};
#define numSprites 15
byte sprites[numSprites][8][8];
/*
* #########################################################################
* ### ###
* ### LCD Touch Screen vars ###
* ### ###
* #########################################################################
*/
// pin bullshit
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
#define MINPRESSURE 10
#define MAXPRESSURE 1000
#define TS_MINX 120
#define TS_MAXX 900
#define TS_MINY 70
#define TS_MAXY 920
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
bool resetFramesFlag = false;
int framesSinceTouch = 0;
/*
* #########################################################################
* ### ###
* ### Backbone / OS Vars ###
* ### ###
* #########################################################################
*/
// Os state variables
#define PUSH 0
#define POP 1
/* OS STATES */
#define SPRITE_MAKER 0
#define SPRITE_MANAGER 1 // screen that shows each sprite that can be selected
#define BASE_ENGINE 2 // base engine things
#define KEYBOARD_INPUT 3
// dynamic array of OSState used in popState() and pushToState()
vector<int>* programStack;
// collection of buttons used in drawing
vector<Button*>* buttons;
// osState: current screen of the OS
int osState = SPRITE_MANAGER;
// functions that draw each OS State
void (*drawFuncs[])(void) = {&drawSpriteMaker,
&drawSpriteManager,
&drawEngine,
&drawKeyboard};
// functions that get called every loop for each OS State
void (*runFuncs[])(void) = {&runSpriteMaker,
&runSpriteManager,
&runEngine,
&runKeyboard};
/*
* #########################################################################
* ### ###
* ### Shared Vars Between States ###
* ### ###
* #########################################################################
*/
// state variable passed between Sprite Manager and Sprite Maker
int currentSprite = 0;
// keyboard string variable.
char keyboardS[100];
int keyboardSL = 0;
/*
* #########################################################################
* ### ###
* ### Backbone / OS Funcs ###
* ### ###
* #########################################################################
*/
/* getTouchPoint() - returns a TSPoint variable representing the point
* that is being touched. X and Y represent coordinates
* on the screen. Z represents touch strength. If Z = 500,
* then the touch is valid / strong enough.
* Example use:
* void foo(){
* TSPoint p = getTouchPoint();
* if (p.z > 500){
* Serial.print("x is ");
* Serial.print(p.x);
* Serial.print(" and y is ");
* Serial.print(p.y);
* Serial.print("\n");
* }
* else{
* Serial.println("Not pressing hard enough!");
* }
* }
*/
TSPoint getTouchPoint(){
if (resetFramesFlag == true){
framesSinceTouch = 0;
resetFramesFlag = false;
}
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
int x = p.x;
int y = p.y;
// rotates touch position to match orientation of screen
p.x = map(y, TS_MINY, TS_MAXY, tft.width(), 0);
p.y = map(x, TS_MINX, TS_MAXX, tft.height(), 0);
// set p to 500 if its pressed enough (500 is the signal code)
p.z = 500;
resetFramesFlag = true;
}
else{
framesSinceTouch++;
}
return p;
}
/* isNewTouch() returns whether the given touch (the last touch given from getTouchPoint()) is a new touch
or a continuation of a previous touch. Best called right after getTouchPoint().*/
bool isNewTouch(){
if (framesSinceTouch > 100){
return true;
}
return false;
}
/* setup() - Arduino's required "setup()" function that initializes all necessary
* OS Variables, as well as the touch screen and the graphics library.
*/
void setup(void) {
programStack = new vector<int>();
buttons = new vector<Button*>();
Serial.begin(9600);
#ifdef USE_Elegoo_SHIELD_PINOUT
Serial.println(F("Using Elegoo 2.4\" TFT Arduino Shield Pinout"));
#else
Serial.println(F("Using Elegoo 2.4\" TFT Breakout Board Pinout"));
#endif
Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
tft.reset();
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
Serial.println(F("Found ILI9325 LCD driver"));
} else if(identifier == 0x9328) {
Serial.println(F("Found ILI9328 LCD driver"));
} else if(identifier == 0x4535) {
Serial.println(F("Found LGDP4535 LCD driver"));
}else if(identifier == 0x7575) {
Serial.println(F("Found HX8347G LCD driver"));
} else if(identifier == 0x9341) {
Serial.println(F("Found ILI9341 LCD driver"));
} else if(identifier == 0x8357) {
Serial.println(F("Found HX8357D LCD driver"));
} else if(identifier==0x0101)
{
identifier=0x9341;
Serial.println(F("Found 0x9341 LCD driver"));
}
else if(identifier==0x1111)
{
identifier=0x9328;
Serial.println(F("Found 0x9328 LCD driver"));
}
else {
Serial.print(F("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
identifier=0x9328;
}
tft.begin(identifier);
tft.setRotation(1);
pushToState(osState);
}
/* loop() - Arduino's required "loop()" function that actually runs the project. Calls
* a corresponding run function depending on the current state of the OS. No
* run functions are allowed to block the OS, i.e. no infinite loops inside.
*/
void loop(void) {
runFuncs[osState]();
}
/* pushToState() - OS Function that pushes to a new state. state corresponds to an index in the drawFuncs and
* runFuncs array. Each OS State has a draw function and a run function associated with it and
* should be #defined at the top of this file under "OS States".
* Inputs:
* state - the index of the OS State to push to.
*/
void pushToState(int state){
clearButtons();
programStack->push_back(state);
drawFuncs[state]();
osState = state;
}
/* popState() - OS Function that pops back to the previous state.
* Inputs:
* rip - this variable is not used but is part of the header in order to
* unify the function headers for popState and pushToState
*/
void popState(int rip){
programStack->pop_back();
int state = programStack->back();
pushToState(state);
}
/*
* #########################################################################
* ### ###
* ### UI Functions ###
* ### ###
* #########################################################################
*/
/* makeButton() - UI Function that creates a button struct with a given set of variables. DOES NOT
* DRAW THE ACTUAL BUTTON. To draw a button, see "drawButton()" down below.
* Inputs:
* x - the x coordinate to draw at. Top left corner is 0,0.
* y - the y coordinate to draw at. Top left corner is 0,0.
* w - the width of the button in pixels.
* h - the height of the button in pixels.
* background - the color of the background for the button.
* frame - the color of the frame around the button.
* textColor - the color of whatever text is on the button.
* text - the string to display over the button.
* callFunc - the function to call when the button is called.
* par - the variable to pass in to callFunc when it gets called.
*/
Button* makeButton(int x, int y, int w, int h, unsigned background, unsigned frame, unsigned textColor, char* text, void (*callFunc)(int x), int par){
Button *newBut = malloc(sizeof(Button));
newBut->x = x;
newBut->y = y;
newBut->w = w;
newBut->h = h;
newBut->backgroundColor = background;
newBut->frameColor = frame;
newBut->text = text;
newBut->textColor = textColor;
newBut->callFunc = callFunc;
newBut->p = par;
buttons->push_back(newBut);
return newBut;
}
/* clearButtons() - Clears the buttons array and frees all memory. Called on pushToState and popState.
*/
void clearButtons(){
for (int i = 0; i < buttons->size(); i++){
Button *b = buttons->at(i);
delete b;
}
buttons->clear();
}
/* drawRect() draws a rectangle (does not fill in). Used to hide the tft object from other places.*/
void drawRect(int x, int y, int w, int h, unsigned c){
tft.drawRect(x, y, w, h, c);
}
/* fillRect() fills in a rectangle. Used to hide the tft object from other places (ABSTRACTION)*/
void fillRect(int x, int y, int w, int h, unsigned c){
tft.fillRect(x, y, w, h, c);
}
/* drawButton() - draws a given button.
* Inputs:
* but - the button struct to draw. Must be created using makeButton() or manually.
*/
void drawButton(Button* but){
tft.fillRect(but->x, but->y, but->w, but->h, but->backgroundColor);
tft.drawRect(but->x - 1, but->y - 1, but->w + 2, but->h + 2, but->frameColor);
tft.setCursor(but->x + 3, but->y + 3);
tft.setTextColor(but->textColor);
tft.setTextSize(1);
tft.println(but->text);
}
void drawText(int x, int y, int size, char *text, unsigned color){
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextSize(size);
tft.println(text);
}
/* drawSprite() - draws a sprite at a given set of coordinates.
* Inputs:
* x - the x coordinate to draw at. Top left corner is 0,0.
* y - the y coordinate to draw at. Top left corner is 0,0.
*/
void drawSprite(int x, int y, int spriteN){
int squareP = spriteTileW / 8;
for (int j = 0; j < 8; j++){
for (int i = 0; i < 8; i++){
tft.fillRect(x + i * squareP, y + j * squareP, squareP, squareP, colors[sprites[spriteN][i][j]]);
}
}
}