ELEC2645 (2015/16) / Mbed 2 deprecated SnakeProjectRev1

Dependencies:   Joystick N5110 SDFileSystem beep fsmMenu mbed

Fork of SnakeProjectRev1 by Meurig Phillips

main.cpp

Committer:
meurigp
Date:
2016-04-05
Revision:
1:97ac723959f2
Parent:
0:fcb5a32bc2fc
Child:
2:663b9aadf00c

File content as of revision 1:97ac723959f2:

/* Joystick
 
Example code of how to read a joystick
 
https://www.sparkfun.com/products/9032
 
Craig A. Evans
7 March 2015
*/
 
#include "mbed.h"
#include "N5110.h"
 
// change this to alter tolerance of joystick direction
#define DIRECTION_TOLERANCE 0.05

//         VCC,    SCE,   RST,   D/C,   MOSI,  SCLK,   LED
N5110 lcd (PTD3, PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
// Can also power (VCC) directly from VOUT (3.3 V) -
// Can give better performance due to current limitation from GPIO pin 
 
// connections for joystick
DigitalIn button(PTB18);
AnalogIn xPot(PTC11);
AnalogIn yPot(PTC10);

// buttons to simulate joystick
InterruptIn upButton(PTB19);
InterruptIn downButton(PTC1);
InterruptIn leftButton(SW3);
InterruptIn rightButton(SW2);

// flag - must be volatile as changes within ISR
// g_ prefix makes it easier to distinguish it as global
volatile int g_right_flag = 0;
volatile int g_left_flag = 0;
volatile int g_up_flag = 0;
volatile int g_down_flag = 0;

// function prototypes
void left_isr();
void right_isr();
void up_isr();
void down_isr();

 // SW2 has a pull-up resistor, so the pin will be at 3.3 V by default
    // and fall to 0 V when pressed. We therefore need to look for a falling edge
    // on the pin to fire the interrupt
    sw2.fall(&sw2_isr);
    sw3.fall(&sw3_isr);
    
 // since SW2 has an external pull-up, we should disable to internal pull-down
    // resistor that is enabled by default using InterruptIn
    sw2.mode(PullNone);
    sw3.mode(PullNone);


// timer to regularly read the joystick
Ticker pollJoystick;
// Serial for debug
Serial serial(USBTX,USBRX);
 
// create enumerated type (0,1,2,3 etc. for direction)
// could be extended for diagonals etc.
enum DirectionName {
    UP,
    DOWN,
    LEFT,
    RIGHT,
    CENTRE,
    UNKNOWN
};

/// create enumerated type (0,1,2,3 etc. for current direction snake is travelling (not joystick reading))
enum CurrentDirection {
    up,
    down,
    left,
    right,
    centre,
};
CurrentDirection currentDirection = centre; /// intialise direction at beginning
 
// struct for Joystick
typedef struct JoyStick Joystick;
struct JoyStick {
    float x;    // current x value
    float x0;   // 'centred' x value
    float y;    // current y value
    float y0;   // 'centred' y value
    int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
    DirectionName direction;  // current direction
};
// create struct variable
Joystick joystick;


 
int printFlag = 0;
 
// function prototypes
void calibrateJoystick();
void updateJoystick();
 
int main()
{
    calibrateJoystick();  // get centred values of joystick
    pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
    upButton.mode(PullDown);
    downButton.mode(PullDown);
    
    int i = 41;
    int j = 23;

    lcd.init();
    lcd.drawRect(i,j,2,2,1);
 
    while(1) {
        
        if (printFlag) {  // if flag set, clear flag and print joystick values to serial port
            printFlag = 0;
            serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
 
            // check joystick direction
            if (joystick.direction == UP)
                serial.printf(" UP\n");
            if (joystick.direction == DOWN)
                serial.printf(" DOWN\n");
            if (joystick.direction == LEFT)
                serial.printf(" LEFT\n");
            if (joystick.direction == RIGHT)
                serial.printf(" RIGHT\n");
            if (joystick.direction == CENTRE)
                serial.printf(" CENTRE\n");
            if (joystick.direction == UNKNOWN)
                serial.printf(" Unsupported direction\n");
                
            if (leftButton == 0) {
                if (currentDirection != right) { // user can't turn opposite way
                currentDirection = left; // direct enum updated
                }
                while (currentDirection == left) { // enters left movement loop
                lcd.clear();
                i -= 3; // shifts three pixels (snake thickness)
                lcd.drawRect(i,j,2,2,1);
                wait(1.0);
                if (upButton == 1) {
                    currentDirection = up; // direction enum updated, this while loop ends, up while loop starts
                    }
                if (downButton == 1) {
                    currentDirection = down; // direction enum updated, this while loop ends, down while loop starts
                    }
                }
            }
            if (rightButton == 0) {
                if (currentDirection != left) {
                currentDirection = right;
                }
                while (currentDirection == right) {
                lcd.clear();
                i += 3;
                lcd.drawRect(i,j,2,2,1);
                wait(1.0);
                if (upButton == 1) {
                    currentDirection = up; // direction enum updated, this while loop ends, up while loop starts
                    }
                if (downButton == 1) {
                    currentDirection = down; // direction enum updated, this while loop ends, down while loop starts
                    }
                }
            }
            if (upButton == 1) {
                if (currentDirection != down) {
                currentDirection = up; 
                }
                while (currentDirection == up) {
                lcd.clear();
                j -= 3;
                lcd.drawRect(i,j,2,2,1);
                wait(1.0);
                if (leftButton == 0) {
                    currentDirection = left; // direction enum updated, this while loop ends, left while loop starts
                    }
                if (rightButton == 0) {
                    currentDirection = right; // direction enum updated, this while loop ends, right while loop starts
                    }
                }
            }
            if (downButton == 1) {
                if (currentDirection != up) {
                currentDirection = down;
                }
                while (currentDirection == down) { 
                lcd.clear();
                j += 3;
                lcd.drawRect(i,j,2,2,1);
                wait(1.0);
                 if (leftButton == 0) {
                    currentDirection = left; // direction enum updated, this while loop ends, left while loop starts
                    }
                if (rightButton == 0) {
                    currentDirection = right; // direction enum updated, this while loop ends, right while loop starts
                    } 
                }
            }
            
                
 
        }
 
    }
}
 
// read default positions of the joystick to calibrate later readings
void calibrateJoystick()
{
    button.mode(PullDown);
    // must not move during calibration
    joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
    joystick.y0 = yPot;
}
void updateJoystick()
{
    // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
    joystick.x = xPot - joystick.x0;
    joystick.y = yPot - joystick.y0;
    // read button state
    joystick.button = button;
 
    // calculate direction depending on x,y values
    // tolerance allows a little lee-way in case joystick not exactly in the stated direction
    if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
        joystick.direction = CENTRE;
    } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
        joystick.direction = UP;
    } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
        joystick.direction = DOWN;
    } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
        joystick.direction = RIGHT;
    } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
        joystick.direction = LEFT;
    } else {
        joystick.direction = UNKNOWN;
    }
 
    // set flag for printing
    printFlag = 1;
}

void left_isr()
{
    g_left_flag = 1;   // set flag in ISR
}

void right_isr()
{
    g_right_flag = 1;   // set flag in ISR
}

void up_isr()
{
    g_up_flag = 1;   // set flag in ISR
}

void down_isr()
{
    g_down_flag = 1;   // set flag in ISR
}