working demo, with iPhone support
Dependencies: NeoPixels NeoStrip mbed
Fork of NeoPixels by
main.cpp@1:3b59b44884cd, 2014-04-25 (annotated)
- Committer:
- zchen311
- Date:
- Fri Apr 25 14:23:32 2014 +0000
- Revision:
- 1:3b59b44884cd
- Parent:
- 0:f38492690f0e
- Child:
- 2:b322ea7290a5
final project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aswild | 0:f38492690f0e | 1 | /* |
aswild | 0:f38492690f0e | 2 | * Adafruit NeoPixel 8x8 matrix example |
aswild | 0:f38492690f0e | 3 | * |
aswild | 0:f38492690f0e | 4 | * This program displays a couple simple patterns on an 8x8 NeoPixel matrix. |
aswild | 0:f38492690f0e | 5 | * |
aswild | 0:f38492690f0e | 6 | * 3 buttons are used for DigitalIns, 2 control the brightness up and down, |
aswild | 0:f38492690f0e | 7 | * and the third switches patterns |
aswild | 0:f38492690f0e | 8 | */ |
aswild | 0:f38492690f0e | 9 | |
aswild | 0:f38492690f0e | 10 | #include "mbed.h" |
aswild | 0:f38492690f0e | 11 | #include "NeoStrip.h" |
aswild | 0:f38492690f0e | 12 | #include "gt.h" |
zchen311 | 1:3b59b44884cd | 13 | #include "text.h" |
zchen311 | 1:3b59b44884cd | 14 | #define N 128 |
zchen311 | 1:3b59b44884cd | 15 | #define PATTERNS 4 |
zchen311 | 1:3b59b44884cd | 16 | #include <string> |
aswild | 0:f38492690f0e | 17 | int hueToRGB(float h); |
aswild | 0:f38492690f0e | 18 | void pattern0(); |
aswild | 0:f38492690f0e | 19 | void pattern1(); |
aswild | 0:f38492690f0e | 20 | void pattern2(); |
zchen311 | 1:3b59b44884cd | 21 | void fillBlue(); |
zchen311 | 1:3b59b44884cd | 22 | void progressBar(bool); |
zchen311 | 1:3b59b44884cd | 23 | int getIndex(int, int); |
zchen311 | 1:3b59b44884cd | 24 | void putChar(int row, int col, char ch, int color); |
zchen311 | 1:3b59b44884cd | 25 | void putString(std::string); |
zchen311 | 1:3b59b44884cd | 26 | void loopAscii(int count); |
zchen311 | 1:3b59b44884cd | 27 | void MarioBox(); |
zchen311 | 1:3b59b44884cd | 28 | void drawBox(int row);void emptyBox(int row); |
zchen311 | 1:3b59b44884cd | 29 | void putCharV(int row, int col, char ch, int color); |
aswild | 0:f38492690f0e | 30 | // array of function pointers to the various patterns |
zchen311 | 1:3b59b44884cd | 31 | //void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2}; |
aswild | 0:f38492690f0e | 32 | |
aswild | 0:f38492690f0e | 33 | NeoStrip strip(p18, N); |
aswild | 0:f38492690f0e | 34 | DigitalIn b1(p20); // brightness up |
aswild | 0:f38492690f0e | 35 | DigitalIn b2(p19); // brightness down |
aswild | 0:f38492690f0e | 36 | DigitalIn b3(p21); // next pattern |
aswild | 0:f38492690f0e | 37 | |
aswild | 0:f38492690f0e | 38 | // timer used for debugging |
aswild | 0:f38492690f0e | 39 | Timer timer; |
zchen311 | 1:3b59b44884cd | 40 | int chars[128]; |
zchen311 | 1:3b59b44884cd | 41 | char ch = 'A'; |
aswild | 0:f38492690f0e | 42 | |
aswild | 0:f38492690f0e | 43 | int main() |
aswild | 0:f38492690f0e | 44 | { |
aswild | 0:f38492690f0e | 45 | |
zchen311 | 1:3b59b44884cd | 46 | std::string emoji[9] = {"*_*", "^_^", "$_$", ">_<", "=_=", "+_=", ":)", ":(", ":3"}; |
aswild | 0:f38492690f0e | 47 | float bright = 0.2; // 20% is plenty for indoor use |
aswild | 0:f38492690f0e | 48 | strip.setBrightness(bright); // set default brightness |
zchen311 | 1:3b59b44884cd | 49 | int count = 0; |
aswild | 0:f38492690f0e | 50 | while (true) |
aswild | 0:f38492690f0e | 51 | { |
aswild | 0:f38492690f0e | 52 | timer.reset(); // use a timer to measure loop execution time for debugging purposes |
aswild | 0:f38492690f0e | 53 | timer.start(); // for this application, the main loop takes approximately 3ms to run |
zchen311 | 1:3b59b44884cd | 54 | progressBar(true); |
zchen311 | 1:3b59b44884cd | 55 | wait(5); |
zchen311 | 1:3b59b44884cd | 56 | progressBar(false); |
aswild | 0:f38492690f0e | 57 | |
zchen311 | 1:3b59b44884cd | 58 | // fillBlue(); |
aswild | 0:f38492690f0e | 59 | |
zchen311 | 1:3b59b44884cd | 60 | // write character |
zchen311 | 1:3b59b44884cd | 61 | // MarioBox(); |
zchen311 | 1:3b59b44884cd | 62 | // displayEmoji(); |
zchen311 | 1:3b59b44884cd | 63 | // loopAscii(count); |
zchen311 | 1:3b59b44884cd | 64 | // ch+=2; |
zchen311 | 1:3b59b44884cd | 65 | // putString(emoji[count%9]); |
zchen311 | 1:3b59b44884cd | 66 | // count++; |
zchen311 | 1:3b59b44884cd | 67 | wait(2); |
aswild | 0:f38492690f0e | 68 | timer.stop(); |
zchen311 | 1:3b59b44884cd | 69 | // print loop time if b3 is pressed |
aswild | 0:f38492690f0e | 70 | wait_ms(10); |
aswild | 0:f38492690f0e | 71 | } |
aswild | 0:f38492690f0e | 72 | } |
aswild | 0:f38492690f0e | 73 | |
aswild | 0:f38492690f0e | 74 | // pattern0 displays a static image |
aswild | 0:f38492690f0e | 75 | void pattern0() |
aswild | 0:f38492690f0e | 76 | { |
zchen311 | 1:3b59b44884cd | 77 | fillBlue(); |
zchen311 | 1:3b59b44884cd | 78 | // write character |
zchen311 | 1:3b59b44884cd | 79 | int color = 0xffff00; |
zchen311 | 1:3b59b44884cd | 80 | putChar(0,0,'G',color); |
zchen311 | 1:3b59b44884cd | 81 | putChar(0,6,'O',color); |
zchen311 | 1:3b59b44884cd | 82 | strip.setPixels(0, N, chars); |
aswild | 0:f38492690f0e | 83 | } |
aswild | 0:f38492690f0e | 84 | |
aswild | 0:f38492690f0e | 85 | // display a shifting rainbow, all colors have maximum |
aswild | 0:f38492690f0e | 86 | // saturation and value, with evenly spaced hue |
aswild | 0:f38492690f0e | 87 | void pattern1() |
aswild | 0:f38492690f0e | 88 | { |
aswild | 0:f38492690f0e | 89 | static float dh = 360.0 / N; |
aswild | 0:f38492690f0e | 90 | static float x = 0; |
aswild | 0:f38492690f0e | 91 | |
aswild | 0:f38492690f0e | 92 | for (int i = 0; i < N; i++) |
aswild | 0:f38492690f0e | 93 | strip.setPixel(i, hueToRGB((dh * i) - x)); |
aswild | 0:f38492690f0e | 94 | |
aswild | 0:f38492690f0e | 95 | x += 1; |
aswild | 0:f38492690f0e | 96 | if (x > 360) |
aswild | 0:f38492690f0e | 97 | x = 0; |
aswild | 0:f38492690f0e | 98 | } |
aswild | 0:f38492690f0e | 99 | |
aswild | 0:f38492690f0e | 100 | |
aswild | 0:f38492690f0e | 101 | |
aswild | 0:f38492690f0e | 102 | // Converts HSV to RGB with the given hue, assuming |
aswild | 0:f38492690f0e | 103 | // maximum saturation and value |
aswild | 0:f38492690f0e | 104 | int hueToRGB(float h) |
aswild | 0:f38492690f0e | 105 | { |
aswild | 0:f38492690f0e | 106 | // lots of floating point magic from the internet and scratching my head |
aswild | 0:f38492690f0e | 107 | float r, g, b; |
aswild | 0:f38492690f0e | 108 | if (h > 360) |
aswild | 0:f38492690f0e | 109 | h -= 360; |
aswild | 0:f38492690f0e | 110 | if (h < 0) |
aswild | 0:f38492690f0e | 111 | h += 360; |
aswild | 0:f38492690f0e | 112 | int i = (int)(h / 60.0); |
aswild | 0:f38492690f0e | 113 | float f = (h / 60.0) - i; |
aswild | 0:f38492690f0e | 114 | float q = 1 - f; |
aswild | 0:f38492690f0e | 115 | |
aswild | 0:f38492690f0e | 116 | switch (i % 6) |
aswild | 0:f38492690f0e | 117 | { |
aswild | 0:f38492690f0e | 118 | case 0: r = 1; g = f; b = 0; break; |
aswild | 0:f38492690f0e | 119 | case 1: r = q; g = 1; b = 0; break; |
aswild | 0:f38492690f0e | 120 | case 2: r = 0; g = 1; b = f; break; |
aswild | 0:f38492690f0e | 121 | case 3: r = 0; g = q; b = 1; break; |
aswild | 0:f38492690f0e | 122 | case 4: r = f; g = 0; b = 1; break; |
aswild | 0:f38492690f0e | 123 | case 5: r = 1; g = 0; b = q; break; |
aswild | 0:f38492690f0e | 124 | default: r = 0; g = 0; b = 0; break; |
aswild | 0:f38492690f0e | 125 | } |
aswild | 0:f38492690f0e | 126 | |
aswild | 0:f38492690f0e | 127 | // scale to integers and return the packed value |
aswild | 0:f38492690f0e | 128 | uint8_t R = (uint8_t)(r * 255); |
aswild | 0:f38492690f0e | 129 | uint8_t G = (uint8_t)(g * 255); |
aswild | 0:f38492690f0e | 130 | uint8_t B = (uint8_t)(b * 255); |
aswild | 0:f38492690f0e | 131 | |
aswild | 0:f38492690f0e | 132 | return (R << 16) | (G << 8) | B; |
aswild | 0:f38492690f0e | 133 | } |
aswild | 0:f38492690f0e | 134 | |
zchen311 | 1:3b59b44884cd | 135 | void fillBlue(){ |
zchen311 | 1:3b59b44884cd | 136 | for(int i = 0; i < N; i++){ |
zchen311 | 1:3b59b44884cd | 137 | chars[i] = 0x122446; |
zchen311 | 1:3b59b44884cd | 138 | } |
zchen311 | 1:3b59b44884cd | 139 | } |
zchen311 | 1:3b59b44884cd | 140 | |
zchen311 | 1:3b59b44884cd | 141 | int getIndex(int r, int c){ |
zchen311 | 1:3b59b44884cd | 142 | if(c < 8){ |
zchen311 | 1:3b59b44884cd | 143 | return (r * 8 + c); |
zchen311 | 1:3b59b44884cd | 144 | }else if(c > 15){ |
zchen311 | 1:3b59b44884cd | 145 | return -1; |
zchen311 | 1:3b59b44884cd | 146 | } |
zchen311 | 1:3b59b44884cd | 147 | else{ |
zchen311 | 1:3b59b44884cd | 148 | return (64 + r * 8 + (c-8)); |
zchen311 | 1:3b59b44884cd | 149 | } |
zchen311 | 1:3b59b44884cd | 150 | } |
zchen311 | 1:3b59b44884cd | 151 | |
zchen311 | 1:3b59b44884cd | 152 | |
zchen311 | 1:3b59b44884cd | 153 | void putChar(int row, int col, char ch, int color){ |
zchen311 | 1:3b59b44884cd | 154 | for(int r = 0; r < 8; r++){ |
zchen311 | 1:3b59b44884cd | 155 | for(int c = 0; c < 6; c++){ |
zchen311 | 1:3b59b44884cd | 156 | if(fontdata_6x8[ch * 48 + r * 6 +c]){ |
zchen311 | 1:3b59b44884cd | 157 | int idx = getIndex(row+r, col+c); |
zchen311 | 1:3b59b44884cd | 158 | if(idx != -1) |
zchen311 | 1:3b59b44884cd | 159 | chars[idx] = color; |
zchen311 | 1:3b59b44884cd | 160 | } |
zchen311 | 1:3b59b44884cd | 161 | } |
zchen311 | 1:3b59b44884cd | 162 | } |
zchen311 | 1:3b59b44884cd | 163 | } |
zchen311 | 1:3b59b44884cd | 164 | void putCharV(int row, int col, char ch, int color){ |
zchen311 | 1:3b59b44884cd | 165 | for(int r = 0; r < 8; r++){ |
zchen311 | 1:3b59b44884cd | 166 | for(int c = 0; c < 6; c++){ |
zchen311 | 1:3b59b44884cd | 167 | if(fontdata_6x8[ch * 48 + r * 6 +c]){ |
zchen311 | 1:3b59b44884cd | 168 | int idx = (row + r) *8 +( col +c); |
zchen311 | 1:3b59b44884cd | 169 | if(idx != -1) |
zchen311 | 1:3b59b44884cd | 170 | chars[idx] = color; |
zchen311 | 1:3b59b44884cd | 171 | } |
zchen311 | 1:3b59b44884cd | 172 | } |
zchen311 | 1:3b59b44884cd | 173 | } |
zchen311 | 1:3b59b44884cd | 174 | } |
zchen311 | 1:3b59b44884cd | 175 | |
zchen311 | 1:3b59b44884cd | 176 | void MarioBox(){ |
zchen311 | 1:3b59b44884cd | 177 | int color = 0xffff00; |
zchen311 | 1:3b59b44884cd | 178 | int red = 0xff0000; |
zchen311 | 1:3b59b44884cd | 179 | |
zchen311 | 1:3b59b44884cd | 180 | drawBox(8); |
zchen311 | 1:3b59b44884cd | 181 | strip.setPixels(0, N, chars); |
zchen311 | 1:3b59b44884cd | 182 | strip.write(); |
zchen311 | 1:3b59b44884cd | 183 | wait(5); |
zchen311 | 1:3b59b44884cd | 184 | |
zchen311 | 1:3b59b44884cd | 185 | for(int i = 1; i < 3; i++){ |
zchen311 | 1:3b59b44884cd | 186 | fillBlue(); |
zchen311 | 1:3b59b44884cd | 187 | drawBox(8-i); |
zchen311 | 1:3b59b44884cd | 188 | strip.setPixels(0, N, chars); |
zchen311 | 1:3b59b44884cd | 189 | strip.write(); |
zchen311 | 1:3b59b44884cd | 190 | wait(0.3); |
zchen311 | 1:3b59b44884cd | 191 | } |
zchen311 | 1:3b59b44884cd | 192 | for(int i = 0; i < 3; i++){ |
zchen311 | 1:3b59b44884cd | 193 | fillBlue(); |
zchen311 | 1:3b59b44884cd | 194 | drawBox(6 + i); |
zchen311 | 1:3b59b44884cd | 195 | strip.setPixels(0, N, chars); |
zchen311 | 1:3b59b44884cd | 196 | strip.write(); |
zchen311 | 1:3b59b44884cd | 197 | wait(0.3); |
zchen311 | 1:3b59b44884cd | 198 | } |
zchen311 | 1:3b59b44884cd | 199 | fillBlue(); |
zchen311 | 1:3b59b44884cd | 200 | emptyBox(8); |
zchen311 | 1:3b59b44884cd | 201 | putChar(0,0,'0',color); |
zchen311 | 1:3b59b44884cd | 202 | strip.setPixels(0, N, chars); |
zchen311 | 1:3b59b44884cd | 203 | strip.write(); |
zchen311 | 1:3b59b44884cd | 204 | wait(30); |
zchen311 | 1:3b59b44884cd | 205 | } |
zchen311 | 1:3b59b44884cd | 206 | void putString(std::string str){ |
zchen311 | 1:3b59b44884cd | 207 | int color = 0xffff00; |
zchen311 | 1:3b59b44884cd | 208 | for(int i = 0; i < str.length(); ++i){ |
zchen311 | 1:3b59b44884cd | 209 | putChar(0,5*i,str[i], color); |
zchen311 | 1:3b59b44884cd | 210 | } |
zchen311 | 1:3b59b44884cd | 211 | strip.setPixels(0, N, chars); |
zchen311 | 1:3b59b44884cd | 212 | strip.write(); |
zchen311 | 1:3b59b44884cd | 213 | |
zchen311 | 1:3b59b44884cd | 214 | } |
zchen311 | 1:3b59b44884cd | 215 | |
zchen311 | 1:3b59b44884cd | 216 | void loopAscii(int count){ |
zchen311 | 1:3b59b44884cd | 217 | int color = 0xffff00; |
zchen311 | 1:3b59b44884cd | 218 | putChar(0,0,ch+count,color); |
zchen311 | 1:3b59b44884cd | 219 | putChar(0,6,ch+count+1,color); |
zchen311 | 1:3b59b44884cd | 220 | strip.setPixels(0, N, chars); |
zchen311 | 1:3b59b44884cd | 221 | strip.write(); |
zchen311 | 1:3b59b44884cd | 222 | } |
zchen311 | 1:3b59b44884cd | 223 | |
zchen311 | 1:3b59b44884cd | 224 | void drawBox(int row){ |
zchen311 | 1:3b59b44884cd | 225 | for(int i = 0; i < 64; ++i) |
zchen311 | 1:3b59b44884cd | 226 | chars[row*8 + i] = 0x000000; |
zchen311 | 1:3b59b44884cd | 227 | putCharV(row,0,'?',0xffff00); |
zchen311 | 1:3b59b44884cd | 228 | } |
zchen311 | 1:3b59b44884cd | 229 | void emptyBox(int row){ |
zchen311 | 1:3b59b44884cd | 230 | for(int i = 0; i < 64; ++i) |
zchen311 | 1:3b59b44884cd | 231 | chars[row*8 + i] = 0x000000; |
zchen311 | 1:3b59b44884cd | 232 | } |
zchen311 | 1:3b59b44884cd | 233 | void progressBar(bool up){ |
zchen311 | 1:3b59b44884cd | 234 | int index; |
zchen311 | 1:3b59b44884cd | 235 | int init; |
zchen311 | 1:3b59b44884cd | 236 | int increment; |
zchen311 | 1:3b59b44884cd | 237 | int color; |
zchen311 | 1:3b59b44884cd | 238 | if(up == true){ |
zchen311 | 1:3b59b44884cd | 239 | init = 0; |
zchen311 | 1:3b59b44884cd | 240 | increment = 1; |
zchen311 | 1:3b59b44884cd | 241 | color = 0x122446; |
zchen311 | 1:3b59b44884cd | 242 | }else { |
zchen311 | 1:3b59b44884cd | 243 | init = 15; |
zchen311 | 1:3b59b44884cd | 244 | increment = -1; |
zchen311 | 1:3b59b44884cd | 245 | fillBlue(); |
zchen311 | 1:3b59b44884cd | 246 | color = 0x000000; |
zchen311 | 1:3b59b44884cd | 247 | } |
zchen311 | 1:3b59b44884cd | 248 | |
zchen311 | 1:3b59b44884cd | 249 | for(int x = 0; x < 16 ; x++){ |
zchen311 | 1:3b59b44884cd | 250 | |
zchen311 | 1:3b59b44884cd | 251 | for (int j = 0; j < 8; j++){ |
zchen311 | 1:3b59b44884cd | 252 | index = getIndex(j,init + increment * x); |
zchen311 | 1:3b59b44884cd | 253 | chars[index] = color; |
zchen311 | 1:3b59b44884cd | 254 | } |
zchen311 | 1:3b59b44884cd | 255 | strip.setPixels(0, N, chars); |
zchen311 | 1:3b59b44884cd | 256 | strip.write(); |
zchen311 | 1:3b59b44884cd | 257 | wait(1); |
zchen311 | 1:3b59b44884cd | 258 | } |
zchen311 | 1:3b59b44884cd | 259 | } |