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

Dependencies:   N5110 SDFileSystem mbed

Committer:
el14jw
Date:
Thu Apr 21 16:20:25 2016 +0000
Revision:
2:80a91a737e17
Parent:
1:c4928de1f922
Child:
3:02d9072a2507
- Added power ups to plink

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