Sound update

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

Files at this revision

API Documentation at this revision

Comitter:
jstephens78
Date:
Tue Nov 29 23:05:30 2022 +0000
Parent:
10:f5a84133bd65
Child:
12:5d913b57da7c
Commit message:
Update speaker thread. Project now builds

Changed in this revision

Speaker.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Tue Nov 29 23:05:30 2022 +0000
@@ -0,0 +1,21 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin)
+    {
+// _pin(pin) means pass pin to the Speaker Constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlayNote(float frequency, float duration, float volume)
+    {
+        _pin.period(1.0/frequency);
+        _pin = volume/2.0;
+        wait(duration);
+        _pin = 0.0;
+    }
+
+private:
+    PwmOut _pin;
+};
\ No newline at end of file
--- a/main.cpp	Tue Nov 29 22:51:16 2022 +0000
+++ b/main.cpp	Tue Nov 29 23:05:30 2022 +0000
@@ -13,57 +13,62 @@
 PwmOut rGb(p24);
 PwmOut rgB(p25);
 PwmOut PWMout(p21);
-wave_player waver(&DACout,&PWMout);
+wave_player waver(&DACout);
 
 volatile bool game1 = false;
 volatile bool game2 = false;
 
-void thread2(void const *argument){
+void thread2(void const *argument)
+{
 
 }
 
-void thread3(void const *argument){
+void thread3(void const *argument)
+{
     FILE *wave_file;
-    PWNout.period(1.0/400000.0);
-    while (game1 == false && game2 == false){
+    PWMout.period(1.0/400000.0);
+    while (game1 == false && game2 == false) {
         wave_file=fopen("/sd/MiiMenu.wav","r");
         waver.play(wave_file);
         fclose(wave_file);
     }
-    while (game1 == true && game2 == false){
+    while (game1 == true && game2 == false) {
         wave_file=fopen("/sd/tetris.wav","r");
         waver.play(wave_file);
         fclose(wave_file);
     }
-    while (game2 == true && game1 == false){
+    while (game2 == true && game1 == false) {
         wave_file=fopen("/sd/WiiPlayAirHockey.wav","r");
         waver.play(wave_file);
         fclose(wave_file);
     }
 }
 
-void thread4(void const *argument){
+void thread4(void const *argument)
+{
+    float x = 0.0;
     while(1) {
-        float x = 0.0;
         //get a new random number for PWM
         x = rand() / float(RAND_MAX);
         //add some exponential brightness scaling
         //for more of a fast flash effect
         x = x*x*x;
         Rgb = x;
-        rGb = x * dim;
-        rgB = x * dim;
+        rGb = x;
+        rgB = x;
         //fast update rate for welding flashes
         Thread::wait(20);
         //add a random pause between welds
-        if (rand() / float(RAND_MAX) > 0.97) {
+        if (rand() / float(RAND_MAX) > 0.95) {
             Rgb = 0;
             rGb = 0;
             rgB = 0;
             Thread::wait(4000.0 * rand() / float(RAND_MAX));
         }
+    }
 }
-int main() {
+int main()
+{
     Thread thread2(thread2);
     Thread thread3(thread3);
     Thread thread4(thread4);