ELEC2645 (2015/16)
/
2645_ProjectCode
Asteroids
cometCrusher.h@3:520df1778717, 2016-05-05 (annotated)
- Committer:
- el15s3p
- Date:
- Thu May 05 14:59:03 2016 +0000
- Revision:
- 3:520df1778717
Asteroids like game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el15s3p | 3:520df1778717 | 1 | #include "mbed.h" |
el15s3p | 3:520df1778717 | 2 | #include "N5110.h" |
el15s3p | 3:520df1778717 | 3 | |
el15s3p | 3:520df1778717 | 4 | #define DIRECTION_TOLERANCE 0.05L |
el15s3p | 3:520df1778717 | 5 | |
el15s3p | 3:520df1778717 | 6 | // VCC, SCE, RST, D/C, MOSI, SCLK, LED |
el15s3p | 3:520df1778717 | 7 | N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); |
el15s3p | 3:520df1778717 | 8 | |
el15s3p | 3:520df1778717 | 9 | // connections for joystick |
el15s3p | 3:520df1778717 | 10 | DigitalIn button(PTB11); |
el15s3p | 3:520df1778717 | 11 | AnalogIn xPot(PTB2); |
el15s3p | 3:520df1778717 | 12 | AnalogIn yPot(PTB3); |
el15s3p | 3:520df1778717 | 13 | |
el15s3p | 3:520df1778717 | 14 | AnalogIn Pot1(PTB10); |
el15s3p | 3:520df1778717 | 15 | |
el15s3p | 3:520df1778717 | 16 | PwmOut led1(PTC2); |
el15s3p | 3:520df1778717 | 17 | |
el15s3p | 3:520df1778717 | 18 | //InterruptIn sw2 (SW2); |
el15s3p | 3:520df1778717 | 19 | DigitalIn buttonA(PTB18); |
el15s3p | 3:520df1778717 | 20 | DigitalIn buttonB(PTB19); |
el15s3p | 3:520df1778717 | 21 | PwmOut buzzer(PTA2); |
el15s3p | 3:520df1778717 | 22 | |
el15s3p | 3:520df1778717 | 23 | //volatile int g_sw2_flag = 0; |
el15s3p | 3:520df1778717 | 24 | int shipShape [7][11] = { |
el15s3p | 3:520df1778717 | 25 | {0,0,0,0,0,0,0,0,0,0,1}, |
el15s3p | 3:520df1778717 | 26 | {0,0,0,0,0,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 27 | {0,0,1,1,1,1,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 28 | {1,1,1,1,1,1,1,1,0,0,0}, |
el15s3p | 3:520df1778717 | 29 | {0,0,1,1,1,1,0,1,1,0,0}, |
el15s3p | 3:520df1778717 | 30 | {0,0,0,0,0,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 31 | {0,0,0,0,0,0,0,0,0,0,1} |
el15s3p | 3:520df1778717 | 32 | }; |
el15s3p | 3:520df1778717 | 33 | |
el15s3p | 3:520df1778717 | 34 | void ship() |
el15s3p | 3:520df1778717 | 35 | { |
el15s3p | 3:520df1778717 | 36 | for(int shipR = 0; shipR <= 6; shipR ++) { |
el15s3p | 3:520df1778717 | 37 | for(int shipC = 0; shipC <= 10; shipC ++) { |
el15s3p | 3:520df1778717 | 38 | if(shipShape[shipR][shipC] == 0) { |
el15s3p | 3:520df1778717 | 39 | lcd.clearPixel(shipR + 35,shipC + 20); |
el15s3p | 3:520df1778717 | 40 | } else if(shipShape[shipR][shipC] == 1) { |
el15s3p | 3:520df1778717 | 41 | lcd.setPixel(shipR + 35,shipC + 20); |
el15s3p | 3:520df1778717 | 42 | } |
el15s3p | 3:520df1778717 | 43 | } |
el15s3p | 3:520df1778717 | 44 | } |
el15s3p | 3:520df1778717 | 45 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 46 | |
el15s3p | 3:520df1778717 | 47 | } |
el15s3p | 3:520df1778717 | 48 | |
el15s3p | 3:520df1778717 | 49 | |
el15s3p | 3:520df1778717 | 50 | // timer to regularly read the joystick |
el15s3p | 3:520df1778717 | 51 | Ticker pollJoystick; |
el15s3p | 3:520df1778717 | 52 | |
el15s3p | 3:520df1778717 | 53 | enum DirectionName { |
el15s3p | 3:520df1778717 | 54 | UP, |
el15s3p | 3:520df1778717 | 55 | DOWN, |
el15s3p | 3:520df1778717 | 56 | LEFT, |
el15s3p | 3:520df1778717 | 57 | RIGHT, |
el15s3p | 3:520df1778717 | 58 | CENTRE, |
el15s3p | 3:520df1778717 | 59 | UNKNOWN |
el15s3p | 3:520df1778717 | 60 | }; |
el15s3p | 3:520df1778717 | 61 | |
el15s3p | 3:520df1778717 | 62 | // struct for Joystick |
el15s3p | 3:520df1778717 | 63 | typedef struct JoyStick Joystick; |
el15s3p | 3:520df1778717 | 64 | struct JoyStick { |
el15s3p | 3:520df1778717 | 65 | float x; // current x value |
el15s3p | 3:520df1778717 | 66 | float x0; // 'centred' x value |
el15s3p | 3:520df1778717 | 67 | float y; // current y value |
el15s3p | 3:520df1778717 | 68 | float y0; // 'centred' y value |
el15s3p | 3:520df1778717 | 69 | int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
el15s3p | 3:520df1778717 | 70 | int buttonA; // buttonA state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
el15s3p | 3:520df1778717 | 71 | int buttonB; // buttonB state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
el15s3p | 3:520df1778717 | 72 | DirectionName direction; // current direction |
el15s3p | 3:520df1778717 | 73 | }; |
el15s3p | 3:520df1778717 | 74 | // create struct variable |
el15s3p | 3:520df1778717 | 75 | Joystick joystick; |
el15s3p | 3:520df1778717 | 76 | |
el15s3p | 3:520df1778717 | 77 | int printFlag = 0; |
el15s3p | 3:520df1778717 | 78 | |
el15s3p | 3:520df1778717 | 79 | // function prototypes |
el15s3p | 3:520df1778717 | 80 | void calibrateJoystick(); |
el15s3p | 3:520df1778717 | 81 | void updateJoystick(); |
el15s3p | 3:520df1778717 | 82 | |
el15s3p | 3:520df1778717 | 83 | //void sw2_isr(); |
el15s3p | 3:520df1778717 | 84 | |
el15s3p | 3:520df1778717 | 85 | void menu(); |
el15s3p | 3:520df1778717 | 86 | void game(); |
el15s3p | 3:520df1778717 | 87 | void difficultySet(); |
el15s3p | 3:520df1778717 | 88 | void highScore(); |
el15s3p | 3:520df1778717 | 89 | void instructions(); |
el15s3p | 3:520df1778717 | 90 | |
el15s3p | 3:520df1778717 | 91 | |
el15s3p | 3:520df1778717 | 92 | int menuSelect = 2; //Default value of menuSelect so cursor is already on "Start" option |
el15s3p | 3:520df1778717 | 93 | int menuUp = 1;//default value for indicator to show menu is up |
el15s3p | 3:520df1778717 | 94 | |
el15s3p | 3:520df1778717 | 95 | int main() |
el15s3p | 3:520df1778717 | 96 | { |
el15s3p | 3:520df1778717 | 97 | while(1) { |
el15s3p | 3:520df1778717 | 98 | calibrateJoystick(); // get centred values of joystick |
el15s3p | 3:520df1778717 | 99 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
el15s3p | 3:520df1778717 | 100 | while(menuUp == 1) { |
el15s3p | 3:520df1778717 | 101 | menu(); |
el15s3p | 3:520df1778717 | 102 | } |
el15s3p | 3:520df1778717 | 103 | } |
el15s3p | 3:520df1778717 | 104 | } |
el15s3p | 3:520df1778717 | 105 | |
el15s3p | 3:520df1778717 | 106 | void menu() |
el15s3p | 3:520df1778717 | 107 | { |
el15s3p | 3:520df1778717 | 108 | calibrateJoystick(); // get centred values of joystick |
el15s3p | 3:520df1778717 | 109 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
el15s3p | 3:520df1778717 | 110 | |
el15s3p | 3:520df1778717 | 111 | lcd.init(); |
el15s3p | 3:520df1778717 | 112 | lcd.clear(); |
el15s3p | 3:520df1778717 | 113 | lcd.normalMode(); |
el15s3p | 3:520df1778717 | 114 | lcd.printString("Comet Crusher!",0,0); |
el15s3p | 3:520df1778717 | 115 | lcd.printString("Start",10,2); |
el15s3p | 3:520df1778717 | 116 | lcd.printString("Instructions",10,3); |
el15s3p | 3:520df1778717 | 117 | lcd.printString("Difficulty",10,4); |
el15s3p | 3:520df1778717 | 118 | lcd.printString("Scores",10,5); |
el15s3p | 3:520df1778717 | 119 | lcd.printString(">",5,menuSelect); |
el15s3p | 3:520df1778717 | 120 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 121 | sleep(); |
el15s3p | 3:520df1778717 | 122 | switch (menuSelect) { |
el15s3p | 3:520df1778717 | 123 | case 2: |
el15s3p | 3:520df1778717 | 124 | if (joystick.direction == DOWN) { |
el15s3p | 3:520df1778717 | 125 | menuSelect = 3; |
el15s3p | 3:520df1778717 | 126 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 127 | wait (0.2); |
el15s3p | 3:520df1778717 | 128 | sleep(); |
el15s3p | 3:520df1778717 | 129 | } else if (joystick.direction == UP) { |
el15s3p | 3:520df1778717 | 130 | menuSelect = 5; |
el15s3p | 3:520df1778717 | 131 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 132 | wait (0.2); |
el15s3p | 3:520df1778717 | 133 | sleep(); |
el15s3p | 3:520df1778717 | 134 | } |
el15s3p | 3:520df1778717 | 135 | if (buttonA == 1) { |
el15s3p | 3:520df1778717 | 136 | menuUp = 0; |
el15s3p | 3:520df1778717 | 137 | game(); |
el15s3p | 3:520df1778717 | 138 | } |
el15s3p | 3:520df1778717 | 139 | break; |
el15s3p | 3:520df1778717 | 140 | case 3: |
el15s3p | 3:520df1778717 | 141 | if (joystick.direction == DOWN) { |
el15s3p | 3:520df1778717 | 142 | menuSelect = 4; |
el15s3p | 3:520df1778717 | 143 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 144 | wait (0.2); |
el15s3p | 3:520df1778717 | 145 | sleep(); |
el15s3p | 3:520df1778717 | 146 | } else if (joystick.direction == UP) { |
el15s3p | 3:520df1778717 | 147 | menuSelect = 2; |
el15s3p | 3:520df1778717 | 148 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 149 | wait (0.2); |
el15s3p | 3:520df1778717 | 150 | sleep(); |
el15s3p | 3:520df1778717 | 151 | } |
el15s3p | 3:520df1778717 | 152 | break; |
el15s3p | 3:520df1778717 | 153 | case 4: |
el15s3p | 3:520df1778717 | 154 | if (joystick.direction == DOWN) { |
el15s3p | 3:520df1778717 | 155 | menuSelect = 5; |
el15s3p | 3:520df1778717 | 156 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 157 | wait (0.2); |
el15s3p | 3:520df1778717 | 158 | sleep(); |
el15s3p | 3:520df1778717 | 159 | } else if (joystick.direction == UP) { |
el15s3p | 3:520df1778717 | 160 | menuSelect = 3; |
el15s3p | 3:520df1778717 | 161 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 162 | wait (0.2); |
el15s3p | 3:520df1778717 | 163 | sleep(); |
el15s3p | 3:520df1778717 | 164 | } |
el15s3p | 3:520df1778717 | 165 | break; |
el15s3p | 3:520df1778717 | 166 | case 5: |
el15s3p | 3:520df1778717 | 167 | if (joystick.direction == DOWN) { |
el15s3p | 3:520df1778717 | 168 | menuSelect = 2; |
el15s3p | 3:520df1778717 | 169 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 170 | wait (0.2); |
el15s3p | 3:520df1778717 | 171 | sleep(); |
el15s3p | 3:520df1778717 | 172 | } else if (joystick.direction == UP) { |
el15s3p | 3:520df1778717 | 173 | menuSelect = 4; |
el15s3p | 3:520df1778717 | 174 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 175 | wait (0.2); |
el15s3p | 3:520df1778717 | 176 | sleep(); |
el15s3p | 3:520df1778717 | 177 | } |
el15s3p | 3:520df1778717 | 178 | break; |
el15s3p | 3:520df1778717 | 179 | } |
el15s3p | 3:520df1778717 | 180 | } |
el15s3p | 3:520df1778717 | 181 | void game() |
el15s3p | 3:520df1778717 | 182 | { |
el15s3p | 3:520df1778717 | 183 | // SW2 has a pull-up resistor, so the pin will be at 3.3 V by default |
el15s3p | 3:520df1778717 | 184 | // and fall to 0 V when pressed. We therefore need to look for a falling edge |
el15s3p | 3:520df1778717 | 185 | // on the pin to fire the interrupt |
el15s3p | 3:520df1778717 | 186 | //sw2.fall(&sw2_isr); |
el15s3p | 3:520df1778717 | 187 | //since SW2 has an external pull-up, we should disable to internal pull-down |
el15s3p | 3:520df1778717 | 188 | // resistor that is enabled by default using InterruptIn |
el15s3p | 3:520df1778717 | 189 | //sw2.mode(PullNone); |
el15s3p | 3:520df1778717 | 190 | |
el15s3p | 3:520df1778717 | 191 | lcd.clear(); //Clear menu and start game |
el15s3p | 3:520df1778717 | 192 | ship(); |
el15s3p | 3:520df1778717 | 193 | } |
el15s3p | 3:520df1778717 | 194 | |
el15s3p | 3:520df1778717 | 195 | void difficultySet() |
el15s3p | 3:520df1778717 | 196 | { |
el15s3p | 3:520df1778717 | 197 | } |
el15s3p | 3:520df1778717 | 198 | |
el15s3p | 3:520df1778717 | 199 | void highScore() |
el15s3p | 3:520df1778717 | 200 | { |
el15s3p | 3:520df1778717 | 201 | } |
el15s3p | 3:520df1778717 | 202 | |
el15s3p | 3:520df1778717 | 203 | void instructions() |
el15s3p | 3:520df1778717 | 204 | { |
el15s3p | 3:520df1778717 | 205 | } |
el15s3p | 3:520df1778717 | 206 | |
el15s3p | 3:520df1778717 | 207 | |
el15s3p | 3:520df1778717 | 208 | |
el15s3p | 3:520df1778717 | 209 | |
el15s3p | 3:520df1778717 | 210 | void calibrateJoystick() |
el15s3p | 3:520df1778717 | 211 | { |
el15s3p | 3:520df1778717 | 212 | button.mode(PullDown); |
el15s3p | 3:520df1778717 | 213 | buttonA.mode(PullDown); |
el15s3p | 3:520df1778717 | 214 | buttonB.mode(PullDown); |
el15s3p | 3:520df1778717 | 215 | |
el15s3p | 3:520df1778717 | 216 | // must not move during calibration |
el15s3p | 3:520df1778717 | 217 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
el15s3p | 3:520df1778717 | 218 | joystick.y0 = yPot; |
el15s3p | 3:520df1778717 | 219 | } |
el15s3p | 3:520df1778717 | 220 | void updateJoystick() |
el15s3p | 3:520df1778717 | 221 | { |
el15s3p | 3:520df1778717 | 222 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
el15s3p | 3:520df1778717 | 223 | joystick.x = xPot - joystick.x0; |
el15s3p | 3:520df1778717 | 224 | joystick.y = yPot - joystick.y0; |
el15s3p | 3:520df1778717 | 225 | // read button state |
el15s3p | 3:520df1778717 | 226 | joystick.button = button; |
el15s3p | 3:520df1778717 | 227 | joystick.buttonA = buttonA; |
el15s3p | 3:520df1778717 | 228 | joystick.buttonB = buttonB; |
el15s3p | 3:520df1778717 | 229 | |
el15s3p | 3:520df1778717 | 230 | // calculate direction depending on x,y values |
el15s3p | 3:520df1778717 | 231 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
el15s3p | 3:520df1778717 | 232 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el15s3p | 3:520df1778717 | 233 | joystick.direction = CENTRE; |
el15s3p | 3:520df1778717 | 234 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el15s3p | 3:520df1778717 | 235 | joystick.direction = DOWN; |
el15s3p | 3:520df1778717 | 236 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el15s3p | 3:520df1778717 | 237 | joystick.direction = UP; |
el15s3p | 3:520df1778717 | 238 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el15s3p | 3:520df1778717 | 239 | joystick.direction = LEFT; |
el15s3p | 3:520df1778717 | 240 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el15s3p | 3:520df1778717 | 241 | joystick.direction = RIGHT; |
el15s3p | 3:520df1778717 | 242 | } else { |
el15s3p | 3:520df1778717 | 243 | joystick.direction = UNKNOWN; |
el15s3p | 3:520df1778717 | 244 | } |
el15s3p | 3:520df1778717 | 245 | |
el15s3p | 3:520df1778717 | 246 | // set flag for printing |
el15s3p | 3:520df1778717 | 247 | printFlag = 1; |
el15s3p | 3:520df1778717 | 248 | } |