Simple audio out with variable tempo using mbed templates

Dependencies:   mbed tsi_sensor

Files at this revision

API Documentation at this revision

Comitter:
mm73
Date:
Tue Jun 10 12:11:41 2014 +0000
Commit message:
Tempo using slider

Changed in this revision

Music_tempo_main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
tsi_sensor.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 614598ef90dd Music_tempo_main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Music_tempo_main.cpp	Tue Jun 10 12:11:41 2014 +0000
@@ -0,0 +1,67 @@
+#include "mbed.h"
+#include "tsi_sensor.h"
+
+/* This defines will be replaced by PinNames soon */
+#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
+  #define ELEC0 9
+  #define ELEC1 10
+#elif defined (TARGET_KL05Z)
+  #define ELEC0 9
+  #define ELEC1 8
+#else
+  #error TARGET NOT DEFINED
+#endif
+
+#define C3 131
+#define D3 147
+#define E3 165
+#define F3 175
+#define G3 196
+#define A3 220
+#define B3 247
+#define C4 262
+#define D4 294
+#define E4 330
+#define F4 349
+#define G4 392
+#define A4 440
+#define B4 494
+#define C5 523
+#define A5 880
+#define B5 988
+#define Pause 1
+
+Ticker tick;
+DigitalOut led1(LED_RED);
+DigitalOut led2(LED_GREEN);
+DigitalOut gpo(D10);
+
+void flip() {
+    led2 = !led2;
+    gpo = !gpo;
+}
+
+int main() {
+    int Notes_Hz[] = {G4, F4, G4, Pause, D3, B5, A4, Pause, G4, E3, E4, E4, E4, F4, G4,Pause,
+                      G4, F4, G4, Pause, D3, B5, A4, Pause, G4, E3, E4, E4, E4, F4, G4, Pause};
+    int i;
+    float bpm =120;
+    //tick.attach(&flip, 0.7); // setup ticker to call flip led2 after 0.7 seconds
+    //PwmOut led3(LED_BLUE);
+    TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
+
+    while (1) {
+        //bpm = 2000.0 * tsi.readPercentage();
+        //led3 = 1.0 - tsi.readPercentage();
+        i = 0;
+        while (Notes_Hz[i]) {
+            led1 = !led1;
+            tick.attach(&flip, 0.5/Notes_Hz[i]);
+            wait (50.0/bpm); // bpm = beats per minute
+            tick.attach(&flip, 1);
+            wait (10.0/bpm); // 
+            bpm = 400.0 * (1.0 - tsi.readPercentage());
+            i++;
+        }        
+    }
+}
diff -r 000000000000 -r 614598ef90dd mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Jun 10 12:11:41 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/0b3ab51c8877
\ No newline at end of file
diff -r 000000000000 -r 614598ef90dd tsi_sensor.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tsi_sensor.lib	Tue Jun 10 12:11:41 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Kojto/code/tsi_sensor/#20ffa9b18488