Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MMA8451Q Multi_WS2811 NVIC_set_all_priorities TSI cc3000_hostdriver_mbedsocket mbed
Fork of CubicHand by
ledCube.cpp
00001 /* 00002 * Neopixel LED Cube library 00003 * by Robert Bui and Jose Oyola 00004 * UC Berkeley 2014 00005 */ 00006 00007 #include "main.h" 00008 #include "mbed.h" 00009 #include "WS2811.h" 00010 #include "Colors.h" 00011 #include "TSISensor.h" 00012 #include "MMA8451Q.h" 00013 #include "ledCube.h" 00014 00015 00016 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00017 00018 #define PANEL1 0 00019 #define PANEL2 1 00020 #define PANEL3 2 00021 #define nLEDs 200 //MAX_LEDS_PER_STRIP; 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, 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; 00049 saturation = 1.0; 00050 00051 ledStrip1.begin(); 00052 ledStrip2.begin(); 00053 } 00054 00055 /*Determines which LED should be lit based on its 00056 cartesian coordinate. The origin is the led at 00057 bottom left of panel 1 when using a three panel cube 00058 ________ 00059 / /| 00060 / 3 / | 00061 /_______/ 2| 00062 | | | 00063 | 1 | / 00064 | | 00065 -------- 00066 */ 00067 00068 int LedCube::getLedIndex(int panel, int x, int y) { 00069 if (panel == PANEL1) { 00070 if (y % 2 == 0) { 00071 return nCOLs*2 * y + x; 00072 } 00073 else { 00074 return nCOLs*2 * y + nCOLs + ((nCOLs - 1) - x); 00075 } 00076 } 00077 00078 if (panel == PANEL2) { 00079 if (y % 2 == 0) { 00080 return nCOLs*2 * y + nCOLs + x; 00081 } 00082 else { 00083 return nCOLs*2 * y + ((nCOLs - 1) - x); 00084 } 00085 } 00086 00087 if (panel == PANEL3) { 00088 if (y % 2 == 0) { 00089 return nCOLs * y + x; 00090 } 00091 else { 00092 return nCOLs * y + ((nCOLs - 1) - x); 00093 } 00094 } 00095 00096 else return -1; 00097 } 00098 00099 void LedCube::updateLEDs(bool on, int size, int x, int y, int z) { 00100 //Panel 1 00101 double bright; 00102 bright = 1.0 / ((y + 1) * (y + 1)); 00103 printf("Panel 1 Brightness: %f\r\n", bright); 00104 for(int i = x; i < x + size; i++) { 00105 for(int j = z; j < z + size; j++) { 00106 int led = getLedIndex(PANEL1, i, j); 00107 if(on) { 00108 ledStrip1.setPixelColor(led, r*bright, g*bright, b*bright); 00109 } else { 00110 ledStrip1.setPixelColor(led, 0, 0, 0); 00111 } 00112 } 00113 } 00114 00115 //Panel 2 00116 bright = 1.0 / (((nCOLs-1) - x - (size-1) + 1) * ((nCOLs-1) - x - (size-1) + 1)); 00117 printf("Panel 2 Brightness: %f\r\n", bright); 00118 for(int i = y; i < y + size; i++) { 00119 for(int j = z; j < z + size; j++) { 00120 int led = getLedIndex(PANEL2, i, j); 00121 if(on) { 00122 ledStrip1.setPixelColor(led, r*bright, g*bright, b*bright); 00123 } else { 00124 ledStrip1.setPixelColor(led, 0, 0, 0); 00125 } 00126 } 00127 } 00128 00129 //Panel 3 00130 bright = 1.0 / (((nCOLs-1) - z - (size-1) + 1) * ((nCOLs-1) - z - (size-1) + 1)); 00131 printf("Panel 3 Brightness: %f\r\n", bright); 00132 for(int i = x; i < x + size; i++) { 00133 for(int j = y; j < y + size; j++) { 00134 int led = getLedIndex(PANEL3, i, j); 00135 if(on) { 00136 ledStrip2.setPixelColor(led, r*bright, g*bright, b*bright); 00137 } else { 00138 ledStrip2.setPixelColor(led, 0, 0, 0); 00139 } 00140 } 00141 } 00142 } 00143 00144 void LedCube::updateLEDsOld(bool on, int size, int x, int y, int z) { 00145 //Panel 1 00146 if(y == 0) { 00147 for(int i = x; i < x + size; i++) { 00148 for(int j = z; j < z + size; j++) { 00149 int led = getLedIndex(PANEL1, i, j); 00150 if(on) { 00151 ledStrip1.setPixelColor(led, r, g, b); 00152 } else { 00153 ledStrip1.setPixelColor(led, 0, 0, 0); 00154 } 00155 } 00156 } 00157 } 00158 00159 //Panel 2 00160 if(x + size - 1 == (nCOLs - 1)) { 00161 for(int i = y; i < y + size; i++) { 00162 for(int j = z; j < z + size; j++) { 00163 int led = getLedIndex(PANEL2, i, j); 00164 if(on) { 00165 ledStrip1.setPixelColor(led, r, g, b); 00166 } else { 00167 ledStrip1.setPixelColor(led, 0, 0, 0); 00168 } 00169 } 00170 } 00171 } 00172 00173 //Panel 3 00174 if(z + size - 1 == (nCOLs - 1)) { 00175 for(int i = x; i < x + size; i++) { 00176 for(int j = y; j < y + size; j++) { 00177 int led = getLedIndex(PANEL3, i, j); 00178 if(on) { 00179 ledStrip2.setPixelColor(led, r, g, b); 00180 } else { 00181 ledStrip2.setPixelColor(led, 0, 0, 0); 00182 } 00183 } 00184 } 00185 } 00186 } 00187 00188 00189 void LedCube::cubeUpdate() { 00190 updateLEDs(false, prevSize, prevPos[X], prevPos[Y], prevPos[Z]); //Turn off LEDs from previous state 00191 updateLEDs(true, size, pos[X], pos[Y], pos[Z]); //Turn on new LEDs for new state 00192 prevSize = size; 00193 prevPos[X] = pos[X]; 00194 prevPos[Y] = pos[Y]; 00195 prevPos[Z] = pos[Z]; 00196 ledStrip1.show(); 00197 ledStrip2.show(); 00198 ledStrip1.startDMA(); 00199 ledStrip2.startDMA(); 00200 printf("Position: %d, %d, %d \t Size: %d\r\n", pos[X], pos[Y], pos[Z], size); 00201 } 00202 00203 int LedCube::move(int deltaX, int deltaY, int deltaZ) { 00204 int retVal = -1; 00205 //Moving in X direction 00206 if(pos[Y] == 0 || pos[Z] + size - 1 == (nCOLs - 1)) { //Making sure square is "stuck" to panel 1 or 3 00207 if((pos[X] + size + deltaX - 1) < nCOLs && (pos[X] + deltaX) >= 0) { 00208 pos[X] += deltaX; 00209 if (deltaX != 0) retVal = 1; 00210 } 00211 } 00212 00213 //Moving in Y direction 00214 if(pos[X] + size - 1 == (nCOLs - 1) || pos[Z] + size - 1 == (nCOLs - 1)) {//Making sure square is "stuck" to panel 2 or 3 00215 if((pos[Y] + size + deltaY - 1) < nCOLs && (pos[Y] + deltaY) >= 0) { 00216 pos[Y] += deltaY; 00217 if (deltaY != 0) retVal = 1; 00218 } 00219 } 00220 00221 //Moving in Z direction 00222 if(pos[X] + size - 1 == (nCOLs - 1) || pos[Y] == 0) {//Making sure square is "stuck" to panel 1 or 2 00223 if((pos[Z] + size + deltaZ - 1) < nCOLs && (pos[Z] + deltaZ) >= 0) { 00224 pos[Z] += deltaZ; 00225 if (deltaZ != 0) retVal = 1; 00226 } 00227 } 00228 return retVal; 00229 } 00230 00231 void LedCube::changeColor(float hue){ 00232 Colors::HSBtoRGB(hue, saturation, brightness, &r, &g, &b); 00233 } 00234 00235 void LedCube::changeSize(int newSize) { 00236 if((pos[X] + newSize) < nCOLs && (pos[Y] + newSize) < nCOLs && (pos[Z] + newSize) < nCOLs) { 00237 size = newSize; 00238 } 00239 } 00240 00241 void LedCube::UpdateCube(int size, int deltaX, int deltaY, int deltaZ, float hue) { 00242 changeSize(size); 00243 move(deltaX, deltaY, deltaZ); 00244 changeColor(hue); 00245 cubeUpdate(); 00246 } 00247 00248 00249
Generated on Tue Jul 12 2022 21:52:20 by
1.7.2
