testing documentation

Dependencies:   mbed ll16j23s_test_docs

Committer:
JoeShotton
Date:
Sun Apr 05 21:16:16 2020 +0000
Revision:
2:86b67b492cbc
Parent:
1:985dfa6cee28
Child:
3:fcd6d70e9694
implemented collision detection and added it to new SnakeEngine header and source files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eencae 0:b7f1f47bb26a 1 /*
eencae 0:b7f1f47bb26a 2 ELEC2645 Embedded Systems Project
eencae 0:b7f1f47bb26a 3 School of Electronic & Electrical Engineering
eencae 0:b7f1f47bb26a 4 University of Leeds
eencae 0:b7f1f47bb26a 5 2019/20
eencae 0:b7f1f47bb26a 6
JoeShotton 2:86b67b492cbc 7 Name: Joe Shotton
JoeShotton 2:86b67b492cbc 8 Username: ll16j23s
JoeShotton 2:86b67b492cbc 9 Student ID Number: 201127267
JoeShotton 2:86b67b492cbc 10 Date: 4/4/20
eencae 0:b7f1f47bb26a 11 */
eencae 0:b7f1f47bb26a 12
eencae 0:b7f1f47bb26a 13 // includes
eencae 0:b7f1f47bb26a 14 #include "mbed.h"
eencae 0:b7f1f47bb26a 15 #include "Gamepad.h"
eencae 0:b7f1f47bb26a 16 #include "N5110.h"
eencae 0:b7f1f47bb26a 17
JoeShotton 1:985dfa6cee28 18 #define X_MAX 84
JoeShotton 1:985dfa6cee28 19 #define Y_MAX 48
eencae 0:b7f1f47bb26a 20
eencae 0:b7f1f47bb26a 21 // objects
eencae 0:b7f1f47bb26a 22 Gamepad pad;
eencae 0:b7f1f47bb26a 23 N5110 lcd;
eencae 0:b7f1f47bb26a 24
JoeShotton 1:985dfa6cee28 25 int main() {
JoeShotton 1:985dfa6cee28 26
JoeShotton 1:985dfa6cee28 27 lcd.init();
JoeShotton 1:985dfa6cee28 28 pad.init();
JoeShotton 1:985dfa6cee28 29
JoeShotton 1:985dfa6cee28 30 float speed = 0.5;
JoeShotton 1:985dfa6cee28 31 int cell_size = 2;
JoeShotton 1:985dfa6cee28 32 int fps = 25;
JoeShotton 1:985dfa6cee28 33 int frame_t = 1000/fps;
JoeShotton 2:86b67b492cbc 34 bool death = false;
JoeShotton 1:985dfa6cee28 35
JoeShotton 1:985dfa6cee28 36 int x = X_MAX/2; //start coords are centre of the screen
JoeShotton 1:985dfa6cee28 37 int y = Y_MAX/2;
JoeShotton 1:985dfa6cee28 38
JoeShotton 1:985dfa6cee28 39 struct Dir {
JoeShotton 1:985dfa6cee28 40 int output; // output value
JoeShotton 2:86b67b492cbc 41 int delta_x; // increment value for x
JoeShotton 2:86b67b492cbc 42 int delta_y; // increment value for y
JoeShotton 1:985dfa6cee28 43 int nextState[5]; // array of next states
JoeShotton 1:985dfa6cee28 44 };
JoeShotton 1:985dfa6cee28 45
JoeShotton 1:985dfa6cee28 46 int d = 0; //direction starts in centre
JoeShotton 1:985dfa6cee28 47 int state = 0; //first state is centre of joystick
eencae 0:b7f1f47bb26a 48
JoeShotton 1:985dfa6cee28 49 Dir fsm[5] = {
JoeShotton 1:985dfa6cee28 50 {0, 0, 0, {0,1,2,3,4}}, // Centred
JoeShotton 1:985dfa6cee28 51 {1, 0, -1, {1,1,2,1,4}}, // North, will not go to centre or south
JoeShotton 1:985dfa6cee28 52 {2, 1, 0, {2,1,2,3,2}}, // East, will not go to centre or west
JoeShotton 1:985dfa6cee28 53 {3, 0, 1, {3,3,2,3,4}}, // South, will not go to centre or north
JoeShotton 1:985dfa6cee28 54 {4, -1, 0, {4,1,4,3,4}} // West, will not go to centre or east
JoeShotton 1:985dfa6cee28 55 };
JoeShotton 1:985dfa6cee28 56
JoeShotton 1:985dfa6cee28 57 while(1){
JoeShotton 1:985dfa6cee28 58
JoeShotton 1:985dfa6cee28 59 speed = pad.read_pot2();
JoeShotton 1:985dfa6cee28 60
JoeShotton 2:86b67b492cbc 61 if ((x % cell_size) + (y % cell_size) == 0) { // only allows changing movement when the snake is cell-aligned
JoeShotton 2:86b67b492cbc 62 d = pad.get_cardinal(); // gets joystick cardinal direction: 0 for centre, 1 - 4 for NESW respectively
JoeShotton 2:86b67b492cbc 63 state = fsm[state].nextState[d]; // adjusts direction fsm state based on direction
JoeShotton 1:985dfa6cee28 64 }
JoeShotton 1:985dfa6cee28 65
JoeShotton 2:86b67b492cbc 66 x += fsm[state].delta_x; // increments x value based on fsm state value
JoeShotton 2:86b67b492cbc 67 y += fsm[state].delta_y; // increments y value based on fsm state value
JoeShotton 1:985dfa6cee28 68
JoeShotton 2:86b67b492cbc 69 x = ((x % X_MAX) + X_MAX)% X_MAX; // wraps x back to within range 0-83
JoeShotton 2:86b67b492cbc 70 y = ((y % Y_MAX) + Y_MAX)% Y_MAX; // wraps y back to within range 0-83
JoeShotton 1:985dfa6cee28 71
JoeShotton 2:86b67b492cbc 72 if ((lcd.getPixel(x, y) == 1 && ((state == 1) || (state == 4))) || (lcd.getPixel(x+1, y+1) == 1 && ((state == 2) || (state == 3)))) {
JoeShotton 2:86b67b492cbc 73 // checks infront of head to see if pixel is set
JoeShotton 2:86b67b492cbc 74 // due to the size of the head, there is an offset for the check for North and Eastward directions
JoeShotton 2:86b67b492cbc 75 death = true;
JoeShotton 2:86b67b492cbc 76 pad.led(1,0.9);
JoeShotton 2:86b67b492cbc 77 } else {
JoeShotton 2:86b67b492cbc 78 death = false;
JoeShotton 2:86b67b492cbc 79 pad.led(1,0.0);
JoeShotton 2:86b67b492cbc 80 }
JoeShotton 2:86b67b492cbc 81
JoeShotton 1:985dfa6cee28 82 lcd.clear();
JoeShotton 1:985dfa6cee28 83 lcd.setContrast(0.5);
JoeShotton 1:985dfa6cee28 84 lcd.drawRect(x,y,cell_size,cell_size,FILL_BLACK);
JoeShotton 2:86b67b492cbc 85 lcd.drawRect(10,10,6,6,FILL_BLACK);
JoeShotton 1:985dfa6cee28 86 lcd.refresh();
JoeShotton 1:985dfa6cee28 87 wait_ms(25/speed);
JoeShotton 1:985dfa6cee28 88 }
eencae 0:b7f1f47bb26a 89 }
eencae 0:b7f1f47bb26a 90