testing documentation

Dependencies:   mbed ll16j23s_test_docs

main.cpp

Committer:
JoeShotton
Date:
2020-04-05
Revision:
1:985dfa6cee28
Parent:
0:b7f1f47bb26a
Child:
2:86b67b492cbc

File content as of revision 1:985dfa6cee28:

/* 
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
2019/20

Name:
Username:
Student ID Number:
Date:
*/

// includes
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"

#define X_MAX       84
#define Y_MAX       48

// objects
Gamepad pad;
N5110 lcd;

int main() {
    
    lcd.init();
    pad.init();
    
    float speed = 0.5;
    int cell_size = 2;
    int fps = 25;
    int frame_t = 1000/fps;
    
    int x = X_MAX/2; //start coords are centre of the screen
    int y = Y_MAX/2;
    
    struct Dir {
        int output;  // output value
        int delta_x; //increment value for x
        int delta_y; //increment value for y
        int nextState[5];  // array of next states
    };
    
    int d = 0; //direction starts in centre
    int state = 0; //first state is centre of joystick
    
    Dir fsm[5] = {
        {0, 0,  0, {0,1,2,3,4}},  // Centred
        {1, 0, -1, {1,1,2,1,4}},  // North, will not go to centre or south
        {2, 1,  0, {2,1,2,3,2}},  // East, will not go to centre or west
        {3, 0,  1, {3,3,2,3,4}},  // South, will not go to centre or north
        {4, -1, 0, {4,1,4,3,4}}   // West, will not go to centre or east
    };

    while(1){
        
        speed = pad.read_pot2();
        
        if ((x % cell_size) + (y % cell_size) == 0) {   //only allows changing movement when the snake is cell-aligned
            d = pad.get_cardinal();          //gets joystick cardinal direction: 0 for centre, 1 - 4 for NESW respectively
            state = fsm[state].nextState[d]; //adjusts direction fsm state based on direction
        }
        
        x += fsm[state].delta_x; //increments x value based on fsm state value
        y += fsm[state].delta_y; //increments y value based on fsm state value
            
        x = ((x % X_MAX) + X_MAX)% X_MAX; //wraps x back to within range 0-83
        y = ((y % Y_MAX) + Y_MAX)% Y_MAX; //wraps y back to within range 0-83
        
        lcd.clear();
        lcd.setContrast(0.5);
        lcd.drawRect(x,y,cell_size,cell_size,FILL_BLACK);
        lcd.refresh();
        wait_ms(25/speed);
    }   
}