Michele Furlanetto / Mbed 2 deprecated mbed_keyboard

Dependencies:   TextLCD mbed MMA8451Q TSI

main.cpp

Committer:
mfurlanetto
Date:
2015-09-19
Revision:
8:60231840370f
Parent:
7:98bccc314b54
Child:
9:f1fa0dad4096

File content as of revision 8:60231840370f:

#include "ButtonMusical.cpp"
#include "ButtonDiesis.cpp"

#include "TextLCD.h"
#include "MMA8451Q.h"
#include "TSISensor.h"

#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 MIN_OCTAVE 1
#define MAX_OCTAVE 8
#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;
bool doDiesis=false;

Note LA4(440, true, &multiplier, &doDiesis);
Note MI4(330, true, &multiplier, &doDiesis);
//Note FA4(349, true, &multiplier, &doDiesis);

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();

    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));
    }
}


void octaveSettings()
{
    float octave=4;
    float tmp = slider.readPercentage();
    lcd.cls();
    lcd.printf("prova");
    if(tmp>0) {
        while (tmp>0) {
            octave = MIN_OCTAVE+tmp*(MAX_OCTAVE-MIN_OCTAVE);
            multiplier=((int)octave)-4;
            tmp = slider.readPercentage();
            lcd.cls();
            lcd.printf("OCTAVE: %.0f",octave);
            wait(0.1);
        }
    }
}
int main()
{
    led1=1;

    Speaker speaker (PTA4);

    float* volume=speaker.getVolumeBuffer();
    ButtonMusical a (PTA1, LA4, &speaker);
    ButtonMusical b (PTA2, MI4, &speaker);
    ButtonDiesis c (PTA12, &doDiesis, &speaker);

    bool allowEdit=false;
    int mode=PLAY;


    t.attach(&flipLed, 1.0);

    while (true) {
        lcd.cls();
        menuManager(&mode, &allowEdit);
        lcd.printf("f=%.3f, d=%d", speaker.getFreq(), doDiesis);
        wait(0.1);
        switch(mode) {
            case PLAY:
                octaveSettings();
                break;
            case VOLUME:
                volumeSettings(volume);
                break;
            case METRONOME:
                metronomeSettings();
                break;
            default:
                mode=PLAY;
        }

    }
}