-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
368 lines (321 loc) · 8.93 KB
/
Copy pathmain.cpp
File metadata and controls
368 lines (321 loc) · 8.93 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
// main.cpp
// Runs on LM4F120/TM4C123
// Aarushi Ramesh and Melanie Boleng
// This is a starter project for the EE319K Lab 10 in C++
// Last Modified: 4/19/2018
// http://www.spaceinvaders.de/
// sounds at http://www.classicgaming.cc/classics/spaceinvaders/sounds.php
// http://www.classicgaming.cc/classics/spaceinvaders/playguide.php
/* This example accompanies the books
"Embedded Systems: Real Time Interfacing to Arm Cortex M Microcontrollers",
ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2017
"Embedded Systems: Introduction to Arm Cortex M Microcontrollers",
ISBN: 978-1469998749, Jonathan Valvano, copyright (c) 2017
Copyright 2018 by Jonathan W. Valvano, valvano@mail.utexas.edu
You may use, edit, run or distribute this file
as long as the above copyright notice remains
THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
For more information about my classes, my research, and my books, see
http://users.ece.utexas.edu/~valvano/
*/
// ******* Possible Hardware I/O connections*******************
// Slide pot pin 1 connected to ground
// Slide pot pin 2 connected to PD2/AIN5
// Slide pot pin 3 connected to +3.3V
// fire button connected to PE0
// special weapon fire button connected to PE1
// 8*R resistor DAC bit 0 on PB0 (least significant bit)
// 4*R resistor DAC bit 1 on PB1
// 2*R resistor DAC bit 2 on PB2
// 1*R resistor DAC bit 3 on PB3 (most significant bit)
// LED on PB4
// LED on PB5
// Backlight (pin 10) connected to +3.3 V
// MISO (pin 9) unconnected
// SCK (pin 8) connected to PA2 (SSI0Clk)
// MOSI (pin 7) connected to PA5 (SSI0Tx)
// TFT_CS (pin 6) connected to PA3 (SSI0Fss)
// CARD_CS (pin 5) unconnected
// Data/Command (pin 4) connected to PA6 (GPIO), high for data, low for command
// RESET (pin 3) connected to PA7 (GPIO)
// VCC (pin 2) connected to +3.3 V
// Gnd (pin 1) connected to ground
#include <stdint.h>
#include "../inc/tm4c123gh6pm.h"
#include "PLL.h"
#include "ST7735.h"
#include "Random.h"
#include "PLL.h"
#include "SlidePot.h"
#include "Images.h"
#include "UART.h"
#include "Timer0.h"
//#include "Sound.h"
#include "DAC.h"
#include "Timer1.h"
#include "Timer2.h"
void altSongPlay(void);
//extern "C" void Timer2A_Handler(void);
SlidePot my(2000,0);
uint32_t Data = 0;
uint32_t Position = 0;
uint32_t x = 53;
uint32_t y = 137;
uint32_t score = 0;
extern "C" void DisableInterrupts(void);
extern "C" void EnableInterrupts(void);
extern "C" void SysTick_Handler(void);
// Period = 80000000/64/Freq=1250000/Freq
#define C1 597 // 2093 Hz
#define B1 633 // 1975.5 Hz
#define BF1 670 // 1864.7 Hz
#define A1 710 // 1760 Hz
#define AF1 752 // 1661.2 Hz
#define G1 797 // 1568 Hz
#define GF1 845 // 1480 Hz
#define F1 895 // 1396.9 Hz
#define E1 948 // 1318.5 Hz
#define EF1 1004 // 1244.5 Hz
#define D1 1064 // 1174.7 Hz
#define DF1 1127 // 1108.7 Hz
#define C 1194 // 1046.5 Hz
#define B 1265 // 987.8 Hz
#define BF 1341 // 932.3 Hz
#define A 1420 // 880 Hz
#define AF 1505 // 830.6 Hz
#define G 1594 // 784 Hz
#define GF 1689 // 740 Hz
#define F 1790 // 698.5 Hz
#define E 1896 // 659.3 Hz
#define EF 2009 // 622.3 Hz
#define D 2128 // 587.3 Hz
#define DF 2255 // 554.4 Hz
#define C0 2389 // 523.3 Hz
#define B0 2531 // 493.9 Hz
#define BF0 2681 // 466.2 Hz
#define A0 2841 // 440 Hz
#define AF0 3010 // 415.3 Hz
#define G0 3189 // 392 Hz
#define GF0 3378 // 370 Hz
#define F0 3579 // 349.2 Hz
#define E0 3792 // 329.6 Hz
#define EF0 4018 // 311.1 Hz
#define D0 4257 // 293.7 Hz
#define DF0 4510 // 277.2 Hz
#define C7 4778 // 261.6 Hz
#define B7 5062 // 246.9 Hz
#define BF7 5363 // 233.1 Hz
#define A7 5682 // 220 Hz
#define AF7 6020 // 207.7 Hz
#define G7 6378 // 196 Hz
#define GF7 6757 // 185 Hz
#define F7 7159 // 174.6 Hz
#define E7 7584 // 164.8 Hz
#define EF7 8035 // 155.6 Hz
#define D7 8513 // 146.8 Hz
#define DF7 9019 // 138.6 Hz
#define C6 9556 // 130.8 Hz
int ind = 0; //counter index
struct music {
uint16_t note;
uint32_t duration;
};
struct music song[48] = {
{A, 1250}, //ms 1
{AF, 1250},
{G, 1250},
{GF, 1250},
{G, 1250},
{GF, 1250},
{F, 1250},
{E, 1250},
{F, 1250}, //ms 2
{E, 1250},
{EF, 1250},
{D, 1250},
{DF, 1250},
{C, 1250},
{B, 1250},
{BF, 1250},
{A, 1250}, //ms 3
{AF, 1250},
{G, 1250},
{GF, 1250},
{G, 1250},
{GF, 1250},
{F, 1250},
{E, 1250},
{F, 1250}, //ms 4
{E, 1250},
{EF, 1250},
{D, 1250},
{DF, 1250},
{C, 1250},
{B, 1250},
{BF, 1250},
{A0, 1250}, //ms 5
{AF0, 1250},
{G0, 1250},
{GF0, 1250},
{G0, 1250},
{GF0, 1250},
{F0, 1250},
{E0, 1250},
{A0, 1250}, //ms 6
{AF0, 1250},
{G0, 1250},
{GF0, 1250},
{G0, 1250},
{GF0, 1250},
{F0, 1250},
{E0, 1250},
};
const uint8_t wave[64] = {32,35,38,41,44,47,49,52,54,56,58,59,61,62,62,63,63,63,62,62,61,59,58,56,54,52,49,47,44,41,38,35,32,29,26,23,20,17,15,12,10,8,6,5,3,2,2,1,1,1,2,2,3,5,6,8,10,12,15,17,20,23,26,29};
uint32_t I = 0;
int32_t leftButton;
int32_t rightButton;
int8_t startGameFlag = 0;
void initButtons() {
SYSCTL_RCGCGPIO_R |= 0x10; //activate clock
while(SYSCTL_RCGCGPIO_R == 0) {} //wait
GPIO_PORTE_DIR_R &= 0x03; //make PE1-0 inputs
GPIO_PORTE_DEN_R |= 0x03;
}
void SysTick_Init(uint32_t period) {
NVIC_ST_CTRL_R=0; //disable SysTick during setup
NVIC_ST_RELOAD_R=period; //maximun reload value
NVIC_ST_CURRENT_R=0; //any write to current clears it
NVIC_ST_CTRL_R=0x00000005; //enable SysTick with core clock
NVIC_SYS_PRI3_R = (NVIC_SYS_PRI3_R & 0x00FFFFFF) | 0x20000000; //set priority to 1
NVIC_ST_CTRL_R=0x00000005;
}
void SysTick_Handler(void) {
uint32_t sample = my.ADCsample();
my.Save(sample);
}
void reload(void) {
I = (I+1) & 0x3F; //increments the index
DAC_Out(wave[I]);
}
uint16_t x_brick = (Random32()>>24)%47 + 30;
uint16_t y_brick = 0; //(Random32()>>24)%35 + 50;
uint16_t x_scot = (Random32()>>24)%47 + 30;
uint16_t y_scot = 0; //(Random32()>>24)%35 + 100;
uint16_t x_coins = (Random32()>>24)%47 + 30;
uint16_t y_coins = 0;
uint16_t pickOne = 1;
void fallingObjects(void) {
if (y_scot >= 190 || y_brick >= 190 || y_coins >=190) { // || y_bevo >= 190) {
x_brick = (Random32()>>24)%47 + 30;
x_scot = (Random32()>>24)%47 + 30;
x_coins = (Random32()>>24)%47 +30;
y_scot = 0;
y_brick = 0;
y_coins = 0;
pickOne = (Random32()>>24)%3 + 1;
}
ind++;
if(ind == 48) {
ind = 0;
}
if (pickOne == 1) {
y_scot++;
}
if (pickOne == 2) {
y_brick++;
}
if (pickOne == 3) {
y_coins++;
}
}
int main(void){
// initializations
// update coins
DisableInterrupts();
PLL_Init(Bus80MHz);
// Sound_Init();
initButtons();
SysTick_Init(1333333);
ADC_Init();
DAC_Init();
EnableInterrupts();
SlidePot *SlideP = new SlidePot(2,16);
ST7735_InitR(INITR_REDTAB);
ST7735_DrawBitmap(0, 160, starterScreen, 128, 160);
while(startGameFlag == 0) {
leftButton = (GPIO_PORTE_DATA_R & 0x00000002)>>1;
if(leftButton == 1) {
startGameFlag = 1;
}
}
ST7735_FillScreen(0);
ST7735_DrawBitmap(30, 160, gameBackground, 69, 160);
Timer0_Init(&fallingObjects, 1333333);
Timer1_Init(&reload, song[ind].note);
while(startGameFlag == 1) {
TIMER1_TAILR_R = song[ind].note;
rightButton = GPIO_PORTE_DATA_R & 0x00000001;
my.Sync();
Data = ADC_In();
Position = my.Convert(Data);
ST7735_DrawBitmap(Position, 160, runningFenves, 23, 23);
x = Position;
if (pickOne == 1) {
ST7735_DrawBitmap(x_scot, y_scot+1, scooter, 23, 23);
//y_scot++;
}
if (pickOne == 2) {
ST7735_DrawBitmap(x_brick, y_brick+1, bricks, 23, 23);
//y_brick++;
}
if (pickOne == 3) {
ST7735_DrawBitmap(x_coins, y_coins+1, coin, 23, 23);
//y_coins++;
}
//game over sequence thing
// brick detection
if (y >= y_brick-23 && y <= y_brick) {
if (x >= x_brick && x <= x_brick+23) {
ST7735_FillScreen(0);
ST7735_DrawBitmap(0, 80, gameOver, 122, 80);
ST7735_SetCursor(0, 90);
ST7735_SetTextColor(ST7735_YELLOW);
ST7735_OutUDec(score);
startGameFlag = 0;
}
}
// scooter detection
if (y >= y_scot-23 && y <= y_scot) {
if (x >= x_scot && x <= x_scot+23) {
ST7735_FillScreen(0);
ST7735_DrawBitmap(0, 80, gameOver, 122, 80);
ST7735_SetCursor(0, 90);
ST7735_SetTextColor(ST7735_YELLOW);
ST7735_OutUDec(score);
startGameFlag = 0;
}
}
if (y >= y_coins-23 && y <= y_coins) {
if (x >= x_coins && x <= x_coins+23) {
//ST7735_FillScreen(0);
score++;
//ST7735_SetCursor(0, 90);
//ST7735_SetTextColor(ST7735_YELLOW);
//ST7735_OutUDec(score);
//startGameFlag = 0;
}
}
if (rightButton == 1) {
ST7735_FillScreen(0);
ST7735_DrawBitmap(0, 80, gameOver, 122, 80);
ST7735_SetCursor(0, 90);
ST7735_SetTextColor(ST7735_YELLOW);
ST7735_OutUDec(score);
startGameFlag = 0;
}
}
}