Dependencies:   N5110 mbed

main.cpp

Committer:
jordaahh
Date:
2015-04-20
Revision:
7:b27a91c673dc
Parent:
6:c257b2060803
Child:
8:db2994eef1ab

File content as of revision 7:b27a91c673dc:

/* 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.1
 
// connections for joystick
DigitalIn button(p15);
AnalogIn xPot(p16);
AnalogIn yPot(p17);

// LCD connections
//vcc, sce, rst, dc, mosi, clk, led
N5110 lcd(p7,p8,p9,p10,p11,p13,p26); 

// LCD dimentions
int nx= 84; //screen is 84 pixels across
int ny= 48; // by 48 pixels downwards
int i=24; 
int j=42;

// Output to screen LED's (backlights)
PwmOut PWM(p26); 

// 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,
    upLeft,
    downLeft,
    RIGHT,
    upRight,
    downRight,
    CENTRE,
    UNKNOWN
};
 
// 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();
void clearCells();
void moveUp();
void getPixels();
void rules();
 
int main()
{
    lcd.init();
    calibrateJoystick();  // get centred values of joystick
    pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
 
    while(1) {
        lcd.drawRect(i,j,2,2,0);
        clearCells();
        lcd.drawLine(20,30,60,30,1);

        if (printFlag) {  // if flag set, clear flag and implicate joystick functions
            printFlag = 0;
 
            // check joystick direction
            if (joystick.direction == UP){
                j--;
                }
            if (joystick.direction == DOWN)
               {
                j++;
                }
            if (joystick.direction == LEFT){
                i--;
                }
            if (joystick.direction == RIGHT){
                i++;
                }
            if (joystick.direction == upRight){
                j--;
                i++;
                }
            if (joystick.direction == downRight){
                j++;
                i++;
                }
           if (joystick.direction == upLeft){
                j--;
                i--;
                }
           if (joystick.direction == downLeft){
                j++;
                i--;
                }
           if (joystick.direction == CENTRE){
                i=i;
                j=j;
                }
           if (lcd.getPixel(i,j-1)){
                lcd.printString("game over",1,2);
                }
           if (lcd.getPixel(i+1,j-1)){
                lcd.printString("game over",1,2);
                wait(5);
                }
           if (lcd.getPixel(i+2,j-1)){
                lcd.printString("game over",1,2);
                }
           if (lcd.getPixel(i+3,j)){
                lcd.printString("game over",1,2);
                }
           if (lcd.getPixel(i+3,j+1)){
                lcd.printString("game over",1,2);
                wait(5);
                }
           if (lcd.getPixel(i+3,j+2)){
                lcd.printString("game over",1,2);
                }
           if (lcd.getPixel(i+2,j+3)){
                lcd.printString("game over",1,2);
                }
           if (lcd.getPixel(i+1,j+3)){
                lcd.printString("game over",1,2);
                wait(5);
                }
           if (lcd.getPixel(i,j+3)){
                lcd.printString("game over",1,2);
                }
           if (lcd.getPixel(i-1,j+2)){
                lcd.printString("game over",1,2);
                }
           if (lcd.getPixel(i-1,j+1)){
                lcd.printString("game over",1,2);
                wait(5);
                }
           if (lcd.getPixel(i-1,j)){
                lcd.printString("game over",1,2);
                }
       }
    }
}

// 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 if ( joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE){
        joystick.direction = upRight;
    } else if ( joystick.y < DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE){
        joystick.direction = downRight;
    } else if ( joystick.y > DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE){
        joystick.direction = upLeft;
    } else if ( joystick.y < DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE){
        joystick.direction = downLeft;
        }
    // set flag for printing
    printFlag = 1;
}


void clearCells() // turns all cells on the display off
{
    // loop through cells, and clear
    for (int i = 0; i < nx ; i++) {
        for (int j = 0; j < ny ; j++) {
            lcd.clearPixel(i,j);
        }
    }
    lcd.refresh(); // must refresh to write buffer display
}