A MIDI piano synthesizer that implements the Karplus Strong physical modeling algorithm.

Dependencies:   mbed USBDevice PinDetect

LEDController.cpp

Committer:
ndelfino
Date:
2016-04-14
Revision:
11:f65806ee5833
Parent:
8:deaedb59243e
Child:
17:55e6132c54a8

File content as of revision 11:f65806ee5833:

#include "mbed.h"
#include "LEDController.h"

DigitalOut myled1_1(p5);
DigitalOut myled1_2(p6);
DigitalOut myled1_3(p7);

DigitalOut myled2_1(p8);
DigitalOut myled2_2(p9);
DigitalOut myled2_3(p10);

DigitalOut myled3_1(p11);
DigitalOut myled3_2(p12);
DigitalOut myled3_3(p13);

DigitalOut myled4_1(p14);
DigitalOut myled4_2(p15);
DigitalOut myled4_3(p16);

void identifyKeyForLed(int key, int type);
void chooseLedForKey(int colors[3], int type, int signature, int key);
void setLedToKey(int colors[3], int type, int signature, int led, int key);

//These are the notes numbers and RGB values.
//int noteC[] = {24,0,0,0};
//int noteD[] = {26,1,0,0};
//int noteE[] = {16,0,1,0};
//int noteF[] = {17,0,0,1};
//int noteG[] = {32,1,1,0};
//int noteA[] = {34,0,1,1};
//int noteB[] = {36,1,1,1};

//A larger array of all the keys to be iterated over.
//int keys[7][4] = {noteC, noteD, noteE, noteF, noteG, noteA, noteB};
int colors[12][3] = {{0,0,0},{0,0,0},{1,0,0},{1,0,0},{0,1,0},{0,0,1},{0,0,1},{1,1,0},{1,1,0},{0,1,1},{0,1,1},{1,1,1}};


int keys[11][12] = {{0,1,2,3,4,5,6,7,8,9,10,11},
                    {12,13,14,15,16,17,18,19,20,21,22,23},
                    {24,25,26,27,28,29,30,31,32,33,34,35},
                    {36,37,38,39,40,41,42,43,44,45,46,47},
                    {48,49,50,51,52,53,54,55,56,57,58,59},
                    {60,61,62,63,64,65,66,67,68,69,70,71},
                    {72,73,74,75,76,77,78,79,80,81,82,83},
                    {84,85,86,87,88,89,90,91,92,93,94,95},
                    {96,97,98,99,100,101,102,103,104,105,106,107},
                    {108,109,110,111,112,113,114,115,116,117,118,119},
                    {120,121,122,123,124,125,126,127,0,0,0,0}};

const int keysSize = 12;

int ledKeys[4] = {-1,-1,-1,-1};

LEDController::LEDController() {

}

void LEDController::identifyKeyForLed(int key, int type){
    //pc.printf("\r\nGetting note");
    int octave = key/12;
    
    for(int i = 0; i < keysSize; i++){
        if(keys[octave][i] == key){
           //pc.printf("\r\nNote Chosen, no signature");
           chooseLedForKey(colors[i], type,0, keys[octave][i]);     
        }
    }
} 

//Chooses which LED to use based upon which are set or if they needed to be turned off.
void LEDController::chooseLedForKey(int colors[3], int type, int signature, int key){
    
    //Determines if the LED is not set and therefore can be used. 
    //Or if it is in use by the same key being passed in which means it needs to be turned off.
    if(type == -1){
        if(ledKeys[0] == key){
          setLedToKey(colors, type, signature, 1, key);
        }else if(ledKeys[1] == key){
          setLedToKey(colors, type, signature, 2, key);
        }else if(ledKeys[2] == key){
          setLedToKey(colors, type, signature, 3, key);
        }else if(ledKeys[3] == key){
          setLedToKey(colors, type, signature, 4, key);
          }
            
        }else{
            if(ledKeys[0] == -1){
                //pc.printf("\r\nLED1 chosen.");
                setLedToKey(colors, type, signature, 1, key);
            }else if(ledKeys[1] == -1){
                setLedToKey(colors, type, signature, 2, key);
            }else if(ledKeys[2] == -1){
                setLedToKey(colors, type, signature, 3, key);
            }else if(ledKeys[3] == -1){
                setLedToKey(colors, type, signature, 4, key);
            }
        }
}

//This sets the key or clears the previously set key if the type is off.
void LEDController::setLedToKey(int colors[3], int type, int signature, int led, int key){
    if(led == 1){
        if(type == 1){
            //pc.printf("\r\nTurning on light 1");
            ledKeys[0] = key;
            myled1_1 = colors[0];
            myled1_2 = colors[1];
            myled1_3 = colors[2];
         }else{
            //pc.printf("\r\nTurning off light 1");
            ledKeys[0] = -1;
            myled1_1 = 0;
            myled1_2 = 0;
            myled1_3 = 0;
         }
    }else if(led == 2){
        if(type == 1){
            //pc.printf("\r\nTurning on light 2");
            ledKeys[1] = key;
            myled2_1 = colors[0];
            myled2_2 = colors[1];
            myled2_3 = colors[2];
     }else{
    //        pc.printf("\r\nTurning off light 2");
            ledKeys[1] = -1;
            myled2_1 = 0;
            myled2_2 = 0;
            myled2_3 = 0;
     }
    }else if(led == 3){
        if(type == 1){
    //        pc.printf("\r\nTurning on light 3");
            ledKeys[2] = key;
            myled3_1 = colors[0];
            myled3_2 = colors[1];
            myled3_3 = colors[2];
     }else{
    //        pc.printf("\r\nTurning off light 3");
            ledKeys[2] = -1;
            myled3_1 = 0;
            myled3_2 = 0;
            myled3_3 = 0;
     }
    }else if(led == 4){
        if(type == 1){
    //        pc.printf("\r\nTurning on light 4");
            ledKeys[3] = key;
            myled4_1 = colors[0];;
            myled4_2 = colors[1];
            myled4_3 = colors[2];
     }else{
    //        pc.printf("\r\nTurning off light 4");
            ledKeys[3] = -1;
            myled4_1 = 0;
            myled4_2 = 0;
            myled4_3 = 0;
     }
    }
        
}