Sound update

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

Committer:
jstephens78
Date:
Tue Nov 29 23:05:30 2022 +0000
Revision:
11:e00a208bd85a
Parent:
10:f5a84133bd65
Child:
12:5d913b57da7c
Update speaker thread. Project now builds

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"
dangulo7 1:d8cc5013651e 3 #include "wave_player.h"
jstephens78 10:f5a84133bd65 4 //#include "Speaker.h"
jstephens78 10:f5a84133bd65 5 #include "SDFileSystem.h"
dangulo7 1:d8cc5013651e 6 #include "uLCD_4DGL.h"
jstephens78 0:da114b98e013 7
dangulo7 1:d8cc5013651e 8 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
dangulo7 2:c85af14a0d53 9
jstephens78 0:da114b98e013 10 DigitalOut myled(LED1);
dangulo7 1:d8cc5013651e 11 AnalogOut DACout(p20);
dangulo7 1:d8cc5013651e 12 PwmOut Rgb(p23);
dangulo7 1:d8cc5013651e 13 PwmOut rGb(p24);
dangulo7 1:d8cc5013651e 14 PwmOut rgB(p25);
dangulo7 1:d8cc5013651e 15 PwmOut PWMout(p21);
jstephens78 11:e00a208bd85a 16 wave_player waver(&DACout);
jstephens78 0:da114b98e013 17
dangulo7 1:d8cc5013651e 18 volatile bool game1 = false;
dangulo7 1:d8cc5013651e 19 volatile bool game2 = false;
dangulo7 1:d8cc5013651e 20
jstephens78 11:e00a208bd85a 21 void thread2(void const *argument)
jstephens78 11:e00a208bd85a 22 {
dangulo7 1:d8cc5013651e 23
dangulo7 1:d8cc5013651e 24 }
dangulo7 1:d8cc5013651e 25
jstephens78 11:e00a208bd85a 26 void thread3(void const *argument)
jstephens78 11:e00a208bd85a 27 {
dangulo7 1:d8cc5013651e 28 FILE *wave_file;
jstephens78 11:e00a208bd85a 29 PWMout.period(1.0/400000.0);
jstephens78 11:e00a208bd85a 30 while (game1 == false && game2 == false) {
dangulo7 3:7886a3097c0c 31 wave_file=fopen("/sd/MiiMenu.wav","r");
dangulo7 1:d8cc5013651e 32 waver.play(wave_file);
dangulo7 1:d8cc5013651e 33 fclose(wave_file);
dangulo7 1:d8cc5013651e 34 }
jstephens78 11:e00a208bd85a 35 while (game1 == true && game2 == false) {
dangulo7 3:7886a3097c0c 36 wave_file=fopen("/sd/tetris.wav","r");
dangulo7 1:d8cc5013651e 37 waver.play(wave_file);
dangulo7 1:d8cc5013651e 38 fclose(wave_file);
dangulo7 1:d8cc5013651e 39 }
jstephens78 11:e00a208bd85a 40 while (game2 == true && game1 == false) {
dangulo7 3:7886a3097c0c 41 wave_file=fopen("/sd/WiiPlayAirHockey.wav","r");
dangulo7 1:d8cc5013651e 42 waver.play(wave_file);
dangulo7 1:d8cc5013651e 43 fclose(wave_file);
dangulo7 1:d8cc5013651e 44 }
dangulo7 1:d8cc5013651e 45 }
dangulo7 1:d8cc5013651e 46
jstephens78 11:e00a208bd85a 47 void thread4(void const *argument)
jstephens78 11:e00a208bd85a 48 {
jstephens78 11:e00a208bd85a 49 float x = 0.0;
dangulo7 1:d8cc5013651e 50 while(1) {
dangulo7 1:d8cc5013651e 51 //get a new random number for PWM
dangulo7 1:d8cc5013651e 52 x = rand() / float(RAND_MAX);
dangulo7 1:d8cc5013651e 53 //add some exponential brightness scaling
dangulo7 1:d8cc5013651e 54 //for more of a fast flash effect
dangulo7 1:d8cc5013651e 55 x = x*x*x;
dangulo7 1:d8cc5013651e 56 Rgb = x;
jstephens78 11:e00a208bd85a 57 rGb = x;
jstephens78 11:e00a208bd85a 58 rgB = x;
dangulo7 1:d8cc5013651e 59 //fast update rate for welding flashes
dangulo7 1:d8cc5013651e 60 Thread::wait(20);
dangulo7 1:d8cc5013651e 61 //add a random pause between welds
jstephens78 11:e00a208bd85a 62 if (rand() / float(RAND_MAX) > 0.95) {
dangulo7 1:d8cc5013651e 63 Rgb = 0;
dangulo7 1:d8cc5013651e 64 rGb = 0;
dangulo7 1:d8cc5013651e 65 rgB = 0;
dangulo7 1:d8cc5013651e 66 Thread::wait(4000.0 * rand() / float(RAND_MAX));
dangulo7 1:d8cc5013651e 67 }
jstephens78 11:e00a208bd85a 68 }
dangulo7 1:d8cc5013651e 69 }
jstephens78 11:e00a208bd85a 70 int main()
jstephens78 11:e00a208bd85a 71 {
dangulo7 1:d8cc5013651e 72 Thread thread2(thread2);
dangulo7 1:d8cc5013651e 73 Thread thread3(thread3);
dangulo7 1:d8cc5013651e 74 Thread thread4(thread4);
jstephens78 0:da114b98e013 75 while(1) {
jstephens78 0:da114b98e013 76 myled = 1;
jstephens78 0:da114b98e013 77 wait(0.2);
jstephens78 0:da114b98e013 78 myled = 0;
jstephens78 0:da114b98e013 79 wait(0.2);
jstephens78 0:da114b98e013 80 }
jstephens78 0:da114b98e013 81 }