Temp Fork

Dependencies:   MMA8451Q Multi_WS2811 NVIC_set_all_priorities TSI cc3000_hostdriver_mbedsocket mbed

Fork of CubicHand by Model-Based Team

Files at this revision

API Documentation at this revision

Comitter:
joseoyola
Date:
Mon Dec 08 03:28:50 2014 +0000
Parent:
13:c701f1122797
Child:
17:c2e59ada97ee
Commit message:
Tested and fixed bugs in ledCube.cpp. ; ; Tested using 60 LEDs (3 strips of 20) for the bottom of Panels 1 and 2, and moving the square between these two. Also tested changing size as well as color. More tests to follow once cube is built.

Changed in this revision

led.cpp Show diff for this revision Revisions of this file
ledCube.cpp Show annotated file Show diff for this revision Revisions of this file
ledCube.h Show annotated file Show diff for this revision Revisions of this file
--- a/led.cpp	Sat Dec 06 23:25:32 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,231 +0,0 @@
-/*
- * LED Cube library
- * 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 PANEL1             0
-#define PANEL2             1
-#define PANEL3             2
-
-unsigned const X = 0;
-unsigned const Y = 1;
-unsigned const Z = 2;
-
-
-
-int pos[3];
-int size = 0;
-
-// 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 = 10;
-unsigned const nCOLs = 10; 
-
-
-
-// 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
-unsigned const DATA_OUT_PIN3 = 4; // 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 ledStrip1(nLEDs, DATA_OUT_PIN1);
-WS2811 ledStrip2(nLEDs, DATA_OUT_PIN2);
-WS2811 ledStrip3(nLEDs, DATA_OUT_PIN2);
-
-/*
-void readTouchSensor()
-{
-    touchPercentage *= 0.(nCOLs - 1);
-    touchPercentage += touchSensor.readPercentage() * 0.1;
-    brite = minBrite + (maxBrite - minBrite) * touchPercentage;
-}*/
-
-
-
-
-static int getLedIndex(int panel, int x, int y) {
-    if (panel == PANEL1) {
-        if (y % 2 == 0) {
-            return nCOLs*2 * y + x;
-        }
-        else {
-            return nCOLs*2 * y + nCOLs + ((nCOLs - 1) - x);
-        }
-    }
-
-    if (panel == PANEL2) {
-        if (y % 2 == 0) {
-            return nCOLs*2 * y + nCOLs + x;
-        }
-        else {
-            return nCOLs*2 * y + ((nCOLs - 1) - x);
-        }
-    }
-    
-    if (panel == PANEL3) {
-        if (y % 2 == 0) {
-            return nCOLs * y + x;
-        }
-        else {
-            return nCOLs * y + ((nCOLs - 1) - x);
-        }
-    }
-    
-    else return -1;
-} 
-
-
-
-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.setPixelColor(led, r, g, b);
-                } else {
-                    ledStrip1.setPixelColor(led, 0, 0, 0);
-                }
-            }
-        }
-    }
-    
-    //Panel 2
-    if(pos[X] + size == (nCOLs - 1)) {
-        for(int i = pos[Y]; i < pos[Y] + size; i++) {
-            for(int j = pos[Z]; j < pos[Z] + size; j++) {
-                int led = getLedIndex(PANEL2, i, j);
-                if(on) {
-                    ledStrip2.setPixelColor(led, r, g, b);
-                } else {
-                    ledStrip2.setPixelColor(led, 0, 0, 0);
-                }
-            }
-        }
-    }
-    
-    //Panel 3
-    if(pos[Z] + size == (nCOLs - 1)) {
-        for(int i = pos[X]; i < pos[X] + size; i++) {
-            for(int j = pos[Y]; j < pos[Y] + size; j++) {
-                int led = getLedIndex(PANEL3, i, j);
-                if(on) {
-                    ledStrip3.setPixelColor(led, r, g, b);
-                } else {
-                    ledStrip3.setPixelColor(led, 0, 0, 0);
-                }
-            }
-        }
-    }
-}
-
-
-void cubeUpdate() {
-    updateLEDs(false); //Turn off LEDs from previous state
-    updateLEDs(true); //Turn on new LEDs for new state
-}
-
-void move(int deltaX, int deltaY, int deltaZ) {
-    //Moving in X direction
-    if(pos[Y] == 0 || pos[Z] == (nCOLs - 1)) { //Making sure square is "stuck" to panel 1 or 3
-        if((pos[X] + size + deltaX) < nCOLs && (pos[X] + deltaX) >= 0) {
-            pos[X] += deltaX;
-        }
-    }
-    
-    //Moving in Y direction
-    if(pos[X] == (nCOLs - 1) || pos[Z] == (nCOLs - 1)) {//Making sure square is "stuck" to panel 2 or 3
-        if((pos[Y] + size + deltaY) < nCOLs && (pos[Y] + deltaY) >= 0) {
-            pos[Y] += deltaY;
-        }
-    }
-    
-    //Moving in Z direction
-    if(pos[X] == (nCOLs - 1) || pos[Y] == 0) {//Making sure square is "stuck" to panel 1 or 2
-        if((pos[Z] + size + deltaZ) < nCOLs && (pos[Z] + deltaZ) >= 0) {
-            pos[Z] += deltaZ;
-        }
-    }
-    
-    cubeUpdate();
-}
-
-
-
-
-
-
-
-/*
-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();
-}
-*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ledCube.cpp	Mon Dec 08 03:28:50 2014 +0000
@@ -0,0 +1,229 @@
+/*
+ * Neopixel LED Cube library
+ * 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"
+#include "ledCube.h"
+
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+
+#define PANEL1             0
+#define PANEL2             1
+#define PANEL3             2
+#define nLEDs              60//MAX_LEDS_PER_STRIP;
+#define nROWs              10
+#define nCOLs              10  
+#define DATA_OUT_PIN1      2 // PTD2
+#define DATA_OUT_PIN2      3 // PTD3
+#define DATA_OUT_PIN3      4 // PTD3  
+
+
+LedCube::LedCube():X(0),Y(1),Z(2),ledStrip1(nLEDs, DATA_OUT_PIN1),ledStrip2(nLEDs, DATA_OUT_PIN2),ledStrip3(nLEDs, DATA_OUT_PIN3)
+{
+}
+
+LedCube::~LedCube()
+{
+}
+
+void LedCube::Init(int x, int y, int z)
+{
+    size = 2;
+    prevSize = size;
+    pos[X] = prevPos[X] = x;
+    pos[Y] = prevPos[Y] = y;
+    pos[Z] = prevPos[Z] = z;
+    r = 255*0.1;
+    g = 255*0.1;
+    b = 255*0.1;
+
+    brightness = 0.05;
+    saturation = 1.0;
+    
+    ledStrip1.begin();
+    ledStrip2.begin();
+    ledStrip3.begin();
+}
+
+int LedCube::getLedIndex(int panel, int x, int y) {
+    if (panel == PANEL1) {
+        if (y % 2 == 0) {
+            return nCOLs*2 * y + x;
+        }
+        else {
+            return nCOLs*2 * y + nCOLs + ((nCOLs - 1) - x);
+        }
+    }
+
+    if (panel == PANEL2) {
+        if (y % 2 == 0) {
+            return nCOLs*2 * y + nCOLs + x;
+        }
+        else {
+            return nCOLs*2 * y + ((nCOLs - 1) - x);
+        }
+    }
+    
+    if (panel == PANEL3) {
+        if (y % 2 == 0) {
+            return nCOLs * y + x;
+        }
+        else {
+            return nCOLs * y + ((nCOLs - 1) - x);
+        }
+    }
+    
+    else return -1;
+} 
+
+void LedCube::updateLEDs(bool on, int size, int x, int y, int z) {
+    //Panel 1
+    if(y == 0) {
+        for(int i = x; i < x + size; i++) {
+            for(int j = z; j < z + size; j++) {
+                int led = getLedIndex(PANEL1, i, j);
+                if(on) {
+                    ledStrip1.setPixelColor(led, r, g, b);
+                } else {
+                    ledStrip1.setPixelColor(led, 0, 0, 0);
+                }
+            }
+        }
+    }
+    
+    //Panel 2
+    if(x + size - 1 == (nCOLs - 1)) {
+        for(int i = y; i < y + size; i++) {
+            for(int j = z; j < z + size; j++) {
+                int led = getLedIndex(PANEL2, i, j);
+                if(on) {
+                    if(z < 3) ledStrip1.setPixelColor(led, r, g, b);
+                    else ledStrip2.setPixelColor(led, r, g, b);
+                } else {
+                    if (z < 3) ledStrip1.setPixelColor(led, 0, 0, 0);
+                    ledStrip2.setPixelColor(led, 0, 0, 0);
+                }
+            }
+        }
+    }
+    
+    //Panel 3
+    if(z + size - 1 == (nCOLs - 1)) {
+        for(int i = x; i < x + size; i++) {
+            for(int j = y; j < y + size; j++) {
+                int led = getLedIndex(PANEL3, i, j);
+                if(on) {
+                    ledStrip3.setPixelColor(led, r, g, b);
+                } else {
+                    ledStrip3.setPixelColor(led, 0, 0, 0);
+                }
+            }
+        }
+    }
+}
+
+
+void LedCube::cubeUpdate() {
+    updateLEDs(false, prevSize, prevPos[X], prevPos[Y], prevPos[Z]); //Turn off LEDs from previous state
+    updateLEDs(true, size, pos[X], pos[Y], pos[Z]); //Turn on new LEDs for new state
+    prevSize = size;
+    prevPos[X] = pos[X];
+    prevPos[Y] = pos[Y];
+    prevPos[Z] = pos[Z];
+    ledStrip1.show();
+    ledStrip2.show();
+    ledStrip3.show();
+    ledStrip1.startDMA();
+    ledStrip2.startDMA();
+    ledStrip3.startDMA();
+    printf("Position: %d, %d, %d \t Size: %d\r\n", pos[X], pos[Y], pos[Z], size);
+}
+
+int LedCube::move(int deltaX, int deltaY, int deltaZ) {
+    int retVal = -1;
+    //Moving in X direction
+    if(pos[Y] == 0 || pos[Z] + size - 1 == (nCOLs - 1)) { //Making sure square is "stuck" to panel 1 or 3
+        if((pos[X] + size + deltaX - 1) < nCOLs && (pos[X] + deltaX) >= 0) {
+            pos[X] += deltaX;
+            if (deltaX != 0) retVal = 1;
+            printf("Here1, %d\r\n",retVal);
+        }
+    }
+    
+    //Moving in Y direction
+    if(pos[X] + size - 1 == (nCOLs - 1) || pos[Z] + size - 1 == (nCOLs - 1)) {//Making sure square is "stuck" to panel 2 or 3
+        if((pos[Y] + size + deltaY - 1) < nCOLs && (pos[Y] + deltaY) >= 0) {
+            pos[Y] += deltaY;
+            if (deltaY != 0) retVal = 1;
+            printf("Here2, %d\r\n",retVal);
+        }
+    }
+    
+    //Moving in Z direction
+    if(pos[X] + size - 1 == (nCOLs - 1) || pos[Y] == 0) {//Making sure square is "stuck" to panel 1 or 2
+        if((pos[Z] + size + deltaZ - 1) < nCOLs && (pos[Z] + deltaZ) >= 0) {
+            pos[Z] += deltaZ;
+            if (deltaZ != 0) retVal = 1;
+            printf("Here3, %d\r\n",retVal);
+        }
+    }
+    return retVal;
+    //cubeUpdate();
+}
+
+void LedCube::changeColor(float hue){
+    HSBtoRGB(hue, saturation, brightness, &r, &g, &b);
+}
+
+void LedCube::changeSize(int newSize) {
+    if((pos[X] + newSize) < nCOLs && (pos[Y] + newSize) < nCOLs && (pos[Z] + newSize) < nCOLs) {
+        size = newSize;
+    }
+}
+
+
+
+
+
+
+
+/*
+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();
+}
+*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ledCube.h	Mon Dec 08 03:28:50 2014 +0000
@@ -0,0 +1,42 @@
+#include "mbed.h"
+#include "WS2811.h"
+
+#pragma once
+
+class LedCube
+{
+    public:
+        LedCube();
+        ~LedCube();
+        void Init(int x, int y, int z);
+        int getLedIndex(int panel, int x, int y);
+        void updateLEDs(bool on, int size, int x, int y, int z);
+        void cubeUpdate();
+        int move(int deltaX, int deltaY, int deltaZ);
+        void changeColor(float hue);
+        void changeSize(int newSize);
+        
+    private:
+        unsigned const X;
+        unsigned const Y;
+        unsigned const Z;
+        int pos[3];
+        int prevPos[3];
+        int size;
+        int prevSize;
+        //unsigned const nLEDs;//MAX_LEDS_PER_STRIP;
+        //unsigned const nROWs;
+        //unsigned const nCOLs; 
+        //unsigned const DATA_OUT_PIN1; // PTD2
+        //unsigned const DATA_OUT_PIN2; // PTD3
+        //unsigned const DATA_OUT_PIN3; // PTD3
+        float saturation;
+        float brightness;
+        uint8_t r;
+        uint8_t g;
+        uint8_t b;        
+        WS2811 ledStrip1;
+        WS2811 ledStrip2;
+        WS2811 ledStrip3;
+    
+};
\ No newline at end of file