Snake and Physics based jumping game with high scores and different difficulties.

Dependencies:   N5110 SDFileSystem mbed

Committer:
el14jw
Date:
Sat Mar 12 17:41:11 2016 +0000
Revision:
0:23a749719479
Child:
1:c4928de1f922
Commit of Project2645_Games; - Implemented Menu functions for ease of use when adding menus; - Added several sound arrays for intro and snake game; - Implemented snake game

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el14jw 0:23a749719479 1 /**
el14jw 0:23a749719479 2 @file main.h
el14jw 0:23a749719479 3
el14jw 0:23a749719479 4 @brief Header file containing functions prototypes, defines and global variables.
el14jw 0:23a749719479 5 @brief Revision 1.0.
el14jw 0:23a749719479 6 @author Joel W. Webb
el14jw 0:23a749719479 7 @date March 2016
el14jw 0:23a749719479 8 */
el14jw 0:23a749719479 9
el14jw 0:23a749719479 10 #ifndef MAIN_H
el14jw 0:23a749719479 11 #define MAIN_H
el14jw 0:23a749719479 12
el14jw 0:23a749719479 13 #define PI 3.14159265359
el14jw 0:23a749719479 14 #define DIRECTION_TOLERANCE 0.05
el14jw 0:23a749719479 15
el14jw 0:23a749719479 16 #include "mbed.h"
el14jw 0:23a749719479 17 #include "N5110.h"
el14jw 0:23a749719479 18
el14jw 0:23a749719479 19
el14jw 0:23a749719479 20
el14jw 0:23a749719479 21 // GPIO Objects
el14jw 0:23a749719479 22 /*
el14jw 0:23a749719479 23 @namespace lcd
el14jw 0:23a749719479 24 @brief N5110 object for interfacing with Nokia 5110 LCD
el14jw 0:23a749719479 25 @param VCC - PTE26
el14jw 0:23a749719479 26 @param SCE - PTA0
el14jw 0:23a749719479 27 @param RST - PTC4
el14jw 0:23a749719479 28 @param D/C - PTD0
el14jw 0:23a749719479 29 @param MOSI - PTD2
el14jw 0:23a749719479 30 @param SCLK - PTD1
el14jw 0:23a749719479 31 @param LED - PTC3
el14jw 0:23a749719479 32 */
el14jw 0:23a749719479 33 N5110 lcd(PTE26, PTA0, PTC4, PTD0, PTD2, PTD1, PTC3);
el14jw 0:23a749719479 34
el14jw 0:23a749719479 35 /**
el14jw 0:23a749719479 36 @namespace errorLED
el14jw 0:23a749719479 37 @brief GPIO output for error LED
el14jw 0:23a749719479 38 */
el14jw 0:23a749719479 39 DigitalOut errorLED(LED1);
el14jw 0:23a749719479 40
el14jw 0:23a749719479 41 /**
el14jw 0:23a749719479 42 @namespace buzzer
el14jw 0:23a749719479 43 @brief PWM output for buzzer
el14jw 0:23a749719479 44 */
el14jw 0:23a749719479 45 PwmOut buzzer(PTC10);
el14jw 0:23a749719479 46
el14jw 0:23a749719479 47 /**
el14jw 0:23a749719479 48 @namespace xpot
el14jw 0:23a749719479 49 @brief The joystick x axis potentiometer
el14jw 0:23a749719479 50 */
el14jw 0:23a749719479 51 AnalogIn xPot(PTB11);
el14jw 0:23a749719479 52
el14jw 0:23a749719479 53 /**
el14jw 0:23a749719479 54 @namespace ypot
el14jw 0:23a749719479 55 @brief The joystick y axis potentiometer
el14jw 0:23a749719479 56 */
el14jw 0:23a749719479 57 AnalogIn yPot(PTB10);
el14jw 0:23a749719479 58
el14jw 0:23a749719479 59 /**
el14jw 0:23a749719479 60 @namespace buttonjoy
el14jw 0:23a749719479 61 @brief Interrupt input for Joystick button
el14jw 0:23a749719479 62 */
el14jw 0:23a749719479 63 InterruptIn buttonjoy(PTB9);
el14jw 0:23a749719479 64
el14jw 0:23a749719479 65 /**
el14jw 0:23a749719479 66 @namespace buttonA
el14jw 0:23a749719479 67 @brief Interrupt input for Button A
el14jw 0:23a749719479 68 */
el14jw 0:23a749719479 69 InterruptIn buttonA(PTA1);
el14jw 0:23a749719479 70
el14jw 0:23a749719479 71 /**
el14jw 0:23a749719479 72 @namespace buttonB
el14jw 0:23a749719479 73 @brief Interrupt input for Button B
el14jw 0:23a749719479 74 */
el14jw 0:23a749719479 75 InterruptIn buttonB(PTB23);
el14jw 0:23a749719479 76
el14jw 0:23a749719479 77
el14jw 0:23a749719479 78
el14jw 0:23a749719479 79 //Data Structures
el14jw 0:23a749719479 80
el14jw 0:23a749719479 81 /// stringList struct for dealing with printing strings to LCD easier
el14jw 0:23a749719479 82 struct stringList {
el14jw 0:23a749719479 83 char str[14];
el14jw 0:23a749719479 84 int offset;
el14jw 0:23a749719479 85 };
el14jw 0:23a749719479 86 typedef stringList stringList;
el14jw 0:23a749719479 87
el14jw 0:23a749719479 88 /// cell struct for dealing with coordinates
el14jw 0:23a749719479 89 struct cell {
el14jw 0:23a749719479 90 int x;
el14jw 0:23a749719479 91 int y;
el14jw 0:23a749719479 92 };
el14jw 0:23a749719479 93 typedef cell cell;
el14jw 0:23a749719479 94
el14jw 0:23a749719479 95
el14jw 0:23a749719479 96
el14jw 0:23a749719479 97 // Global variables
el14jw 0:23a749719479 98
el14jw 0:23a749719479 99 volatile int g_buttonA_flag; /*!< Button A flag set in ISR */
el14jw 0:23a749719479 100 volatile int g_buttonB_flag; /*!< Button B flag set in ISR */
el14jw 0:23a749719479 101 volatile int g_buttonjoy_flag; /*!< Joystick Button flag set in ISR */
el14jw 0:23a749719479 102 volatile int g_joystick_flag; /*!< Joystick x and y values have been polled in ISR */
el14jw 0:23a749719479 103 volatile int g_gametick_flag; /*!< gametick flag is set in gametick_isr by Ticker gametick */
el14jw 0:23a749719479 104
el14jw 0:23a749719479 105 volatile const float* noteArray; /*!< Float pointer to next note in array. Used in sound Timeout ISR */
el14jw 0:23a749719479 106
el14jw 0:23a749719479 107 // Menu stringList arrays kept in FLASH
el14jw 0:23a749719479 108 const stringList menuList[] = {{"Games",20},{"Snake",0},{"Pong",0},{"replace",0},{"replace",0},{"replace",0}};
el14jw 0:23a749719479 109 const stringList snakeList[] = {{"Snake",20},{"Start",0},{"Difficulty",0},{"Back",0}};
el14jw 0:23a749719479 110 const stringList difficultyList[] = {{"Difficulty",10},{"Easy",0},{"Medium",0},{"Hard",0}};
el14jw 0:23a749719479 111 const stringList plinkList[] = {{"Plink",20},{"Start",0},{"Difficulty",0},{"Back",0}};
el14jw 0:23a749719479 112
el14jw 0:23a749719479 113
el14jw 0:23a749719479 114
el14jw 0:23a749719479 115 // Function Prototypes
el14jw 0:23a749719479 116
el14jw 0:23a749719479 117 //ISR
el14jw 0:23a749719479 118 /**
el14jw 0:23a749719479 119 Interrupt service routine for button A
el14jw 0:23a749719479 120 @brief Sets g_buttonA_flag
el14jw 0:23a749719479 121 */
el14jw 0:23a749719479 122 void buttonA_isr();
el14jw 0:23a749719479 123 /**
el14jw 0:23a749719479 124 Interrupt service routine for button B
el14jw 0:23a749719479 125 @brief Sets g_buttonB_flag
el14jw 0:23a749719479 126 */
el14jw 0:23a749719479 127 void buttonB_isr();
el14jw 0:23a749719479 128 /**
el14jw 0:23a749719479 129 Interrupt service routine for buttonjoy
el14jw 0:23a749719479 130 @brief Set g_buttonjoy_flag
el14jw 0:23a749719479 131 */
el14jw 0:23a749719479 132 void buttonjoy_isr();
el14jw 0:23a749719479 133
el14jw 0:23a749719479 134 /**
el14jw 0:23a749719479 135 Interrupt service routine for playing the current note
el14jw 0:23a749719479 136 @brief Alters the current PWM signal to the current note frequency read from noteArray
el14jw 0:23a749719479 137 */
el14jw 0:23a749719479 138 void sound_isr();
el14jw 0:23a749719479 139
el14jw 0:23a749719479 140 /**
el14jw 0:23a749719479 141 Interrupt service routine for Ticker gametick
el14jw 0:23a749719479 142 @brief Used to control the update speed of games
el14jw 0:23a749719479 143 */
el14jw 0:23a749719479 144 void gametick_isr();
el14jw 0:23a749719479 145
el14jw 0:23a749719479 146
el14jw 0:23a749719479 147 // Snake functions
el14jw 0:23a749719479 148 /**
el14jw 0:23a749719479 149 Main Snake game function
el14jw 0:23a749719479 150 @brief The snake function is called from the main loop and contains everything needed to play the snake game
el14jw 0:23a749719479 151 @param difficulty - The difficulty is easy(0) medium(1) or hard(2) and it alters how fast the game ticker is applied
el14jw 0:23a749719479 152 */
el14jw 0:23a749719479 153 void snake(int difficulty);
el14jw 0:23a749719479 154 /**
el14jw 0:23a749719479 155 drawCellArray function
el14jw 0:23a749719479 156 @brief A function for simplifying the drawing of pixels in an array of cell structs
el14jw 0:23a749719479 157 @param array[] - The array of cells the user wishes to plot
el14jw 0:23a749719479 158 @param length - Integer corresponding to how many array[] elements exist
el14jw 0:23a749719479 159 */
el14jw 0:23a749719479 160 void drawCellArray(cell array[], int length);
el14jw 0:23a749719479 161 /**
el14jw 0:23a749719479 162 drawBoundary function
el14jw 0:23a749719479 163 @brief Used for plotting a 2 pixel thick boundary to the edges of the LCD display
el14jw 0:23a749719479 164 */
el14jw 0:23a749719479 165 void drawBoundary();
el14jw 0:23a749719479 166
el14jw 0:23a749719479 167
el14jw 0:23a749719479 168 // Pong functions
el14jw 0:23a749719479 169
el14jw 0:23a749719479 170 void pong(int difficulty);
el14jw 0:23a749719479 171
el14jw 0:23a749719479 172
el14jw 0:23a749719479 173 // Menu functions
el14jw 0:23a749719479 174 /**
el14jw 0:23a749719479 175 Menu screen Controller
el14jw 0:23a749719479 176 @brief menu is a function that handles the selection of different menu selections
el14jw 0:23a749719479 177 @param menuList - The pointer to the array of strings handled in the menu (Must be 6 or less)
el14jw 0:23a749719479 178 @param line - The number of lines in the menu list (Must be 6 or less)
el14jw 0:23a749719479 179 @returns Selected menu element (0-4)
el14jw 0:23a749719479 180 */
el14jw 0:23a749719479 181 int menu(const stringList* menuList,int lines);
el14jw 0:23a749719479 182 /**
el14jw 0:23a749719479 183 The drawString function
el14jw 0:23a749719479 184 @brief This function is used to simplify the menu function
el14jw 0:23a749719479 185 @param stringList - a const string array containing the strings needed to be written on each line
el14jw 0:23a749719479 186 @param lines - Integer representing how many strings should be written
el14jw 0:23a749719479 187 */
el14jw 0:23a749719479 188 void drawStrings(const stringList* list,int lines);
el14jw 0:23a749719479 189
el14jw 0:23a749719479 190
el14jw 0:23a749719479 191 // Sound and error functions
el14jw 0:23a749719479 192 /**
el14jw 0:23a749719479 193 Attaches Timeout to play sounds immediately
el14jw 0:23a749719479 194 @brief playSound is a function to provide ease fo use when calling sound arrays
el14jw 0:23a749719479 195 @param sound - The pointer to the noteArray
el14jw 0:23a749719479 196 */
el14jw 0:23a749719479 197 void playSound(const float* sound);
el14jw 0:23a749719479 198
el14jw 0:23a749719479 199 /**
el14jw 0:23a749719479 200 Error function
el14jw 0:23a749719479 201 @brief Hangs while flashing errorLED
el14jw 0:23a749719479 202 */
el14jw 0:23a749719479 203 void error();
el14jw 0:23a749719479 204
el14jw 0:23a749719479 205
el14jw 0:23a749719479 206 // Initialising and joystick
el14jw 0:23a749719479 207 /**
el14jw 0:23a749719479 208 Initializes Inputs
el14jw 0:23a749719479 209 @brief Used to group code that will run only once at startup
el14jw 0:23a749719479 210 */
el14jw 0:23a749719479 211 void initInputs();
el14jw 0:23a749719479 212 /**
el14jw 0:23a749719479 213 Initializes the joystick
el14jw 0:23a749719479 214 @brief Aquires xpot and ypot default values
el14jw 0:23a749719479 215 */
el14jw 0:23a749719479 216 void calibrateJoystick();
el14jw 0:23a749719479 217
el14jw 0:23a749719479 218 /**
el14jw 0:23a749719479 219 updateJoystick
el14jw 0:23a749719479 220 @brief Updates direction the joystick is pointing in on a Ticker event
el14jw 0:23a749719479 221 */
el14jw 0:23a749719479 222 void updateJoystick();
el14jw 0:23a749719479 223
el14jw 0:23a749719479 224
el14jw 0:23a749719479 225
el14jw 0:23a749719479 226 #include "sounds.h"
el14jw 0:23a749719479 227
el14jw 0:23a749719479 228 #endif
el14jw 0:23a749719479 229