Michele Furlanetto / Mbed 2 deprecated mbed_keyboard

Dependencies:   TextLCD mbed MMA8451Q TSI

Revision:
6:459ddd3079fa
Parent:
4:f4bd9fe2200b
Child:
7:98bccc314b54
--- a/main.cpp	Sat Aug 15 15:14:30 2015 +0000
+++ b/main.cpp	Tue Aug 25 14:12:19 2015 +0000
@@ -17,15 +17,17 @@
 #define METRONOME 2
 #define TILT_TOLERANCE_SET 0.75
 #define TILT_TOLERANCE_RESET 0.05
+#define MIN_METRONOME_BPM 40
+#define MAX_METRONOME_BPM 208
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
 
 TextLCD lcd(PTE5, PTE3, PTE2, PTB11, PTB10, PTB9, TextLCD::LCD16x2); //TextLCD (PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type=LCD16x2, PinName bl=NC, PinName e2=NC, LCDCtrl ctrl=HD44780)
-Speaker speaker (PTA4);
+
 DigitalOut led1(LED1);
 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
 TSISensor slider;
-
-int freq =0;
+Ticker t;
+int freq;
 int multiplier =0;
 
 void volumeSettings(float* volume)
@@ -36,66 +38,87 @@
     wait(0.2);
 }
 
-void play(float volume)
-{
-    if (freq>0) {
-        speaker.PlayNote(freq, 0.005, volume);
-    }
-}
 
-int menuManager(int oldMode, bool* allowEdit)
+void menuManager(int* mode, bool* allowEdit)
 {
     double y=acc.getAccY();
 
+    lcd.printf("modo: %d", *mode);
+    wait(0.05);
     if (*allowEdit==false) {
-
         if (y<-TILT_TOLERANCE_SET) {
             *allowEdit=true;
-            return oldMode+1;
+            *mode=*mode+1;
         } else if (y>TILT_TOLERANCE_SET) {
             *allowEdit=true;
-            return oldMode-1;
+            *mode=*mode-1;
         }
     } else {
         if (abs(y)<TILT_TOLERANCE_RESET) {
             *allowEdit=false;
         }
     }
-    return oldMode;
+    return;
+}
+
+void flipLed(){
+    led1=!led1;   
+}
+
+void metronomeSettings(){
+    float bpm=6;
+    lcd.cls();
+    lcd.printf("bpm: %.0f",bpm*10);
+    wait(0.1);
+    float tmp = slider.readPercentage();
+    if(tmp>0){
+        t.detach();
+        while (tmp>0){
+            bpm = MIN_METRONOME_BPM/10+tmp*(MAX_METRONOME_BPM-MIN_METRONOME_BPM)/10;
+            tmp = slider.readPercentage();
+            lcd.cls();
+            lcd.printf("bpm: %.0f",bpm*10);
+            wait(0.1);
+        }
+        t.attach(&flipLed, 6/(bpm));
+    }
 }
 
 int main()
 {
     led1=1;
-    Button a (PTA1, LA4, &freq, &multiplier);
-    Button b (PTA2, DO4, &freq, &multiplier);
-    Button c (PTA12, FA4, &freq, &multiplier);
-
+    
+    Speaker speaker (PTA4);
+    
+    float* volume=speaker.getVolumeBuffer();
+    Button a (PTA1, LA4, &speaker, &multiplier);
+    Button b (PTA2, DO4, &speaker, &multiplier);
+    Button c (PTA12, FA4, &speaker, &multiplier);
 
     bool allowEdit=false;
     int mode=PLAY;
-    float volume=0.2;
-
+    
+   
+    t.attach(&flipLed, 1.0); 
+    
     while (true) {
         lcd.cls();
-
-        mode = menuManager(mode, &allowEdit);
-
+        menuManager(&mode, &allowEdit);
         switch(mode) {
             case PLAY:
-                play(volume);
                 break;
             case VOLUME:
-                volumeSettings(&volume);
+                volumeSettings(volume);
                 break;
             case METRONOME:
-                lcd.printf("metronomo");
+                metronomeSettings();
                 break;
             case 3:
                 lcd.printf("altro ancora");
                 break;
             default:
-                mode=0;
+                mode=PLAY;
         }
+        
     }
 }
\ No newline at end of file