Austin Suszek / Mbed 2 deprecated MIDISynthesizer

Dependencies:   mbed USBDevice PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LEDController.cpp Source File

LEDController.cpp

00001 #include "mbed.h"
00002 #include "LEDController.h"
00003 
00004 //Pins that output values to the brick LED
00005 DigitalOut brickled1(p5);
00006 DigitalOut brickled2(p6);
00007 DigitalOut brickled3(p7);
00008 DigitalOut brickled4(p8);
00009 
00010 //Pins that output values to the tri-color LEDS for cLED1
00011 DigitalOut myled1_1(p9);
00012 DigitalOut myled1_2(p10);
00013 DigitalOut myled1_3(p11);
00014 
00015 //Pins that output values to the tri-color LEDS for LED2
00016 DigitalOut myled2_1(p12);
00017 DigitalOut myled2_2(p13);
00018 DigitalOut myled2_3(p14);
00019 
00020 //Pins that output values to the tri-color LEDS for LED3
00021 DigitalOut myled3_1(p15);
00022 DigitalOut myled3_2(p16);
00023 DigitalOut myled3_3(p17);
00024 
00025 //Pins that output values to the tri-color LEDS for LED4
00026 DigitalOut myled4_1(p18);
00027 DigitalOut myled4_2(p19);
00028 DigitalOut myled4_3(p20);
00029 
00030 void identifyKeyForLed(int key, int type);
00031 void chooseLedForKey(int colors[3], int type, int signature, int key);
00032 void setLedToKey(int colors[3], int type, int signature, int led, int key);
00033 
00034 //These are the color assignments for the keys. In order, C, C#, D, D#, E,F, F#, G, G#, A, A#, B
00035 int colors[12][3] = {{1,0,1},{1,0,1},{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}};
00036 
00037 //This holds the current key of the LED. If no key is in the LED then the value for that slot is -1.
00038 int ledKeys[4] = {-1,-1,-1,-1};
00039 
00040 const int SHARP = 1;
00041 const int NOT_SHARP = 0;
00042 
00043 const int cLED1 = 1;
00044 const int cLED2 = 2;
00045 const int cLED3 = 3;
00046 const int cLED4 = 4;
00047 
00048 const int cSharp = 1;
00049 const int dSharp = 3;
00050 const int fSharp = 6;
00051 const int gSharp = 8;
00052 const int aSharp = 10;
00053 
00054 //Identifies the key.
00055 void LEDController::identifyKeyForLed(int key, int type){
00056    
00057    //Performs modulus to determine key. This also determines color and signature.
00058    int determineColorSignature = key%12;
00059 
00060    //If modulus operation is any of these values then it means the key is sharp. 
00061    if(determineColorSignature == cSharp || determineColorSignature == dSharp || determineColorSignature == fSharp 
00062       || determineColorSignature == gSharp || determineColorSignature == aSharp){
00063         chooseLedForKey(colors[determineColorSignature], type, SHARP, key);
00064     }else{
00065         chooseLedForKey(colors[determineColorSignature], type, NOT_SHARP, key);
00066     }     
00067 } 
00068 
00069 //Chooses which LED needs to perform an action. Either turning on a light to the specific note
00070 //color or turning off the LED when the key is no longer active.
00071 void LEDController::chooseLedForKey(int colors[3], int type, int signature, int key){
00072     
00073     //Determines if the LED is not set and therefore can be used. 
00074     if(type == -1){
00075         if(ledKeys[0] == key){
00076           setLedToKey(colors, type, signature, 1, key);
00077         }else if(ledKeys[1] == key){
00078           setLedToKey(colors, type, signature, 2, key);
00079         }else if(ledKeys[2] == key){
00080           setLedToKey(colors, type, signature, 3, key);
00081         }else if(ledKeys[3] == key){
00082           setLedToKey(colors, type, signature, 4, key);
00083         }        
00084     } else {
00085         if(ledKeys[0] == -1){
00086             setLedToKey(colors, type, signature, 1, key);
00087         }else if(ledKeys[1] == -1){
00088             setLedToKey(colors, type, signature, 2, key);
00089         }else if(ledKeys[2] == -1){
00090             setLedToKey(colors, type, signature, 3, key);
00091         }else if(ledKeys[3] == -1){
00092             setLedToKey(colors, type, signature, 4, key);
00093         }
00094     }
00095 }
00096 
00097 //This sets the LED to the key or clears the previously set LED of that specific key if the type is off.
00098 void LEDController::setLedToKey(int colors[3], int type, int signature, int led, int key){
00099  if(led == cLED1){
00100         if(type == 1){
00101             ledKeys[0] = key;
00102             myled1_1 = colors[0];
00103             myled1_2 = colors[1];
00104             myled1_3 = colors[2];
00105             if(signature == SHARP){
00106                 brickled1 = 1;
00107              }
00108          }else{
00109             ledKeys[0] = -1;
00110             myled1_1 = 0;
00111             myled1_2 = 0;
00112             myled1_3 = 0;
00113             brickled1 = 0;
00114          }
00115     }else if(led == cLED2){
00116         if(type == 1){
00117             ledKeys[1] = key;
00118             myled2_1 = colors[0];
00119             myled2_2 = colors[1];
00120             myled2_3 = colors[2];
00121             if(signature == SHARP){
00122                 brickled2 = 1;
00123              }
00124      }else{
00125             ledKeys[1] = -1;
00126             myled2_1 = 0;
00127             myled2_2 = 0;
00128             myled2_3 = 0;
00129             brickled2 = 0;
00130      }
00131     }else if(led == cLED3){
00132         if(type == 1){
00133             ledKeys[2] = key;
00134             myled3_1 = colors[0];
00135             myled3_2 = colors[1];
00136             myled3_3 = colors[2];
00137             if(signature == SHARP){
00138                 brickled3 = 1;
00139              }
00140      }else{
00141             ledKeys[2] = -1;
00142             myled3_1 = 0;
00143             myled3_2 = 0;
00144             myled3_3 = 0;
00145             brickled3 = 0;
00146      }
00147     }else if(led == cLED4){
00148         if(type == 1){
00149             ledKeys[3] = key;
00150             myled4_1 = colors[0];;
00151             myled4_2 = colors[1];
00152             myled4_3 = colors[2];
00153             if(signature == SHARP){
00154                 brickled4 = 1;
00155              }
00156      }else{
00157             ledKeys[3] = -1;
00158             myled4_1 = 0;
00159             myled4_2 = 0;
00160             myled4_3 = 0;
00161             brickled4 = 0;
00162      }
00163     }
00164         
00165 }