Cubic Hand project for EECS 249A course.

Dependencies:   MMA8451Q TSI cc3000_hostdriver_mbedsocket NVIC_set_all_priorities mbed Multi_WS2811

Committer:
joseoyola
Date:
Fri Dec 05 22:54:35 2014 +0000
Revision:
12:df94d17b5bee
Parent:
10:9d87a886f066
Finished fixing errors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joseoyola 2:45b90e8d7d82 1 /*
joseoyola 2:45b90e8d7d82 2 * 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 5:41f28ce7f5a9 13
joseoyola 5:41f28ce7f5a9 14
joseoyola 5:41f28ce7f5a9 15 #define MMA8451_I2C_ADDRESS (0x1d<<1)
joseoyola 5:41f28ce7f5a9 16
joseoyola 5:41f28ce7f5a9 17 #define PANEL1 0
joseoyola 5:41f28ce7f5a9 18 #define PANEL2 1
joseoyola 5:41f28ce7f5a9 19 #define PANEL3 2
joseoyola 5:41f28ce7f5a9 20
joseoyola 12:df94d17b5bee 21 unsigned const X = 0;
joseoyola 12:df94d17b5bee 22 unsigned const Y = 1;
joseoyola 12:df94d17b5bee 23 unsigned const Z = 2;
joseoyola 12:df94d17b5bee 24
joseoyola 12:df94d17b5bee 25
joseoyola 12:df94d17b5bee 26
joseoyola 5:41f28ce7f5a9 27 int pos[3];
joseoyola 8:c06f5046c9f7 28 int size = 0;
joseoyola 5:41f28ce7f5a9 29
joseoyola 5:41f28ce7f5a9 30 // per LED: 3 * 20 mA = 60mA max
joseoyola 5:41f28ce7f5a9 31 // 60 LEDs: 60 * 60mA = 3600 mA max
joseoyola 5:41f28ce7f5a9 32 // 120 LEDs: 7200 mA max
joseoyola 5:41f28ce7f5a9 33 unsigned const nLEDs = 60;//MAX_LEDS_PER_STRIP;
joseoyola 8:c06f5046c9f7 34 unsigned const nROWs = 10;
joseoyola 8:c06f5046c9f7 35 unsigned const nCOLs = 10;
joseoyola 5:41f28ce7f5a9 36
joseoyola 5:41f28ce7f5a9 37
joseoyola 5:41f28ce7f5a9 38
joseoyola 5:41f28ce7f5a9 39 // I/O pin usage
joseoyola 5:41f28ce7f5a9 40 // PTD0 TPM0 CH0 monitor
joseoyola 5:41f28ce7f5a9 41 // PTD1 TPM0 CH1 monitor
joseoyola 5:41f28ce7f5a9 42 // PTD2 data output
joseoyola 5:41f28ce7f5a9 43 // PTD3 debug output
joseoyola 5:41f28ce7f5a9 44
joseoyola 5:41f28ce7f5a9 45 unsigned const DATA_OUT_PIN1 = 2; // PTD2
joseoyola 5:41f28ce7f5a9 46 unsigned const DATA_OUT_PIN2 = 3; // PTD3
joseoyola 8:c06f5046c9f7 47 unsigned const DATA_OUT_PIN3 = 4; // PTD3
joseoyola 5:41f28ce7f5a9 48
joseoyola 5:41f28ce7f5a9 49 Serial pc(USBTX, USBRX);
joseoyola 5:41f28ce7f5a9 50 Timer timeRunning;
joseoyola 5:41f28ce7f5a9 51
joseoyola 5:41f28ce7f5a9 52 TSISensor touchSensor;
joseoyola 5:41f28ce7f5a9 53 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
joseoyola 5:41f28ce7f5a9 54 PwmOut rled(LED_RED);
joseoyola 5:41f28ce7f5a9 55 PwmOut gled(LED_GREEN);
joseoyola 5:41f28ce7f5a9 56 // LED_BLUE is on PTD1
joseoyola 5:41f28ce7f5a9 57
joseoyola 5:41f28ce7f5a9 58 float touchPercentage;
joseoyola 5:41f28ce7f5a9 59 unsigned frames;
joseoyola 5:41f28ce7f5a9 60
joseoyola 5:41f28ce7f5a9 61 float const minBrite = 0.2;
joseoyola 5:41f28ce7f5a9 62 float const maxBrite = 0.5;
joseoyola 5:41f28ce7f5a9 63 float brightness;
joseoyola 5:41f28ce7f5a9 64
joseoyola 5:41f28ce7f5a9 65
joseoyola 5:41f28ce7f5a9 66 uint8_t r = 255*0.1;
joseoyola 5:41f28ce7f5a9 67 uint8_t g = 255*0.1;
joseoyola 5:41f28ce7f5a9 68 uint8_t b = 255*0.1;
joseoyola 5:41f28ce7f5a9 69
joseoyola 5:41f28ce7f5a9 70
joseoyola 10:9d87a886f066 71 WS2811 ledStrip1(nLEDs, DATA_OUT_PIN1);
joseoyola 10:9d87a886f066 72 WS2811 ledStrip2(nLEDs, DATA_OUT_PIN2);
joseoyola 10:9d87a886f066 73 WS2811 ledStrip3(nLEDs, DATA_OUT_PIN2);
joseoyola 5:41f28ce7f5a9 74
joseoyola 10:9d87a886f066 75 /*
joseoyola 5:41f28ce7f5a9 76 void readTouchSensor()
joseoyola 5:41f28ce7f5a9 77 {
joseoyola 8:c06f5046c9f7 78 touchPercentage *= 0.(nCOLs - 1);
joseoyola 5:41f28ce7f5a9 79 touchPercentage += touchSensor.readPercentage() * 0.1;
joseoyola 5:41f28ce7f5a9 80 brite = minBrite + (maxBrite - minBrite) * touchPercentage;
joseoyola 10:9d87a886f066 81 }*/
joseoyola 5:41f28ce7f5a9 82
joseoyola 5:41f28ce7f5a9 83
joseoyola 8:c06f5046c9f7 84
joseoyola 8:c06f5046c9f7 85
joseoyola 8:c06f5046c9f7 86 static int getLedIndex(int panel, int x, int y) {
joseoyola 8:c06f5046c9f7 87 if (panel == PANEL1) {
joseoyola 8:c06f5046c9f7 88 if (y % 2 == 0) {
joseoyola 8:c06f5046c9f7 89 return nCOLs*2 * y + x;
joseoyola 8:c06f5046c9f7 90 }
joseoyola 8:c06f5046c9f7 91 else {
joseoyola 8:c06f5046c9f7 92 return nCOLs*2 * y + nCOLs + ((nCOLs - 1) - x);
joseoyola 8:c06f5046c9f7 93 }
joseoyola 5:41f28ce7f5a9 94 }
joseoyola 8:c06f5046c9f7 95
joseoyola 8:c06f5046c9f7 96 if (panel == PANEL2) {
joseoyola 8:c06f5046c9f7 97 if (y % 2 == 0) {
joseoyola 8:c06f5046c9f7 98 return nCOLs*2 * y + nCOLs + x;
joseoyola 8:c06f5046c9f7 99 }
joseoyola 8:c06f5046c9f7 100 else {
joseoyola 8:c06f5046c9f7 101 return nCOLs*2 * y + ((nCOLs - 1) - x);
joseoyola 8:c06f5046c9f7 102 }
joseoyola 5:41f28ce7f5a9 103 }
joseoyola 8:c06f5046c9f7 104
joseoyola 8:c06f5046c9f7 105 if (panel == PANEL3) {
joseoyola 8:c06f5046c9f7 106 if (y % 2 == 0) {
joseoyola 8:c06f5046c9f7 107 return nCOLs * y + x;
joseoyola 8:c06f5046c9f7 108 }
joseoyola 8:c06f5046c9f7 109 else {
joseoyola 8:c06f5046c9f7 110 return nCOLs * y + ((nCOLs - 1) - x);
joseoyola 8:c06f5046c9f7 111 }
joseoyola 8:c06f5046c9f7 112 }
joseoyola 8:c06f5046c9f7 113
joseoyola 8:c06f5046c9f7 114 else return -1;
joseoyola 5:41f28ce7f5a9 115 }
joseoyola 5:41f28ce7f5a9 116
joseoyola 10:9d87a886f066 117
joseoyola 10:9d87a886f066 118
joseoyola 10:9d87a886f066 119 void updateLEDs(bool on) {
joseoyola 10:9d87a886f066 120 //Panel 1
joseoyola 10:9d87a886f066 121 if(pos[Y] == 0) {
joseoyola 10:9d87a886f066 122 for(int i = pos[X]; i < pos[X] + size; i++) {
joseoyola 10:9d87a886f066 123 for(int j = pos[Z]; j < pos[Z] + size; j++) {
joseoyola 10:9d87a886f066 124 int led = getLedIndex(PANEL1, i, j);
joseoyola 10:9d87a886f066 125 if(on) {
joseoyola 10:9d87a886f066 126 ledStrip1.setPixelColor(led, r, g, b);
joseoyola 10:9d87a886f066 127 } else {
joseoyola 10:9d87a886f066 128 ledStrip1.setPixelColor(led, 0, 0, 0);
joseoyola 10:9d87a886f066 129 }
joseoyola 10:9d87a886f066 130 }
joseoyola 10:9d87a886f066 131 }
joseoyola 10:9d87a886f066 132 }
joseoyola 10:9d87a886f066 133
joseoyola 10:9d87a886f066 134 //Panel 2
joseoyola 10:9d87a886f066 135 if(pos[X] + size == (nCOLs - 1)) {
joseoyola 10:9d87a886f066 136 for(int i = pos[Y]; i < pos[Y] + size; i++) {
joseoyola 10:9d87a886f066 137 for(int j = pos[Z]; j < pos[Z] + size; j++) {
joseoyola 10:9d87a886f066 138 int led = getLedIndex(PANEL2, i, j);
joseoyola 10:9d87a886f066 139 if(on) {
joseoyola 10:9d87a886f066 140 ledStrip2.setPixelColor(led, r, g, b);
joseoyola 10:9d87a886f066 141 } else {
joseoyola 10:9d87a886f066 142 ledStrip2.setPixelColor(led, 0, 0, 0);
joseoyola 10:9d87a886f066 143 }
joseoyola 10:9d87a886f066 144 }
joseoyola 10:9d87a886f066 145 }
joseoyola 10:9d87a886f066 146 }
joseoyola 10:9d87a886f066 147
joseoyola 10:9d87a886f066 148 //Panel 3
joseoyola 10:9d87a886f066 149 if(pos[Z] + size == (nCOLs - 1)) {
joseoyola 10:9d87a886f066 150 for(int i = pos[X]; i < pos[X] + size; i++) {
joseoyola 10:9d87a886f066 151 for(int j = pos[Y]; j < pos[Y] + size; j++) {
joseoyola 10:9d87a886f066 152 int led = getLedIndex(PANEL3, i, j);
joseoyola 10:9d87a886f066 153 if(on) {
joseoyola 10:9d87a886f066 154 ledStrip3.setPixelColor(led, r, g, b);
joseoyola 10:9d87a886f066 155 } else {
joseoyola 10:9d87a886f066 156 ledStrip3.setPixelColor(led, 0, 0, 0);
joseoyola 10:9d87a886f066 157 }
joseoyola 10:9d87a886f066 158 }
joseoyola 10:9d87a886f066 159 }
joseoyola 10:9d87a886f066 160 }
joseoyola 10:9d87a886f066 161 }
joseoyola 10:9d87a886f066 162
joseoyola 10:9d87a886f066 163
joseoyola 10:9d87a886f066 164 void cubeUpdate() {
joseoyola 10:9d87a886f066 165 updateLEDs(false); //Turn off LEDs from previous state
joseoyola 10:9d87a886f066 166 updateLEDs(true); //Turn on new LEDs for new state
joseoyola 10:9d87a886f066 167 }
joseoyola 10:9d87a886f066 168
joseoyola 10:9d87a886f066 169 void move(int deltaX, int deltaY, int deltaZ) {
joseoyola 10:9d87a886f066 170 //Moving in X direction
joseoyola 10:9d87a886f066 171 if(pos[Y] == 0 || pos[Z] == (nCOLs - 1)) { //Making sure square is "stuck" to panel 1 or 3
joseoyola 10:9d87a886f066 172 if((pos[X] + size + deltaX) < nCOLs && (pos[X] + deltaX) >= 0) {
joseoyola 10:9d87a886f066 173 pos[X] += deltaX;
joseoyola 10:9d87a886f066 174 }
joseoyola 10:9d87a886f066 175 }
joseoyola 10:9d87a886f066 176
joseoyola 10:9d87a886f066 177 //Moving in Y direction
joseoyola 10:9d87a886f066 178 if(pos[X] == (nCOLs - 1) || pos[Z] == (nCOLs - 1)) {//Making sure square is "stuck" to panel 2 or 3
joseoyola 10:9d87a886f066 179 if((pos[Y] + size + deltaY) < nCOLs && (pos[Y] + deltaY) >= 0) {
joseoyola 10:9d87a886f066 180 pos[Y] += deltaY;
joseoyola 10:9d87a886f066 181 }
joseoyola 10:9d87a886f066 182 }
joseoyola 10:9d87a886f066 183
joseoyola 10:9d87a886f066 184 //Moving in Z direction
joseoyola 10:9d87a886f066 185 if(pos[X] == (nCOLs - 1) || pos[Y] == 0) {//Making sure square is "stuck" to panel 1 or 2
joseoyola 10:9d87a886f066 186 if((pos[Z] + size + deltaZ) < nCOLs && (pos[Z] + deltaZ) >= 0) {
joseoyola 10:9d87a886f066 187 pos[Z] += deltaZ;
joseoyola 10:9d87a886f066 188 }
joseoyola 10:9d87a886f066 189 }
joseoyola 10:9d87a886f066 190
joseoyola 10:9d87a886f066 191 cubeUpdate();
joseoyola 10:9d87a886f066 192 }
joseoyola 10:9d87a886f066 193
joseoyola 10:9d87a886f066 194
joseoyola 10:9d87a886f066 195
joseoyola 10:9d87a886f066 196
joseoyola 10:9d87a886f066 197
joseoyola 10:9d87a886f066 198
joseoyola 10:9d87a886f066 199
joseoyola 8:c06f5046c9f7 200 /*
joseoyola 5:41f28ce7f5a9 201 static void move( int delta_x, int delta_y, int old_size, int new_size)
joseoyola 5:41f28ce7f5a9 202 {
joseoyola 5:41f28ce7f5a9 203 for( int i = pos_y; i < pos_y + old_size; i++){
joseoyola 5:41f28ce7f5a9 204 for( int j= pos_x; j < pos_x + old_size; j++){
joseoyola 5:41f28ce7f5a9 205 int pos_off = matrixtolinear(i,j);
joseoyola 5:41f28ce7f5a9 206 if( pos_off >= nLEDs){lightStrip2.setPixelColor(pos_off-nLEDs, 0, 0, 0);}
joseoyola 5:41f28ce7f5a9 207 else
joseoyola 5:41f28ce7f5a9 208 {lightStrip1.setPixelColor(pos_off, 0, 0, 0);}
joseoyola 5:41f28ce7f5a9 209
joseoyola 5:41f28ce7f5a9 210 }
joseoyola 5:41f28ce7f5a9 211 }
joseoyola 5:41f28ce7f5a9 212 if ( (pos_y + (new_size-1) + delta_y < nROWs) && (pos_y + delta_y ) >= 0 ) {
joseoyola 5:41f28ce7f5a9 213 pos_y = pos_y + delta_y;}
joseoyola 5:41f28ce7f5a9 214 if ( (pos_x + (new_size-1) + delta_x < nCOLs ) && (pos_x + delta_x ) >= 0) {
joseoyola 5:41f28ce7f5a9 215 pos_x = pos_x + delta_x;}
joseoyola 5:41f28ce7f5a9 216
joseoyola 5:41f28ce7f5a9 217 for( int i = pos_y; i < pos_y + new_size; i++){
joseoyola 5:41f28ce7f5a9 218 for( int j= pos_x; j < pos_x + new_size; j++){
joseoyola 5:41f28ce7f5a9 219 int pos_on = matrixtolinear(i,j);
joseoyola 5:41f28ce7f5a9 220 if( pos_on >= nLEDs){lightStrip2.setPixelColor(pos_on-nLEDs, 0, g, 0);}
joseoyola 5:41f28ce7f5a9 221 else
joseoyola 5:41f28ce7f5a9 222 {lightStrip1.setPixelColor(pos_on, 0, g, 0);}
joseoyola 5:41f28ce7f5a9 223 }
joseoyola 5:41f28ce7f5a9 224 }
joseoyola 5:41f28ce7f5a9 225
joseoyola 5:41f28ce7f5a9 226 lightStrip1.show();
joseoyola 5:41f28ce7f5a9 227 lightStrip2.show();
joseoyola 5:41f28ce7f5a9 228 lightStrip1.startDMA();
joseoyola 5:41f28ce7f5a9 229 lightStrip2.startDMA();
joseoyola 5:41f28ce7f5a9 230 }
joseoyola 8:c06f5046c9f7 231 */