Sound update

Dependencies:   4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed

Committer:
jstephens78
Date:
Mon Dec 05 20:51:36 2022 +0000
Revision:
20:73e4f12277bd
Parent:
19:6d9bee043ede
Child:
21:df9e8f45e14c
All four threads launch, air hockey is the default

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jstephens78 0:da114b98e013 1 #include "mbed.h"
dangulo7 1:d8cc5013651e 2 #include "rtos.h"
jstephens78 12:5d913b57da7c 3
jstephens78 12:5d913b57da7c 4 #include "globals.h"
jstephens78 12:5d913b57da7c 5 #include "hockey.h"
jstephens78 20:73e4f12277bd 6 #include "tetris.h"
jstephens78 12:5d913b57da7c 7
jstephens78 10:f5a84133bd65 8 //#include "Speaker.h"
jstephens78 10:f5a84133bd65 9 #include "SDFileSystem.h"
dangulo7 1:d8cc5013651e 10 #include "uLCD_4DGL.h"
jstephens78 0:da114b98e013 11
jstephens78 20:73e4f12277bd 12
jstephens78 20:73e4f12277bd 13
jstephens78 20:73e4f12277bd 14 ///////////////////////////
jstephens78 20:73e4f12277bd 15 // GLOBALS.H DEFINITIONS //
jstephens78 20:73e4f12277bd 16 ///////////////////////////
jstephens78 20:73e4f12277bd 17 // The following variables are declared as extern in "globals.h", and we define
jstephens78 20:73e4f12277bd 18 // them here:
jstephens78 20:73e4f12277bd 19 volatile bool game1 = false;
jstephens78 20:73e4f12277bd 20 volatile bool game2 = false;
jstephens78 20:73e4f12277bd 21
jstephens78 19:6d9bee043ede 22 Serial pc(USBTX, USBRX);
jstephens78 20:73e4f12277bd 23 Mutex pc_mutex;
jstephens78 20:73e4f12277bd 24
jstephens78 19:6d9bee043ede 25 //uLCD_4DGL uLCD(p28, p27, p29);
jstephens78 16:6cf744b2623a 26 uLCD_4DGL uLCD(p9,p10,p30);
jstephens78 17:3517251daf96 27 Mutex uLCD_mutex;
jstephens78 17:3517251daf96 28
jstephens78 16:6cf744b2623a 29 BluefruitController blue(p13,p14);
jstephens78 16:6cf744b2623a 30 BusIn navSwitch(p15, p16, p17, p18, p19);
jstephens78 16:6cf744b2623a 31
jstephens78 20:73e4f12277bd 32 SDFileSystem sd(p5, p6, p7, p8, "sd");
jstephens78 16:6cf744b2623a 33 PwmOut Rgb(p23);
jstephens78 16:6cf744b2623a 34 PwmOut rGb(p24);
jstephens78 16:6cf744b2623a 35 PwmOut rgB(p25);
jstephens78 16:6cf744b2623a 36 PwmOut PWMout(p21);
jstephens78 20:73e4f12277bd 37 //AnalogOut DACout(p20);
jstephens78 12:5d913b57da7c 38 //wave_player waver(&DACout);
jstephens78 12:5d913b57da7c 39
jstephens78 0:da114b98e013 40
jstephens78 20:73e4f12277bd 41
jstephens78 20:73e4f12277bd 42 ////////////////////////////////
jstephens78 20:73e4f12277bd 43 // MAIN.CPP LOCAL DEFINITIONS //
jstephens78 20:73e4f12277bd 44 ////////////////////////////////
jstephens78 20:73e4f12277bd 45 // The following variables are *not* from "globals.h" and are not meant for
jstephens78 20:73e4f12277bd 46 // reference outside of main.cpp
jstephens78 20:73e4f12277bd 47 DigitalOut myled(LED1);
jstephens78 20:73e4f12277bd 48
jstephens78 20:73e4f12277bd 49
jstephens78 20:73e4f12277bd 50
jstephens78 20:73e4f12277bd 51 //////////////////////
jstephens78 20:73e4f12277bd 52 // MAIN.CPP THREADS //
jstephens78 20:73e4f12277bd 53 //////////////////////
jstephens78 20:73e4f12277bd 54 void audioThread()
jstephens78 11:e00a208bd85a 55 {
jstephens78 20:73e4f12277bd 56 while(true) Thread::wait(1000);
jstephens78 20:73e4f12277bd 57 /*
dangulo7 1:d8cc5013651e 58 FILE *wave_file;
jstephens78 11:e00a208bd85a 59 PWMout.period(1.0/400000.0);
jstephens78 11:e00a208bd85a 60 while (game1 == false && game2 == false) {
dangulo7 3:7886a3097c0c 61 wave_file=fopen("/sd/MiiMenu.wav","r");
dangulo7 1:d8cc5013651e 62 waver.play(wave_file);
dangulo7 1:d8cc5013651e 63 fclose(wave_file);
dangulo7 1:d8cc5013651e 64 }
jstephens78 11:e00a208bd85a 65 while (game1 == true && game2 == false) {
dangulo7 3:7886a3097c0c 66 wave_file=fopen("/sd/tetris.wav","r");
dangulo7 1:d8cc5013651e 67 waver.play(wave_file);
dangulo7 1:d8cc5013651e 68 fclose(wave_file);
dangulo7 1:d8cc5013651e 69 }
jstephens78 11:e00a208bd85a 70 while (game2 == true && game1 == false) {
dangulo7 3:7886a3097c0c 71 wave_file=fopen("/sd/WiiPlayAirHockey.wav","r");
dangulo7 1:d8cc5013651e 72 waver.play(wave_file);
dangulo7 1:d8cc5013651e 73 fclose(wave_file);
dangulo7 1:d8cc5013651e 74 }
jstephens78 20:73e4f12277bd 75 */
dangulo7 1:d8cc5013651e 76 }
dangulo7 1:d8cc5013651e 77
jstephens78 20:73e4f12277bd 78 void ledThread()
jstephens78 11:e00a208bd85a 79 {
jstephens78 11:e00a208bd85a 80 float x = 0.0;
dangulo7 1:d8cc5013651e 81 while(1) {
dangulo7 1:d8cc5013651e 82 //get a new random number for PWM
dangulo7 1:d8cc5013651e 83 x = rand() / float(RAND_MAX);
dangulo7 1:d8cc5013651e 84 //add some exponential brightness scaling
dangulo7 1:d8cc5013651e 85 //for more of a fast flash effect
dangulo7 1:d8cc5013651e 86 x = x*x*x;
dangulo7 1:d8cc5013651e 87 Rgb = x;
jstephens78 11:e00a208bd85a 88 rGb = x;
jstephens78 11:e00a208bd85a 89 rgB = x;
dangulo7 1:d8cc5013651e 90 //fast update rate for welding flashes
dangulo7 1:d8cc5013651e 91 Thread::wait(20);
dangulo7 1:d8cc5013651e 92 //add a random pause between welds
jstephens78 11:e00a208bd85a 93 if (rand() / float(RAND_MAX) > 0.95) {
dangulo7 1:d8cc5013651e 94 Rgb = 0;
dangulo7 1:d8cc5013651e 95 rGb = 0;
dangulo7 1:d8cc5013651e 96 rgB = 0;
dangulo7 1:d8cc5013651e 97 Thread::wait(4000.0 * rand() / float(RAND_MAX));
dangulo7 1:d8cc5013651e 98 }
jstephens78 11:e00a208bd85a 99 }
dangulo7 1:d8cc5013651e 100 }
jstephens78 16:6cf744b2623a 101
jstephens78 11:e00a208bd85a 102 int main()
jstephens78 11:e00a208bd85a 103 {
jstephens78 20:73e4f12277bd 104 // Launch four threads
jstephens78 20:73e4f12277bd 105 Thread thread1(tetrisGame, osPriorityHigh);
jstephens78 20:73e4f12277bd 106 Thread thread2(hockeyGame, osPriorityHigh);
jstephens78 20:73e4f12277bd 107 Thread thread3(audioThread, osPriorityLow);
jstephens78 20:73e4f12277bd 108 Thread thread4(ledThread, osPriorityLow);
jstephens78 16:6cf744b2623a 109
jstephens78 20:73e4f12277bd 110 // The main thread goes on to blink LEDs
jstephens78 12:5d913b57da7c 111 while (true) {
jstephens78 0:da114b98e013 112 myled = 1;
jstephens78 12:5d913b57da7c 113 Thread::wait(500);
jstephens78 0:da114b98e013 114 myled = 0;
jstephens78 12:5d913b57da7c 115 Thread::wait(500);
jstephens78 20:73e4f12277bd 116
jstephens78 20:73e4f12277bd 117 PRINTF("[MAIN] Thread stacks: %lu %lu %lu %lu\r\n",
jstephens78 20:73e4f12277bd 118 thread1.used_stack(),
jstephens78 20:73e4f12277bd 119 thread2.used_stack(),
jstephens78 20:73e4f12277bd 120 thread3.used_stack(),
jstephens78 20:73e4f12277bd 121 thread4.used_stack());
jstephens78 20:73e4f12277bd 122
jstephens78 20:73e4f12277bd 123 // Launch into the air hockey game
jstephens78 20:73e4f12277bd 124 if (game2 != true) game2 = true;
jstephens78 0:da114b98e013 125 }
jstephens78 0:da114b98e013 126 }