for percussion1

Dependencies:   mbed

Fork of penkich_test_percussion by kitac_mbed_WS

Committer:
penkich
Date:
Sun Aug 23 10:27:57 2015 +0000
Revision:
2:269fadd5d1f2
Parent:
1:3252e4fbccab
Child:
3:396b2628e3d7
add soft pullup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
penkich 0:e363f64b5744 1 #include "mbed.h"
penkich 0:e363f64b5744 2
penkich 1:3252e4fbccab 3 DigitalIn sw(D7);
penkich 2:269fadd5d1f2 4
penkich 0:e363f64b5744 5 Ticker timer;
penkich 1:3252e4fbccab 6 PwmOut speaker(D5);
penkich 0:e363f64b5744 7 Ticker envelope;
penkich 1:3252e4fbccab 8 PwmOut env(D3);
penkich 0:e363f64b5744 9 static int tms_per10=0;
penkich 0:e363f64b5744 10
penkich 0:e363f64b5744 11 void env_out(void){
penkich 0:e363f64b5744 12 float attack_ms =10;
penkich 0:e363f64b5744 13 float decay_ms = 10;
penkich 1:3252e4fbccab 14 float sustain_ms = 20;
penkich 1:3252e4fbccab 15 float sustain = 0.2;
penkich 1:3252e4fbccab 16 float release_ms = 5000;
penkich 0:e363f64b5744 17
penkich 0:e363f64b5744 18 float out_level;
penkich 0:e363f64b5744 19 tms_per10++;
penkich 0:e363f64b5744 20 if (tms_per10 > (attack_ms + decay_ms + sustain_ms + release_ms) * 10){
penkich 0:e363f64b5744 21 env.write(0.0f);
penkich 0:e363f64b5744 22 }else if(tms_per10 > (attack_ms + decay_ms + sustain_ms) * 10){
penkich 0:e363f64b5744 23 out_level = sustain/(tms_per10 - (attack_ms + decay_ms + sustain_ms)) * release_ms;
penkich 0:e363f64b5744 24 env.write(out_level);
penkich 0:e363f64b5744 25 }else if(tms_per10 > (attack_ms + decay_ms) * 10){
penkich 0:e363f64b5744 26 env.write(sustain);
penkich 0:e363f64b5744 27 }else if(tms_per10 > attack_ms * 10){
penkich 0:e363f64b5744 28 env.write(1.0f);
penkich 0:e363f64b5744 29 }else{
penkich 1:3252e4fbccab 30 out_level = 1.0f/attack_ms * tms_per10;
penkich 0:e363f64b5744 31 env.write(out_level);
penkich 0:e363f64b5744 32 }
penkich 0:e363f64b5744 33 }
penkich 0:e363f64b5744 34
penkich 0:e363f64b5744 35 void sound_out(void){
penkich 0:e363f64b5744 36 speaker.write(((float)rand()/RAND_MAX));
penkich 0:e363f64b5744 37 }
penkich 0:e363f64b5744 38
penkich 0:e363f64b5744 39
penkich 0:e363f64b5744 40 int main()
penkich 0:e363f64b5744 41 {
penkich 2:269fadd5d1f2 42 sw.mode(PullUp);
penkich 0:e363f64b5744 43 while(1){
penkich 0:e363f64b5744 44 if (sw ==0){
penkich 0:e363f64b5744 45 tms_per10=0;
penkich 0:e363f64b5744 46 speaker.period_us(10);
penkich 0:e363f64b5744 47 env.period_us(10);
penkich 0:e363f64b5744 48 timer.attach_us(&sound_out,100); //10kHz
penkich 0:e363f64b5744 49 envelope.attach_us(&env_out,100); //10kHz
penkich 0:e363f64b5744 50 }
penkich 0:e363f64b5744 51 wait(0.01f);
penkich 0:e363f64b5744 52 }
penkich 0:e363f64b5744 53 }