Finished Lab 4 Pt 1
Dependencies: mbed Sounds PinDetect
main.cpp@0:daf9e2f8e1a1, 2019-04-05 (annotated)
- Committer:
- trmontgomery
- Date:
- Fri Apr 05 19:46:26 2019 +0000
- Revision:
- 0:daf9e2f8e1a1
Finished Lab 4 pt 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
trmontgomery | 0:daf9e2f8e1a1 | 1 | #include "mbed.h" |
trmontgomery | 0:daf9e2f8e1a1 | 2 | #include <stdio.h> |
trmontgomery | 0:daf9e2f8e1a1 | 3 | #include "Speaker.h" |
trmontgomery | 0:daf9e2f8e1a1 | 4 | #include "PinDetect.h" |
trmontgomery | 0:daf9e2f8e1a1 | 5 | #include "BuzzyGraphics.h" |
trmontgomery | 0:daf9e2f8e1a1 | 6 | #include "uLCD_4DGL.h" |
trmontgomery | 0:daf9e2f8e1a1 | 7 | |
trmontgomery | 0:daf9e2f8e1a1 | 8 | #include "Buzzy.h" |
trmontgomery | 0:daf9e2f8e1a1 | 9 | #include "Ghosts.h" |
trmontgomery | 0:daf9e2f8e1a1 | 10 | |
trmontgomery | 0:daf9e2f8e1a1 | 11 | DigitalOut myled1(LED1); |
trmontgomery | 0:daf9e2f8e1a1 | 12 | DigitalOut myled2(LED2); |
trmontgomery | 0:daf9e2f8e1a1 | 13 | DigitalOut myled3(LED3); |
trmontgomery | 0:daf9e2f8e1a1 | 14 | DigitalOut myled4(LED4); |
trmontgomery | 0:daf9e2f8e1a1 | 15 | //////////////////////////////////////// |
trmontgomery | 0:daf9e2f8e1a1 | 16 | // Setup instance of LCD display |
trmontgomery | 0:daf9e2f8e1a1 | 17 | uLCD_4DGL guLCD(p9, p10, p11); // serial tx, serial rx, reset pin; |
trmontgomery | 0:daf9e2f8e1a1 | 18 | //////////////////////////////////////// |
trmontgomery | 0:daf9e2f8e1a1 | 19 | // Setup instances of push button pins |
trmontgomery | 0:daf9e2f8e1a1 | 20 | PinDetect gPB_left(p21); |
trmontgomery | 0:daf9e2f8e1a1 | 21 | PinDetect gPB_right(p22); |
trmontgomery | 0:daf9e2f8e1a1 | 22 | PinDetect gPB_up(p23); |
trmontgomery | 0:daf9e2f8e1a1 | 23 | PinDetect gPB_down(p24); |
trmontgomery | 0:daf9e2f8e1a1 | 24 | // Create Buzzy and Ghosts |
trmontgomery | 0:daf9e2f8e1a1 | 25 | Buzzy gBuzzy; |
trmontgomery | 0:daf9e2f8e1a1 | 26 | Ghosts gGhosts[NUM_GHOSTS]; |
trmontgomery | 0:daf9e2f8e1a1 | 27 | // Variable indicates if game is paused or running |
trmontgomery | 0:daf9e2f8e1a1 | 28 | int gGameState = GAME_PAUSED; |
trmontgomery | 0:daf9e2f8e1a1 | 29 | // Declare and initialize the speaker |
trmontgomery | 0:daf9e2f8e1a1 | 30 | Speaker gSpeakerOut(p26); |
trmontgomery | 0:daf9e2f8e1a1 | 31 | |
trmontgomery | 0:daf9e2f8e1a1 | 32 | //////////////////////////////////////////////////////// |
trmontgomery | 0:daf9e2f8e1a1 | 33 | // This is the maze that changes as the game is played |
trmontgomery | 0:daf9e2f8e1a1 | 34 | char gDynaMaze[MAZE_NUM_ROW][MAZE_NUM_COL]; |
trmontgomery | 0:daf9e2f8e1a1 | 35 | |
trmontgomery | 0:daf9e2f8e1a1 | 36 | ////////////////////////// |
trmontgomery | 0:daf9e2f8e1a1 | 37 | // Prototype functions |
trmontgomery | 0:daf9e2f8e1a1 | 38 | void DrawMaze(); |
trmontgomery | 0:daf9e2f8e1a1 | 39 | |
trmontgomery | 0:daf9e2f8e1a1 | 40 | ////////////////////////////////////////////////////////////////////// |
trmontgomery | 0:daf9e2f8e1a1 | 41 | // Interrupt routine |
trmontgomery | 0:daf9e2f8e1a1 | 42 | // used to output next analog sample whenever a timer interrupt occurs |
trmontgomery | 0:daf9e2f8e1a1 | 43 | void Sample_timer_interrupt(void) |
trmontgomery | 0:daf9e2f8e1a1 | 44 | { |
trmontgomery | 0:daf9e2f8e1a1 | 45 | // Call speaker function to play next value |
trmontgomery | 0:daf9e2f8e1a1 | 46 | } |
trmontgomery | 0:daf9e2f8e1a1 | 47 | //--------------------------------------------------------------------------------------------------- |
trmontgomery | 0:daf9e2f8e1a1 | 48 | // Callback routine is interrupt activated by a debounced pb_left hit |
trmontgomery | 0:daf9e2f8e1a1 | 49 | void pb_left_hit_callback (void) |
trmontgomery | 0:daf9e2f8e1a1 | 50 | { |
trmontgomery | 0:daf9e2f8e1a1 | 51 | // Update game state and tell Buzzy to go left |
trmontgomery | 0:daf9e2f8e1a1 | 52 | gBuzzy.SetDesiredDirectionToMove(Sprite::LEFT_DIR); |
trmontgomery | 0:daf9e2f8e1a1 | 53 | } |
trmontgomery | 0:daf9e2f8e1a1 | 54 | //--------------------------------------------------------------------------------------------------- |
trmontgomery | 0:daf9e2f8e1a1 | 55 | // Callback routine is interrupt activated by a debounced pb_right hit |
trmontgomery | 0:daf9e2f8e1a1 | 56 | void pb_right_hit_callback (void) |
trmontgomery | 0:daf9e2f8e1a1 | 57 | { |
trmontgomery | 0:daf9e2f8e1a1 | 58 | // Update game state and tell Buzzy to go right |
trmontgomery | 0:daf9e2f8e1a1 | 59 | gBuzzy.SetDesiredDirectionToMove(Sprite::RIGHT_DIR); |
trmontgomery | 0:daf9e2f8e1a1 | 60 | } |
trmontgomery | 0:daf9e2f8e1a1 | 61 | //--------------------------------------------------------------------------------------------------- |
trmontgomery | 0:daf9e2f8e1a1 | 62 | // Callback routine is interrupt activated by a debounced pb_up hit |
trmontgomery | 0:daf9e2f8e1a1 | 63 | void pb_up_hit_callback (void) |
trmontgomery | 0:daf9e2f8e1a1 | 64 | { |
trmontgomery | 0:daf9e2f8e1a1 | 65 | // Update game state and tell Buzzy to go up |
trmontgomery | 0:daf9e2f8e1a1 | 66 | gBuzzy.SetDesiredDirectionToMove(Sprite::UP_DIR); |
trmontgomery | 0:daf9e2f8e1a1 | 67 | } |
trmontgomery | 0:daf9e2f8e1a1 | 68 | //--------------------------------------------------------------------------------------------------- |
trmontgomery | 0:daf9e2f8e1a1 | 69 | // Callback routine is interrupt activated by a debounced pb_down hit |
trmontgomery | 0:daf9e2f8e1a1 | 70 | void pb_down_hit_callback (void) |
trmontgomery | 0:daf9e2f8e1a1 | 71 | { |
trmontgomery | 0:daf9e2f8e1a1 | 72 | // Update game state and tell Buzzy to go down |
trmontgomery | 0:daf9e2f8e1a1 | 73 | gBuzzy.SetDesiredDirectionToMove(Sprite::DOWN_DIR); |
trmontgomery | 0:daf9e2f8e1a1 | 74 | } |
trmontgomery | 0:daf9e2f8e1a1 | 75 | //--------------------------------------------------------------------------------------------------- |
trmontgomery | 0:daf9e2f8e1a1 | 76 | int main() |
trmontgomery | 0:daf9e2f8e1a1 | 77 | { |
trmontgomery | 0:daf9e2f8e1a1 | 78 | |
trmontgomery | 0:daf9e2f8e1a1 | 79 | // Setup push buttons |
trmontgomery | 0:daf9e2f8e1a1 | 80 | gPB_left.mode(PullUp); |
trmontgomery | 0:daf9e2f8e1a1 | 81 | gPB_right.mode(PullUp); |
trmontgomery | 0:daf9e2f8e1a1 | 82 | gPB_up.mode(PullUp); |
trmontgomery | 0:daf9e2f8e1a1 | 83 | gPB_down.mode(PullUp); |
trmontgomery | 0:daf9e2f8e1a1 | 84 | // Delay for initial pullup to take effect |
trmontgomery | 0:daf9e2f8e1a1 | 85 | wait(.01); |
trmontgomery | 0:daf9e2f8e1a1 | 86 | // Setup Interrupt callback functions for a pb hit |
trmontgomery | 0:daf9e2f8e1a1 | 87 | gPB_left.attach_deasserted(&pb_left_hit_callback); |
trmontgomery | 0:daf9e2f8e1a1 | 88 | gPB_right.attach_deasserted(&pb_right_hit_callback); |
trmontgomery | 0:daf9e2f8e1a1 | 89 | gPB_up.attach_deasserted(&pb_up_hit_callback); |
trmontgomery | 0:daf9e2f8e1a1 | 90 | gPB_down.attach_deasserted(&pb_down_hit_callback); |
trmontgomery | 0:daf9e2f8e1a1 | 91 | // Setup speaker |
trmontgomery | 0:daf9e2f8e1a1 | 92 | gSpeakerOut.period(1.0/200000.0); |
trmontgomery | 0:daf9e2f8e1a1 | 93 | // set up a timer to be used for sample rate interrupts |
trmontgomery | 0:daf9e2f8e1a1 | 94 | Ticker Sample_Period; |
trmontgomery | 0:daf9e2f8e1a1 | 95 | Sample_Period.attach(&Sample_timer_interrupt, 1.0/(20000.0)); |
trmontgomery | 0:daf9e2f8e1a1 | 96 | |
trmontgomery | 0:daf9e2f8e1a1 | 97 | //Setup LCD display |
trmontgomery | 0:daf9e2f8e1a1 | 98 | guLCD.display_control(PORTRAIT); |
trmontgomery | 0:daf9e2f8e1a1 | 99 | guLCD.background_color(BLACK); |
trmontgomery | 0:daf9e2f8e1a1 | 100 | guLCD.cls(); |
trmontgomery | 0:daf9e2f8e1a1 | 101 | guLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display |
trmontgomery | 0:daf9e2f8e1a1 | 102 | wait(1.0); |
trmontgomery | 0:daf9e2f8e1a1 | 103 | |
trmontgomery | 0:daf9e2f8e1a1 | 104 | // Start sampling pb inputs using interrupts |
trmontgomery | 0:daf9e2f8e1a1 | 105 | gPB_left.setSampleFrequency(); |
trmontgomery | 0:daf9e2f8e1a1 | 106 | gPB_right.setSampleFrequency(); |
trmontgomery | 0:daf9e2f8e1a1 | 107 | gPB_up.setSampleFrequency(); |
trmontgomery | 0:daf9e2f8e1a1 | 108 | gPB_down.setSampleFrequency(); |
trmontgomery | 0:daf9e2f8e1a1 | 109 | ////////////////////////////////////// |
trmontgomery | 0:daf9e2f8e1a1 | 110 | // Everything should be ready to start playing the game. |
trmontgomery | 0:daf9e2f8e1a1 | 111 | |
trmontgomery | 0:daf9e2f8e1a1 | 112 | while(1) |
trmontgomery | 0:daf9e2f8e1a1 | 113 | { |
trmontgomery | 0:daf9e2f8e1a1 | 114 | guLCD.cls(); |
trmontgomery | 0:daf9e2f8e1a1 | 115 | // Ask the user if they would like to play a game. |
trmontgomery | 0:daf9e2f8e1a1 | 116 | guLCD.printf("Would you like to play??"); |
trmontgomery | 0:daf9e2f8e1a1 | 117 | // Wait for a button to be pressed |
trmontgomery | 0:daf9e2f8e1a1 | 118 | while(gPB_left == 1){ |
trmontgomery | 0:daf9e2f8e1a1 | 119 | wait(1); |
trmontgomery | 0:daf9e2f8e1a1 | 120 | } |
trmontgomery | 0:daf9e2f8e1a1 | 121 | guLCD.cls(); |
trmontgomery | 0:daf9e2f8e1a1 | 122 | // Initialize needed parts |
trmontgomery | 0:daf9e2f8e1a1 | 123 | for (int ii = 0 ; ii < MAZE_NUM_ROW ; ii++) |
trmontgomery | 0:daf9e2f8e1a1 | 124 | { |
trmontgomery | 0:daf9e2f8e1a1 | 125 | for (int jj = 0 ; jj < MAZE_NUM_COL ; jj++) |
trmontgomery | 0:daf9e2f8e1a1 | 126 | { |
trmontgomery | 0:daf9e2f8e1a1 | 127 | gDynaMaze[ii][jj] = gCnstMaze[ii][jj]; |
trmontgomery | 0:daf9e2f8e1a1 | 128 | } |
trmontgomery | 0:daf9e2f8e1a1 | 129 | } |
trmontgomery | 0:daf9e2f8e1a1 | 130 | // Reset the Ghosts and Buzzy states |
trmontgomery | 0:daf9e2f8e1a1 | 131 | gBuzzy.SetDesiredDirectionToMove(Sprite::RIGHT_DIR); |
trmontgomery | 0:daf9e2f8e1a1 | 132 | // Start up new game |
trmontgomery | 0:daf9e2f8e1a1 | 133 | gBuzzy.honey = 0; |
trmontgomery | 0:daf9e2f8e1a1 | 134 | |
trmontgomery | 0:daf9e2f8e1a1 | 135 | // Play introduction sounds while drawing the Maze |
trmontgomery | 0:daf9e2f8e1a1 | 136 | gSpeakerOut.SwitchSound(Speaker::BEGIN); |
trmontgomery | 0:daf9e2f8e1a1 | 137 | DrawMaze(); |
trmontgomery | 0:daf9e2f8e1a1 | 138 | // Start Game loop |
trmontgomery | 0:daf9e2f8e1a1 | 139 | gGameState = GAME_RUNNING; |
trmontgomery | 0:daf9e2f8e1a1 | 140 | while (gGameState == GAME_RUNNING) |
trmontgomery | 0:daf9e2f8e1a1 | 141 | { |
trmontgomery | 0:daf9e2f8e1a1 | 142 | // Move Buzzy and any active ghosts |
trmontgomery | 0:daf9e2f8e1a1 | 143 | gBuzzy.Move(); |
trmontgomery | 0:daf9e2f8e1a1 | 144 | // Check to see if Buzzy has eaten all the honey drops |
trmontgomery | 0:daf9e2f8e1a1 | 145 | if(gBuzzy.honey == 10){ |
trmontgomery | 0:daf9e2f8e1a1 | 146 | gGameState = 0; |
trmontgomery | 0:daf9e2f8e1a1 | 147 | } |
trmontgomery | 0:daf9e2f8e1a1 | 148 | // Break out of loop if all honey drops are consumed |
trmontgomery | 0:daf9e2f8e1a1 | 149 | |
trmontgomery | 0:daf9e2f8e1a1 | 150 | wait(0.001); |
trmontgomery | 0:daf9e2f8e1a1 | 151 | } |
trmontgomery | 0:daf9e2f8e1a1 | 152 | |
trmontgomery | 0:daf9e2f8e1a1 | 153 | guLCD.cls(); |
trmontgomery | 0:daf9e2f8e1a1 | 154 | guLCD.printf("GAME OVER!!"); |
trmontgomery | 0:daf9e2f8e1a1 | 155 | wait(2); |
trmontgomery | 0:daf9e2f8e1a1 | 156 | |
trmontgomery | 0:daf9e2f8e1a1 | 157 | gGameState = GAME_PAUSED; |
trmontgomery | 0:daf9e2f8e1a1 | 158 | } |
trmontgomery | 0:daf9e2f8e1a1 | 159 | |
trmontgomery | 0:daf9e2f8e1a1 | 160 | } //end main |