Cubic Hand project for EECS 249A course.

Dependencies:   MMA8451Q TSI cc3000_hostdriver_mbedsocket NVIC_set_all_priorities mbed Multi_WS2811

Committer:
joseoyola
Date:
Wed Dec 10 22:45:49 2014 +0000
Revision:
20:c2bc970bbff8
Parent:
16:a3de0c0dbe33
Child:
24:554ea084eddf
Changed Neopixel library to support 240 leds/strip.

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
joseoyola 20:c2bc970bbff8 21 #define nLEDs 200//MAX_LEDS_PER_STRIP;
joseoyola 16:a3de0c0dbe33 22 #define nROWs 10
joseoyola 16:a3de0c0dbe33 23 #define nCOLs 10
joseoyola 16:a3de0c0dbe33 24 #define DATA_OUT_PIN1 2 // PTD2
joseoyola 20:c2bc970bbff8 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
joseoyola 16:a3de0c0dbe33 36 void LedCube::Init(int x, int y, int z)
joseoyola 16:a3de0c0dbe33 37 {
joseoyola 16:a3de0c0dbe33 38 size = 2;
joseoyola 16:a3de0c0dbe33 39 prevSize = size;
joseoyola 16:a3de0c0dbe33 40 pos[X] = prevPos[X] = x;
joseoyola 16:a3de0c0dbe33 41 pos[Y] = prevPos[Y] = y;
joseoyola 16:a3de0c0dbe33 42 pos[Z] = prevPos[Z] = z;
joseoyola 16:a3de0c0dbe33 43 r = 255*0.1;
joseoyola 16:a3de0c0dbe33 44 g = 255*0.1;
joseoyola 16:a3de0c0dbe33 45 b = 255*0.1;
joseoyola 5:41f28ce7f5a9 46
joseoyola 16:a3de0c0dbe33 47 brightness = 0.05;
joseoyola 16:a3de0c0dbe33 48 saturation = 1.0;
joseoyola 16:a3de0c0dbe33 49
joseoyola 16:a3de0c0dbe33 50 ledStrip1.begin();
joseoyola 16:a3de0c0dbe33 51 ledStrip2.begin();
joseoyola 16:a3de0c0dbe33 52 }
joseoyola 5:41f28ce7f5a9 53
joseoyola 16:a3de0c0dbe33 54 int LedCube::getLedIndex(int panel, int x, int y) {
joseoyola 8:c06f5046c9f7 55 if (panel == PANEL1) {
joseoyola 8:c06f5046c9f7 56 if (y % 2 == 0) {
joseoyola 8:c06f5046c9f7 57 return nCOLs*2 * y + x;
joseoyola 8:c06f5046c9f7 58 }
joseoyola 8:c06f5046c9f7 59 else {
joseoyola 8:c06f5046c9f7 60 return nCOLs*2 * y + nCOLs + ((nCOLs - 1) - x);
joseoyola 8:c06f5046c9f7 61 }
joseoyola 5:41f28ce7f5a9 62 }
joseoyola 8:c06f5046c9f7 63
joseoyola 8:c06f5046c9f7 64 if (panel == PANEL2) {
joseoyola 8:c06f5046c9f7 65 if (y % 2 == 0) {
joseoyola 8:c06f5046c9f7 66 return nCOLs*2 * y + nCOLs + x;
joseoyola 8:c06f5046c9f7 67 }
joseoyola 8:c06f5046c9f7 68 else {
joseoyola 8:c06f5046c9f7 69 return nCOLs*2 * y + ((nCOLs - 1) - x);
joseoyola 8:c06f5046c9f7 70 }
joseoyola 5:41f28ce7f5a9 71 }
joseoyola 8:c06f5046c9f7 72
joseoyola 8:c06f5046c9f7 73 if (panel == PANEL3) {
joseoyola 8:c06f5046c9f7 74 if (y % 2 == 0) {
joseoyola 8:c06f5046c9f7 75 return nCOLs * y + x;
joseoyola 8:c06f5046c9f7 76 }
joseoyola 8:c06f5046c9f7 77 else {
joseoyola 8:c06f5046c9f7 78 return nCOLs * y + ((nCOLs - 1) - x);
joseoyola 8:c06f5046c9f7 79 }
joseoyola 8:c06f5046c9f7 80 }
joseoyola 8:c06f5046c9f7 81
joseoyola 8:c06f5046c9f7 82 else return -1;
joseoyola 5:41f28ce7f5a9 83 }
joseoyola 5:41f28ce7f5a9 84
joseoyola 16:a3de0c0dbe33 85 void LedCube::updateLEDs(bool on, int size, int x, int y, int z) {
joseoyola 10:9d87a886f066 86 //Panel 1
joseoyola 16:a3de0c0dbe33 87 if(y == 0) {
joseoyola 16:a3de0c0dbe33 88 for(int i = x; i < x + size; i++) {
joseoyola 16:a3de0c0dbe33 89 for(int j = z; j < z + size; j++) {
joseoyola 10:9d87a886f066 90 int led = getLedIndex(PANEL1, i, j);
joseoyola 10:9d87a886f066 91 if(on) {
joseoyola 10:9d87a886f066 92 ledStrip1.setPixelColor(led, r, g, b);
joseoyola 10:9d87a886f066 93 } else {
joseoyola 10:9d87a886f066 94 ledStrip1.setPixelColor(led, 0, 0, 0);
joseoyola 10:9d87a886f066 95 }
joseoyola 10:9d87a886f066 96 }
joseoyola 10:9d87a886f066 97 }
joseoyola 10:9d87a886f066 98 }
joseoyola 10:9d87a886f066 99
joseoyola 10:9d87a886f066 100 //Panel 2
joseoyola 16:a3de0c0dbe33 101 if(x + size - 1 == (nCOLs - 1)) {
joseoyola 16:a3de0c0dbe33 102 for(int i = y; i < y + size; i++) {
joseoyola 16:a3de0c0dbe33 103 for(int j = z; j < z + size; j++) {
joseoyola 10:9d87a886f066 104 int led = getLedIndex(PANEL2, i, j);
joseoyola 10:9d87a886f066 105 if(on) {
joseoyola 20:c2bc970bbff8 106 ledStrip1.setPixelColor(led, r, g, b);
joseoyola 10:9d87a886f066 107 } else {
joseoyola 20:c2bc970bbff8 108 ledStrip1.setPixelColor(led, 0, 0, 0);
joseoyola 10:9d87a886f066 109 }
joseoyola 10:9d87a886f066 110 }
joseoyola 10:9d87a886f066 111 }
joseoyola 10:9d87a886f066 112 }
joseoyola 10:9d87a886f066 113
joseoyola 10:9d87a886f066 114 //Panel 3
joseoyola 16:a3de0c0dbe33 115 if(z + size - 1 == (nCOLs - 1)) {
joseoyola 16:a3de0c0dbe33 116 for(int i = x; i < x + size; i++) {
joseoyola 16:a3de0c0dbe33 117 for(int j = y; j < y + size; j++) {
joseoyola 10:9d87a886f066 118 int led = getLedIndex(PANEL3, i, j);
joseoyola 10:9d87a886f066 119 if(on) {
joseoyola 20:c2bc970bbff8 120 ledStrip2.setPixelColor(led, r, g, b);
joseoyola 10:9d87a886f066 121 } else {
joseoyola 20:c2bc970bbff8 122 ledStrip2.setPixelColor(led, 0, 0, 0);
joseoyola 10:9d87a886f066 123 }
joseoyola 10:9d87a886f066 124 }
joseoyola 10:9d87a886f066 125 }
joseoyola 10:9d87a886f066 126 }
joseoyola 10:9d87a886f066 127 }
joseoyola 10:9d87a886f066 128
joseoyola 10:9d87a886f066 129
joseoyola 16:a3de0c0dbe33 130 void LedCube::cubeUpdate() {
joseoyola 16:a3de0c0dbe33 131 updateLEDs(false, prevSize, prevPos[X], prevPos[Y], prevPos[Z]); //Turn off LEDs from previous state
joseoyola 16:a3de0c0dbe33 132 updateLEDs(true, size, pos[X], pos[Y], pos[Z]); //Turn on new LEDs for new state
joseoyola 16:a3de0c0dbe33 133 prevSize = size;
joseoyola 16:a3de0c0dbe33 134 prevPos[X] = pos[X];
joseoyola 16:a3de0c0dbe33 135 prevPos[Y] = pos[Y];
joseoyola 16:a3de0c0dbe33 136 prevPos[Z] = pos[Z];
joseoyola 16:a3de0c0dbe33 137 ledStrip1.show();
joseoyola 16:a3de0c0dbe33 138 ledStrip2.show();
joseoyola 16:a3de0c0dbe33 139 ledStrip1.startDMA();
joseoyola 16:a3de0c0dbe33 140 ledStrip2.startDMA();
joseoyola 16:a3de0c0dbe33 141 printf("Position: %d, %d, %d \t Size: %d\r\n", pos[X], pos[Y], pos[Z], size);
joseoyola 10:9d87a886f066 142 }
joseoyola 10:9d87a886f066 143
joseoyola 16:a3de0c0dbe33 144 int LedCube::move(int deltaX, int deltaY, int deltaZ) {
joseoyola 16:a3de0c0dbe33 145 int retVal = -1;
joseoyola 10:9d87a886f066 146 //Moving in X direction
joseoyola 16:a3de0c0dbe33 147 if(pos[Y] == 0 || pos[Z] + size - 1 == (nCOLs - 1)) { //Making sure square is "stuck" to panel 1 or 3
joseoyola 16:a3de0c0dbe33 148 if((pos[X] + size + deltaX - 1) < nCOLs && (pos[X] + deltaX) >= 0) {
joseoyola 10:9d87a886f066 149 pos[X] += deltaX;
joseoyola 16:a3de0c0dbe33 150 if (deltaX != 0) retVal = 1;
joseoyola 10:9d87a886f066 151 }
joseoyola 10:9d87a886f066 152 }
joseoyola 10:9d87a886f066 153
joseoyola 10:9d87a886f066 154 //Moving in Y direction
joseoyola 16:a3de0c0dbe33 155 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 156 if((pos[Y] + size + deltaY - 1) < nCOLs && (pos[Y] + deltaY) >= 0) {
joseoyola 10:9d87a886f066 157 pos[Y] += deltaY;
joseoyola 16:a3de0c0dbe33 158 if (deltaY != 0) retVal = 1;
joseoyola 10:9d87a886f066 159 }
joseoyola 10:9d87a886f066 160 }
joseoyola 10:9d87a886f066 161
joseoyola 10:9d87a886f066 162 //Moving in Z direction
joseoyola 16:a3de0c0dbe33 163 if(pos[X] + size - 1 == (nCOLs - 1) || pos[Y] == 0) {//Making sure square is "stuck" to panel 1 or 2
joseoyola 16:a3de0c0dbe33 164 if((pos[Z] + size + deltaZ - 1) < nCOLs && (pos[Z] + deltaZ) >= 0) {
joseoyola 10:9d87a886f066 165 pos[Z] += deltaZ;
joseoyola 16:a3de0c0dbe33 166 if (deltaZ != 0) retVal = 1;
joseoyola 10:9d87a886f066 167 }
joseoyola 10:9d87a886f066 168 }
joseoyola 16:a3de0c0dbe33 169 return retVal;
joseoyola 16:a3de0c0dbe33 170 }
joseoyola 16:a3de0c0dbe33 171
joseoyola 16:a3de0c0dbe33 172 void LedCube::changeColor(float hue){
joseoyola 20:c2bc970bbff8 173 Colors::HSBtoRGB(hue, saturation, brightness, &r, &g, &b);
joseoyola 16:a3de0c0dbe33 174 }
joseoyola 16:a3de0c0dbe33 175
joseoyola 16:a3de0c0dbe33 176 void LedCube::changeSize(int newSize) {
joseoyola 16:a3de0c0dbe33 177 if((pos[X] + newSize) < nCOLs && (pos[Y] + newSize) < nCOLs && (pos[Z] + newSize) < nCOLs) {
joseoyola 16:a3de0c0dbe33 178 size = newSize;
joseoyola 16:a3de0c0dbe33 179 }
joseoyola 20:c2bc970bbff8 180 }