Kevin Chen / Mbed 2 deprecated drue

Dependencies:   mbed SDFileSystem

main.cpp

Committer:
kchen7
Date:
2019-04-26
Revision:
4:d414aee7ce9d
Parent:
3:07afde41e7e6
Child:
5:e297f321fa20

File content as of revision 4:d414aee7ce9d:

// 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 i2cLED(p9, p10); 

// LED for tests
DigitalOut testLED(LED1);

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

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

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

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

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;
            prevNote = -1; 
            score = 0; 
            
            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;}
    prevNote = count; 
}

void lightLED(int key) {
    switch(key) {
        case 0: 
            cmd[0] = 0xFE; 
            break; 
        case 1: 
            cmd[0] = 0xFD; 
            break; 
        case 2: 
            cmd[0] = 0xFB; 
            break; 
        case 3: 
            cmd[0] = 0xF7; 
            break; 
        case 4: 
            cmd[0] = 0x7F; 
            break; 
        case 5: 
            cmd[0] = 0xBF; 
            break; 
        case 6: 
            cmd[0] = 0xDF; 
            break; 
        case 7: 
            cmd[0] = 0xEF; 
            break; 
        default: 
            cmd[0] = 0xFF; 
    }
    i2cLED.write(addr, cmd, 1); 
}

int main() {
    variableInit(); 
    buttonSetup(); 
    
    while(1) {
        
        lightLED(prevNote); 
        switchModeCheck(); 
        
        if (mode == 0) {
            testLED = 0; // remove once whac-a-mole is implemented
            pressKey();
        } else if (mode == 1) {
            testLED = 1; // remove once whac-a-mole is implemented
        } else if (mode == 2) {
            testLED = 0; // remove once whac-a-mole is implemented   
            cycleSound(); 
        }
    }
}