Wave file player helloworld for mbed LCP1768 running mbed OS 5. See https://os.mbed.com/users/4180_1/notebook/using-a-speaker-for-audio-output/

Dependencies:   wave_player

Revision:
2:509911a88b64
Parent:
0:9ed9bcd7cc87
--- a/main.cpp	Tue Oct 01 00:45:18 2019 +0000
+++ b/main.cpp	Wed Oct 02 13:45:40 2019 +0000
@@ -1,11 +1,16 @@
 #include "mbed.h"
-#include "rtos.h"
+// Waveplayer Helloworld for mbed LP1768 running mbed OS 5
+// NOTE: Need "target.components_add": ["SD"] in json project file!
+// This adds the filesystem driver below for SD cards in an OS 5 project
+// See https://os.mbed.com/users/4180_1/notebook/using-a-speaker-for-audio-output/
+// for additional details
 #include "SDBlockDevice.h"
 #include "FATFileSystem.h"
 #include "wave_player.h"
 
 // LED Components
 PwmOut led(LED1);
+DigitalOut led4(LED4);
 
 // SD File System Components
 SDBlockDevice   sd(p5, p6, p7, p8);
@@ -14,11 +19,14 @@
 // Audio Components
 AnalogOut       dac(p18);
 PwmOut          speaker(p25);
+//outputs audio to both DAC and PWM pin
+//select one to drive speaker
 wave_player     wav(&dac, &speaker);
 
 Thread led_thread, audio_thread;
 
-void led_siren() {
+void led_siren()
+{
     const int build_time = 100;
     while (1) {
         //LED warm up effect using PWM
@@ -40,26 +48,31 @@
     }
 }
 
-void play_audio() {
+void play_audio()
+{
     FILE * wav_file;
-    speaker.period(1.0/1000000.0);
+    speaker.period(1.0/400000.0); //increase PWM clock rate for audio
+    sd.init();
+    sd.frequency(25000000); //increase SPI clock rate for audio
     while (1) {
-        sd.init();
         fs.mount(&sd);
-        wav_file = fopen("/sd/siren.wav", "r");
+        //need sample.wav file on SD card
+        //wave file format mono 16Khz or less (not mp3!)
+        wav_file = fopen("/sd/sample.wav", "r");
         wav.play(wav_file);
         fclose(wav_file);
-        sd.deinit();
         fs.unmount();
+        wait(1);
     }
 }
 
 // main() runs in its own thread in the OS
 int main()
-{   
+{
     led_thread.start(led_siren);
     audio_thread.start(play_audio);
     while (true) {
         ThisThread::sleep_for(500);
+        led4 = !led4;
     }
 }