Michele Furlanetto / Mbed 2 deprecated mbed_keyboard

Dependencies:   TextLCD mbed MMA8451Q TSI

main.cpp

Committer:
mfurlanetto
Date:
2015-08-25
Revision:
6:459ddd3079fa
Parent:
4:f4bd9fe2200b
Child:
7:98bccc314b54

File content as of revision 6:459ddd3079fa:

#include "mbed.h"
#include <Button.cpp>
#include "TextLCD.h"
#include "MMA8451Q.h"
#include "TSISensor.h"

#define DO4 262
#define RE4 294
#define MI4 330
#define FA4 349
#define SOL4 392
#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 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)

DigitalOut led1(LED1);
MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
TSISensor slider;
Ticker t;
int freq;
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 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;
            *mode=*mode+1;
        } else if (y>TILT_TOLERANCE_SET) {
            *allowEdit=true;
            *mode=*mode-1;
        }
    } else {
        if (abs(y)<TILT_TOLERANCE_RESET) {
            *allowEdit=false;
        }
    }
    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;
    
    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;
    
   
    t.attach(&flipLed, 1.0); 
    
    while (true) {
        lcd.cls();
        menuManager(&mode, &allowEdit);
        switch(mode) {
            case PLAY:
                break;
            case VOLUME:
                volumeSettings(volume);
                break;
            case METRONOME:
                metronomeSettings();
                break;
            case 3:
                lcd.printf("altro ancora");
                break;
            default:
                mode=PLAY;
        }
        
    }
}