Model-Based Team / Mbed 2 deprecated CubicHand

Dependencies:   MMA8451Q TSI cc3000_hostdriver_mbedsocket NVIC_set_all_priorities mbed Multi_WS2811

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LedCube.cpp Source File

LedCube.cpp

00001 /*
00002  * Neopixel LED Cube library
00003  * by Robert Bui and Jose Oyola
00004  * UC Berkeley 2014
00005  */
00006 
00007 #include "mbed.h"
00008 #include "WS2811.h"
00009 #include "Colors.h"
00010 #include "TSISensor.h"
00011 #include "MMA8451Q.h"
00012 #include "LedCube.h"
00013 
00014 
00015 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00016 
00017 #define PANEL1             0
00018 #define PANEL2             1
00019 #define PANEL3             2
00020 #define nLEDs              200 //MAX_LEDS_PER_STRIP;
00021 #define MAX_SIZE           10
00022 #define nROWs              10  //number of rows per cube panel 
00023 #define nCOLs              10  //number of columns per cube panel
00024 #define DATA_OUT_PIN1      2   // PTD2
00025 #define DATA_OUT_PIN2      3   // PTD3 
00026 
00027 
00028 LedCube::LedCube():X(0),Y(1),Z(2),ledStrip1(nLEDs, DATA_OUT_PIN1),ledStrip2(nLEDs/2, DATA_OUT_PIN2)
00029 {
00030 }
00031 
00032 LedCube::~LedCube()
00033 {
00034 }
00035 
00036 /*Sets the initial size and position of the lighted cube*/ 
00037 void LedCube::Init(int x, int y, int z)
00038 {
00039     size = 2;
00040     prevSize = size;
00041     pos[X] = prevPos[X] = x;
00042     pos[Y] = prevPos[Y] = y;
00043     pos[Z] = prevPos[Z] = z;
00044     r = 255*0.1;
00045     g = 255*0.1;
00046     b = 255*0.1;
00047 
00048     brightness = 0.5/8;
00049     saturation = 1.0;
00050     
00051     ledStrip1.begin();
00052     ledStrip2.begin();
00053 }
00054 
00055 /* Returns the index of the LED given the cartesian 
00056  * coordinates of the LED on a given panel. The origin 
00057  * is the led at the bottom left of panel 1 when using 
00058  * a three panel cube.
00059      ________
00060     /       /|
00061    /   3   / |
00062   /_______/ 2|
00063   |       |  | 
00064   |   1   | /
00065   |       |
00066   --------      
00067   
00068   Z    Y
00069   |   /
00070   |  /
00071   | /
00072   |/
00073    -------X   
00074 
00075  */
00076 int LedCube::getLedIndex(int panel, int x, int y) {
00077     if (panel == PANEL1) {
00078         if (y % 2 == 0) {
00079             return nCOLs*2 * y + x;
00080         }
00081         else {
00082             return nCOLs*2 * y + nCOLs + ((nCOLs - 1) - x);
00083         }
00084     }
00085 
00086     if (panel == PANEL2) {
00087         if (y % 2 == 0) {
00088             return nCOLs*2 * y + nCOLs + x;
00089         }
00090         else {
00091             return nCOLs*2 * y + ((nCOLs - 1) - x);
00092         }
00093     }
00094     
00095     if (panel == PANEL3) {
00096         if (y % 2 == 0) {
00097             return nCOLs * y + x;
00098         }
00099         else {
00100             return nCOLs * y + ((nCOLs - 1) - x);
00101         }
00102     }
00103     
00104     else return -1;
00105 }  
00106 
00107 /*
00108  * Lights up (if on) or turns off (if !on) the LEDs on the LED cube 
00109  * corresponding to the location of the square. All panels will show 
00110  * the cube, with brightness depending on the distance from the 
00111  * square to the panel.
00112  */
00113 void LedCube::updateLEDs(bool on, int size, int x, int y, int z) {
00114     //Panel 1
00115     double bright;
00116     bright = 1.0 / ((y + 1) * (y + 1));
00117     for(int i = x; i < x + size; i++) {
00118         for(int j = z; j < z + size; j++) {
00119             int led = getLedIndex(PANEL1, i, j);
00120             if(on) {
00121                 ledStrip1.setPixelColor(led, r*bright, g*bright, b*bright);
00122             } else {
00123                 ledStrip1.setPixelColor(led, 0, 0, 0);
00124             }
00125         }
00126     }
00127     
00128     //Panel 2
00129     bright = 1.0 / (((nCOLs-1) - x - (size-1) + 1) * ((nCOLs-1) - x - (size-1) + 1));
00130     for(int i = y; i < y + size; i++) {
00131         for(int j = z; j < z + size; j++) {
00132             int led = getLedIndex(PANEL2, i, j);
00133             if(on) {
00134                 ledStrip1.setPixelColor(led, r*bright, g*bright, b*bright);
00135             } else {
00136                 ledStrip1.setPixelColor(led, 0, 0, 0);
00137             }
00138         }
00139     }
00140     
00141     //Panel 3
00142     bright = 1.0 / (((nCOLs-1) - z - (size-1) + 1) * ((nCOLs-1) - z - (size-1) + 1));   
00143     for(int i = x; i < x + size; i++) {
00144         for(int j = y; j < y + size; j++) {
00145             int led = getLedIndex(PANEL3, i, j);
00146             if(on) {
00147                 ledStrip2.setPixelColor(led, r*bright, g*bright, b*bright);
00148             } else {
00149                 ledStrip2.setPixelColor(led, 0, 0, 0);
00150             }
00151         }
00152     }
00153 }
00154 
00155 void LedCube::updateLEDsOld(bool on, int size, int x, int y, int z) {
00156     //Panel 1
00157     if(y == 0) {
00158         for(int i = x; i < x + size; i++) {
00159             for(int j = z; j < z + size; j++) {
00160                 int led = getLedIndex(PANEL1, i, j);
00161                 if(on) {
00162                     ledStrip1.setPixelColor(led, r, g, b);
00163                 } else {
00164                     ledStrip1.setPixelColor(led, 0, 0, 0);
00165                 }
00166             }
00167         }
00168     }
00169     
00170     //Panel 2
00171     if(x + size - 1 == (nCOLs - 1)) {
00172         for(int i = y; i < y + size; i++) {
00173             for(int j = z; j < z + size; j++) {
00174                 int led = getLedIndex(PANEL2, i, j);
00175                 if(on) {
00176                     ledStrip1.setPixelColor(led, r, g, b);
00177                 } else {
00178                     ledStrip1.setPixelColor(led, 0, 0, 0);
00179                 }
00180             }
00181         }
00182     }
00183     
00184     //Panel 3
00185     if(z + size - 1 == (nCOLs - 1)) {
00186         for(int i = x; i < x + size; i++) {
00187             for(int j = y; j < y + size; j++) {
00188                 int led = getLedIndex(PANEL3, i, j);
00189                 if(on) {
00190                     ledStrip2.setPixelColor(led, r, g, b);
00191                 } else {
00192                     ledStrip2.setPixelColor(led, 0, 0, 0);
00193                 }
00194             }
00195         }
00196     }
00197 }
00198 
00199 /*
00200  * Updates the LED cube.
00201  */
00202 void LedCube::cubeUpdate() {
00203     updateLEDs(false, prevSize, prevPos[X], prevPos[Y], prevPos[Z]); //Turn off LEDs from previous state
00204     updateLEDs(true, size, pos[X], pos[Y], pos[Z]); //Turn on new LEDs for new state
00205     prevSize = size;
00206     prevPos[X] = pos[X];
00207     prevPos[Y] = pos[Y];
00208     prevPos[Z] = pos[Z];
00209     ledStrip1.show();
00210     ledStrip2.show();
00211     ledStrip1.startDMA();
00212     ledStrip2.startDMA();
00213 }
00214 
00215 /*
00216  * Moves the square inside the cube by deltaX in the x-axis,
00217  * by deltaY in the y-axis, and deltaZ in the z-axis. Returns
00218  * 1 if movement occured, and -1 if no movement occured.
00219  */
00220 int LedCube::move(int deltaX, int deltaY, int deltaZ) {
00221     int retVal = -1;
00222     //Moving in X direction
00223     if((pos[X] + size + deltaX - 1) < nCOLs && (pos[X] + deltaX) >= 0) {
00224         pos[X] += deltaX;
00225         if (deltaX != 0) retVal = 1;
00226     }
00227     
00228     //Moving in Y direction
00229     if((pos[Y] + size + deltaY - 1) < nCOLs && (pos[Y] + deltaY) >= 0) {
00230         pos[Y] += deltaY;
00231         if (deltaY != 0) retVal = 1;
00232     }
00233     
00234     //Moving in Z direction
00235     if((pos[Z] + size + deltaZ - 1) < nCOLs && (pos[Z] + deltaZ) >= 0) {
00236         pos[Z] += deltaZ;
00237         if (deltaZ != 0) retVal = 1;
00238     }
00239     return retVal;
00240 }
00241 
00242 /*
00243  * Changes the color of the square in the LED cube to the given hue.
00244  */
00245 void LedCube::changeColor(float hue){
00246     Colors::HSBtoRGB(hue, saturation, brightness, &r, &g, &b);
00247 }
00248 
00249 /*
00250  * Changes the size of the square in the LED cube to the given size.
00251  * The minimum size is 1, corresponding to a square of a single LED.
00252  * A size of 2 corresponds to a 2x2 LED square, 3 corresponds to 3x3 
00253  * and so forth. If square is on an edge, it moves accordingly in 
00254  * order to be able to increase size.
00255  */
00256 void LedCube::changeSize(int newSize) {
00257     if(newSize > 0 && newSize <= MAX_SIZE) {
00258         if ((pos[X] + newSize) <= nCOLs && (pos[Y] + newSize) <= nCOLs && (pos[Z] + newSize) <= nCOLs) {
00259             size = newSize;
00260             return;
00261         }
00262         else {
00263             int delta_x = 0;
00264             int delta_y = 0;
00265             int delta_z = 0;
00266             if ((pos[X] + newSize) > nCOLs) {
00267                 delta_x = nCOLs - (pos[X] + newSize);
00268             }
00269             if ((pos[Y] + newSize) > nCOLs) {
00270                 delta_y = nCOLs - (pos[Y] + newSize);
00271             }
00272             if ((pos[Z] + newSize) > nCOLs) {
00273                 delta_z = nCOLs - (pos[Z] + newSize);
00274             }
00275             move(delta_x, delta_y, delta_z);
00276             size = newSize;
00277             return;
00278         }
00279     }   
00280 }
00281 
00282 /*
00283  * Updates the LED cube given the size, hue and offsets in the X, Y and Z axes.
00284  */
00285 void LedCube::UpdateCube(int ledSize, int deltaX, int deltaY, int deltaZ, float hue) {
00286      changeSize(ledSize);
00287      move(deltaX, deltaY, deltaZ); 
00288      changeColor(hue); 
00289      cubeUpdate();
00290 }
00291 
00292 /*
00293  * Updates the LED cube given parameters in a CubeUpdateParameters struct.
00294  */
00295 void LedCube::UpdateCube2(CubeUpdateParameters cubeParams){
00296     UpdateCube(cubeParams.size, cubeParams.deltaX, cubeParams.deltaY, cubeParams.deltaZ, cubeParams.hue); 
00297 }
00298