Michele Furlanetto / Mbed 2 deprecated mbed_keyboard

Dependencies:   TextLCD mbed MMA8451Q TSI

Revision:
4:f4bd9fe2200b
Parent:
3:a703eda2b125
Child:
6:459ddd3079fa
--- a/main.cpp	Sat Aug 15 13:56:42 2015 +0000
+++ b/main.cpp	Sat Aug 15 15:13:34 2015 +0000
@@ -1,6 +1,8 @@
 #include "mbed.h"
 #include <Button.cpp>
 #include "TextLCD.h"
+#include "MMA8451Q.h"
+#include "TSISensor.h"
 
 #define DO4 262
 #define RE4 294
@@ -10,29 +12,90 @@
 #define LA4 440
 #define SI4 494
 
+#define PLAY 0
+#define VOLUME 1
+#define METRONOME 2
+#define TILT_TOLERANCE_SET 0.75
+#define TILT_TOLERANCE_RESET 0.05
+#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);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
+MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
+TSISensor slider;
+
 int freq =0;
-int multiplier =1;
+int multiplier =0;
+
+void volumeSettings(float* volume)
+{
+    float tmp= slider.readPercentage();
+    if (tmp>0) *volume = tmp;
+    lcd.printf("Volume: %.3f", *volume);
+    wait(0.2);
+}
+
+void play(float volume)
+{
+    if (freq>0) {
+        speaker.PlayNote(freq, 0.005, volume);
+    }
+}
 
+int menuManager(int oldMode, bool* allowEdit)
+{
+    double y=acc.getAccY();
 
-int main(){
+    if (*allowEdit==false) {
+
+        if (y<-TILT_TOLERANCE_SET) {
+            *allowEdit=true;
+            return oldMode+1;
+        } else if (y>TILT_TOLERANCE_SET) {
+            *allowEdit=true;
+            return oldMode-1;
+        }
+    } else {
+        if (abs(y)<TILT_TOLERANCE_RESET) {
+            *allowEdit=false;
+        }
+    }
+    return oldMode;
+}
+
+int main()
+{
     led1=1;
-    led2=1;
-    led3=1;
     Button a (PTA1, LA4, &freq, &multiplier);
     Button b (PTA2, DO4, &freq, &multiplier);
     Button c (PTA12, FA4, &freq, &multiplier);
-    lcd.cls();
-    while (true){
-        if(freq>0){
-            speaker.PlayNote(freq, 0.005, 0.2);
+
+
+    bool allowEdit=false;
+    int mode=PLAY;
+    float volume=0.2;
+
+    while (true) {
+        lcd.cls();
+
+        mode = menuManager(mode, &allowEdit);
+
+        switch(mode) {
+            case PLAY:
+                play(volume);
+                break;
+            case VOLUME:
+                volumeSettings(&volume);
+                break;
+            case METRONOME:
+                lcd.printf("metronomo");
+                break;
+            case 3:
+                lcd.printf("altro ancora");
+                break;
+            default:
+                mode=0;
         }
-      /*  lcd.printf("%d", freq);
-        wait(0.1);
-        lcd.cls();  */
     }
 }
\ No newline at end of file