Meteor Dodger, simple mbed game w/ joystick on N5110 LCD

Dependencies:   N5110 PowerControl mbed

Committer:
j_ogus
Date:
Tue May 12 22:05:06 2015 +0000
Revision:
0:4686e853d105
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j_ogus 0:4686e853d105 1 /**
j_ogus 0:4686e853d105 2 *@file main.h
j_ogus 0:4686e853d105 3 *
j_ogus 0:4686e853d105 4 *@brief Library for enabling the main.cpp of the METEOR DODGER game to work.
j_ogus 0:4686e853d105 5 *@brief So that the code will work, the definitions, variables, parameters.
j_ogus 0:4686e853d105 6 *@brief and other libraries will need to be included and stated.
j_ogus 0:4686e853d105 7 *
j_ogus 0:4686e853d105 8 *@date 10/05/2015
j_ogus 0:4686e853d105 9 *
j_ogus 0:4686e853d105 10 */
j_ogus 0:4686e853d105 11
j_ogus 0:4686e853d105 12
j_ogus 0:4686e853d105 13 #ifndef MAIN_H
j_ogus 0:4686e853d105 14 #define MAIN_H
j_ogus 0:4686e853d105 15
j_ogus 0:4686e853d105 16 /**
j_ogus 0:4686e853d105 17 *@brief List of require libraries that enable lines of code to function.
j_ogus 0:4686e853d105 18 *@brief Some lines of code have words that execute integers of other functions.
j_ogus 0:4686e853d105 19 *@brief These are substituted in the definitions section.
j_ogus 0:4686e853d105 20 *@author Jeremy Ogus
j_ogus 0:4686e853d105 21 *@date 10/05/2015
j_ogus 0:4686e853d105 22 */
j_ogus 0:4686e853d105 23
j_ogus 0:4686e853d105 24 //libraries included
j_ogus 0:4686e853d105 25 #include "mbed.h"
j_ogus 0:4686e853d105 26 #include "N5110.h"
j_ogus 0:4686e853d105 27 #include "PowerControl/PowerControl.h"
j_ogus 0:4686e853d105 28 #include "PowerControl/EthernetPowerControl.h"
j_ogus 0:4686e853d105 29
j_ogus 0:4686e853d105 30 //stated definitions
j_ogus 0:4686e853d105 31 #define DIRECTION_TOLERANCE 0.15
j_ogus 0:4686e853d105 32 #define getTo goto
j_ogus 0:4686e853d105 33 #define zipTo goto
j_ogus 0:4686e853d105 34
j_ogus 0:4686e853d105 35 /**
j_ogus 0:4686e853d105 36 *@namespace lcd
j_ogus 0:4686e853d105 37 *@brief output pins for the LCD displace.
j_ogus 0:4686e853d105 38 */
j_ogus 0:4686e853d105 39 //Nokia LCD inputs, VCC,SCE,RST,D/C,MOSI,SCLK,LED
j_ogus 0:4686e853d105 40 N5110 lcd(p7,p8,p9,p10,p11,p13,p21);
j_ogus 0:4686e853d105 41
j_ogus 0:4686e853d105 42 /**
j_ogus 0:4686e853d105 43 *@namespace leds, led1, led3, led4
j_ogus 0:4686e853d105 44 *@brief internal mbed leds used in the intro to signify powerup.
j_ogus 0:4686e853d105 45 */
j_ogus 0:4686e853d105 46 //LED inputs on mbed
j_ogus 0:4686e853d105 47 BusOut leds(LED4, LED3, LED2, LED1);
j_ogus 0:4686e853d105 48 DigitalOut led1(LED1);
j_ogus 0:4686e853d105 49 DigitalOut led2(LED2);
j_ogus 0:4686e853d105 50 DigitalOut led3(LED3);
j_ogus 0:4686e853d105 51 DigitalOut led4(LED4);
j_ogus 0:4686e853d105 52
j_ogus 0:4686e853d105 53 /**
j_ogus 0:4686e853d105 54 *@namespace buttonA, buttonB
j_ogus 0:4686e853d105 55 *@brief inputs for the two push buttons.
j_ogus 0:4686e853d105 56 */
j_ogus 0:4686e853d105 57 //push buttons A & B inputs
j_ogus 0:4686e853d105 58 DigitalIn buttonA(p16);
j_ogus 0:4686e853d105 59 InterruptIn buttonB(p17);
j_ogus 0:4686e853d105 60
j_ogus 0:4686e853d105 61 /**
j_ogus 0:4686e853d105 62 *@namespace button, xPot, yPot
j_ogus 0:4686e853d105 63 *@brief inputs to enable the joystick to funciton.
j_ogus 0:4686e853d105 64 */
j_ogus 0:4686e853d105 65 //joystick inputs
j_ogus 0:4686e853d105 66 DigitalIn button(p18);
j_ogus 0:4686e853d105 67 AnalogIn xPot(p19);
j_ogus 0:4686e853d105 68 AnalogIn yPot(p20);
j_ogus 0:4686e853d105 69
j_ogus 0:4686e853d105 70 /**
j_ogus 0:4686e853d105 71 *@namespace buzzer
j_ogus 0:4686e853d105 72 *@brief input on PWM for the buzzer.
j_ogus 0:4686e853d105 73 */
j_ogus 0:4686e853d105 74 //buzzer input
j_ogus 0:4686e853d105 75 PwmOut buzzer(p22);
j_ogus 0:4686e853d105 76
j_ogus 0:4686e853d105 77 /**
j_ogus 0:4686e853d105 78 *@param meteorstart - interger to check state whilst spawning the AI.
j_ogus 0:4686e853d105 79 *@param magchange - interger to check whether to break out of game loop when the charcer
j_ogus 0:4686e853d105 80 *@param pxpos - interger to control position for both the character and AI in the horizontal direction
j_ogus 0:4686e853d105 81 *@param pypos - interger to control position for both the character and AI in the vertical direction
j_ogus 0:4686e853d105 82 *@param mxpos - interger to create array to limit hostile AI on display
j_ogus 0:4686e853d105 83 *@param mypos - interger to create array to limit hostile AI on display
j_ogus 0:4686e853d105 84 *@param goneFishing - timer to turn enter deep power down 10 seconds after game end, with no user input to replay
j_ogus 0:4686e853d105 85 *@param enable - loop for pause system, interrupt by button B
j_ogus 0:4686e853d105 86 */
j_ogus 0:4686e853d105 87
j_ogus 0:4686e853d105 88 //intergers for AI loop system
j_ogus 0:4686e853d105 89 int meteorstart = 1;
j_ogus 0:4686e853d105 90 int magchange;
j_ogus 0:4686e853d105 91
j_ogus 0:4686e853d105 92 //integers for user position
j_ogus 0:4686e853d105 93 int pxpos;
j_ogus 0:4686e853d105 94 int pypos;
j_ogus 0:4686e853d105 95
j_ogus 0:4686e853d105 96 // int for AI array
j_ogus 0:4686e853d105 97 int mxpos[5], mypos[5];
j_ogus 0:4686e853d105 98
j_ogus 0:4686e853d105 99
j_ogus 0:4686e853d105 100 Timer goneFishing;
j_ogus 0:4686e853d105 101
j_ogus 0:4686e853d105 102 void enable(void);
j_ogus 0:4686e853d105 103
j_ogus 0:4686e853d105 104
j_ogus 0:4686e853d105 105 //int joyCont(); RIP, was used in an earlier version. still here for personal reference
j_ogus 0:4686e853d105 106
j_ogus 0:4686e853d105 107
j_ogus 0:4686e853d105 108 /*
j_ogus 0:4686e853d105 109 *____________________________________________________Definitions and inputs END_____________________________________________________________________
j_ogus 0:4686e853d105 110 *
j_ogus 0:4686e853d105 111 *
j_ogus 0:4686e853d105 112 *
j_ogus 0:4686e853d105 113 *____________________________________________________Joystick (variables, definition, sensor) START___________________________________________________
j_ogus 0:4686e853d105 114 */
j_ogus 0:4686e853d105 115
j_ogus 0:4686e853d105 116 /**
j_ogus 0:4686e853d105 117 *@param pollJosytick - timer to constantly read joystick sensor 20 times/second
j_ogus 0:4686e853d105 118 *@param DirectionName - ironically, defines direction names when listed below in the if direction loop
j_ogus 0:4686e853d105 119 *@brief the remaining code is self explanatory, reading it will tell you what it does, however trivial it seems
j_ogus 0:4686e853d105 120 */
j_ogus 0:4686e853d105 121
j_ogus 0:4686e853d105 122 // timer to regularly read the joystick
j_ogus 0:4686e853d105 123 Ticker pollJoystick;
j_ogus 0:4686e853d105 124
j_ogus 0:4686e853d105 125
j_ogus 0:4686e853d105 126 // create enumerated type (0,1,2,3 etc. for direction)
j_ogus 0:4686e853d105 127 enum DirectionName {
j_ogus 0:4686e853d105 128 UP,
j_ogus 0:4686e853d105 129 DOWN,
j_ogus 0:4686e853d105 130 LEFT,
j_ogus 0:4686e853d105 131 RIGHT,
j_ogus 0:4686e853d105 132 UP_RIGHT,
j_ogus 0:4686e853d105 133 UP_LEFT,
j_ogus 0:4686e853d105 134 DOWN_RIGHT,
j_ogus 0:4686e853d105 135 DOWN_LEFT,
j_ogus 0:4686e853d105 136 CENTRE,
j_ogus 0:4686e853d105 137 UNKNOWN
j_ogus 0:4686e853d105 138 };
j_ogus 0:4686e853d105 139
j_ogus 0:4686e853d105 140 // struct for Joystick
j_ogus 0:4686e853d105 141 typedef struct JoyStick Joystick;
j_ogus 0:4686e853d105 142 struct JoyStick {
j_ogus 0:4686e853d105 143 float x; // current x value
j_ogus 0:4686e853d105 144 float x0; // 'centred' x value
j_ogus 0:4686e853d105 145 float y; // current y value
j_ogus 0:4686e853d105 146 float y0; // 'centred' y value
j_ogus 0:4686e853d105 147 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
j_ogus 0:4686e853d105 148 DirectionName direction; // current direction
j_ogus 0:4686e853d105 149 };
j_ogus 0:4686e853d105 150 // create struct variable
j_ogus 0:4686e853d105 151 Joystick joystick;
j_ogus 0:4686e853d105 152
j_ogus 0:4686e853d105 153 int printFlag = 0;
j_ogus 0:4686e853d105 154
j_ogus 0:4686e853d105 155 // function prototypes
j_ogus 0:4686e853d105 156 void calibrateJoystick();
j_ogus 0:4686e853d105 157 void updateJoystick();
j_ogus 0:4686e853d105 158
j_ogus 0:4686e853d105 159
j_ogus 0:4686e853d105 160 // read default positions of the joystick to calibrate later readings
j_ogus 0:4686e853d105 161 void calibrateJoystick()
j_ogus 0:4686e853d105 162 {
j_ogus 0:4686e853d105 163 button.mode(PullUp);
j_ogus 0:4686e853d105 164 // must not move during calibration
j_ogus 0:4686e853d105 165 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
j_ogus 0:4686e853d105 166 joystick.y0 = yPot;
j_ogus 0:4686e853d105 167 }
j_ogus 0:4686e853d105 168 void updateJoystick()
j_ogus 0:4686e853d105 169 {
j_ogus 0:4686e853d105 170 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
j_ogus 0:4686e853d105 171 joystick.x = xPot - joystick.x0;
j_ogus 0:4686e853d105 172 joystick.y = yPot - joystick.y0;
j_ogus 0:4686e853d105 173 // read button state
j_ogus 0:4686e853d105 174 joystick.button = button;
j_ogus 0:4686e853d105 175
j_ogus 0:4686e853d105 176 // calculate direction depending on x,y values
j_ogus 0:4686e853d105 177 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
j_ogus 0:4686e853d105 178 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
j_ogus 0:4686e853d105 179 joystick.direction = CENTRE;
j_ogus 0:4686e853d105 180 }
j_ogus 0:4686e853d105 181 else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
j_ogus 0:4686e853d105 182 joystick.direction = UP;
j_ogus 0:4686e853d105 183 }
j_ogus 0:4686e853d105 184 else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
j_ogus 0:4686e853d105 185 joystick.direction = DOWN;
j_ogus 0:4686e853d105 186 }
j_ogus 0:4686e853d105 187 else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
j_ogus 0:4686e853d105 188 joystick.direction = LEFT;
j_ogus 0:4686e853d105 189 }
j_ogus 0:4686e853d105 190 else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
j_ogus 0:4686e853d105 191 joystick.direction = RIGHT;
j_ogus 0:4686e853d105 192 }
j_ogus 0:4686e853d105 193 else if ( joystick.x < DIRECTION_TOLERANCE && joystick.y > DIRECTION_TOLERANCE) {
j_ogus 0:4686e853d105 194 joystick.direction = UP_RIGHT;
j_ogus 0:4686e853d105 195 }
j_ogus 0:4686e853d105 196 else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y > DIRECTION_TOLERANCE) {
j_ogus 0:4686e853d105 197 joystick.direction = UP_LEFT;
j_ogus 0:4686e853d105 198 }
j_ogus 0:4686e853d105 199 else if ( joystick.x < DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
j_ogus 0:4686e853d105 200 joystick.direction = DOWN_RIGHT;
j_ogus 0:4686e853d105 201 }
j_ogus 0:4686e853d105 202 else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
j_ogus 0:4686e853d105 203 joystick.direction = DOWN_LEFT;
j_ogus 0:4686e853d105 204 }
j_ogus 0:4686e853d105 205
j_ogus 0:4686e853d105 206 else {
j_ogus 0:4686e853d105 207 joystick.direction = UNKNOWN;
j_ogus 0:4686e853d105 208 }
j_ogus 0:4686e853d105 209
j_ogus 0:4686e853d105 210 // set flag for printing
j_ogus 0:4686e853d105 211 printFlag = 1;
j_ogus 0:4686e853d105 212 }
j_ogus 0:4686e853d105 213
j_ogus 0:4686e853d105 214 /*
j_ogus 0:4686e853d105 215 *____________________________________________________Joystick (variables, definition, sensor) END___________________________________________________
j_ogus 0:4686e853d105 216 *
j_ogus 0:4686e853d105 217 *
j_ogus 0:4686e853d105 218 *____________________________________________________Pause game interrupt START______________________________________________________________________
j_ogus 0:4686e853d105 219 */
j_ogus 0:4686e853d105 220
j_ogus 0:4686e853d105 221 /**
j_ogus 0:4686e853d105 222 *@brief this section of code is a pause section.
j_ogus 0:4686e853d105 223 *@brief when B is pushed, code is paused.
j_ogus 0:4686e853d105 224 *@brief when B is pushed again, code is resumed.
j_ogus 0:4686e853d105 225 *@author Jeremy Ogus
j_ogus 0:4686e853d105 226 *@date 10/05/2015
j_ogus 0:4686e853d105 227 *@param buttonB - Push button, can have a state of either 0 or 1
j_ogus 0:4686e853d105 228 */
j_ogus 0:4686e853d105 229
j_ogus 0:4686e853d105 230 //when enabled, short delay. This is so you don't switch the state
j_ogus 0:4686e853d105 231 //of the button by the time code has a chance to loop. Should take
j_ogus 0:4686e853d105 232 //less than 0.2 seconds for the user to press buttonB and let go.
j_ogus 0:4686e853d105 233 void enable(){
j_ogus 0:4686e853d105 234 wait(0.2);
j_ogus 0:4686e853d105 235 //if buttonB is pushed, wait 0.5s. This is for release time, as above
j_ogus 0:4686e853d105 236 if(buttonB==1){
j_ogus 0:4686e853d105 237 wait(0.5);
j_ogus 0:4686e853d105 238 //wait forever until buttonB is pushed again
j_ogus 0:4686e853d105 239 while(buttonB==1){
j_ogus 0:4686e853d105 240 }
j_ogus 0:4686e853d105 241 wait(0.2); //timer for release of the button to resume
j_ogus 0:4686e853d105 242
j_ogus 0:4686e853d105 243 }
j_ogus 0:4686e853d105 244
j_ogus 0:4686e853d105 245 }
j_ogus 0:4686e853d105 246
j_ogus 0:4686e853d105 247 #endif