Cubic Hand project for EECS 249A course.

Dependencies:   MMA8451Q TSI cc3000_hostdriver_mbedsocket NVIC_set_all_priorities mbed Multi_WS2811

Revision:
5:41f28ce7f5a9
Parent:
2:45b90e8d7d82
Child:
8:c06f5046c9f7
--- a/led.cpp	Fri Dec 05 20:59:10 2014 +0000
+++ b/led.cpp	Fri Dec 05 21:40:30 2014 +0000
@@ -1,13 +1,191 @@
 /*
  * LED Cube library
- * by Jose Oyola and Robert Bui
+ * by Robert Bui and Jose Oyola
  * UC Berkeley 2014
  */
 
 #include "main.h"
 #include "mbed.h"
+#include "WS2811.h"
+#include "Colors.h"
+#include "TSISensor.h"
+#include "MMA8451Q.h"
+
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+
+#define X                  0
+#define Y                  1
+#define Z                  2
+
+#define PANEL1             0
+#define PANEL2             1
+#define PANEL3             2
+
+int pos[3];
+int size;
+
+// per LED: 3 * 20 mA = 60mA max
+// 60 LEDs: 60 * 60mA = 3600 mA max
+// 120 LEDs: 7200 mA max
+unsigned const nLEDs = 60;//MAX_LEDS_PER_STRIP;
+unsigned const nROWs = 3;
+unsigned const nCOLs = 20; 
+
+
+
+// I/O pin usage
+// PTD0 TPM0 CH0 monitor
+// PTD1 TPM0 CH1 monitor
+// PTD2 data output
+// PTD3 debug output
+
+unsigned const DATA_OUT_PIN1 = 2; // PTD2
+unsigned const DATA_OUT_PIN2 = 3; // PTD3
+
+Serial pc(USBTX, USBRX);
+Timer timeRunning;
+
+TSISensor touchSensor;
+MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
+PwmOut rled(LED_RED);
+PwmOut gled(LED_GREEN);
+// LED_BLUE is on PTD1
+
+float touchPercentage;
+unsigned frames;
+
+float const minBrite = 0.2;
+float const maxBrite = 0.5;
+float brightness;
+
+
+uint8_t r = 255*0.1;
+uint8_t g = 255*0.1;
+uint8_t b = 255*0.1;
+
+
+WS2811 lightStrip1(nLEDs, DATA_OUT_PIN1);
+WS2811 lightStrip2(nLEDs, DATA_OUT_PIN2);
+
+
+void readTouchSensor()
+{
+    touchPercentage *= 0.9;
+    touchPercentage += touchSensor.readPercentage() * 0.1;
+    brite = minBrite + (maxBrite - minBrite) * touchPercentage;
+}
 
 
- void move(deltaX, deltaY, deltaZ) {
-     if(pos[Y] == 0 || pos[Z] == 9) {
+void move(deltaX, deltaY, deltaZ) {
+    //Moving in X direction
+    if(pos[Y] == 0 || pos[Z] == 9) { //Making sure square is "stuck" to panel 1 or 3
+        if((pos[X] + size + deltaX) < 10 && (pos[X] + deltaX) >= 0) {
+            pos[X] += deltaX;
+        }
+    }
+    
+    //Moving in Y direction
+    if(pos[X] == 9 || pos[Z] == 9) {//Making sure square is "stuck" to panel 2 or 3
+        if((pos[Y] + size + deltaY) < 10 && (pos[Y] + deltaY) >= 0) {
+            pos[Y] += deltaY;
+        }
+    }
+    
+    //Moving in Z direction
+    if(pos[X] == 9 || pos[Y] == 0) {//Making sure square is "stuck" to panel 1 or 2
+        if((pos[Z] + size + deltaZ) < 10 && (pos[Z] + deltaZ) >= 0) {
+            pos[Z] += deltaZ;
+        }
+    }
+    
+    cubeUpdate();
+}
+
+void cubeUpdate() {
+    updateLEDs(false); //Turn off LEDs from previous state
+    updateLEDs(true); //Turn on new LEDs for new state
+}
+
+void updateLEDs(bool on) {
+    //Panel 1
+    if(pos[Y] == 0) {
+        for(int i = pos[X]; i < pos[X] + size; i++) {
+            for(int j = pos[Z]; j < pos[Z] + size; j++) {
+                int led = getLedIndex(PANEL1, i, j);
+                if(on) {
+                    ledStrip1(led, r, g, b);
+                } else {
+                    ledStrip1(led, 0, 0, 0);
+                }
+            }
+        }
+    }
+    
+    //Panel 2
+    if(pos[X] + size == 0) {
+        for(int i = pos[X]; i < pos[X] + size; i++) {
+            for(int j = pos[Z]; j < pos[Z] + size; j++) {
+                int led = getLedIndex(PANEL1, i, j);
+                if(on) {
+                    ledStrip1(led, r, g, b);
+                } else {
+                    ledStrip1(led, 0, 0, 0);
+                }
+            }
+        }
+    }
+}
+
+
+
+
+static int matrixtolinear(int x, int y) {
+    int pos; 
+    if( x % 2 == 0) {
+        pos = (x*nCOLs) +y; 
+    }
+    else {
+        pos = (x*nCOLs) + ((nCOLs-1)-y);
+    }
+    return pos;  
+} 
+
+
+static void move( int delta_x, int delta_y, int old_size, int new_size)
+{
+    for( int i = pos_y; i < pos_y + old_size; i++){
+        for( int j= pos_x; j < pos_x + old_size; j++){
+            int pos_off = matrixtolinear(i,j);
+            if( pos_off >= nLEDs){lightStrip2.setPixelColor(pos_off-nLEDs, 0, 0, 0);}
+            else 
+            {lightStrip1.setPixelColor(pos_off, 0, 0, 0);}
+            
+            }
+        }
+        if ( (pos_y + (new_size-1) + delta_y < nROWs) && (pos_y + delta_y ) >= 0 ) {
+        pos_y = pos_y + delta_y;}
+        if ( (pos_x + (new_size-1) + delta_x < nCOLs ) && (pos_x + delta_x ) >= 0) {
+        pos_x = pos_x + delta_x;}
+         
+    for( int i = pos_y; i < pos_y + new_size; i++){
+        for( int j= pos_x; j < pos_x + new_size; j++){
+            int pos_on = matrixtolinear(i,j);
+            if( pos_on >= nLEDs){lightStrip2.setPixelColor(pos_on-nLEDs, 0, g, 0);}
+            else
+            {lightStrip1.setPixelColor(pos_on, 0, g, 0);}
+            }
+        }
+    
+    lightStrip1.show();
+    lightStrip2.show();
+    lightStrip1.startDMA();
+    lightStrip2.startDMA();
+}
+
+//Set initial position and size 
+void init(void) {
+    pos_x = 0;
+    pos_y = 0; 
+    size = 2;
 }
\ No newline at end of file