Plays WAV file

Dependencies:   mbed Gamepad2

Revision:
4:a46b7a818e39
Parent:
3:8daf84a849e5
--- a/main.cpp	Fri Jan 24 10:50:28 2020 +0000
+++ b/main.cpp	Mon Jan 27 14:02:36 2020 +0000
@@ -1,4 +1,4 @@
-/* 
+/*
 
 2645_Ticker
 
@@ -10,49 +10,54 @@
 
 updated January 2020 - uses Gamepad2 peripherals
 
-*/ 
+*/
 
 #include "mbed.h"
+#include "Gamepad.h"
+#include "SoundData.h"
 
 // Create objects for ticker and red LED
 Ticker ticker;
-DigitalOut led1(PTA2);
+Gamepad pad;
 
 // flag - must be volatile as changes within ISR
 // g_ prefix makes it easier to distinguish it as global
 volatile int g_timer_flag = 0;
+volatile int g_sample = 0;
 
 // function prototypes
 void timer_isr();
 
 int main()
 {
-    // set-up the ticker so that the ISR it is called every 0.5 seconds
-    ticker.attach(&timer_isr,0.5);
-    
-    // the LED is common anode - writing a 1 to the pin will turn the LED OFF
-    led1 = 1;
+    pad.init();
+    // sample rate of music
+    ticker.attach(&timer_isr,220e-6);
 
     while (1) {
 
-        // check if flag is set i.e. interrupt has occured
-        if (g_timer_flag) {
-            g_timer_flag = 0;  // if it has, clear the flag
+        if (g_timer_flag == 1) {
+            g_timer_flag = 0;
 
-            // send message over serial port - can observe in CoolTerm etc.
-            printf("Tick \n");
-            // DO TASK HERE
-            
-            led1 = ! led1;
-    
+            // loop if reach end of samples
+            if (g_sample >= NUM_ELEMENTS) {
+                g_sample = 0;
+            }
+            // convert from 0 to 255 to 0.0 to 1.0
+            float val = float(sound_data[g_sample])/ 256.0f;
+            // write to DAC
+            pad.write_dac(val);
+            // move onto next sample
+            g_sample++;
+
         }
 
         // put the MCU to sleep until an interrupt wakes it up
         sleep();
-
     }
 }
 
+
 // time-triggered interrupt
 void timer_isr()
 {