-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
325 lines (209 loc) · 5.45 KB
/
main.cpp
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
#include <Arduino.h>
// core graphics library (written by Adafruit)
#include <Adafruit_GFX.h>
// Hardware-specific graphics library for MCU Friend 3.5" TFT LCD shield
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>
#include <SD.h>
#include <math.h>
#define SD_CS 10
#define JOY_VERT A9 // should connect A9 to pin VRx
#define JOY_HORIZ A8 // should connect A8 to pin VRy
#define JOY_SEL 53
#define JOY_CENTER 512
#define JOY_DEADZONE 64
#define CURSOR_SIZE 9
#define BULLET_SIZE 4
#define DISPLAY_WIDTH 480
#define DISPLAY_HEIGHT 320
#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
#define TS_MINX 100
#define TS_MINY 110
#define TS_MAXX 960
#define TS_MAXY 910
// thresholds to determine if there was a touch
#define MINPRESSURE 10
#define MAXPRESSURE 1000
MCUFRIEND_kbv tft;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
/*void redrawCursor(uint16_t colour, int x, int y);*/
bool shot = false;
struct tank
{
int x, y, bullets = 0;
tank(int inputx, int inputy){
x = inputx;
y = inputy;
}
void redrawCursor(uint16_t colour, int &x, int &y) {
tft.fillRect(x - CURSOR_SIZE/2, y - CURSOR_SIZE/2,
CURSOR_SIZE, CURSOR_SIZE, colour);
}
void update(){
//obtaining the velocity by scaling down the values obtained from the x and y pins
//so we won't go too fast, then subtracting so that while the joystick is
//centered we will always get a velocity of 0.
int velX = abs(0.002 * analogRead(JOY_HORIZ) - 10);
int velY = abs(0.002 * analogRead(JOY_VERT) - 10);
// so we only redraw the screen when we are moving
bool moving = false;
int xVal = analogRead(JOY_HORIZ);
int yVal = analogRead(JOY_VERT);
int buttonVal = digitalRead(JOY_SEL);
// for redrawing later
int oldY = y;
int oldX = x;
// now move the cursor
if (yVal < JOY_CENTER - JOY_DEADZONE) {
y -= (1 + velY - 9); // decrease the y coordinate of the cursor
moving = true;
}
else if (yVal > JOY_CENTER + JOY_DEADZONE) {
y += 1 + velY - 6;
moving = true;
}
// remember the x-reading increases as we push left
if (xVal > JOY_CENTER + JOY_DEADZONE) {
x -= (1 + velX - 6);
moving = true;
}
else if (xVal < JOY_CENTER - JOY_DEADZONE) {
x += 1 + velX - 9;
moving = true;
}
// check if at edge, if so, redraw next section
// draw a small patch of the Edmonton map at the old cursor position before
// drawing a red rectangle at the new cursor position
// redrawing patch at old position.
if (moving){
tft.fillRect(oldX - CURSOR_SIZE/2, oldY - CURSOR_SIZE/2,
CURSOR_SIZE, CURSOR_SIZE, TFT_BLACK);
redrawCursor(TFT_RED, x, y);
}
delay(20);
}
};
struct bullet
{
bool active = 0;
int x, y, velX = 5, velY = 5;
int bounce = 0;
bullet(int inputx = 0, int inputy = 0){
x = inputx;
y = inputy;
}
void updateBullet(int &numBullets){
//Serial.println(bounce);
if (!active){
//Serial.print("notactive");
return;
}
if (bounce > 2){
active = 0;
bounce = 0;
tft.fillCircle(x, y, BULLET_SIZE, TFT_BLACK);
//numBullets--;
//Serial.print("boom");
return;
}
tft.fillCircle(x, y, BULLET_SIZE, TFT_BLACK);
x += velX;
y += velY;
if (x < 0){
x = 0;
velX *= -1;
bounce++;
}
if (x > DISPLAY_WIDTH){
x = DISPLAY_WIDTH;
velX *= -1;
bounce++;
}
if (y <=0 ){
y = 0;
velY *=-1;
bounce++;
}
if (y >= DISPLAY_HEIGHT){
y = DISPLAY_HEIGHT;
velY *= -1;
bounce++;
}
tft.fillCircle(x, y, BULLET_SIZE, TFT_BLUE);
}
void fire(int tapX, int tapY){
float vecX = tapX - x;
float vecY = -1*(tapY - y);
float radAng = atan(vecY/vecX);
if ( (vecY > 0 && vecX < 0) ){
radAng += 3.14;
}
if (vecY < 0 && vecX < 0){
radAng -= 3.14;
}
if ()
active = 1;
}
};
void setup(){
init();
Serial.begin(9600);
pinMode(JOY_SEL, INPUT_PULLUP);
// tft.reset(); // hardware reset
uint16_t ID = tft.readID(); // read ID from display
Serial.print("ID = 0x");
Serial.println(ID, HEX);
if (ID == 0xD3D3) ID = 0x9481; // write-only shield
// must come before SD.begin() ...
tft.begin(ID); // LCD gets ready to work
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("failed! Is it inserted properly?");
while (true) {}
}
Serial.println("OK!");
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
}
void readTouch(int &cooldown, bullet bullArray[], tank &Atank){
TSPoint touch = ts.getPoint();
pinMode(YP, OUTPUT);
pinMode(XM, OUTPUT);
if ( (touch.z > MINPRESSURE && touch.z < MAXPRESSURE ) && Atank.bullets <= 5 ){
bullet bull = bullArray[Atank.bullets];
bull.x = Atank.x;
bull.y = Atank.y;
int touchX = map(touch.y, TS_MINX, TS_MAXX, DISPLAY_WIDTH - 1, 0);
int touchY = map(touch.x, TS_MINY, TS_MAXY, DISPLAY_HEIGHT - 1, 0);
bull.fire(touchX, touchY);
bullArray[Atank.bullets] = bull;
Atank.bullets += 1;
cooldown = millis();
}
}
int main(){
setup();
tank thisTank(50, 50);
int cooldown = 0;
bullet bullArray[5] =
{
bullet(20, 20),
bullet(20, 20),
bullet(20, 20),
bullet(20, 20),
bullet(20, 20)
};
while(1){
thisTank.update();
if (millis() - cooldown > 1000){
readTouch(cooldown, bullArray, thisTank);
}
for (int i = 0; i < thisTank.bullets; i++){
bullArray[i].updateBullet(thisTank.bullets);
//Serial.println(thisTank.bullets);
}
}
}