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