ELEC2645 (2015/16)
/
fy13tmtProject
Simple (basic) snake game
main.h
- Committer:
- MsTee
- Date:
- 2016-05-09
- Revision:
- 3:79d3148204e1
- Parent:
- 2:dbfe6019b558
- Child:
- 4:d1f0cf1ea11b
File content as of revision 3:79d3148204e1:
/** @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 }; // 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 of the game (enum) enum GAME_STATES { INIT, // 0 WAIT_FOR_USER, // 1 PLAY, // 2 GAME_OVER // 3 }; 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 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();