penkich_percussion_2_NUCLEO

Dependencies:   mbed

Fork of penkich_test_percussion by kitac_mbed_WS

Revision:
0:e363f64b5744
Child:
1:b102b9476594
diff -r 000000000000 -r e363f64b5744 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Aug 18 12:26:25 2015 +0000
@@ -0,0 +1,51 @@
+#include "mbed.h"
+
+DigitalIn sw(dp9);
+Ticker timer;
+PwmOut speaker(dp18);
+Ticker envelope;
+PwmOut env(dp24);
+static int tms_per10=0; 
+
+void env_out(void){
+    float attack_ms =10;
+    float decay_ms = 10;
+    float sustain_ms = 200;
+    float sustain = 0.5;
+    float release_ms = 400;
+
+    float out_level;
+    tms_per10++;
+    if (tms_per10 > (attack_ms + decay_ms + sustain_ms + release_ms) * 10){
+        env.write(0.0f);
+    }else if(tms_per10 > (attack_ms + decay_ms + sustain_ms) * 10){
+        out_level = sustain/(tms_per10 - (attack_ms + decay_ms + sustain_ms)) * release_ms; 
+        env.write(out_level);
+    }else if(tms_per10 > (attack_ms + decay_ms) * 10){
+        env.write(sustain);
+    }else if(tms_per10 > attack_ms * 10){
+        env.write(1.0f);
+    }else{
+        out_level = 1.0/attack_ms * tms_per10;
+        env.write(out_level);
+    }
+}
+
+void sound_out(void){
+    speaker.write(((float)rand()/RAND_MAX));
+}
+
+     
+int main()
+{
+    while(1){
+        if (sw ==0){
+            tms_per10=0; 
+            speaker.period_us(10);
+            env.period_us(10);
+            timer.attach_us(&sound_out,100); //10kHz
+            envelope.attach_us(&env_out,100); //10kHz
+        }
+    wait(0.01f);
+    }
+}
\ No newline at end of file