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