testing documentation
Dependencies: mbed ll16j23s_test_docs
main.cpp@1:985dfa6cee28, 2020-04-05 (annotated)
- Committer:
- JoeShotton
- Date:
- Sun Apr 05 20:00:17 2020 +0000
- Revision:
- 1:985dfa6cee28
- Parent:
- 0:b7f1f47bb26a
- Child:
- 2:86b67b492cbc
Basic movement implemented with an fsm and cell alignment
Who changed what in which revision?
User | Revision | Line number | New 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 | |
eencae | 0:b7f1f47bb26a | 7 | Name: |
eencae | 0:b7f1f47bb26a | 8 | Username: |
eencae | 0:b7f1f47bb26a | 9 | Student ID Number: |
eencae | 0:b7f1f47bb26a | 10 | Date: |
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 | 1:985dfa6cee28 | 34 | |
JoeShotton | 1:985dfa6cee28 | 35 | int x = X_MAX/2; //start coords are centre of the screen |
JoeShotton | 1:985dfa6cee28 | 36 | int y = Y_MAX/2; |
JoeShotton | 1:985dfa6cee28 | 37 | |
JoeShotton | 1:985dfa6cee28 | 38 | struct Dir { |
JoeShotton | 1:985dfa6cee28 | 39 | int output; // output value |
JoeShotton | 1:985dfa6cee28 | 40 | int delta_x; //increment value for x |
JoeShotton | 1:985dfa6cee28 | 41 | int delta_y; //increment value for y |
JoeShotton | 1:985dfa6cee28 | 42 | int nextState[5]; // array of next states |
JoeShotton | 1:985dfa6cee28 | 43 | }; |
JoeShotton | 1:985dfa6cee28 | 44 | |
JoeShotton | 1:985dfa6cee28 | 45 | int d = 0; //direction starts in centre |
JoeShotton | 1:985dfa6cee28 | 46 | int state = 0; //first state is centre of joystick |
eencae | 0:b7f1f47bb26a | 47 | |
JoeShotton | 1:985dfa6cee28 | 48 | Dir fsm[5] = { |
JoeShotton | 1:985dfa6cee28 | 49 | {0, 0, 0, {0,1,2,3,4}}, // Centred |
JoeShotton | 1:985dfa6cee28 | 50 | {1, 0, -1, {1,1,2,1,4}}, // North, will not go to centre or south |
JoeShotton | 1:985dfa6cee28 | 51 | {2, 1, 0, {2,1,2,3,2}}, // East, will not go to centre or west |
JoeShotton | 1:985dfa6cee28 | 52 | {3, 0, 1, {3,3,2,3,4}}, // South, will not go to centre or north |
JoeShotton | 1:985dfa6cee28 | 53 | {4, -1, 0, {4,1,4,3,4}} // West, will not go to centre or east |
JoeShotton | 1:985dfa6cee28 | 54 | }; |
JoeShotton | 1:985dfa6cee28 | 55 | |
JoeShotton | 1:985dfa6cee28 | 56 | while(1){ |
JoeShotton | 1:985dfa6cee28 | 57 | |
JoeShotton | 1:985dfa6cee28 | 58 | speed = pad.read_pot2(); |
JoeShotton | 1:985dfa6cee28 | 59 | |
JoeShotton | 1:985dfa6cee28 | 60 | if ((x % cell_size) + (y % cell_size) == 0) { //only allows changing movement when the snake is cell-aligned |
JoeShotton | 1:985dfa6cee28 | 61 | d = pad.get_cardinal(); //gets joystick cardinal direction: 0 for centre, 1 - 4 for NESW respectively |
JoeShotton | 1:985dfa6cee28 | 62 | state = fsm[state].nextState[d]; //adjusts direction fsm state based on direction |
JoeShotton | 1:985dfa6cee28 | 63 | } |
JoeShotton | 1:985dfa6cee28 | 64 | |
JoeShotton | 1:985dfa6cee28 | 65 | x += fsm[state].delta_x; //increments x value based on fsm state value |
JoeShotton | 1:985dfa6cee28 | 66 | y += fsm[state].delta_y; //increments y value based on fsm state value |
JoeShotton | 1:985dfa6cee28 | 67 | |
JoeShotton | 1:985dfa6cee28 | 68 | x = ((x % X_MAX) + X_MAX)% X_MAX; //wraps x back to within range 0-83 |
JoeShotton | 1:985dfa6cee28 | 69 | y = ((y % Y_MAX) + Y_MAX)% Y_MAX; //wraps y back to within range 0-83 |
JoeShotton | 1:985dfa6cee28 | 70 | |
JoeShotton | 1:985dfa6cee28 | 71 | lcd.clear(); |
JoeShotton | 1:985dfa6cee28 | 72 | lcd.setContrast(0.5); |
JoeShotton | 1:985dfa6cee28 | 73 | lcd.drawRect(x,y,cell_size,cell_size,FILL_BLACK); |
JoeShotton | 1:985dfa6cee28 | 74 | lcd.refresh(); |
JoeShotton | 1:985dfa6cee28 | 75 | wait_ms(25/speed); |
JoeShotton | 1:985dfa6cee28 | 76 | } |
eencae | 0:b7f1f47bb26a | 77 | } |
eencae | 0:b7f1f47bb26a | 78 |