Cubic Hand project for EECS 249A course.

Dependencies:   MMA8451Q TSI cc3000_hostdriver_mbedsocket NVIC_set_all_priorities mbed Multi_WS2811

Committer:
robertbui
Date:
Thu Dec 11 06:08:13 2014 +0000
Revision:
32:bf5f545621aa
Parent:
24:554ea084eddf
Child:
33:ff87df17c55d
initial draft of gesture recognition.

Who changed what in which revision?

UserRevisionLine numberNew 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 32:bf5f545621aa 21 #define nLEDs 200 //MAX_LEDS_PER_STRIP;
robertbui 32:bf5f545621aa 22 #define nROWs 10 //number of rows per cube panel
robertbui 32:bf5f545621aa 23 #define nCOLs 10 //number of columns per cube panel
robertbui 32:bf5f545621aa 24 #define DATA_OUT_PIN1 2 // PTD2
robertbui 32:bf5f545621aa 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 32:bf5f545621aa 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 32:bf5f545621aa 55 /*Determines which LED should be lit based on its
robertbui 32:bf5f545621aa 56 cartesian coordinate. The origin is the led at
robertbui 32:bf5f545621aa 57 bottom left of panel 1 when using a three panel cube
robertbui 32:bf5f545621aa 58 ________
robertbui 32:bf5f545621aa 59 / /|
robertbui 32:bf5f545621aa 60 / 3 / |
robertbui 32:bf5f545621aa 61 /_______/ 2|
robertbui 32:bf5f545621aa 62 | | |
robertbui 32:bf5f545621aa 63 | 1 | /
robertbui 32:bf5f545621aa 64 | |
robertbui 32:bf5f545621aa 65 --------
robertbui 32:bf5f545621aa 66 */
robertbui 32:bf5f545621aa 67
joseoyola 16:a3de0c0dbe33 68 int LedCube::getLedIndex(int panel, int x, int y) {
joseoyola 8:c06f5046c9f7 69 if (panel == PANEL1) {
joseoyola 8:c06f5046c9f7 70 if (y % 2 == 0) {
joseoyola 8:c06f5046c9f7 71 return nCOLs*2 * y + x;
joseoyola 8:c06f5046c9f7 72 }
joseoyola 8:c06f5046c9f7 73 else {
joseoyola 8:c06f5046c9f7 74 return nCOLs*2 * y + nCOLs + ((nCOLs - 1) - x);
joseoyola 8:c06f5046c9f7 75 }
joseoyola 5:41f28ce7f5a9 76 }
joseoyola 8:c06f5046c9f7 77
joseoyola 8:c06f5046c9f7 78 if (panel == PANEL2) {
joseoyola 8:c06f5046c9f7 79 if (y % 2 == 0) {
joseoyola 8:c06f5046c9f7 80 return nCOLs*2 * y + nCOLs + x;
joseoyola 8:c06f5046c9f7 81 }
joseoyola 8:c06f5046c9f7 82 else {
joseoyola 8:c06f5046c9f7 83 return nCOLs*2 * y + ((nCOLs - 1) - x);
joseoyola 8:c06f5046c9f7 84 }
joseoyola 5:41f28ce7f5a9 85 }
joseoyola 8:c06f5046c9f7 86
joseoyola 8:c06f5046c9f7 87 if (panel == PANEL3) {
joseoyola 8:c06f5046c9f7 88 if (y % 2 == 0) {
joseoyola 8:c06f5046c9f7 89 return nCOLs * y + x;
joseoyola 8:c06f5046c9f7 90 }
joseoyola 8:c06f5046c9f7 91 else {
joseoyola 8:c06f5046c9f7 92 return nCOLs * y + ((nCOLs - 1) - x);
joseoyola 8:c06f5046c9f7 93 }
joseoyola 8:c06f5046c9f7 94 }
joseoyola 8:c06f5046c9f7 95
joseoyola 8:c06f5046c9f7 96 else return -1;
joseoyola 5:41f28ce7f5a9 97 }
joseoyola 5:41f28ce7f5a9 98
joseoyola 16:a3de0c0dbe33 99 void LedCube::updateLEDs(bool on, int size, int x, int y, int z) {
joseoyola 10:9d87a886f066 100 //Panel 1
joseoyola 24:554ea084eddf 101 double bright;
joseoyola 24:554ea084eddf 102 bright = 1.0 / ((y + 1) * (y + 1));
joseoyola 24:554ea084eddf 103 printf("Panel 1 Brightness: %f\r\n", bright);
joseoyola 24:554ea084eddf 104 for(int i = x; i < x + size; i++) {
joseoyola 24:554ea084eddf 105 for(int j = z; j < z + size; j++) {
joseoyola 24:554ea084eddf 106 int led = getLedIndex(PANEL1, i, j);
joseoyola 24:554ea084eddf 107 if(on) {
joseoyola 24:554ea084eddf 108 ledStrip1.setPixelColor(led, r*bright, g*bright, b*bright);
joseoyola 24:554ea084eddf 109 } else {
joseoyola 24:554ea084eddf 110 ledStrip1.setPixelColor(led, 0, 0, 0);
joseoyola 24:554ea084eddf 111 }
joseoyola 24:554ea084eddf 112 }
joseoyola 24:554ea084eddf 113 }
joseoyola 24:554ea084eddf 114
joseoyola 24:554ea084eddf 115 //Panel 2
joseoyola 24:554ea084eddf 116 bright = 1.0 / (((nCOLs-1) - x - (size-1) + 1) * ((nCOLs-1) - x - (size-1) + 1));
joseoyola 24:554ea084eddf 117 printf("Panel 2 Brightness: %f\r\n", bright);
joseoyola 24:554ea084eddf 118 for(int i = y; i < y + size; i++) {
joseoyola 24:554ea084eddf 119 for(int j = z; j < z + size; j++) {
joseoyola 24:554ea084eddf 120 int led = getLedIndex(PANEL2, i, j);
joseoyola 24:554ea084eddf 121 if(on) {
joseoyola 24:554ea084eddf 122 ledStrip1.setPixelColor(led, r*bright, g*bright, b*bright);
joseoyola 24:554ea084eddf 123 } else {
joseoyola 24:554ea084eddf 124 ledStrip1.setPixelColor(led, 0, 0, 0);
joseoyola 24:554ea084eddf 125 }
joseoyola 24:554ea084eddf 126 }
joseoyola 24:554ea084eddf 127 }
joseoyola 24:554ea084eddf 128
joseoyola 24:554ea084eddf 129 //Panel 3
joseoyola 24:554ea084eddf 130 bright = 1.0 / (((nCOLs-1) - z - (size-1) + 1) * ((nCOLs-1) - z - (size-1) + 1));
joseoyola 24:554ea084eddf 131 printf("Panel 3 Brightness: %f\r\n", bright);
joseoyola 24:554ea084eddf 132 for(int i = x; i < x + size; i++) {
joseoyola 24:554ea084eddf 133 for(int j = y; j < y + size; j++) {
joseoyola 24:554ea084eddf 134 int led = getLedIndex(PANEL3, i, j);
joseoyola 24:554ea084eddf 135 if(on) {
joseoyola 24:554ea084eddf 136 ledStrip2.setPixelColor(led, r*bright, g*bright, b*bright);
joseoyola 24:554ea084eddf 137 } else {
joseoyola 24:554ea084eddf 138 ledStrip2.setPixelColor(led, 0, 0, 0);
joseoyola 24:554ea084eddf 139 }
joseoyola 24:554ea084eddf 140 }
joseoyola 24:554ea084eddf 141 }
joseoyola 24:554ea084eddf 142 }
joseoyola 24:554ea084eddf 143
joseoyola 24:554ea084eddf 144 void LedCube::updateLEDsOld(bool on, int size, int x, int y, int z) {
joseoyola 24:554ea084eddf 145 //Panel 1
joseoyola 16:a3de0c0dbe33 146 if(y == 0) {
joseoyola 16:a3de0c0dbe33 147 for(int i = x; i < x + size; i++) {
joseoyola 16:a3de0c0dbe33 148 for(int j = z; j < z + size; j++) {
joseoyola 10:9d87a886f066 149 int led = getLedIndex(PANEL1, i, j);
joseoyola 10:9d87a886f066 150 if(on) {
joseoyola 10:9d87a886f066 151 ledStrip1.setPixelColor(led, r, g, b);
joseoyola 10:9d87a886f066 152 } else {
joseoyola 10:9d87a886f066 153 ledStrip1.setPixelColor(led, 0, 0, 0);
joseoyola 10:9d87a886f066 154 }
joseoyola 10:9d87a886f066 155 }
joseoyola 10:9d87a886f066 156 }
joseoyola 10:9d87a886f066 157 }
joseoyola 10:9d87a886f066 158
joseoyola 10:9d87a886f066 159 //Panel 2
joseoyola 16:a3de0c0dbe33 160 if(x + size - 1 == (nCOLs - 1)) {
joseoyola 16:a3de0c0dbe33 161 for(int i = y; i < y + size; i++) {
joseoyola 16:a3de0c0dbe33 162 for(int j = z; j < z + size; j++) {
joseoyola 10:9d87a886f066 163 int led = getLedIndex(PANEL2, i, j);
joseoyola 10:9d87a886f066 164 if(on) {
joseoyola 20:c2bc970bbff8 165 ledStrip1.setPixelColor(led, r, g, b);
joseoyola 10:9d87a886f066 166 } else {
joseoyola 20:c2bc970bbff8 167 ledStrip1.setPixelColor(led, 0, 0, 0);
joseoyola 10:9d87a886f066 168 }
joseoyola 10:9d87a886f066 169 }
joseoyola 10:9d87a886f066 170 }
joseoyola 10:9d87a886f066 171 }
joseoyola 10:9d87a886f066 172
joseoyola 10:9d87a886f066 173 //Panel 3
joseoyola 16:a3de0c0dbe33 174 if(z + size - 1 == (nCOLs - 1)) {
joseoyola 16:a3de0c0dbe33 175 for(int i = x; i < x + size; i++) {
joseoyola 16:a3de0c0dbe33 176 for(int j = y; j < y + size; j++) {
joseoyola 10:9d87a886f066 177 int led = getLedIndex(PANEL3, i, j);
joseoyola 10:9d87a886f066 178 if(on) {
joseoyola 20:c2bc970bbff8 179 ledStrip2.setPixelColor(led, r, g, b);
joseoyola 10:9d87a886f066 180 } else {
joseoyola 20:c2bc970bbff8 181 ledStrip2.setPixelColor(led, 0, 0, 0);
joseoyola 10:9d87a886f066 182 }
joseoyola 10:9d87a886f066 183 }
joseoyola 10:9d87a886f066 184 }
joseoyola 10:9d87a886f066 185 }
joseoyola 10:9d87a886f066 186 }
joseoyola 10:9d87a886f066 187
joseoyola 10:9d87a886f066 188
joseoyola 16:a3de0c0dbe33 189 void LedCube::cubeUpdate() {
joseoyola 16:a3de0c0dbe33 190 updateLEDs(false, prevSize, prevPos[X], prevPos[Y], prevPos[Z]); //Turn off LEDs from previous state
joseoyola 16:a3de0c0dbe33 191 updateLEDs(true, size, pos[X], pos[Y], pos[Z]); //Turn on new LEDs for new state
joseoyola 16:a3de0c0dbe33 192 prevSize = size;
joseoyola 16:a3de0c0dbe33 193 prevPos[X] = pos[X];
joseoyola 16:a3de0c0dbe33 194 prevPos[Y] = pos[Y];
joseoyola 16:a3de0c0dbe33 195 prevPos[Z] = pos[Z];
joseoyola 16:a3de0c0dbe33 196 ledStrip1.show();
joseoyola 16:a3de0c0dbe33 197 ledStrip2.show();
joseoyola 16:a3de0c0dbe33 198 ledStrip1.startDMA();
joseoyola 16:a3de0c0dbe33 199 ledStrip2.startDMA();
joseoyola 16:a3de0c0dbe33 200 printf("Position: %d, %d, %d \t Size: %d\r\n", pos[X], pos[Y], pos[Z], size);
joseoyola 10:9d87a886f066 201 }
joseoyola 10:9d87a886f066 202
joseoyola 16:a3de0c0dbe33 203 int LedCube::move(int deltaX, int deltaY, int deltaZ) {
joseoyola 16:a3de0c0dbe33 204 int retVal = -1;
joseoyola 10:9d87a886f066 205 //Moving in X direction
joseoyola 16:a3de0c0dbe33 206 if(pos[Y] == 0 || pos[Z] + size - 1 == (nCOLs - 1)) { //Making sure square is "stuck" to panel 1 or 3
joseoyola 16:a3de0c0dbe33 207 if((pos[X] + size + deltaX - 1) < nCOLs && (pos[X] + deltaX) >= 0) {
joseoyola 10:9d87a886f066 208 pos[X] += deltaX;
joseoyola 16:a3de0c0dbe33 209 if (deltaX != 0) retVal = 1;
joseoyola 10:9d87a886f066 210 }
joseoyola 10:9d87a886f066 211 }
joseoyola 10:9d87a886f066 212
joseoyola 10:9d87a886f066 213 //Moving in Y direction
joseoyola 16:a3de0c0dbe33 214 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 215 if((pos[Y] + size + deltaY - 1) < nCOLs && (pos[Y] + deltaY) >= 0) {
joseoyola 10:9d87a886f066 216 pos[Y] += deltaY;
joseoyola 16:a3de0c0dbe33 217 if (deltaY != 0) retVal = 1;
joseoyola 10:9d87a886f066 218 }
joseoyola 10:9d87a886f066 219 }
joseoyola 10:9d87a886f066 220
joseoyola 10:9d87a886f066 221 //Moving in Z direction
joseoyola 16:a3de0c0dbe33 222 if(pos[X] + size - 1 == (nCOLs - 1) || pos[Y] == 0) {//Making sure square is "stuck" to panel 1 or 2
joseoyola 16:a3de0c0dbe33 223 if((pos[Z] + size + deltaZ - 1) < nCOLs && (pos[Z] + deltaZ) >= 0) {
joseoyola 10:9d87a886f066 224 pos[Z] += deltaZ;
joseoyola 16:a3de0c0dbe33 225 if (deltaZ != 0) retVal = 1;
joseoyola 10:9d87a886f066 226 }
joseoyola 10:9d87a886f066 227 }
joseoyola 16:a3de0c0dbe33 228 return retVal;
joseoyola 16:a3de0c0dbe33 229 }
joseoyola 16:a3de0c0dbe33 230
joseoyola 16:a3de0c0dbe33 231 void LedCube::changeColor(float hue){
joseoyola 20:c2bc970bbff8 232 Colors::HSBtoRGB(hue, saturation, brightness, &r, &g, &b);
joseoyola 16:a3de0c0dbe33 233 }
joseoyola 16:a3de0c0dbe33 234
joseoyola 16:a3de0c0dbe33 235 void LedCube::changeSize(int newSize) {
joseoyola 16:a3de0c0dbe33 236 if((pos[X] + newSize) < nCOLs && (pos[Y] + newSize) < nCOLs && (pos[Z] + newSize) < nCOLs) {
joseoyola 16:a3de0c0dbe33 237 size = newSize;
joseoyola 16:a3de0c0dbe33 238 }
robertbui 32:bf5f545621aa 239 }
robertbui 32:bf5f545621aa 240
robertbui 32:bf5f545621aa 241 void LedCube::UpdateCube(int size, int deltaX, int deltaY, int deltaZ, float hue) {
robertbui 32:bf5f545621aa 242 changeSize(size);
robertbui 32:bf5f545621aa 243 move(deltaX, deltaY, deltaZ);
robertbui 32:bf5f545621aa 244 changeColor(hue);
robertbui 32:bf5f545621aa 245 cubeUpdate();
robertbui 32:bf5f545621aa 246 }
robertbui 32:bf5f545621aa 247
robertbui 32:bf5f545621aa 248
robertbui 32:bf5f545621aa 249