working demo, with iPhone support
Dependencies: NeoPixels NeoStrip mbed
Fork of NeoPixels by
main.cpp@2:b322ea7290a5, 2014-04-28 (annotated)
- Committer:
- zchen311
- Date:
- Mon Apr 28 02:20:46 2014 +0000
- Revision:
- 2:b322ea7290a5
- Parent:
- 1:3b59b44884cd
working demo with iphone support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aswild | 0:f38492690f0e | 1 | #include "mbed.h" |
aswild | 0:f38492690f0e | 2 | #include "NeoStrip.h" |
zchen311 | 1:3b59b44884cd | 3 | #include "text.h" |
zchen311 | 1:3b59b44884cd | 4 | #define N 128 |
zchen311 | 1:3b59b44884cd | 5 | #define PATTERNS 4 |
zchen311 | 1:3b59b44884cd | 6 | #include <string> |
zchen311 | 2:b322ea7290a5 | 7 | #include "helper.h" |
zchen311 | 2:b322ea7290a5 | 8 | |
zchen311 | 2:b322ea7290a5 | 9 | |
zchen311 | 2:b322ea7290a5 | 10 | NeoStrip strip(p18, N); |
zchen311 | 2:b322ea7290a5 | 11 | |
zchen311 | 2:b322ea7290a5 | 12 | //callback function for incoming serial communication |
zchen311 | 2:b322ea7290a5 | 13 | void callback(); |
zchen311 | 2:b322ea7290a5 | 14 | //necessary functions for distanceDemo and bikeDemo |
zchen311 | 2:b322ea7290a5 | 15 | void blinkOn(); |
zchen311 | 2:b322ea7290a5 | 16 | void blinkOff(); |
zchen311 | 2:b322ea7290a5 | 17 | void patternLeft(); |
zchen311 | 2:b322ea7290a5 | 18 | void patternRight(); |
zchen311 | 2:b322ea7290a5 | 19 | void patternStop(); |
zchen311 | 2:b322ea7290a5 | 20 | void patternNone(); |
zchen311 | 2:b322ea7290a5 | 21 | void distanceDemo(); |
zchen311 | 2:b322ea7290a5 | 22 | void rainBow(); |
zchen311 | 2:b322ea7290a5 | 23 | void emojiDemo(); |
zchen311 | 2:b322ea7290a5 | 24 | //necessary functions for Animation Demo |
zchen311 | 1:3b59b44884cd | 25 | void fillBlue(); |
zchen311 | 1:3b59b44884cd | 26 | void progressBar(bool); |
zchen311 | 1:3b59b44884cd | 27 | void putChar(int row, int col, char ch, int color); |
zchen311 | 1:3b59b44884cd | 28 | void putString(std::string); |
zchen311 | 2:b322ea7290a5 | 29 | void loopAscii(); |
zchen311 | 1:3b59b44884cd | 30 | void MarioBox(); |
zchen311 | 1:3b59b44884cd | 31 | void drawBox(int row);void emptyBox(int row); |
zchen311 | 1:3b59b44884cd | 32 | void putCharV(int row, int col, char ch, int color); |
zchen311 | 2:b322ea7290a5 | 33 | int getIndex(int r, int c); |
zchen311 | 2:b322ea7290a5 | 34 | |
zchen311 | 2:b322ea7290a5 | 35 | |
aswild | 0:f38492690f0e | 36 | |
zchen311 | 2:b322ea7290a5 | 37 | //hardware initilization |
zchen311 | 2:b322ea7290a5 | 38 | Serial device(p13,p14); |
zchen311 | 2:b322ea7290a5 | 39 | AnalogIn ir(p20); |
zchen311 | 2:b322ea7290a5 | 40 | Serial pc(USBTX,USBRX); |
aswild | 0:f38492690f0e | 41 | |
zchen311 | 2:b322ea7290a5 | 42 | //variable initilization |
zchen311 | 2:b322ea7290a5 | 43 | Timer t; |
zchen311 | 1:3b59b44884cd | 44 | int chars[128]; |
zchen311 | 2:b322ea7290a5 | 45 | std::string emoji[9] = {"*_*", "^_^", "$_$", ">_<", "=_=", "+_=", ":)", ":(", ":3"}; |
zchen311 | 2:b322ea7290a5 | 46 | void (*patterns[])(void) = {&patternLeft, &patternRight, &patternStop, &patternNone}; |
zchen311 | 2:b322ea7290a5 | 47 | int bikeDir; |
zchen311 | 1:3b59b44884cd | 48 | char ch = 'A'; |
aswild | 0:f38492690f0e | 49 | |
aswild | 0:f38492690f0e | 50 | int main() |
zchen311 | 2:b322ea7290a5 | 51 | { |
zchen311 | 2:b322ea7290a5 | 52 | // std::string emoji[9] = {"*_*", "^_^", "$_$", ">_<", "=_=", "+_=", ":)", ":(", ":3"}; |
zchen311 | 2:b322ea7290a5 | 53 | float bright = 0.2; // 20% is plenty for indoor use |
zchen311 | 2:b322ea7290a5 | 54 | strip.setBrightness(bright); // set default brightness |
zchen311 | 2:b322ea7290a5 | 55 | |
zchen311 | 2:b322ea7290a5 | 56 | //setup serial device |
zchen311 | 2:b322ea7290a5 | 57 | device.baud(57600); |
zchen311 | 2:b322ea7290a5 | 58 | device.attach(&callback); |
zchen311 | 2:b322ea7290a5 | 59 | |
zchen311 | 2:b322ea7290a5 | 60 | //wait until phone is connected |
zchen311 | 2:b322ea7290a5 | 61 | while(1){ |
zchen311 | 2:b322ea7290a5 | 62 | if(device.readable()) break; |
zchen311 | 2:b322ea7290a5 | 63 | } |
zchen311 | 2:b322ea7290a5 | 64 | pc.printf("Device connected\n"); |
aswild | 0:f38492690f0e | 65 | |
zchen311 | 2:b322ea7290a5 | 66 | //ready for commands, clear serael buffer |
zchen311 | 2:b322ea7290a5 | 67 | while(device.readable()) device.getc(); |
zchen311 | 2:b322ea7290a5 | 68 | pc.printf("Device ready for commands;\n"); |
zchen311 | 2:b322ea7290a5 | 69 | //phone is disconnected, flash red light warning |
zchen311 | 2:b322ea7290a5 | 70 | while(1){ |
zchen311 | 2:b322ea7290a5 | 71 | if(t.read_ms()>3000){ |
zchen311 | 2:b322ea7290a5 | 72 | pc.printf("time passed i %d\n",t.read_ms()); |
zchen311 | 2:b322ea7290a5 | 73 | blinkOn(); |
zchen311 | 2:b322ea7290a5 | 74 | //progressBar(true); |
zchen311 | 2:b322ea7290a5 | 75 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 76 | blinkOff(); |
zchen311 | 2:b322ea7290a5 | 77 | // wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 78 | } |
zchen311 | 2:b322ea7290a5 | 79 | wait(5); |
zchen311 | 2:b322ea7290a5 | 80 | } |
aswild | 0:f38492690f0e | 81 | |
zchen311 | 2:b322ea7290a5 | 82 | |
zchen311 | 2:b322ea7290a5 | 83 | |
aswild | 0:f38492690f0e | 84 | } |
aswild | 0:f38492690f0e | 85 | |
zchen311 | 2:b322ea7290a5 | 86 | |
zchen311 | 2:b322ea7290a5 | 87 | void callback(){ |
zchen311 | 2:b322ea7290a5 | 88 | pc.printf("new commands arrive \n"); |
zchen311 | 2:b322ea7290a5 | 89 | t.reset(); |
zchen311 | 2:b322ea7290a5 | 90 | switch(device.getc()){ |
zchen311 | 2:b322ea7290a5 | 91 | |
zchen311 | 2:b322ea7290a5 | 92 | case PhoneDemo: |
zchen311 | 2:b322ea7290a5 | 93 | pc.printf("Lost Phone Demo\n"); |
zchen311 | 2:b322ea7290a5 | 94 | while(device.readable()) device.getc(); |
zchen311 | 2:b322ea7290a5 | 95 | //reply ack |
zchen311 | 2:b322ea7290a5 | 96 | device.putc('P'); |
zchen311 | 2:b322ea7290a5 | 97 | t.start(); |
zchen311 | 2:b322ea7290a5 | 98 | break; |
zchen311 | 2:b322ea7290a5 | 99 | |
zchen311 | 2:b322ea7290a5 | 100 | case MusicDemo: |
zchen311 | 2:b322ea7290a5 | 101 | pc.printf("Music Demo\n"); |
zchen311 | 2:b322ea7290a5 | 102 | rainBow(); |
zchen311 | 2:b322ea7290a5 | 103 | //progressBar(true); |
zchen311 | 2:b322ea7290a5 | 104 | //progressBar(false); |
zchen311 | 2:b322ea7290a5 | 105 | break; |
zchen311 | 2:b322ea7290a5 | 106 | |
zchen311 | 2:b322ea7290a5 | 107 | case DistanceDemo: |
zchen311 | 2:b322ea7290a5 | 108 | pc.printf("Distance Demo\n"); |
zchen311 | 2:b322ea7290a5 | 109 | device.putc('D'); |
zchen311 | 2:b322ea7290a5 | 110 | distanceDemo(); |
zchen311 | 2:b322ea7290a5 | 111 | break; |
zchen311 | 2:b322ea7290a5 | 112 | |
zchen311 | 2:b322ea7290a5 | 113 | case AnimationOneDemo: |
zchen311 | 2:b322ea7290a5 | 114 | MarioBox(); |
zchen311 | 2:b322ea7290a5 | 115 | break; |
zchen311 | 2:b322ea7290a5 | 116 | |
zchen311 | 2:b322ea7290a5 | 117 | case AnimationTwoDemo: |
zchen311 | 2:b322ea7290a5 | 118 | pc.printf("Emoji Demo\n"); |
zchen311 | 2:b322ea7290a5 | 119 | emojiDemo(); |
zchen311 | 2:b322ea7290a5 | 120 | break; |
zchen311 | 2:b322ea7290a5 | 121 | |
zchen311 | 2:b322ea7290a5 | 122 | case AnimationThreeDemo: |
zchen311 | 2:b322ea7290a5 | 123 | pc.printf("ascii demo\n"); |
zchen311 | 2:b322ea7290a5 | 124 | loopAscii(); |
zchen311 | 2:b322ea7290a5 | 125 | break; |
zchen311 | 2:b322ea7290a5 | 126 | |
zchen311 | 2:b322ea7290a5 | 127 | case LEFT: |
zchen311 | 2:b322ea7290a5 | 128 | pc.printf("left arrow\n"); |
zchen311 | 2:b322ea7290a5 | 129 | patternLeft(); |
zchen311 | 2:b322ea7290a5 | 130 | break; |
zchen311 | 2:b322ea7290a5 | 131 | |
zchen311 | 2:b322ea7290a5 | 132 | case RIGHT: |
zchen311 | 2:b322ea7290a5 | 133 | pc.printf("right arrow\n"); |
zchen311 | 2:b322ea7290a5 | 134 | |
zchen311 | 2:b322ea7290a5 | 135 | patternRight(); |
zchen311 | 2:b322ea7290a5 | 136 | break; |
zchen311 | 2:b322ea7290a5 | 137 | |
zchen311 | 2:b322ea7290a5 | 138 | case STOP: |
zchen311 | 2:b322ea7290a5 | 139 | pc.printf("stop\n"); |
zchen311 | 2:b322ea7290a5 | 140 | |
zchen311 | 2:b322ea7290a5 | 141 | patternStop(); |
zchen311 | 2:b322ea7290a5 | 142 | break; |
zchen311 | 2:b322ea7290a5 | 143 | |
zchen311 | 2:b322ea7290a5 | 144 | default: |
zchen311 | 2:b322ea7290a5 | 145 | break; |
aswild | 0:f38492690f0e | 146 | } |
aswild | 0:f38492690f0e | 147 | |
aswild | 0:f38492690f0e | 148 | |
aswild | 0:f38492690f0e | 149 | } |
aswild | 0:f38492690f0e | 150 | |
aswild | 0:f38492690f0e | 151 | |
aswild | 0:f38492690f0e | 152 | |
zchen311 | 2:b322ea7290a5 | 153 | |
aswild | 0:f38492690f0e | 154 | |
zchen311 | 2:b322ea7290a5 | 155 | |
aswild | 0:f38492690f0e | 156 | |
zchen311 | 1:3b59b44884cd | 157 | void fillBlue(){ |
zchen311 | 2:b322ea7290a5 | 158 | for(int i = 0; i < N; i++){ |
zchen311 | 2:b322ea7290a5 | 159 | chars[i] = 0x122446; |
zchen311 | 2:b322ea7290a5 | 160 | } |
zchen311 | 1:3b59b44884cd | 161 | } |
zchen311 | 1:3b59b44884cd | 162 | |
zchen311 | 1:3b59b44884cd | 163 | void putChar(int row, int col, char ch, int color){ |
zchen311 | 2:b322ea7290a5 | 164 | for(int r = 0; r < 8; r++){ |
zchen311 | 2:b322ea7290a5 | 165 | for(int c = 0; c < 6; c++){ |
zchen311 | 2:b322ea7290a5 | 166 | if(fontdata_6x8[ch * 48 + r * 6 +c]){ |
zchen311 | 2:b322ea7290a5 | 167 | int idx = getIndex(row+r, col+c); |
zchen311 | 2:b322ea7290a5 | 168 | if(idx != -1) |
zchen311 | 2:b322ea7290a5 | 169 | chars[idx] = color; |
zchen311 | 2:b322ea7290a5 | 170 | } |
zchen311 | 2:b322ea7290a5 | 171 | } |
zchen311 | 2:b322ea7290a5 | 172 | } |
zchen311 | 1:3b59b44884cd | 173 | } |
zchen311 | 1:3b59b44884cd | 174 | void putCharV(int row, int col, char ch, int color){ |
zchen311 | 2:b322ea7290a5 | 175 | for(int r = 0; r < 8; r++){ |
zchen311 | 2:b322ea7290a5 | 176 | for(int c = 0; c < 6; c++){ |
zchen311 | 2:b322ea7290a5 | 177 | if(fontdata_6x8[ch * 48 + r * 6 +c]){ |
zchen311 | 2:b322ea7290a5 | 178 | int idx = (row + r) *8 +( col +c); |
zchen311 | 2:b322ea7290a5 | 179 | if(idx != -1) |
zchen311 | 2:b322ea7290a5 | 180 | chars[idx] = color; |
zchen311 | 2:b322ea7290a5 | 181 | } |
zchen311 | 2:b322ea7290a5 | 182 | } |
zchen311 | 2:b322ea7290a5 | 183 | } |
zchen311 | 1:3b59b44884cd | 184 | } |
zchen311 | 1:3b59b44884cd | 185 | |
zchen311 | 1:3b59b44884cd | 186 | void MarioBox(){ |
zchen311 | 2:b322ea7290a5 | 187 | int color = 0xffff00; |
zchen311 | 2:b322ea7290a5 | 188 | // int red = 0xff0000; |
zchen311 | 2:b322ea7290a5 | 189 | |
zchen311 | 2:b322ea7290a5 | 190 | drawBox(8); |
zchen311 | 2:b322ea7290a5 | 191 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 192 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 193 | wait(5); |
zchen311 | 1:3b59b44884cd | 194 | |
zchen311 | 2:b322ea7290a5 | 195 | for(int i = 1; i < 3; i++){ |
zchen311 | 2:b322ea7290a5 | 196 | fillBlue(); |
zchen311 | 2:b322ea7290a5 | 197 | drawBox(8-i); |
zchen311 | 2:b322ea7290a5 | 198 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 199 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 200 | wait(0.3); |
zchen311 | 2:b322ea7290a5 | 201 | } |
zchen311 | 2:b322ea7290a5 | 202 | for(int i = 0; i < 3; i++){ |
zchen311 | 2:b322ea7290a5 | 203 | fillBlue(); |
zchen311 | 2:b322ea7290a5 | 204 | drawBox(6 + i); |
zchen311 | 2:b322ea7290a5 | 205 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 206 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 207 | wait(0.3); |
zchen311 | 2:b322ea7290a5 | 208 | } |
zchen311 | 2:b322ea7290a5 | 209 | fillBlue(); |
zchen311 | 2:b322ea7290a5 | 210 | emptyBox(8); |
zchen311 | 2:b322ea7290a5 | 211 | putChar(0,0,'0',color); |
zchen311 | 2:b322ea7290a5 | 212 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 213 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 214 | wait(5); |
zchen311 | 2:b322ea7290a5 | 215 | strip.clear(); |
zchen311 | 2:b322ea7290a5 | 216 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 217 | |
zchen311 | 1:3b59b44884cd | 218 | } |
zchen311 | 1:3b59b44884cd | 219 | void putString(std::string str){ |
zchen311 | 2:b322ea7290a5 | 220 | int color = 0xffff00; |
zchen311 | 2:b322ea7290a5 | 221 | for(int i = 0; i < str.length(); ++i){ |
zchen311 | 2:b322ea7290a5 | 222 | putChar(0,5*i,str[i], color); |
zchen311 | 2:b322ea7290a5 | 223 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 224 | } |
zchen311 | 2:b322ea7290a5 | 225 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 226 | wait_ms(100); |
zchen311 | 2:b322ea7290a5 | 227 | } |
zchen311 | 2:b322ea7290a5 | 228 | |
zchen311 | 2:b322ea7290a5 | 229 | void loopAscii(){ |
zchen311 | 2:b322ea7290a5 | 230 | int color = 0xffff00; |
zchen311 | 2:b322ea7290a5 | 231 | char ch = 'A'; |
zchen311 | 2:b322ea7290a5 | 232 | int count = 50; |
zchen311 | 2:b322ea7290a5 | 233 | |
zchen311 | 2:b322ea7290a5 | 234 | for(int i = 0; i < count; i+=2) { |
zchen311 | 2:b322ea7290a5 | 235 | putChar(0,0,ch+i,color); |
zchen311 | 2:b322ea7290a5 | 236 | putChar(0,7,ch+i+1,color); |
zchen311 | 2:b322ea7290a5 | 237 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 238 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 239 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 240 | } |
zchen311 | 1:3b59b44884cd | 241 | |
zchen311 | 1:3b59b44884cd | 242 | } |
zchen311 | 1:3b59b44884cd | 243 | |
zchen311 | 1:3b59b44884cd | 244 | void drawBox(int row){ |
zchen311 | 2:b322ea7290a5 | 245 | for(int i = 0; i < 64; ++i) |
zchen311 | 2:b322ea7290a5 | 246 | chars[row*8 + i] = 0x000000; |
zchen311 | 2:b322ea7290a5 | 247 | putCharV(row,0,'?',0xffff00); |
zchen311 | 1:3b59b44884cd | 248 | } |
zchen311 | 1:3b59b44884cd | 249 | void emptyBox(int row){ |
zchen311 | 2:b322ea7290a5 | 250 | for(int i = 0; i < 64; ++i) |
zchen311 | 2:b322ea7290a5 | 251 | chars[row*8 + i] = 0x000000; |
zchen311 | 1:3b59b44884cd | 252 | } |
zchen311 | 1:3b59b44884cd | 253 | void progressBar(bool up){ |
zchen311 | 2:b322ea7290a5 | 254 | int index; |
zchen311 | 2:b322ea7290a5 | 255 | int init; |
zchen311 | 2:b322ea7290a5 | 256 | int increment; |
zchen311 | 2:b322ea7290a5 | 257 | int color; |
zchen311 | 2:b322ea7290a5 | 258 | if(up == true){ |
zchen311 | 2:b322ea7290a5 | 259 | init = 0; |
zchen311 | 2:b322ea7290a5 | 260 | increment = 1; |
zchen311 | 2:b322ea7290a5 | 261 | color = 0x122446; |
zchen311 | 2:b322ea7290a5 | 262 | }else { |
zchen311 | 2:b322ea7290a5 | 263 | init = 15; |
zchen311 | 2:b322ea7290a5 | 264 | increment = -1; |
zchen311 | 2:b322ea7290a5 | 265 | fillBlue(); |
zchen311 | 2:b322ea7290a5 | 266 | color = 0x000000; |
zchen311 | 2:b322ea7290a5 | 267 | } |
zchen311 | 2:b322ea7290a5 | 268 | |
zchen311 | 2:b322ea7290a5 | 269 | for(int x = 0; x < 16 ; x++){ |
zchen311 | 2:b322ea7290a5 | 270 | |
zchen311 | 2:b322ea7290a5 | 271 | for (int j = 0; j < 8; j++){ |
zchen311 | 2:b322ea7290a5 | 272 | index = getIndex(j,init + increment * x); |
zchen311 | 2:b322ea7290a5 | 273 | chars[index] = color; |
zchen311 | 2:b322ea7290a5 | 274 | } |
zchen311 | 2:b322ea7290a5 | 275 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 276 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 277 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 278 | } |
zchen311 | 2:b322ea7290a5 | 279 | } |
zchen311 | 2:b322ea7290a5 | 280 | |
zchen311 | 2:b322ea7290a5 | 281 | |
zchen311 | 2:b322ea7290a5 | 282 | void rainBow(){ |
zchen311 | 2:b322ea7290a5 | 283 | int index; |
zchen311 | 2:b322ea7290a5 | 284 | unsigned int black = 0x000000; |
zchen311 | 2:b322ea7290a5 | 285 | unsigned int color[7] = {0x5b00ff,0xff0d19, 0xf0ff03, 0x17ff29, 0x1fffee,0x0d65ff, 0xfb12ff}; |
zchen311 | 2:b322ea7290a5 | 286 | int init = 0; |
zchen311 | 2:b322ea7290a5 | 287 | int increment = 1; |
zchen311 | 2:b322ea7290a5 | 288 | |
zchen311 | 2:b322ea7290a5 | 289 | for(int x = 0; x < 16 ; x++){ |
zchen311 | 2:b322ea7290a5 | 290 | for (int j = 0; j < 8; j++){ |
zchen311 | 2:b322ea7290a5 | 291 | index = getIndex(j,init + increment * x); |
zchen311 | 2:b322ea7290a5 | 292 | chars[index] = color[index%7]; |
zchen311 | 2:b322ea7290a5 | 293 | } |
zchen311 | 2:b322ea7290a5 | 294 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 295 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 296 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 297 | } |
zchen311 | 2:b322ea7290a5 | 298 | |
zchen311 | 2:b322ea7290a5 | 299 | for(int x = 15; x >= 0 ; x--){ |
zchen311 | 2:b322ea7290a5 | 300 | for (int j = 0; j < 8; j++){ |
zchen311 | 2:b322ea7290a5 | 301 | index = getIndex(j,init + increment * x); |
zchen311 | 2:b322ea7290a5 | 302 | chars[index] = black; |
zchen311 | 2:b322ea7290a5 | 303 | } |
zchen311 | 2:b322ea7290a5 | 304 | |
zchen311 | 2:b322ea7290a5 | 305 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 306 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 307 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 308 | } |
zchen311 | 2:b322ea7290a5 | 309 | |
zchen311 | 2:b322ea7290a5 | 310 | } |
zchen311 | 2:b322ea7290a5 | 311 | |
zchen311 | 2:b322ea7290a5 | 312 | |
zchen311 | 2:b322ea7290a5 | 313 | |
zchen311 | 2:b322ea7290a5 | 314 | void patternLeft() |
zchen311 | 2:b322ea7290a5 | 315 | { |
zchen311 | 2:b322ea7290a5 | 316 | for(int j=0;j<3;j++){ |
zchen311 | 2:b322ea7290a5 | 317 | for (int i = 0; i < N; i++) |
zchen311 | 2:b322ea7290a5 | 318 | { |
zchen311 | 2:b322ea7290a5 | 319 | if (maskLeft128[i] == 1) |
zchen311 | 2:b322ea7290a5 | 320 | chars[i]=0x00FF00; |
zchen311 | 2:b322ea7290a5 | 321 | else |
zchen311 | 2:b322ea7290a5 | 322 | chars[i]=0x000000; |
zchen311 | 2:b322ea7290a5 | 323 | } |
zchen311 | 2:b322ea7290a5 | 324 | |
zchen311 | 2:b322ea7290a5 | 325 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 326 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 327 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 328 | strip.clear(); |
zchen311 | 2:b322ea7290a5 | 329 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 330 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 331 | |
zchen311 | 2:b322ea7290a5 | 332 | } |
zchen311 | 2:b322ea7290a5 | 333 | } |
zchen311 | 2:b322ea7290a5 | 334 | |
zchen311 | 2:b322ea7290a5 | 335 | void patternRight() |
zchen311 | 2:b322ea7290a5 | 336 | { |
zchen311 | 2:b322ea7290a5 | 337 | for(int j=0;j<3;j++){ |
zchen311 | 2:b322ea7290a5 | 338 | for (int i = 0; i < N; i++) |
zchen311 | 2:b322ea7290a5 | 339 | { |
zchen311 | 2:b322ea7290a5 | 340 | if (maskRight128[i] == 1) |
zchen311 | 2:b322ea7290a5 | 341 | chars[i]=0x00FF00; |
zchen311 | 2:b322ea7290a5 | 342 | else |
zchen311 | 2:b322ea7290a5 | 343 | chars[i]=0x000000; |
zchen311 | 2:b322ea7290a5 | 344 | } |
zchen311 | 2:b322ea7290a5 | 345 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 346 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 347 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 348 | strip.clear(); |
zchen311 | 2:b322ea7290a5 | 349 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 350 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 351 | |
zchen311 | 2:b322ea7290a5 | 352 | } |
zchen311 | 2:b322ea7290a5 | 353 | |
zchen311 | 2:b322ea7290a5 | 354 | } |
zchen311 | 2:b322ea7290a5 | 355 | |
zchen311 | 2:b322ea7290a5 | 356 | void patternStop() |
zchen311 | 2:b322ea7290a5 | 357 | { |
zchen311 | 2:b322ea7290a5 | 358 | for(int j=0;j<3;j++){ |
zchen311 | 2:b322ea7290a5 | 359 | for (int i = 0; i < N; i++) |
zchen311 | 2:b322ea7290a5 | 360 | { |
zchen311 | 2:b322ea7290a5 | 361 | if (maskStop128[i] == 1) |
zchen311 | 2:b322ea7290a5 | 362 | chars[i]=0xFF0000; |
zchen311 | 2:b322ea7290a5 | 363 | else |
zchen311 | 2:b322ea7290a5 | 364 | chars[i]=0x000000; |
zchen311 | 2:b322ea7290a5 | 365 | } |
zchen311 | 2:b322ea7290a5 | 366 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 367 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 368 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 369 | strip.clear(); |
zchen311 | 2:b322ea7290a5 | 370 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 371 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 372 | |
zchen311 | 2:b322ea7290a5 | 373 | } |
zchen311 | 2:b322ea7290a5 | 374 | |
zchen311 | 2:b322ea7290a5 | 375 | } |
zchen311 | 2:b322ea7290a5 | 376 | |
zchen311 | 2:b322ea7290a5 | 377 | void patternNone() |
zchen311 | 2:b322ea7290a5 | 378 | { |
zchen311 | 2:b322ea7290a5 | 379 | strip.clear(); |
zchen311 | 2:b322ea7290a5 | 380 | } |
zchen311 | 2:b322ea7290a5 | 381 | |
zchen311 | 2:b322ea7290a5 | 382 | |
zchen311 | 2:b322ea7290a5 | 383 | |
zchen311 | 2:b322ea7290a5 | 384 | void blinkOn() |
zchen311 | 2:b322ea7290a5 | 385 | { |
zchen311 | 2:b322ea7290a5 | 386 | strip.clear(); |
zchen311 | 2:b322ea7290a5 | 387 | for (int i = 0; i < N; i++) |
zchen311 | 2:b322ea7290a5 | 388 | { |
zchen311 | 2:b322ea7290a5 | 389 | chars[i]=0xFF0000; |
zchen311 | 2:b322ea7290a5 | 390 | } |
zchen311 | 2:b322ea7290a5 | 391 | |
zchen311 | 2:b322ea7290a5 | 392 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 393 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 394 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 395 | } |
zchen311 | 2:b322ea7290a5 | 396 | |
zchen311 | 2:b322ea7290a5 | 397 | void blinkOff() |
zchen311 | 2:b322ea7290a5 | 398 | { |
zchen311 | 2:b322ea7290a5 | 399 | strip.clear(); |
zchen311 | 2:b322ea7290a5 | 400 | for (int i = 0; i < N; i++) |
zchen311 | 2:b322ea7290a5 | 401 | { |
zchen311 | 2:b322ea7290a5 | 402 | chars[i]=0x000000; |
zchen311 | 2:b322ea7290a5 | 403 | } |
zchen311 | 2:b322ea7290a5 | 404 | strip.setPixels(0, N, chars); |
zchen311 | 2:b322ea7290a5 | 405 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 406 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 407 | } |
zchen311 | 2:b322ea7290a5 | 408 | |
zchen311 | 2:b322ea7290a5 | 409 | |
zchen311 | 2:b322ea7290a5 | 410 | |
zchen311 | 2:b322ea7290a5 | 411 | void distanceDemo(){ |
zchen311 | 2:b322ea7290a5 | 412 | float val = 21/ir; |
zchen311 | 2:b322ea7290a5 | 413 | pc.printf("dist avg=%f\n",val); |
zchen311 | 2:b322ea7290a5 | 414 | |
zchen311 | 2:b322ea7290a5 | 415 | if(val<70) { |
zchen311 | 2:b322ea7290a5 | 416 | blinkOn(); |
zchen311 | 2:b322ea7290a5 | 417 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 418 | blinkOff(); |
zchen311 | 2:b322ea7290a5 | 419 | } |
zchen311 | 2:b322ea7290a5 | 420 | |
zchen311 | 2:b322ea7290a5 | 421 | } |
zchen311 | 2:b322ea7290a5 | 422 | |
zchen311 | 2:b322ea7290a5 | 423 | void emojiDemo(){ |
zchen311 | 2:b322ea7290a5 | 424 | for(int i=0;i<8;i++){ |
zchen311 | 2:b322ea7290a5 | 425 | putString(emoji[i]); |
zchen311 | 2:b322ea7290a5 | 426 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 427 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 428 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 429 | strip.clear(); |
zchen311 | 2:b322ea7290a5 | 430 | strip.write(); |
zchen311 | 2:b322ea7290a5 | 431 | wait_ms(200); |
zchen311 | 2:b322ea7290a5 | 432 | } |
zchen311 | 2:b322ea7290a5 | 433 | } |
zchen311 | 2:b322ea7290a5 | 434 | |
zchen311 | 2:b322ea7290a5 | 435 | |
zchen311 | 2:b322ea7290a5 | 436 | int getIndex(int r, int c){ |
zchen311 | 2:b322ea7290a5 | 437 | if(c < 8){ |
zchen311 | 2:b322ea7290a5 | 438 | return (r * 8 + c); |
zchen311 | 2:b322ea7290a5 | 439 | }else if(c > 15){ |
zchen311 | 2:b322ea7290a5 | 440 | return -1; |
zchen311 | 2:b322ea7290a5 | 441 | } |
zchen311 | 2:b322ea7290a5 | 442 | else{ |
zchen311 | 2:b322ea7290a5 | 443 | return (64 + r * 8 + (c-8)); |
zchen311 | 2:b322ea7290a5 | 444 | } |
zchen311 | 1:3b59b44884cd | 445 | } |