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 21:40:30 2014 +0000
Revision:
5:41f28ce7f5a9
Parent:
2:45b90e8d7d82
Child:
8:c06f5046c9f7
Commiting for Kevin's benefit. Adding functions to led.cpp

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