Simple (basic) snake game

Dependencies:   N5110 mbed

main.h

Committer:
MsTee
Date:
2016-05-08
Revision:
1:ab8ceac59e71
Parent:
0:9eb93eff2469
Child:
2:dbfe6019b558

File content as of revision 1:ab8ceac59e71:

/**
@file main.h
@brief Header file contains functions and variables
@brief Snake Game - Embedded Systems Project
@brief Revision 1.0
@author Thokozile Tembo
@Date 05/05/2016
*/

#include "mbed.h"
#include "N5110.h"
#define DIRECTION_TOLERANCE 0.01

// LCD Object
N5110 lcd(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
// Serial Communication with PC
Serial pc(USBTX, USBRX);
// Analog inputs
AnalogIn joy_x(PTB2);
AnalogIn joy_y(PTB3);
AnalogIn pot(PTB10);
// Buttons
DigitalIn joy_button(PTB11); //  needs pull-down (active high)
InterruptIn pcb_button(PTB18); // needs pull-down (active high)
// PWM Output (LED and Buzzer)
PwmOut pcb_led(PTC2);
PwmOut pcb_buzzer(PTA2);

// Ticker for joystick update
Ticker joystick_update_ticker;
Ticker brightness_update_ticker;

// Initialising board board
void init_K64F();
// Welcome screen
void welcomeScreen();

// Defining possible joystick directions (enum)
enum DirectionName {
    UP,
    DOWN,
    LEFT,
    RIGHT,
    CENTRE,
    UNKNOWN
};
// Variable to store previous direction of the joystick
DirectionName prevDirection = UNKNOWN;

// Joystick type definition
struct JoyStick {
    double x;    /// current x value
    double x0;   /// 'centred' x value
    double y;    /// current y value
    double y0;   /// 'centred' y value
    int button; /// button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
    DirectionName direction;  // current direction
} joystick;

// Apple type definition
struct AppleType {
    int x; // Apple X
    int y; // Apple Y
};

// Defining possible states (enum)
typedef enum {
        INIT,
        WAIT_FOR_USER,
        PLAY,
        GAME_OVER
} GAME_STATES; //States used in the main loop

GAME_STATES game_state = INIT; // Default state of the game

// Global variables
volatile int g_pcb_button_flag = 0;
volatile float g_lcd_bright_level = 0;

// Function prototypes
void calibrateJoystick();
void updateJoystick();
void updateBrightness();
void snakeGame();

// These functions are used to map game
// field of 20x11 to the LCD screen which is 84x48

// Enable square (4x4 pixels) at x, y
void drawBlock(int field_x, int field_y);
// Draw Apple (4x4 pixels) at x, y
void drawApple(int field_x, int field_y);
// Disable square (4x4 pixels) at x, y
void eraseBlock(int field_x, int field_y);
// Get the status of the square (4x4 pixels) at x, y
int getBlock(int field_x, int field_y);
// Place an apple at random location (returns food structure)
AppleType placeRandomApple();
// pcb_button interrupt function
void pcb_button_isr();
// This function make LED blink and buzzer make a sound
void blinkLEDandBuzz();