Cubic Hand project for EECS 249A course.
Dependencies: MMA8451Q TSI cc3000_hostdriver_mbedsocket NVIC_set_all_priorities mbed Multi_WS2811
ledCube.cpp@30:0933d7f2fe78, 2014-12-11 (annotated)
- Committer:
- robertbui
- Date:
- Thu Dec 11 05:40:39 2014 +0000
- Revision:
- 30:0933d7f2fe78
- Parent:
- 24:554ea084eddf
- Child:
- 31:0e341d81ea00
Added updateCube function.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joseoyola | 2:45b90e8d7d82 | 1 | /* |
joseoyola | 16:a3de0c0dbe33 | 2 | * Neopixel LED Cube library |
joseoyola | 5:41f28ce7f5a9 | 3 | * by Robert Bui and Jose Oyola |
joseoyola | 2:45b90e8d7d82 | 4 | * UC Berkeley 2014 |
joseoyola | 2:45b90e8d7d82 | 5 | */ |
joseoyola | 2:45b90e8d7d82 | 6 | |
joseoyola | 2:45b90e8d7d82 | 7 | #include "main.h" |
joseoyola | 2:45b90e8d7d82 | 8 | #include "mbed.h" |
joseoyola | 5:41f28ce7f5a9 | 9 | #include "WS2811.h" |
joseoyola | 5:41f28ce7f5a9 | 10 | #include "Colors.h" |
joseoyola | 5:41f28ce7f5a9 | 11 | #include "TSISensor.h" |
joseoyola | 5:41f28ce7f5a9 | 12 | #include "MMA8451Q.h" |
joseoyola | 16:a3de0c0dbe33 | 13 | #include "ledCube.h" |
joseoyola | 5:41f28ce7f5a9 | 14 | |
joseoyola | 5:41f28ce7f5a9 | 15 | |
joseoyola | 5:41f28ce7f5a9 | 16 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
joseoyola | 5:41f28ce7f5a9 | 17 | |
joseoyola | 5:41f28ce7f5a9 | 18 | #define PANEL1 0 |
joseoyola | 5:41f28ce7f5a9 | 19 | #define PANEL2 1 |
joseoyola | 5:41f28ce7f5a9 | 20 | #define PANEL3 2 |
robertbui | 30:0933d7f2fe78 | 21 | #define nLEDs 200 //MAX_LEDS_PER_STRIP; |
robertbui | 30:0933d7f2fe78 | 22 | #define nROWs 10 //number of rows per cube panel |
robertbui | 30:0933d7f2fe78 | 23 | #define nCOLs 10 //number of columns per cube panel |
robertbui | 30:0933d7f2fe78 | 24 | #define DATA_OUT_PIN1 2 // PTD2 |
robertbui | 30:0933d7f2fe78 | 25 | #define DATA_OUT_PIN2 3 // PTD3 |
joseoyola | 5:41f28ce7f5a9 | 26 | |
joseoyola | 5:41f28ce7f5a9 | 27 | |
joseoyola | 20:c2bc970bbff8 | 28 | LedCube::LedCube():X(0),Y(1),Z(2),ledStrip1(nLEDs, DATA_OUT_PIN1),ledStrip2(nLEDs, DATA_OUT_PIN2) |
joseoyola | 16:a3de0c0dbe33 | 29 | { |
joseoyola | 16:a3de0c0dbe33 | 30 | } |
joseoyola | 5:41f28ce7f5a9 | 31 | |
joseoyola | 16:a3de0c0dbe33 | 32 | LedCube::~LedCube() |
joseoyola | 16:a3de0c0dbe33 | 33 | { |
joseoyola | 16:a3de0c0dbe33 | 34 | } |
joseoyola | 5:41f28ce7f5a9 | 35 | |
robertbui | 30:0933d7f2fe78 | 36 | /*Sets the initial size and position of the lighted cube*/ |
joseoyola | 16:a3de0c0dbe33 | 37 | void LedCube::Init(int x, int y, int z) |
joseoyola | 16:a3de0c0dbe33 | 38 | { |
joseoyola | 16:a3de0c0dbe33 | 39 | size = 2; |
joseoyola | 16:a3de0c0dbe33 | 40 | prevSize = size; |
joseoyola | 16:a3de0c0dbe33 | 41 | pos[X] = prevPos[X] = x; |
joseoyola | 16:a3de0c0dbe33 | 42 | pos[Y] = prevPos[Y] = y; |
joseoyola | 16:a3de0c0dbe33 | 43 | pos[Z] = prevPos[Z] = z; |
joseoyola | 16:a3de0c0dbe33 | 44 | r = 255*0.1; |
joseoyola | 16:a3de0c0dbe33 | 45 | g = 255*0.1; |
joseoyola | 16:a3de0c0dbe33 | 46 | b = 255*0.1; |
joseoyola | 5:41f28ce7f5a9 | 47 | |
joseoyola | 24:554ea084eddf | 48 | brightness = 0.5; |
joseoyola | 16:a3de0c0dbe33 | 49 | saturation = 1.0; |
joseoyola | 16:a3de0c0dbe33 | 50 | |
joseoyola | 16:a3de0c0dbe33 | 51 | ledStrip1.begin(); |
joseoyola | 16:a3de0c0dbe33 | 52 | ledStrip2.begin(); |
joseoyola | 16:a3de0c0dbe33 | 53 | } |
joseoyola | 5:41f28ce7f5a9 | 54 | |
robertbui | 30:0933d7f2fe78 | 55 | /*Determines which LED should be lit based on its |
robertbui | 30:0933d7f2fe78 | 56 | cartesian coordinate. The origin is the led at |
robertbui | 30:0933d7f2fe78 | 57 | bottom left of panel 0 when using a three cube panel. */ |
robertbui | 30:0933d7f2fe78 | 58 | |
joseoyola | 16:a3de0c0dbe33 | 59 | int LedCube::getLedIndex(int panel, int x, int y) { |
joseoyola | 8:c06f5046c9f7 | 60 | if (panel == PANEL1) { |
joseoyola | 8:c06f5046c9f7 | 61 | if (y % 2 == 0) { |
joseoyola | 8:c06f5046c9f7 | 62 | return nCOLs*2 * y + x; |
joseoyola | 8:c06f5046c9f7 | 63 | } |
joseoyola | 8:c06f5046c9f7 | 64 | else { |
joseoyola | 8:c06f5046c9f7 | 65 | return nCOLs*2 * y + nCOLs + ((nCOLs - 1) - x); |
joseoyola | 8:c06f5046c9f7 | 66 | } |
joseoyola | 5:41f28ce7f5a9 | 67 | } |
joseoyola | 8:c06f5046c9f7 | 68 | |
joseoyola | 8:c06f5046c9f7 | 69 | if (panel == PANEL2) { |
joseoyola | 8:c06f5046c9f7 | 70 | if (y % 2 == 0) { |
joseoyola | 8:c06f5046c9f7 | 71 | return nCOLs*2 * y + nCOLs + x; |
joseoyola | 8:c06f5046c9f7 | 72 | } |
joseoyola | 8:c06f5046c9f7 | 73 | else { |
joseoyola | 8:c06f5046c9f7 | 74 | return nCOLs*2 * y + ((nCOLs - 1) - x); |
joseoyola | 8:c06f5046c9f7 | 75 | } |
joseoyola | 5:41f28ce7f5a9 | 76 | } |
joseoyola | 8:c06f5046c9f7 | 77 | |
joseoyola | 8:c06f5046c9f7 | 78 | if (panel == PANEL3) { |
joseoyola | 8:c06f5046c9f7 | 79 | if (y % 2 == 0) { |
joseoyola | 8:c06f5046c9f7 | 80 | return nCOLs * y + x; |
joseoyola | 8:c06f5046c9f7 | 81 | } |
joseoyola | 8:c06f5046c9f7 | 82 | else { |
joseoyola | 8:c06f5046c9f7 | 83 | return nCOLs * y + ((nCOLs - 1) - x); |
joseoyola | 8:c06f5046c9f7 | 84 | } |
joseoyola | 8:c06f5046c9f7 | 85 | } |
joseoyola | 8:c06f5046c9f7 | 86 | |
joseoyola | 8:c06f5046c9f7 | 87 | else return -1; |
joseoyola | 5:41f28ce7f5a9 | 88 | } |
joseoyola | 5:41f28ce7f5a9 | 89 | |
joseoyola | 16:a3de0c0dbe33 | 90 | void LedCube::updateLEDs(bool on, int size, int x, int y, int z) { |
joseoyola | 10:9d87a886f066 | 91 | //Panel 1 |
joseoyola | 24:554ea084eddf | 92 | double bright; |
joseoyola | 24:554ea084eddf | 93 | bright = 1.0 / ((y + 1) * (y + 1)); |
joseoyola | 24:554ea084eddf | 94 | printf("Panel 1 Brightness: %f\r\n", bright); |
joseoyola | 24:554ea084eddf | 95 | for(int i = x; i < x + size; i++) { |
joseoyola | 24:554ea084eddf | 96 | for(int j = z; j < z + size; j++) { |
joseoyola | 24:554ea084eddf | 97 | int led = getLedIndex(PANEL1, i, j); |
joseoyola | 24:554ea084eddf | 98 | if(on) { |
joseoyola | 24:554ea084eddf | 99 | ledStrip1.setPixelColor(led, r*bright, g*bright, b*bright); |
joseoyola | 24:554ea084eddf | 100 | } else { |
joseoyola | 24:554ea084eddf | 101 | ledStrip1.setPixelColor(led, 0, 0, 0); |
joseoyola | 24:554ea084eddf | 102 | } |
joseoyola | 24:554ea084eddf | 103 | } |
joseoyola | 24:554ea084eddf | 104 | } |
joseoyola | 24:554ea084eddf | 105 | |
joseoyola | 24:554ea084eddf | 106 | //Panel 2 |
joseoyola | 24:554ea084eddf | 107 | bright = 1.0 / (((nCOLs-1) - x - (size-1) + 1) * ((nCOLs-1) - x - (size-1) + 1)); |
joseoyola | 24:554ea084eddf | 108 | printf("Panel 2 Brightness: %f\r\n", bright); |
joseoyola | 24:554ea084eddf | 109 | for(int i = y; i < y + size; i++) { |
joseoyola | 24:554ea084eddf | 110 | for(int j = z; j < z + size; j++) { |
joseoyola | 24:554ea084eddf | 111 | int led = getLedIndex(PANEL2, i, j); |
joseoyola | 24:554ea084eddf | 112 | if(on) { |
joseoyola | 24:554ea084eddf | 113 | ledStrip1.setPixelColor(led, r*bright, g*bright, b*bright); |
joseoyola | 24:554ea084eddf | 114 | } else { |
joseoyola | 24:554ea084eddf | 115 | ledStrip1.setPixelColor(led, 0, 0, 0); |
joseoyola | 24:554ea084eddf | 116 | } |
joseoyola | 24:554ea084eddf | 117 | } |
joseoyola | 24:554ea084eddf | 118 | } |
joseoyola | 24:554ea084eddf | 119 | |
joseoyola | 24:554ea084eddf | 120 | //Panel 3 |
joseoyola | 24:554ea084eddf | 121 | bright = 1.0 / (((nCOLs-1) - z - (size-1) + 1) * ((nCOLs-1) - z - (size-1) + 1)); |
joseoyola | 24:554ea084eddf | 122 | printf("Panel 3 Brightness: %f\r\n", bright); |
joseoyola | 24:554ea084eddf | 123 | for(int i = x; i < x + size; i++) { |
joseoyola | 24:554ea084eddf | 124 | for(int j = y; j < y + size; j++) { |
joseoyola | 24:554ea084eddf | 125 | int led = getLedIndex(PANEL3, i, j); |
joseoyola | 24:554ea084eddf | 126 | if(on) { |
joseoyola | 24:554ea084eddf | 127 | ledStrip2.setPixelColor(led, r*bright, g*bright, b*bright); |
joseoyola | 24:554ea084eddf | 128 | } else { |
joseoyola | 24:554ea084eddf | 129 | ledStrip2.setPixelColor(led, 0, 0, 0); |
joseoyola | 24:554ea084eddf | 130 | } |
joseoyola | 24:554ea084eddf | 131 | } |
joseoyola | 24:554ea084eddf | 132 | } |
joseoyola | 24:554ea084eddf | 133 | } |
joseoyola | 24:554ea084eddf | 134 | |
joseoyola | 24:554ea084eddf | 135 | void LedCube::updateLEDsOld(bool on, int size, int x, int y, int z) { |
joseoyola | 24:554ea084eddf | 136 | //Panel 1 |
joseoyola | 16:a3de0c0dbe33 | 137 | if(y == 0) { |
joseoyola | 16:a3de0c0dbe33 | 138 | for(int i = x; i < x + size; i++) { |
joseoyola | 16:a3de0c0dbe33 | 139 | for(int j = z; j < z + size; j++) { |
joseoyola | 10:9d87a886f066 | 140 | int led = getLedIndex(PANEL1, i, j); |
joseoyola | 10:9d87a886f066 | 141 | if(on) { |
joseoyola | 10:9d87a886f066 | 142 | ledStrip1.setPixelColor(led, r, g, b); |
joseoyola | 10:9d87a886f066 | 143 | } else { |
joseoyola | 10:9d87a886f066 | 144 | ledStrip1.setPixelColor(led, 0, 0, 0); |
joseoyola | 10:9d87a886f066 | 145 | } |
joseoyola | 10:9d87a886f066 | 146 | } |
joseoyola | 10:9d87a886f066 | 147 | } |
joseoyola | 10:9d87a886f066 | 148 | } |
joseoyola | 10:9d87a886f066 | 149 | |
joseoyola | 10:9d87a886f066 | 150 | //Panel 2 |
joseoyola | 16:a3de0c0dbe33 | 151 | if(x + size - 1 == (nCOLs - 1)) { |
joseoyola | 16:a3de0c0dbe33 | 152 | for(int i = y; i < y + size; i++) { |
joseoyola | 16:a3de0c0dbe33 | 153 | for(int j = z; j < z + size; j++) { |
joseoyola | 10:9d87a886f066 | 154 | int led = getLedIndex(PANEL2, i, j); |
joseoyola | 10:9d87a886f066 | 155 | if(on) { |
joseoyola | 20:c2bc970bbff8 | 156 | ledStrip1.setPixelColor(led, r, g, b); |
joseoyola | 10:9d87a886f066 | 157 | } else { |
joseoyola | 20:c2bc970bbff8 | 158 | ledStrip1.setPixelColor(led, 0, 0, 0); |
joseoyola | 10:9d87a886f066 | 159 | } |
joseoyola | 10:9d87a886f066 | 160 | } |
joseoyola | 10:9d87a886f066 | 161 | } |
joseoyola | 10:9d87a886f066 | 162 | } |
joseoyola | 10:9d87a886f066 | 163 | |
joseoyola | 10:9d87a886f066 | 164 | //Panel 3 |
joseoyola | 16:a3de0c0dbe33 | 165 | if(z + size - 1 == (nCOLs - 1)) { |
joseoyola | 16:a3de0c0dbe33 | 166 | for(int i = x; i < x + size; i++) { |
joseoyola | 16:a3de0c0dbe33 | 167 | for(int j = y; j < y + size; j++) { |
joseoyola | 10:9d87a886f066 | 168 | int led = getLedIndex(PANEL3, i, j); |
joseoyola | 10:9d87a886f066 | 169 | if(on) { |
joseoyola | 20:c2bc970bbff8 | 170 | ledStrip2.setPixelColor(led, r, g, b); |
joseoyola | 10:9d87a886f066 | 171 | } else { |
joseoyola | 20:c2bc970bbff8 | 172 | ledStrip2.setPixelColor(led, 0, 0, 0); |
joseoyola | 10:9d87a886f066 | 173 | } |
joseoyola | 10:9d87a886f066 | 174 | } |
joseoyola | 10:9d87a886f066 | 175 | } |
joseoyola | 10:9d87a886f066 | 176 | } |
joseoyola | 10:9d87a886f066 | 177 | } |
joseoyola | 10:9d87a886f066 | 178 | |
joseoyola | 10:9d87a886f066 | 179 | |
joseoyola | 16:a3de0c0dbe33 | 180 | void LedCube::cubeUpdate() { |
joseoyola | 16:a3de0c0dbe33 | 181 | updateLEDs(false, prevSize, prevPos[X], prevPos[Y], prevPos[Z]); //Turn off LEDs from previous state |
joseoyola | 16:a3de0c0dbe33 | 182 | updateLEDs(true, size, pos[X], pos[Y], pos[Z]); //Turn on new LEDs for new state |
joseoyola | 16:a3de0c0dbe33 | 183 | prevSize = size; |
joseoyola | 16:a3de0c0dbe33 | 184 | prevPos[X] = pos[X]; |
joseoyola | 16:a3de0c0dbe33 | 185 | prevPos[Y] = pos[Y]; |
joseoyola | 16:a3de0c0dbe33 | 186 | prevPos[Z] = pos[Z]; |
joseoyola | 16:a3de0c0dbe33 | 187 | ledStrip1.show(); |
joseoyola | 16:a3de0c0dbe33 | 188 | ledStrip2.show(); |
joseoyola | 16:a3de0c0dbe33 | 189 | ledStrip1.startDMA(); |
joseoyola | 16:a3de0c0dbe33 | 190 | ledStrip2.startDMA(); |
joseoyola | 16:a3de0c0dbe33 | 191 | printf("Position: %d, %d, %d \t Size: %d\r\n", pos[X], pos[Y], pos[Z], size); |
joseoyola | 10:9d87a886f066 | 192 | } |
joseoyola | 10:9d87a886f066 | 193 | |
joseoyola | 16:a3de0c0dbe33 | 194 | int LedCube::move(int deltaX, int deltaY, int deltaZ) { |
joseoyola | 16:a3de0c0dbe33 | 195 | int retVal = -1; |
joseoyola | 10:9d87a886f066 | 196 | //Moving in X direction |
joseoyola | 16:a3de0c0dbe33 | 197 | if(pos[Y] == 0 || pos[Z] + size - 1 == (nCOLs - 1)) { //Making sure square is "stuck" to panel 1 or 3 |
joseoyola | 16:a3de0c0dbe33 | 198 | if((pos[X] + size + deltaX - 1) < nCOLs && (pos[X] + deltaX) >= 0) { |
joseoyola | 10:9d87a886f066 | 199 | pos[X] += deltaX; |
joseoyola | 16:a3de0c0dbe33 | 200 | if (deltaX != 0) retVal = 1; |
joseoyola | 10:9d87a886f066 | 201 | } |
joseoyola | 10:9d87a886f066 | 202 | } |
joseoyola | 10:9d87a886f066 | 203 | |
joseoyola | 10:9d87a886f066 | 204 | //Moving in Y direction |
joseoyola | 16:a3de0c0dbe33 | 205 | if(pos[X] + size - 1 == (nCOLs - 1) || pos[Z] + size - 1 == (nCOLs - 1)) {//Making sure square is "stuck" to panel 2 or 3 |
joseoyola | 16:a3de0c0dbe33 | 206 | if((pos[Y] + size + deltaY - 1) < nCOLs && (pos[Y] + deltaY) >= 0) { |
joseoyola | 10:9d87a886f066 | 207 | pos[Y] += deltaY; |
joseoyola | 16:a3de0c0dbe33 | 208 | if (deltaY != 0) retVal = 1; |
joseoyola | 10:9d87a886f066 | 209 | } |
joseoyola | 10:9d87a886f066 | 210 | } |
joseoyola | 10:9d87a886f066 | 211 | |
joseoyola | 10:9d87a886f066 | 212 | //Moving in Z direction |
joseoyola | 16:a3de0c0dbe33 | 213 | if(pos[X] + size - 1 == (nCOLs - 1) || pos[Y] == 0) {//Making sure square is "stuck" to panel 1 or 2 |
joseoyola | 16:a3de0c0dbe33 | 214 | if((pos[Z] + size + deltaZ - 1) < nCOLs && (pos[Z] + deltaZ) >= 0) { |
joseoyola | 10:9d87a886f066 | 215 | pos[Z] += deltaZ; |
joseoyola | 16:a3de0c0dbe33 | 216 | if (deltaZ != 0) retVal = 1; |
joseoyola | 10:9d87a886f066 | 217 | } |
joseoyola | 10:9d87a886f066 | 218 | } |
joseoyola | 16:a3de0c0dbe33 | 219 | return retVal; |
joseoyola | 16:a3de0c0dbe33 | 220 | } |
joseoyola | 16:a3de0c0dbe33 | 221 | |
joseoyola | 16:a3de0c0dbe33 | 222 | void LedCube::changeColor(float hue){ |
joseoyola | 20:c2bc970bbff8 | 223 | Colors::HSBtoRGB(hue, saturation, brightness, &r, &g, &b); |
joseoyola | 16:a3de0c0dbe33 | 224 | } |
joseoyola | 16:a3de0c0dbe33 | 225 | |
joseoyola | 16:a3de0c0dbe33 | 226 | void LedCube::changeSize(int newSize) { |
joseoyola | 16:a3de0c0dbe33 | 227 | if((pos[X] + newSize) < nCOLs && (pos[Y] + newSize) < nCOLs && (pos[Z] + newSize) < nCOLs) { |
joseoyola | 16:a3de0c0dbe33 | 228 | size = newSize; |
joseoyola | 16:a3de0c0dbe33 | 229 | } |
robertbui | 30:0933d7f2fe78 | 230 | } |
robertbui | 30:0933d7f2fe78 | 231 | |
robertbui | 30:0933d7f2fe78 | 232 | void LedCube::UpdateCube(int size, int deltaX, int deltaY, int deltaZ, float hue) { |
robertbui | 30:0933d7f2fe78 | 233 | changeSize(size); |
robertbui | 30:0933d7f2fe78 | 234 | move(deltaX, deltaY, deltaZ); |
robertbui | 30:0933d7f2fe78 | 235 | changeColor(hue); |
robertbui | 30:0933d7f2fe78 | 236 | cubeUpdate(); |
robertbui | 30:0933d7f2fe78 | 237 | } |
robertbui | 30:0933d7f2fe78 | 238 | |
robertbui | 30:0933d7f2fe78 | 239 | |
robertbui | 30:0933d7f2fe78 | 240 |