-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
303 lines (257 loc) · 6.5 KB
/
main.c
File metadata and controls
303 lines (257 loc) · 6.5 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
#include <cmsis_os2.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <lpc17xx.h>
#include "random.h"
#include "lfsr113.h"
#include "GLCD.h"
#include "uart.h"
#include "graphics.h"
osMutexId_t snake;
//bool to track state of game
bool stopped;
const uint16_t width = 320;
const uint16_t height = 240;
const int8_t size = 20;
//coords of apple
uint16_t appleX = 0;
uint16_t appleY = 0;
uint8_t score = 0;
enum dirs{stop, left, up, right, down};
enum dirs dir;
//define struct to represent single node of snake
typedef struct Node{
int32_t x;
int32_t y;
struct Node *next;
} snakeNode_t;
//pointers to head and tail
snakeNode_t *head = NULL;
snakeNode_t *tail = NULL;
int16_t prevTailX;
int16_t prevTailY;
//Helper functions
void placeApple() {
//rechoose coords for apple if in snake
bool goodLocation = false;
snakeNode_t *curr = tail;
while(!goodLocation) {
goodLocation = true;
appleX = (rand()%(width/size))*size;
appleY = (rand()%(height/size))*size;
while(curr != NULL) {
if (curr->x == appleX && curr->y == appleY) {
goodLocation = false;
curr = tail;
break;
}
else
curr = curr->next;
}
}
}
//reset position of snake and reset display
void reset() {
osMutexAcquire(snake, osWaitForever);
GLCD_Clear(Black);
score = 0;
LPC_GPIO2->FIOCLR |= 0xFF;
LPC_GPIO1->FIOCLR |= 0xFFFFFFFF;
//initialize linked list and starting position/direction
head->x = width/2 - 2*size;
head->y = height/2;
tail->x = head->x-size;
tail->y = head->y;
head->next = NULL;
tail->next = head;
osMutexRelease(snake);
dir = right;
placeApple();
}
//draws snake before starting
void start() {
//initialize head and tail of snake
head = malloc(sizeof(snakeNode_t));
tail = malloc(sizeof(snakeNode_t));
reset();
GLCD_DisplayString(1, 7, 1, (unsigned char *)"SNAKE");
GLCD_DisplayString(3, 0, 1, (unsigned char *)"Push button to start");
snakeNode_t *curr = tail;
while(curr != NULL) {
GLCD_Bargraph(curr->x, curr->y, size, size, 1024);
curr = curr->next;
}
//wait for button
if ((LPC_GPIO2->FIOPIN & (0x01 << 10)) != 0)
while((LPC_GPIO2->FIOPIN & (0x01 << 10)) != 0);
stopped = false;
osMutexAcquire(snake, osWaitForever);
GLCD_Clear(Black);
osMutexRelease(snake);
}
//detects if head hits the snake
bool hitSelf() {
snakeNode_t *curr = tail;
while(curr != head) {
if (head->x == curr->x && head->y == curr->y)
return true;
curr = curr->next;
}
return false;
}
void turnOn(uint8_t num) {
LPC_GPIO2->FIOCLR |= 0xFF;
LPC_GPIO1->FIOCLR |= 0xFFFFFFFF;
if (num & 0x01)
LPC_GPIO2->FIOSET |= 0x01 << 6;
if (num & 0x02)
LPC_GPIO2->FIOSET |= 0x01 << 5;
if (num & 0x04)
LPC_GPIO2->FIOSET |= 0x01 << 4;
if (num & 0x08)
LPC_GPIO2->FIOSET |= 0x01 << 3;
if (num & 0x10)
LPC_GPIO2->FIOSET |= 0x01 << 2;
if (num & 0x020)
LPC_GPIO1->FIOSET |= 0x01 << 31;
if (num & 0x040)
LPC_GPIO1->FIOSET |= 0x01 << 29;
if (num & 0x80)
LPC_GPIO1->FIOSET |= 0x01 << 28;
}
void gameOver() {
stopped = true;
osMutexAcquire(snake, osWaitForever);
GLCD_Clear(Black);
uint8_t finalScore = score;
//display score
char scoreStr[] = "";
sprintf(scoreStr, "Score: %d", finalScore);
GLCD_DisplayString(1, 5, 1, (unsigned char *)"GAME OVER");
GLCD_DisplayString(3, 2, 1, (unsigned char *)"Push to restart");
GLCD_DisplayString(4, 5, 1, (unsigned char *)scoreStr);
//wait for button to restart
if ((LPC_GPIO2->FIOPIN & (0x01 << 10)) != 0)
while((LPC_GPIO2->FIOPIN & (0x01 << 10)) != 0);
stopped = true;
osMutexRelease(snake);
}
//Threaded functions
//start/stop game with push button
void pushButton(void *arg) {
while(1) {
if((LPC_GPIO2->FIOPIN & (0x01 << 10)) == 0) {
while((LPC_GPIO2->FIOPIN & (0x01 << 10)) == 0) {}
stopped = !stopped;
}
osThreadYield();
}
}
//poll for input from joystick to set direction
void readJoy(void *arg) {
while(1) {
while(!stopped) {
//cant switch back 180 degrees to opposite direction
if((LPC_GPIO1->FIOPIN & (0x01 << 23)) == 0 && dir != down)
dir = up;
else if((LPC_GPIO1->FIOPIN & (0x01 << 25)) == 0 && dir != up)
dir = down;
else if((LPC_GPIO1->FIOPIN & (0x01 << 26)) == 0 && dir != right)
dir = left;
else if((LPC_GPIO1->FIOPIN & (0x01 << 24)) == 0 && dir != left)
dir = right;
}
osThreadYield();
}
}
void moveSnek(void *arg) {
int16_t dx = 0;
int16_t dy = 0;
while(1) {
while(!stopped) {
osDelay(osKernelGetTickFreq()/12);
//if snake head hits wall or hits self
if (hitSelf() || (head->x <= 0 && dir == left) || (head->x >= width-size && dir == right) || (head->y <= 0 && dir == up) || (head->y >= height-size && dir == down)) {
gameOver();
reset();
}
else {
//save previous x,y of tail node so we dont need double linked list
osMutexAcquire(snake, osWaitForever);
prevTailX = tail->x;
prevTailY = tail->y;
//iterate through list and change coords to next coords
snakeNode_t *curr = tail;
while(curr != head) {
curr->x = curr->next->x;
curr->y = curr->next->y;
curr = curr->next;
}
//check direction
if (dir == up) {
dx = 0;
dy = -size;
} else if (dir == down) {
dx = 0;
dy = size;
} else if (dir == right) {
dx = size;
dy = 0;
} else if (dir == left) {
dx = -size;
dy = 0;
}
//move head according to direction
head->x += dx;
head->y += dy;
//increase length of snake
if(head->x == appleX && head->y == appleY) {
snakeNode_t *newNode = malloc(sizeof(struct Node));
newNode->next = tail;
newNode->x = prevTailX;
newNode->y = prevTailY;
tail = newNode;
placeApple();
score++;
turnOn(score);
}
}
osMutexRelease(snake);
}
osThreadYield();
}
}
void display(void *arg) {
while(1) {
osMutexAcquire(snake, osWaitForever);
//iterate through snake and display each node
snakeNode_t *curr = tail;
while(curr != NULL) {
GLCD_Bargraph(curr->x, curr->y, size, size, 1024);
curr = curr->next;
}
GLCD_Bargraph(prevTailX, prevTailY, size, size, 0);
drawApple(appleX, appleY);
osMutexRelease(snake);
osThreadYield();
}
}
int main(void) {
osKernelInitialize();
//mutex used to protect snake linked list data
snake = osMutexNew(NULL);
LPC_GPIO2->FIODIR |= 0x0000007C;
LPC_GPIO1->FIODIR |= 0xB0000000;
GLCD_Init();
GLCD_SetTextColor(White);
GLCD_SetBackColor(Black);
start();
printf("");
osThreadNew(readJoy, NULL, NULL);
osThreadNew(pushButton, NULL, NULL);
osThreadNew(moveSnek, NULL, NULL);
osThreadNew(display, NULL, NULL);
osKernelStart();
while(1){}
}