ELEC2645 (2015/16)
/
fy13tmtProject
Simple (basic) snake game
Diff: main.h
- Revision:
- 0:9eb93eff2469
- Child:
- 1:ab8ceac59e71
diff -r 000000000000 -r 9eb93eff2469 main.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.h Sun May 08 20:10:54 2016 +0000 @@ -0,0 +1,97 @@ +/** +@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.05 + +// 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 pcn_buzzer(PTA2); + +// Ticker for joystick update +Ticker joystick_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; + +// Function prototypes +void calibrateJoystick(); +void updateJoystick(); +void snake_game(); + +// 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(); \ No newline at end of file