Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed USBDevice PinDetect
LEDController.cpp@22:b800e1766647, 2016-04-25 (annotated)
- Committer:
- asuszek
- Date:
- Mon Apr 25 23:58:15 2016 +0000
- Revision:
- 22:b800e1766647
- Parent:
- 21:8b5d753b6bf5
Fixed LED bug
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ndelfino | 3:8f80e267a80d | 1 | #include "mbed.h" |
| ndelfino | 8:deaedb59243e | 2 | #include "LEDController.h" |
| ndelfino | 3:8f80e267a80d | 3 | |
| ndelfino | 21:8b5d753b6bf5 | 4 | //Pins that output values to the brick LED |
| ndelfino | 17:55e6132c54a8 | 5 | DigitalOut brickled1(p5); |
| ndelfino | 17:55e6132c54a8 | 6 | DigitalOut brickled2(p6); |
| ndelfino | 17:55e6132c54a8 | 7 | DigitalOut brickled3(p7); |
| ndelfino | 17:55e6132c54a8 | 8 | DigitalOut brickled4(p8); |
| ndelfino | 3:8f80e267a80d | 9 | |
| ndelfino | 21:8b5d753b6bf5 | 10 | //Pins that output values to the tri-color LEDS for cLED1 |
| ndelfino | 17:55e6132c54a8 | 11 | DigitalOut myled1_1(p9); |
| ndelfino | 17:55e6132c54a8 | 12 | DigitalOut myled1_2(p10); |
| ndelfino | 17:55e6132c54a8 | 13 | DigitalOut myled1_3(p11); |
| ndelfino | 4:bb9f50305401 | 14 | |
| ndelfino | 21:8b5d753b6bf5 | 15 | //Pins that output values to the tri-color LEDS for LED2 |
| ndelfino | 17:55e6132c54a8 | 16 | DigitalOut myled2_1(p12); |
| ndelfino | 17:55e6132c54a8 | 17 | DigitalOut myled2_2(p13); |
| ndelfino | 17:55e6132c54a8 | 18 | DigitalOut myled2_3(p14); |
| ndelfino | 4:bb9f50305401 | 19 | |
| ndelfino | 21:8b5d753b6bf5 | 20 | //Pins that output values to the tri-color LEDS for LED3 |
| ndelfino | 17:55e6132c54a8 | 21 | DigitalOut myled3_1(p15); |
| ndelfino | 17:55e6132c54a8 | 22 | DigitalOut myled3_2(p16); |
| ndelfino | 17:55e6132c54a8 | 23 | DigitalOut myled3_3(p17); |
| ndelfino | 17:55e6132c54a8 | 24 | |
| ndelfino | 21:8b5d753b6bf5 | 25 | //Pins that output values to the tri-color LEDS for LED4 |
| ndelfino | 17:55e6132c54a8 | 26 | DigitalOut myled4_1(p18); |
| ndelfino | 17:55e6132c54a8 | 27 | DigitalOut myled4_2(p19); |
| ndelfino | 17:55e6132c54a8 | 28 | DigitalOut myled4_3(p20); |
| ndelfino | 3:8f80e267a80d | 29 | |
| ndelfino | 8:deaedb59243e | 30 | void identifyKeyForLed(int key, int type); |
| ndelfino | 11:f65806ee5833 | 31 | void chooseLedForKey(int colors[3], int type, int signature, int key); |
| ndelfino | 11:f65806ee5833 | 32 | void setLedToKey(int colors[3], int type, int signature, int led, int key); |
| ndelfino | 4:bb9f50305401 | 33 | |
| ndelfino | 21:8b5d753b6bf5 | 34 | //These are the color assignments for the keys. In order, C, C#, D, D#, E,F, F#, G, G#, A, A#, B |
| ndelfino | 17:55e6132c54a8 | 35 | 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}}; |
| ndelfino | 4:bb9f50305401 | 36 | |
| ndelfino | 21:8b5d753b6bf5 | 37 | //This holds the current key of the LED. If no key is in the LED then the value for that slot is -1. |
| ndelfino | 11:f65806ee5833 | 38 | int ledKeys[4] = {-1,-1,-1,-1}; |
| ndelfino | 5:a687cfa7be62 | 39 | |
| ndelfino | 21:8b5d753b6bf5 | 40 | const int SHARP = 1; |
| ndelfino | 21:8b5d753b6bf5 | 41 | const int NOT_SHARP = 0; |
| ndelfino | 21:8b5d753b6bf5 | 42 | |
| ndelfino | 21:8b5d753b6bf5 | 43 | const int cLED1 = 1; |
| ndelfino | 21:8b5d753b6bf5 | 44 | const int cLED2 = 2; |
| ndelfino | 21:8b5d753b6bf5 | 45 | const int cLED3 = 3; |
| ndelfino | 21:8b5d753b6bf5 | 46 | const int cLED4 = 4; |
| ndelfino | 17:55e6132c54a8 | 47 | |
| ndelfino | 21:8b5d753b6bf5 | 48 | const int cSharp = 1; |
| ndelfino | 21:8b5d753b6bf5 | 49 | const int dSharp = 3; |
| ndelfino | 21:8b5d753b6bf5 | 50 | const int fSharp = 6; |
| ndelfino | 21:8b5d753b6bf5 | 51 | const int gSharp = 8; |
| ndelfino | 21:8b5d753b6bf5 | 52 | const int aSharp = 10; |
| ndelfino | 21:8b5d753b6bf5 | 53 | |
| ndelfino | 21:8b5d753b6bf5 | 54 | //Identifies the key. |
| ndelfino | 21:8b5d753b6bf5 | 55 | void LEDController::identifyKeyForLed(int key, int type){ |
| ndelfino | 21:8b5d753b6bf5 | 56 | |
| ndelfino | 21:8b5d753b6bf5 | 57 | //Performs modulus to determine key. This also determines color and signature. |
| ndelfino | 21:8b5d753b6bf5 | 58 | int determineColorSignature = key%12; |
| ndelfino | 21:8b5d753b6bf5 | 59 | |
| ndelfino | 21:8b5d753b6bf5 | 60 | //If modulus operation is any of these values then it means the key is sharp. |
| ndelfino | 21:8b5d753b6bf5 | 61 | if(determineColorSignature == cSharp || determineColorSignature == dSharp || determineColorSignature == fSharp |
| ndelfino | 21:8b5d753b6bf5 | 62 | || determineColorSignature == gSharp || determineColorSignature == aSharp){ |
| ndelfino | 21:8b5d753b6bf5 | 63 | chooseLedForKey(colors[determineColorSignature], type, SHARP, key); |
| ndelfino | 17:55e6132c54a8 | 64 | }else{ |
| ndelfino | 21:8b5d753b6bf5 | 65 | chooseLedForKey(colors[determineColorSignature], type, NOT_SHARP, key); |
| ndelfino | 17:55e6132c54a8 | 66 | } |
| ndelfino | 4:bb9f50305401 | 67 | } |
| ndelfino | 4:bb9f50305401 | 68 | |
| ndelfino | 21:8b5d753b6bf5 | 69 | //Chooses which LED needs to perform an action. Either turning on a light to the specific note |
| ndelfino | 21:8b5d753b6bf5 | 70 | //color or turning off the LED when the key is no longer active. |
| ndelfino | 11:f65806ee5833 | 71 | void LEDController::chooseLedForKey(int colors[3], int type, int signature, int key){ |
| ndelfino | 4:bb9f50305401 | 72 | |
| ndelfino | 4:bb9f50305401 | 73 | //Determines if the LED is not set and therefore can be used. |
| asuszek | 22:b800e1766647 | 74 | if(type == -1){ |
| asuszek | 22:b800e1766647 | 75 | if(ledKeys[0] == key){ |
| asuszek | 22:b800e1766647 | 76 | setLedToKey(colors, type, signature, 1, key); |
| asuszek | 22:b800e1766647 | 77 | }else if(ledKeys[1] == key){ |
| asuszek | 22:b800e1766647 | 78 | setLedToKey(colors, type, signature, 2, key); |
| asuszek | 22:b800e1766647 | 79 | }else if(ledKeys[2] == key){ |
| asuszek | 22:b800e1766647 | 80 | setLedToKey(colors, type, signature, 3, key); |
| asuszek | 22:b800e1766647 | 81 | }else if(ledKeys[3] == key){ |
| asuszek | 22:b800e1766647 | 82 | setLedToKey(colors, type, signature, 4, key); |
| asuszek | 22:b800e1766647 | 83 | } |
| asuszek | 22:b800e1766647 | 84 | } else { |
| asuszek | 22:b800e1766647 | 85 | if(ledKeys[0] == -1){ |
| asuszek | 22:b800e1766647 | 86 | setLedToKey(colors, type, signature, 1, key); |
| asuszek | 22:b800e1766647 | 87 | }else if(ledKeys[1] == -1){ |
| asuszek | 22:b800e1766647 | 88 | setLedToKey(colors, type, signature, 2, key); |
| asuszek | 22:b800e1766647 | 89 | }else if(ledKeys[2] == -1){ |
| asuszek | 22:b800e1766647 | 90 | setLedToKey(colors, type, signature, 3, key); |
| asuszek | 22:b800e1766647 | 91 | }else if(ledKeys[3] == -1){ |
| asuszek | 22:b800e1766647 | 92 | setLedToKey(colors, type, signature, 4, key); |
| asuszek | 20:bf675ba2c454 | 93 | } |
| asuszek | 22:b800e1766647 | 94 | } |
| ndelfino | 4:bb9f50305401 | 95 | } |
| ndelfino | 4:bb9f50305401 | 96 | |
| ndelfino | 21:8b5d753b6bf5 | 97 | //This sets the LED to the key or clears the previously set LED of that specific key if the type is off. |
| ndelfino | 11:f65806ee5833 | 98 | void LEDController::setLedToKey(int colors[3], int type, int signature, int led, int key){ |
| ndelfino | 21:8b5d753b6bf5 | 99 | if(led == cLED1){ |
| ndelfino | 8:deaedb59243e | 100 | if(type == 1){ |
| ndelfino | 11:f65806ee5833 | 101 | ledKeys[0] = key; |
| ndelfino | 11:f65806ee5833 | 102 | myled1_1 = colors[0]; |
| ndelfino | 11:f65806ee5833 | 103 | myled1_2 = colors[1]; |
| ndelfino | 11:f65806ee5833 | 104 | myled1_3 = colors[2]; |
| ndelfino | 21:8b5d753b6bf5 | 105 | if(signature == SHARP){ |
| ndelfino | 17:55e6132c54a8 | 106 | brickled1 = 1; |
| ndelfino | 17:55e6132c54a8 | 107 | } |
| ndelfino | 8:deaedb59243e | 108 | }else{ |
| ndelfino | 11:f65806ee5833 | 109 | ledKeys[0] = -1; |
| ndelfino | 8:deaedb59243e | 110 | myled1_1 = 0; |
| ndelfino | 8:deaedb59243e | 111 | myled1_2 = 0; |
| ndelfino | 8:deaedb59243e | 112 | myled1_3 = 0; |
| ndelfino | 17:55e6132c54a8 | 113 | brickled1 = 0; |
| ndelfino | 8:deaedb59243e | 114 | } |
| ndelfino | 21:8b5d753b6bf5 | 115 | }else if(led == cLED2){ |
| ndelfino | 8:deaedb59243e | 116 | if(type == 1){ |
| ndelfino | 11:f65806ee5833 | 117 | ledKeys[1] = key; |
| ndelfino | 11:f65806ee5833 | 118 | myled2_1 = colors[0]; |
| ndelfino | 11:f65806ee5833 | 119 | myled2_2 = colors[1]; |
| ndelfino | 11:f65806ee5833 | 120 | myled2_3 = colors[2]; |
| ndelfino | 21:8b5d753b6bf5 | 121 | if(signature == SHARP){ |
| ndelfino | 17:55e6132c54a8 | 122 | brickled2 = 1; |
| ndelfino | 17:55e6132c54a8 | 123 | } |
| ndelfino | 4:bb9f50305401 | 124 | }else{ |
| ndelfino | 11:f65806ee5833 | 125 | ledKeys[1] = -1; |
| ndelfino | 8:deaedb59243e | 126 | myled2_1 = 0; |
| ndelfino | 8:deaedb59243e | 127 | myled2_2 = 0; |
| ndelfino | 8:deaedb59243e | 128 | myled2_3 = 0; |
| ndelfino | 17:55e6132c54a8 | 129 | brickled2 = 0; |
| ndelfino | 4:bb9f50305401 | 130 | } |
| ndelfino | 21:8b5d753b6bf5 | 131 | }else if(led == cLED3){ |
| ndelfino | 8:deaedb59243e | 132 | if(type == 1){ |
| ndelfino | 11:f65806ee5833 | 133 | ledKeys[2] = key; |
| ndelfino | 11:f65806ee5833 | 134 | myled3_1 = colors[0]; |
| ndelfino | 11:f65806ee5833 | 135 | myled3_2 = colors[1]; |
| ndelfino | 11:f65806ee5833 | 136 | myled3_3 = colors[2]; |
| ndelfino | 21:8b5d753b6bf5 | 137 | if(signature == SHARP){ |
| ndelfino | 17:55e6132c54a8 | 138 | brickled3 = 1; |
| ndelfino | 17:55e6132c54a8 | 139 | } |
| ndelfino | 8:deaedb59243e | 140 | }else{ |
| ndelfino | 11:f65806ee5833 | 141 | ledKeys[2] = -1; |
| ndelfino | 8:deaedb59243e | 142 | myled3_1 = 0; |
| ndelfino | 8:deaedb59243e | 143 | myled3_2 = 0; |
| ndelfino | 8:deaedb59243e | 144 | myled3_3 = 0; |
| ndelfino | 17:55e6132c54a8 | 145 | brickled3 = 0; |
| ndelfino | 8:deaedb59243e | 146 | } |
| ndelfino | 21:8b5d753b6bf5 | 147 | }else if(led == cLED4){ |
| ndelfino | 8:deaedb59243e | 148 | if(type == 1){ |
| ndelfino | 11:f65806ee5833 | 149 | ledKeys[3] = key; |
| ndelfino | 11:f65806ee5833 | 150 | myled4_1 = colors[0];; |
| ndelfino | 11:f65806ee5833 | 151 | myled4_2 = colors[1]; |
| ndelfino | 11:f65806ee5833 | 152 | myled4_3 = colors[2]; |
| ndelfino | 21:8b5d753b6bf5 | 153 | if(signature == SHARP){ |
| ndelfino | 17:55e6132c54a8 | 154 | brickled4 = 1; |
| ndelfino | 17:55e6132c54a8 | 155 | } |
| ndelfino | 8:deaedb59243e | 156 | }else{ |
| ndelfino | 11:f65806ee5833 | 157 | ledKeys[3] = -1; |
| ndelfino | 8:deaedb59243e | 158 | myled4_1 = 0; |
| ndelfino | 8:deaedb59243e | 159 | myled4_2 = 0; |
| ndelfino | 8:deaedb59243e | 160 | myled4_3 = 0; |
| ndelfino | 17:55e6132c54a8 | 161 | brickled4 = 0; |
| ndelfino | 8:deaedb59243e | 162 | } |
| ndelfino | 8:deaedb59243e | 163 | } |
| ndelfino | 8:deaedb59243e | 164 | |
| ndelfino | 4:bb9f50305401 | 165 | } |
