![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
baseline features - a little buggy
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
main.cpp@0:95264f964374, 2021-03-29 (annotated)
- Committer:
- DCchico
- Date:
- Mon Mar 29 21:17:26 2021 -0400
- Revision:
- 0:95264f964374
- Child:
- 1:4421c1e849e9
inital commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DCchico |
0:95264f964374 | 1 | //================================================================= |
DCchico |
0:95264f964374 | 2 | // The main program file. |
DCchico |
0:95264f964374 | 3 | // |
DCchico |
0:95264f964374 | 4 | // Copyright 2020 Georgia Tech. All rights reserved. |
DCchico |
0:95264f964374 | 5 | // The materials provided by the instructor in this course are for |
DCchico |
0:95264f964374 | 6 | // the use of the students currently enrolled in the course. |
DCchico |
0:95264f964374 | 7 | // Copyrighted course materials may not be further disseminated. |
DCchico |
0:95264f964374 | 8 | // This file must not be made publicly available anywhere. |
DCchico |
0:95264f964374 | 9 | //================================================================== |
DCchico |
0:95264f964374 | 10 | |
DCchico |
0:95264f964374 | 11 | // External libs |
DCchico |
0:95264f964374 | 12 | #include <stdlib.h> |
DCchico |
0:95264f964374 | 13 | |
DCchico |
0:95264f964374 | 14 | // Project includes |
DCchico |
0:95264f964374 | 15 | #include "globals.h" |
DCchico |
0:95264f964374 | 16 | #include "hardware.h" |
DCchico |
0:95264f964374 | 17 | #include "compost_pile_public.h" |
DCchico |
0:95264f964374 | 18 | #include "fruit_public.h" |
DCchico |
0:95264f964374 | 19 | #include "player_public.h" |
DCchico |
0:95264f964374 | 20 | |
DCchico |
0:95264f964374 | 21 | //For sound components |
DCchico |
0:95264f964374 | 22 | ////AnalogOut DACout(p18); |
DCchico |
0:95264f964374 | 23 | //PwmOut pwmout(p26); |
DCchico |
0:95264f964374 | 24 | ////PwmOut speaker(p25); |
DCchico |
0:95264f964374 | 25 | |
DCchico |
0:95264f964374 | 26 | ////wave_player waver(&DACout); |
DCchico |
0:95264f964374 | 27 | //SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sck, cs |
DCchico |
0:95264f964374 | 28 | |
DCchico |
0:95264f964374 | 29 | DigitalOut myled1(LED1); |
DCchico |
0:95264f964374 | 30 | DigitalOut myled2(LED2); |
DCchico |
0:95264f964374 | 31 | DigitalOut myled3(LED3); |
DCchico |
0:95264f964374 | 32 | DigitalOut myled4(LED4); |
DCchico |
0:95264f964374 | 33 | |
DCchico |
0:95264f964374 | 34 | // ===User implementations start=== |
DCchico |
0:95264f964374 | 35 | int fruit_contact(void); // iterate through fruits and see if any collided |
DCchico |
0:95264f964374 | 36 | void compost_pile_update(void); |
DCchico |
0:95264f964374 | 37 | |
DCchico |
0:95264f964374 | 38 | void playSound(char * wav); |
DCchico |
0:95264f964374 | 39 | void playNotes(void); |
DCchico |
0:95264f964374 | 40 | void set_random_seed(Timer); |
DCchico |
0:95264f964374 | 41 | |
DCchico |
0:95264f964374 | 42 | int main() |
DCchico |
0:95264f964374 | 43 | { |
DCchico |
0:95264f964374 | 44 | GameInputs inputs; |
DCchico |
0:95264f964374 | 45 | // First things first: initialize hardware |
DCchico |
0:95264f964374 | 46 | ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!"); |
DCchico |
0:95264f964374 | 47 | pc.printf("Program Starting\n"); |
DCchico |
0:95264f964374 | 48 | |
DCchico |
0:95264f964374 | 49 | // ===User implementations start=== |
DCchico |
0:95264f964374 | 50 | // Game state variables |
DCchico |
0:95264f964374 | 51 | |
DCchico |
0:95264f964374 | 52 | // Timer to measure game update speed (secondarily used to generate random seed) |
DCchico |
0:95264f964374 | 53 | Timer t; |
DCchico |
0:95264f964374 | 54 | int dt; // delta time |
DCchico |
0:95264f964374 | 55 | set_random_seed(t); |
DCchico |
0:95264f964374 | 56 | |
DCchico |
0:95264f964374 | 57 | //pwmout.period(1.0/4000.0); |
DCchico |
0:95264f964374 | 58 | //playSound("/sd/wavfiles/BUZZER.wav");//test the sound convert to thread |
DCchico |
0:95264f964374 | 59 | //wait(0.1); |
DCchico |
0:95264f964374 | 60 | //playNotes(); |
DCchico |
0:95264f964374 | 61 | |
DCchico |
0:95264f964374 | 62 | //initialize functions |
DCchico |
0:95264f964374 | 63 | compost_pile_init(); |
DCchico |
0:95264f964374 | 64 | fruit_init(); |
DCchico |
0:95264f964374 | 65 | player_init(); |
DCchico |
0:95264f964374 | 66 | pc.printf("Initialization complete\n"); |
DCchico |
0:95264f964374 | 67 | |
DCchico |
0:95264f964374 | 68 | while(1) |
DCchico |
0:95264f964374 | 69 | { |
DCchico |
0:95264f964374 | 70 | t.start(); |
DCchico |
0:95264f964374 | 71 | |
DCchico |
0:95264f964374 | 72 | // Draw fruits first |
DCchico |
0:95264f964374 | 73 | //pc.printf("fruit_generator\n"); |
DCchico |
0:95264f964374 | 74 | fruit_generator(); |
DCchico |
0:95264f964374 | 75 | |
DCchico |
0:95264f964374 | 76 | //Put score on screen |
DCchico |
0:95264f964374 | 77 | // NOTE: Text is 8 pixels tall |
DCchico |
0:95264f964374 | 78 | |
DCchico |
0:95264f964374 | 79 | //pc.printf("player_fruit_draw\n"); |
DCchico |
0:95264f964374 | 80 | player_draw(0x0000FF); |
DCchico |
0:95264f964374 | 81 | player_knife_draw(); |
DCchico |
0:95264f964374 | 82 | |
DCchico |
0:95264f964374 | 83 | |
DCchico |
0:95264f964374 | 84 | |
DCchico |
0:95264f964374 | 85 | //pc.printf("Reading inputs\n"); |
DCchico |
0:95264f964374 | 86 | inputs = read_inputs(); |
DCchico |
0:95264f964374 | 87 | |
DCchico |
0:95264f964374 | 88 | //pc.printf("player motions\n"); |
DCchico |
0:95264f964374 | 89 | if (inputs.b1 || inputs.ay>=0.3) player_moveUp(); |
DCchico |
0:95264f964374 | 90 | if (inputs.b2 || inputs.ay<= -0.3) player_moveDown(); |
DCchico |
0:95264f964374 | 91 | if (inputs.b3) player_throw(); |
DCchico |
0:95264f964374 | 92 | |
DCchico |
0:95264f964374 | 93 | //pc.printf("compost_pile_update\n"); |
DCchico |
0:95264f964374 | 94 | compost_pile_update(); |
DCchico |
0:95264f964374 | 95 | |
DCchico |
0:95264f964374 | 96 | //pc.printf("fruit_contact\n"); |
DCchico |
0:95264f964374 | 97 | int is_bomb = fruit_contact(); |
DCchico |
0:95264f964374 | 98 | |
DCchico |
0:95264f964374 | 99 | // TODO: Check end of the game |
DCchico |
0:95264f964374 | 100 | //pc.printf("check endgame\n"); |
DCchico |
0:95264f964374 | 101 | |
DCchico |
0:95264f964374 | 102 | // TODO: Check if game won |
DCchico |
0:95264f964374 | 103 | |
DCchico |
0:95264f964374 | 104 | // Compute update time |
DCchico |
0:95264f964374 | 105 | t.stop(); |
DCchico |
0:95264f964374 | 106 | dt = t.read_ms(); |
DCchico |
0:95264f964374 | 107 | |
DCchico |
0:95264f964374 | 108 | if (dt < 100) wait_ms(100 - dt); |
DCchico |
0:95264f964374 | 109 | } |
DCchico |
0:95264f964374 | 110 | pc.printf("out of main loop\n"); |
DCchico |
0:95264f964374 | 111 | destroyList(get_fruit_list()); |
DCchico |
0:95264f964374 | 112 | |
DCchico |
0:95264f964374 | 113 | return 0; |
DCchico |
0:95264f964374 | 114 | // ===User implementations end=== |
DCchico |
0:95264f964374 | 115 | } |
DCchico |
0:95264f964374 | 116 | |
DCchico |
0:95264f964374 | 117 | // ===User implementations start=== |
DCchico |
0:95264f964374 | 118 | |
DCchico |
0:95264f964374 | 119 | /* This function iterate through the fruit list, checking if anyone one of them is contacting |
DCchico |
0:95264f964374 | 120 | * with the player's knife. Don't forget to check if the "fruit" is the bomb, since we |
DCchico |
0:95264f964374 | 121 | * generated bombs using the fruit_generator. |
DCchico |
0:95264f964374 | 122 | */ |
DCchico |
0:95264f964374 | 123 | int fruit_contact(void) { |
DCchico |
0:95264f964374 | 124 | |
DCchico |
0:95264f964374 | 125 | } |
DCchico |
0:95264f964374 | 126 | |
DCchico |
0:95264f964374 | 127 | /** Call compost_pile_update() repeatedly in your game-loop. ex: main() |
DCchico |
0:95264f964374 | 128 | This function iterate through the fruit list. Add fruits that reach the bottom of the screen |
DCchico |
0:95264f964374 | 129 | to the compost pile. |
DCchico |
0:95264f964374 | 130 | @return Number of remaining cities. You might end the game when all cities are demolished. |
DCchico |
0:95264f964374 | 131 | */ |
DCchico |
0:95264f964374 | 132 | void compost_pile_update(void){ |
DCchico |
0:95264f964374 | 133 | } |
DCchico |
0:95264f964374 | 134 | |
DCchico |
0:95264f964374 | 135 | //fcn to play a wav |
DCchico |
0:95264f964374 | 136 | void playSound(char* wav) |
DCchico |
0:95264f964374 | 137 | { |
DCchico |
0:95264f964374 | 138 | //open wav file |
DCchico |
0:95264f964374 | 139 | FILE *wave_file; |
DCchico |
0:95264f964374 | 140 | wave_file=fopen(wav,"r"); |
DCchico |
0:95264f964374 | 141 | |
DCchico |
0:95264f964374 | 142 | if(wave_file != NULL) |
DCchico |
0:95264f964374 | 143 | { |
DCchico |
0:95264f964374 | 144 | printf("File opened successfully\n"); |
DCchico |
0:95264f964374 | 145 | |
DCchico |
0:95264f964374 | 146 | //play wav file |
DCchico |
0:95264f964374 | 147 | printf("Sound playing...\n"); |
DCchico |
0:95264f964374 | 148 | waver.play(wave_file); |
DCchico |
0:95264f964374 | 149 | |
DCchico |
0:95264f964374 | 150 | //close wav file |
DCchico |
0:95264f964374 | 151 | printf("Sound stopped...\n"); |
DCchico |
0:95264f964374 | 152 | fclose(wave_file); |
DCchico |
0:95264f964374 | 153 | return; |
DCchico |
0:95264f964374 | 154 | } |
DCchico |
0:95264f964374 | 155 | |
DCchico |
0:95264f964374 | 156 | printf("Could not open file for reading - %s\n", wav); |
DCchico |
0:95264f964374 | 157 | return; |
DCchico |
0:95264f964374 | 158 | } |
DCchico |
0:95264f964374 | 159 | |
DCchico |
0:95264f964374 | 160 | /* Wanna hear some annoying sounds?*/ |
DCchico |
0:95264f964374 | 161 | void playNotes(void) |
DCchico |
0:95264f964374 | 162 | { |
DCchico |
0:95264f964374 | 163 | int i; |
DCchico |
0:95264f964374 | 164 | // generate a 500Hz tone using PWM hardware output |
DCchico |
0:95264f964374 | 165 | speaker.period(1.0/500.0); // 500hz period |
DCchico |
0:95264f964374 | 166 | speaker =0.5; //50% duty cycle - max volume |
DCchico |
0:95264f964374 | 167 | wait(3); |
DCchico |
0:95264f964374 | 168 | speaker=0.0; // turn off audio |
DCchico |
0:95264f964374 | 169 | wait(2); |
DCchico |
0:95264f964374 | 170 | // generate a short 150Hz tone using PWM hardware output |
DCchico |
0:95264f964374 | 171 | // something like this can be used for a button click effect for feedback |
DCchico |
0:95264f964374 | 172 | for (i=0; i<10; i++) { |
DCchico |
0:95264f964374 | 173 | speaker.period(1.0/150.0); // 500hz period |
DCchico |
0:95264f964374 | 174 | speaker =0.25; //25% duty cycle - mid range volume |
DCchico |
0:95264f964374 | 175 | wait(.02); |
DCchico |
0:95264f964374 | 176 | speaker=0.0; // turn off audio |
DCchico |
0:95264f964374 | 177 | wait(0.5); |
DCchico |
0:95264f964374 | 178 | } |
DCchico |
0:95264f964374 | 179 | |
DCchico |
0:95264f964374 | 180 | // sweep up in frequency by changing the PWM period |
DCchico |
0:95264f964374 | 181 | for (i=0; i<8000; i=i+100) { |
DCchico |
0:95264f964374 | 182 | speaker.period(1.0/float(i)); |
DCchico |
0:95264f964374 | 183 | speaker=0.25; |
DCchico |
0:95264f964374 | 184 | wait(.1); |
DCchico |
0:95264f964374 | 185 | } |
DCchico |
0:95264f964374 | 186 | wait(2); |
DCchico |
0:95264f964374 | 187 | |
DCchico |
0:95264f964374 | 188 | // two tone police siren effect - two periods or two frequencies |
DCchico |
0:95264f964374 | 189 | // increase volume - by changing the PWM duty cycle |
DCchico |
0:95264f964374 | 190 | for (i=0; i<26; i=i+2) { |
DCchico |
0:95264f964374 | 191 | speaker.period(1.0/969.0); |
DCchico |
0:95264f964374 | 192 | speaker = float(i)/50.0; |
DCchico |
0:95264f964374 | 193 | wait(.5); |
DCchico |
0:95264f964374 | 194 | speaker.period(1.0/800.0); |
DCchico |
0:95264f964374 | 195 | wait(.5); |
DCchico |
0:95264f964374 | 196 | } |
DCchico |
0:95264f964374 | 197 | // decrease volume |
DCchico |
0:95264f964374 | 198 | for (i=25; i>=0; i=i-2) { |
DCchico |
0:95264f964374 | 199 | speaker.period(1.0/969.0); |
DCchico |
0:95264f964374 | 200 | speaker = float(i)/50.0; |
DCchico |
0:95264f964374 | 201 | wait(.5); |
DCchico |
0:95264f964374 | 202 | speaker.period(1.0/800.0); |
DCchico |
0:95264f964374 | 203 | wait(.5); |
DCchico |
0:95264f964374 | 204 | } |
DCchico |
0:95264f964374 | 205 | speaker =0.0; |
DCchico |
0:95264f964374 | 206 | wait(2); |
DCchico |
0:95264f964374 | 207 | } |
DCchico |
0:95264f964374 | 208 | |
DCchico |
0:95264f964374 | 209 | void set_random_seed(Timer t) { |
DCchico |
0:95264f964374 | 210 | GameInputs inputs; |
DCchico |
0:95264f964374 | 211 | t.start(); |
DCchico |
0:95264f964374 | 212 | uLCD.printf("Push any button to start.\n"); |
DCchico |
0:95264f964374 | 213 | while(1){ |
DCchico |
0:95264f964374 | 214 | inputs = read_inputs(); |
DCchico |
0:95264f964374 | 215 | if (inputs.b1 || inputs.b2 || inputs.b3) break; |
DCchico |
0:95264f964374 | 216 | } |
DCchico |
0:95264f964374 | 217 | uLCD.cls(); |
DCchico |
0:95264f964374 | 218 | t.stop(); |
DCchico |
0:95264f964374 | 219 | int seed = t.read_ms(); |
DCchico |
0:95264f964374 | 220 | srand(seed); |
DCchico |
0:95264f964374 | 221 | } |
DCchico |
0:95264f964374 | 222 | // ===User implementations end=== |