Kevin Chen / Mbed 2 deprecated drue

Dependencies:   mbed SDFileSystem

main.cpp

Committer:
kchen7
Date:
2019-04-21
Revision:
3:07afde41e7e6
Parent:
2:81f364f7a4a6
Child:
4:d414aee7ce9d

File content as of revision 3:07afde41e7e6:

// ESE350 Final Project: Drue
#include "mbed.h"

// Pin setup ////////////////////////////////////////////
// Button Inputs
DigitalIn butC(p14); 
DigitalIn butD(p15);
DigitalIn butE(p16);
DigitalIn butF(p17);
DigitalIn butG(p19);
DigitalIn butA(p20); 
DigitalIn butB(p30); 
DigitalIn butCh(p29); 
DigitalIn butMode(p22); 

// For music
PwmOut speaker(p21);

// I2C to drive LED srips
I2C i2c(p9, p10); 

// LED for tests
DigitalOut testLED(LED1);

/////////////////////////////////////////////////////////

// Variables 
bool switchPressed; 
bool notePressed; 
int prevNote; 
int mode; 
int numModes;
bool needNote; 
const int addr = 0b0100000 << 1; 
char cmd[2]; 

////////////////////////////////////////////////////////

void variableInit() {
    // modes
    mode = 0; 
    numModes = 2; 
    switchPressed = false;
    // keys
    notePressed = false; 
    prevNote = -1; 
    // wack-a-mole
    needNote = true;
}

void buttonSetup() {
    butC.mode(PullUp); 
    butD.mode(PullUp); 
    butE.mode(PullUp); 
    butF.mode(PullUp); 
    butG.mode(PullUp); 
    butA.mode(PullUp); 
    butB.mode(PullUp); 
    butCh.mode(PullUp); 
    butMode.mode(PullUp); 
}

void pressKey() {
    if (butC == 0 || butD == 0 || butE == 0 || butF == 0 || butG == 0 
                || butA == 0 || butB == 0 || butCh == 0) {
        // this if statement makes transitions between notes smoother
        // given that a note was already pressed before 
        if (prevNote != -1) {
            switch(prevNote) {
                case 0: 
                    if (butC == 1) {notePressed = false;}
                    break; 
                case 1: 
                    if (butD == 1) {notePressed = false;}
                    break; 
                case 2: 
                    if (butE == 1) {notePressed = false;}
                    break; 
                case 3: 
                    if (butF == 1) {notePressed = false;}
                    break; 
                case 4: 
                    if (butG == 1) {notePressed = false;}
                    break; 
                case 5: 
                    if (butA == 1) {notePressed = false;}
                    break; 
                case 6: 
                    if (butB == 1) {notePressed = false;}
                    break; 
                case 7: 
                    if (butCh == 1) {notePressed = false;}
                    break; 
            }
        }
        // wait(0.003); needed this before to give period time to update
        // this if statement initializes first note sound
        if (!notePressed) {
            if (butC == 0) {
                speaker.period(1.0/523.25);
                prevNote = 0; 
            } else if (butD == 0) {
                speaker.period(1.0/587.33);
                prevNote = 1; 
            } else if (butE == 0) {
                speaker.period(1.0/659.25); 
                prevNote = 2; 
            } else if (butF == 0) {
                speaker.period(1.0/698.46); 
                prevNote = 3; 
            } else if (butG == 0) {
                speaker.period(1.0/783.99);
                prevNote = 4; 
            } else if (butA == 0) {
                speaker.period(1.0/880.0);
                prevNote = 5; 
            } else if (butB == 0) {
                speaker.period(1.0/987.77);
                prevNote = 6; 
            } else if (butCh == 0) {
                speaker.period(1.0/1040.50);
                prevNote = 7; 
            }
        }
        notePressed = true; 
        speaker = 0.5; 
    } else {
        notePressed = false; 
        speaker = 0; 
        prevNote = -1; 
    }
}

void switchModeCheck() {
    if (butMode == 0) {
        if (!switchPressed) {
            switchPressed = true;
            mode++;
            if (mode == numModes) {
                mode = 0;
            }
        }
    } else {
        switchPressed = false;
    }
}

void cycleSound() {
    static int count = 0; 
    if (count == 0){
        speaker.period(1.0/523.25);
    } else if (count == 1){
        speaker.period(1.0/587.33);
    } else if (count == 2){
        speaker.period(1.0/659.25);
    } else if (count == 3){
        speaker.period(1.0/698.46);
    } else if (count == 4){
        speaker.period(1.0/783.99);
    } else if (count == 5){
        speaker.period(1.0/880.0);
    } else if (count == 6){
        speaker.period(1.0/987.77);
    } else if (count == 7){
        speaker.period(1.0/1040.50);
    }
    speaker = 0.5;
    wait(0.2);
    speaker = 0; 
    count++;
    if (count == 8) {count = 0;}
}

int main() {
    variableInit(); 
    buttonSetup(); 
    
    while(1) {
        switchModeCheck(); 
        
        if (mode == 0) {
            pressKey();
        } else if (mode == 1) {
            cycleSound(); 
        }
    }
}