Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 #include "globals.h" 00005 #include "hockey.h" 00006 #include "tetris.h" 00007 00008 //#include "Speaker.h" 00009 #include "SDFileSystem.h" 00010 #include "uLCD_4DGL.h" 00011 00012 00013 #include "cylonbyc.h" 00014 00015 #define sample_freq 11025.0 00016 00017 00018 /////////////////////////// 00019 // GLOBALS.H DEFINITIONS // 00020 /////////////////////////// 00021 // The following variables are declared as extern in "globals.h", and we define 00022 // them here: 00023 volatile bool game1 = false; 00024 volatile bool game2 = false; 00025 volatile int menu_flag = 0; 00026 00027 Serial pc(USBTX, USBRX); 00028 Mutex pc_mutex; 00029 00030 //uLCD_4DGL uLCD(p28, p27, p29); 00031 uLCD_4DGL uLCD(p13,p14,p30); 00032 Mutex uLCD_mutex; 00033 00034 BluefruitController blue(p28,p27); 00035 //BusIn navSwitch(p15, p16, p17, p19, p20); 00036 Nav_Switch myNav(p20, p16, p17, p15, p19); // pins(up, down, left, right, fire) 00037 00038 SDFileSystem sd(p5, p6, p7, p8, "sd"); 00039 PwmOut Rgb(p23); 00040 PwmOut rGb(p24); 00041 PwmOut rgB(p25); 00042 00043 Ticker sampletick; 00044 AnalogOut speaker(p18); 00045 //Speaker DACout(p18); 00046 //wave_player waver(&DACout); 00047 00048 00049 //////////////////////////////// 00050 // MAIN.CPP LOCAL DEFINITIONS // 00051 //////////////////////////////// 00052 // The following variables are *not* from "globals.h" and are not meant for 00053 // reference outside of main.cpp 00054 DigitalOut led1(LED1); 00055 DigitalOut led2(LED2); 00056 DigitalOut led3(LED3); 00057 DigitalOut led4(LED4); 00058 00059 00060 00061 ////////////////////// 00062 // MAIN.CPP THREADS // 00063 ////////////////////// 00064 int i = 0; 00065 int j = 0; 00066 int k = 0; 00067 void audio_sample () 00068 { 00069 if (game1) { 00070 speaker.write_u16(tetris[i]); 00071 i++; 00072 } 00073 if (game2) { 00074 speaker.write_u16(air_hockey[j]); 00075 j++; 00076 } 00077 if (!game1 && !game2) { 00078 speaker.write_u16(mii_menu[k]); 00079 k++; 00080 } 00081 if (k>= 71904) { 00082 k = 0; 00083 } 00084 if (i>= 72584) { 00085 i = 0; 00086 } 00087 if (j>= 72488) { 00088 j = 0; 00089 } 00090 } 00091 00092 void audioThread() 00093 { 00094 while (true) { 00095 //FILE *wave_file; 00096 while (game1 == false && game2 == false) { 00097 sampletick.attach(&audio_sample, 1.0 / sample_freq); 00098 //DACout.PlayNote(369.99, 0.4, 1); 00099 // DACout.PlayNote(0, 0.4, 1); 00100 // DACout.PlayNote(466.16, 0.2, 1); 00101 // DACout.PlayNote(554.37, 0.2, 1); 00102 // if(game1 == true || game2 == true) break; 00103 // DACout.PlayNote(0, 0.4, 1); 00104 // DACout.PlayNote(466.16, 0.4, 1); 00105 // DACout.PlayNote(0, 0.4, 1); 00106 // DACout.PlayNote(369.99, 0.4, 1); 00107 // if(game1 == true || game2 == true) break; 00108 // DACout.PlayNote(293.66, 0.2, 1); 00109 // DACout.PlayNote(293.66, 0.2, 1); 00110 // DACout.PlayNote(293.66, 0.2, 1); 00111 // DACout.PlayNote(0, 0.8, 1); 00112 // if(game1 == true || game2 == true) break; 00113 // DACout.PlayNote(277.18, 0.4, 1); 00114 // DACout.PlayNote(293.66, 0.2, 1); 00115 // DACout.PlayNote(369.99, 0.2, 1); 00116 // DACout.PlayNote(466.16, 0.2, 1); 00117 // if(game1 == true || game2 == true) break; 00118 // DACout.PlayNote(554.37, 0.2, 1); 00119 // DACout.PlayNote(0, 0.4, 1); 00120 // DACout.PlayNote(466.16, 0.4, 1); 00121 // DACout.PlayNote(0, 0.4, 1); 00122 // if(game1 == true || game2 == true) break; 00123 // DACout.PlayNote(369.99, 0.4, 1); 00124 // DACout.PlayNote(659.25, 0.4, 1); 00125 // DACout.PlayNote(622.25, 0.4, 1); 00126 // DACout.PlayNote(587.33, 0.4, 1); 00127 // if(game1 == true || game2 == true) break; 00128 // DACout.PlayNote(0, 0.4, 1); 00129 // 00130 // 00131 // 00132 // DACout.PlayNote(392.00, 0.4, 1); 00133 // DACout.PlayNote(0, 0.4, 1); 00134 // DACout.PlayNote(554.37, 0.2, 1); 00135 // if(game1 == true || game2 == true) break; 00136 // DACout.PlayNote(369.99, 0.2, 1); 00137 // DACout.PlayNote(0, 0.4, 1); 00138 // DACout.PlayNote(554.37, 0.4, 1); 00139 // DACout.PlayNote(0, 0.4, 1); 00140 // if(game1 == true || game2 == true) break; 00141 // DACout.PlayNote(329.00, 0.4, 1); 00142 // DACout.PlayNote(0, 0.4, 1); 00143 // DACout.PlayNote(523.25, 0.4, 1); 00144 // DACout.PlayNote(0, 0.4, 1); 00145 // if(game1 == true || game2 == true) break; 00146 // DACout.PlayNote(329.00, 0.4, 1); 00147 // DACout.PlayNote(369.99, 0.4, 1); 00148 // DACout.PlayNote(0, 0.4, 1); 00149 // DACout.PlayNote(329.63, 0.4, 1); 00150 // if(game1 == true || game2 == true) break; 00151 // DACout.PlayNote(0, 0.4, 1); 00152 // DACout.PlayNote(277.18, 0.2, 1); 00153 // DACout.PlayNote(277.18, 0.2, 1); 00154 // DACout.PlayNote(277.18, 0.2, 1); 00155 // if(game1 == true || game2 == true) break; 00156 // DACout.PlayNote(0, 0.4, 1); 00157 // DACout.PlayNote(0, 0.4, 1); 00158 // DACout.PlayNote(277.18, 0.2, 1); 00159 // DACout.PlayNote(277.18, 0.2, 1); 00160 // if(game1 == true || game2 == true) break; 00161 // DACout.PlayNote(277.18, 0.4, 1); 00162 // DACout.PlayNote(0, 0.4, 1); 00163 // DACout.PlayNote(0, 0.4, 1); 00164 // DACout.PlayNote(311.13, 0.4, 1); 00165 // if(game1 == true || game2 == true) break; 00166 // DACout.PlayNote(293.66, 0.4, 1); 00167 00168 /*led2 = true; 00169 wave_file=fopen("/sd/MiiMenu.wav","r"); 00170 PRINTF("Opening"); 00171 waver.play(wave_file); 00172 fclose(wave_file); 00173 led2 = false;*/ 00174 } 00175 while (game1 == true && game2 == false) { 00176 sampletick.attach(&audio_sample, 1.0 / sample_freq); 00177 //DACout.PlayNote(659.25, 0.3, 1); 00178 // DACout.PlayNote(493.88, 0.3, 1); 00179 // DACout.PlayNote(523.25, 0.3, 1); 00180 // if(game1 != true && game2 == false) break; 00181 // DACout.PlayNote(587.33, 0.3, 1); 00182 // DACout.PlayNote(523.25, 0.3, 1); 00183 // DACout.PlayNote(493.88, 0.3, 1); 00184 // DACout.PlayNote(440.00, 0.3, 1); 00185 // if(game1 != true && game2 == false) break; 00186 // DACout.PlayNote(440.00, 0.3, 1); 00187 // DACout.PlayNote(523.25, 0.3, 1); 00188 // DACout.PlayNote(659.25, 0.3, 1); 00189 // DACout.PlayNote(587.33, 0.3, 1); 00190 // if(game1 != true && game2 == false) break; 00191 // DACout.PlayNote(523.25, 0.3, 1); 00192 // DACout.PlayNote(493.88, 0.3, 1); 00193 // DACout.PlayNote(523.25, 0.3, 1); 00194 // DACout.PlayNote(587.33, 0.3, 1); 00195 // if(game1 != true && game2 == false) break; 00196 // DACout.PlayNote(659.25, 0.3, 1); 00197 // DACout.PlayNote(523.25, 0.3, 1); 00198 // DACout.PlayNote(440.00, 0.3, 1); 00199 // DACout.PlayNote(440.00, 0.3, 1); 00200 // if(game1 != true && game2 == false) break; 00201 // 00202 // DACout.PlayNote(583.33, 0.3, 1); 00203 // DACout.PlayNote(698.46, 0.3, 1); 00204 // DACout.PlayNote(880.00, 0.3, 1); 00205 // DACout.PlayNote(783.99, 0.3, 1); 00206 // if(game1 != true && game2 == false) break; 00207 // DACout.PlayNote(698.46, 0.3, 1); 00208 // DACout.PlayNote(659.25, 0.3, 1); 00209 // DACout.PlayNote(523.25, 0.3, 1); 00210 // DACout.PlayNote(659.25, 0.3, 1); 00211 // if(game1 != true && game2 == false) break; 00212 // DACout.PlayNote(587.33, 0.3, 1); 00213 // DACout.PlayNote(523.25, 0.3, 1); 00214 // DACout.PlayNote(493.88, 0.3, 1); 00215 // DACout.PlayNote(493.88, 0.3, 1); 00216 // if(game1 != true && game2 == false) break; 00217 // DACout.PlayNote(523.25, 0.3, 1); 00218 // DACout.PlayNote(587.33, 0.3, 1); 00219 // DACout.PlayNote(659.25, 0.3, 1); 00220 // DACout.PlayNote(523.35, 0.3, 1); 00221 // if(game1 != true && game2 == false) break; 00222 // DACout.PlayNote(440.00, 0.3, 1); 00223 // DACout.PlayNote(440.00, 0.3, 1); 00224 /* led2 = true; 00225 wave_file=fopen("/sd/tetris.wav","r"); 00226 waver.play(wave_file); 00227 fclose(wave_file); 00228 led2 = false;*/ 00229 } 00230 while (game2 == true && game1 == false) { 00231 sampletick.attach(&audio_sample, 1.0 / sample_freq); 00232 //DACout.PlayNote(329.63, 0.6, 1); 00233 // DACout.PlayNote(349.23, 0.6, 1); 00234 // DACout.PlayNote(392.00, 0.15, 1); 00235 // DACout.PlayNote(523.25, 0.15, 1); 00236 // if(game1 != true && game2 == false) break; 00237 // DACout.PlayNote(493.88, 0.15, 1); 00238 // DACout.PlayNote(523.25, 0.15, 1); 00239 // DACout.PlayNote(392.00, 0.15, 1); 00240 // DACout.PlayNote(329.63, 0.15, 1); 00241 // if(game1 != true && game2 == false) break; 00242 // DACout.PlayNote(261.63, 0.15, 1); 00243 // DACout.PlayNote(293.66, 0.15, 1); 00244 // DACout.PlayNote(329.33, 0.15, 1); 00245 // DACout.PlayNote(293.66, 0.6, 1); 00246 // if(game1 != true && game2 == false) break; 00247 // DACout.PlayNote(293.66, 1.2, 1); 00248 // DACout.PlayNote(329.63, 0.6, 1); 00249 // DACout.PlayNote(349.23, 0.3, 1); 00250 // DACout.PlayNote(329.63, 0.15, 1); 00251 // if(game1 != true && game2 == false) break; 00252 // DACout.PlayNote(349.23, 0.15, 1); 00253 // 00254 // 00255 // DACout.PlayNote(392.00, 0.15, 1); 00256 // DACout.PlayNote(523.25, 0.15, 1); 00257 // DACout.PlayNote(587.33, 0.15, 1); 00258 // DACout.PlayNote(523.25, 0.3, 1); 00259 // if(game1 != true && game2 == false) break; 00260 // DACout.PlayNote(587.33, 0.1, 1); 00261 // DACout.PlayNote(659.25, 0.1, 1); 00262 // DACout.PlayNote(698.46, 0.15, 1); 00263 // DACout.PlayNote(659.25, 0.15, 1); 00264 // if(game1 != true && game2 == false) break; 00265 // DACout.PlayNote(587.33, 0.15, 1); 00266 // DACout.PlayNote(523.25, 0.3, 1); 00267 // DACout.PlayNote(392.00, 0.15, 1); 00268 // DACout.PlayNote(440.00, 0.15, 1); 00269 // if(game1 != true && game2 == false) break; 00270 // DACout.PlayNote(440.00, 0.15, 1); 00271 // DACout.PlayNote(587.33, 0.15, 1); 00272 // DACout.PlayNote(587.33, 0.1, 1); 00273 // DACout.PlayNote(587.33, 0.15, 1); 00274 // if(game1 != true && game2 == false) break; 00275 // DACout.PlayNote(587.33, 0.1, 1); 00276 // DACout.PlayNote(659.25, 0.1, 1); 00277 // DACout.PlayNote(698.46, 0.15, 1); 00278 // DACout.PlayNote(659.25, 0.15, 1); 00279 // if(game1 != true && game2 == false) break; 00280 // DACout.PlayNote(587.33, 0.15, 1); 00281 // DACout.PlayNote(523.25, 0.3, 1); 00282 // DACout.PlayNote(392.00, 0.3, 1); 00283 // DACout.PlayNote(392.00, 0.6, 1); 00284 // if(game1 != true && game2 == false) break; 00285 // DACout.PlayNote(392.00, 0.15, 1); 00286 // DACout.PlayNote(587.33, 0.1, 1); 00287 // DACout.PlayNote(659.25, 0.1, 1); 00288 // DACout.PlayNote(698.46, 0.15, 1); 00289 // DACout.PlayNote(659.25, 0.15, 1); 00290 // if(game1 != true && game2 == false) break; 00291 // DACout.PlayNote(587.33, 0.15, 1); 00292 // DACout.PlayNote(523.25, 0.3, 1); 00293 // DACout.PlayNote(783.99, 0.3, 1); 00294 // DACout.PlayNote(783.99, 0.6, 1); 00295 // if(game1 != true && game2 == false) break; 00296 // DACout.PlayNote(783.99, 0.15, 1); 00297 // DACout.PlayNote(783.99, 0.1, 1); 00298 // DACout.PlayNote(880.00, 0.1, 1); 00299 // if(game1 != true && game2 == false) break; 00300 // DACout.PlayNote(932.33, 0.15, 1); 00301 // DACout.PlayNote(880.00, 0.15, 1); 00302 // DACout.PlayNote(783.99, 0.15, 1); 00303 // DACout.PlayNote(698.46, 0.6, 1); 00304 // if(game1 != true && game2 == false) break; 00305 // DACout.PlayNote(830.61, 0.15, 1); 00306 // DACout.PlayNote(783.99, 0.15, 1); 00307 // DACout.PlayNote(698.46, 0.15, 1); 00308 // DACout.PlayNote(622.25, 0.3, 1); 00309 // if(game1 != true && game2 == false) break; 00310 // DACout.PlayNote(739.99, 0.15, 1); 00311 // DACout.PlayNote(698.46, 0.1, 1); 00312 // DACout.PlayNote(698.46, 0.15, 1); 00313 // DACout.PlayNote(622.25, 0.15, 1); 00314 // if(game1 != true && game2 == false) break; 00315 // DACout.PlayNote(554.37, 0.15, 1); 00316 // DACout.PlayNote(415.30, 0.15, 1); 00317 // DACout.PlayNote(349.23, 0.15, 1); 00318 // DACout.PlayNote(277.18, 0.15, 1); 00319 // DACout.PlayNote(311.13, 0.3, 1); 00320 // if(game1 != true && game2 == false) break; 00321 // DACout.PlayNote(554.37, 0.3, 1); 00322 // DACout.PlayNote(587.33, 0.6, 1); 00323 // DACout.PlayNote(587.33, 0.6, 1); 00324 // DACout.PlayNote(587.33, 0.15, 1); 00325 // DACout.PlayNote(392.00, 0.15, 1); 00326 // if(game1 != true && game2 == false) break; 00327 // DACout.PlayNote(587.33, 0.3, 1); 00328 // DACout.PlayNote(783.99, 0.3, 1); 00329 // DACout.PlayNote(783.99, 0.6, 1); 00330 // DACout.PlayNote(783.99, 1.2, 1); 00331 // if(game1 != true && game2 == false) break; 00332 /*led2 = true; 00333 wave_file=fopen("/sd/WiiPlayAirHockey.wav","r"); 00334 waver.play(wave_file); 00335 fclose(wave_file); 00336 led2 = false;*/ 00337 } 00338 } 00339 } 00340 00341 void ledThread() 00342 { 00343 float x = 0.0; 00344 while(1) { 00345 if (!game1 && !game2) { 00346 //get a new random number for PWM 00347 x = rand() / float(RAND_MAX); 00348 //add some exponential brightness scaling 00349 //for more of a fast flash effect 00350 x = x*x*x; 00351 Rgb = x; 00352 rGb = x; 00353 rgB = x; 00354 //fast update rate for welding flashes 00355 Thread::wait(20); 00356 //add a random pause between welds 00357 if (rand() / float(RAND_MAX) > 0.95) { 00358 Rgb = 0; 00359 rGb = 0; 00360 rgB = 0; 00361 Thread::wait(4000.0 * rand() / float(RAND_MAX)); 00362 } 00363 } 00364 else if (game1) { 00365 00366 } 00367 } 00368 } 00369 00370 int main() 00371 { 00372 // Launch four threads 00373 Thread thread1(tetrisGame, osPriorityHigh); 00374 Thread thread2(hockeyGame, osPriorityHigh); 00375 Thread thread3(audioThread, osPriorityLow); 00376 Thread thread4(ledThread, osPriorityLow); 00377 00378 // The main thread goes on to blink LEDs 00379 //Speaker mySpeaker(p18); 00380 // loops forever playing two notes on speaker using analog samples 00381 //PRINTF("test"); 00382 // while(1) { 00383 // //mySpeaker.PlayNote(969.0, 0.5, 1.0); 00384 //// mySpeaker.PlayNote(800.0, 0.5, 1.0); 00385 // //PRINTF("loop"); 00386 // } 00387 while (true) { 00388 led1 = 1; 00389 Thread::wait(500); 00390 led1 = 0; 00391 Thread::wait(500); 00392 if(!game1 && !game2 && menu_flag==0){ 00393 uLCD.cls(); 00394 uLCD.color(WHITE); 00395 uLCD.text_height(2); 00396 uLCD.text_width(2); 00397 uLCD.locate(2,0); 00398 uLCD.printf("MENU:\n"); 00399 uLCD.printf("UP:\n"); 00400 uLCD.printf("Tetris \n"); 00401 uLCD.printf("\n"); 00402 uLCD.printf("DOWN:\n"); 00403 uLCD.printf("Air\n"); 00404 uLCD.printf("Hockey"); 00405 menu_flag=1; 00406 } 00407 00408 //DACout.PlayNote(50, 1.0/2.5, 0.08); 00409 00410 PRINTF("[MAIN] Thread stacks: %lu %lu %lu %lu\r\n", 00411 thread1.used_stack(), 00412 thread2.used_stack(), 00413 thread3.used_stack(), 00414 thread4.used_stack()); 00415 00416 // Launch into the air hockey game 00417 if (game1 != true && game2 != true && myNav.up()){ 00418 uLCD.cls(); 00419 game1 = true; 00420 } 00421 if (game2 != true && game1 != true && myNav.down()){ 00422 uLCD.cls(); 00423 game2 = true; 00424 } 00425 } 00426 }
Generated on Fri Dec 9 2022 21:28:44 by
1.7.2