Sound update

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

Committer:
dangulo7
Date:
Tue Nov 29 21:08:00 2022 +0000
Revision:
1:d8cc5013651e
Parent:
0:da114b98e013
Child:
2:c85af14a0d53
thread

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