Sound update

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

Committer:
dangulo7
Date:
Tue Nov 29 21:22:17 2022 +0000
Revision:
2:c85af14a0d53
Parent:
1:d8cc5013651e
Child:
3:7886a3097c0c
change to waveplayer 1

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