Lights up shoes and sends steps to a person's phone over bluetooth
Fork of NeoPixels by
main.cpp@2:1a511cc48d0d, 2016-04-22 (annotated)
- Committer:
- hippi345
- Date:
- Fri Apr 22 15:25:51 2016 +0000
- Revision:
- 2:1a511cc48d0d
- Parent:
- 1:d235193680ab
final led shoes
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" |
aswild | 0:f38492690f0e | 3 | #include "gt.h" |
aswild | 0:f38492690f0e | 4 | #define N 64 |
aswild | 0:f38492690f0e | 5 | #define PATTERNS 3 |
hippi345 | 2:1a511cc48d0d | 6 | Ticker flipper; |
aswild | 0:f38492690f0e | 7 | int hueToRGB(float h); |
hippi345 | 1:d235193680ab | 8 | |
hippi345 | 1:d235193680ab | 9 | |
aswild | 0:f38492690f0e | 10 | |
hippi345 | 1:d235193680ab | 11 | AnalogIn photocell(p16); |
hippi345 | 1:d235193680ab | 12 | RawSerial dev(p28,p27); |
aswild | 0:f38492690f0e | 13 | NeoStrip strip(p18, N); |
hippi345 | 1:d235193680ab | 14 | DigitalOut myled(LED1); |
hippi345 | 2:1a511cc48d0d | 15 | InterruptIn vibration(p15); |
hippi345 | 1:d235193680ab | 16 | Serial pc(USBTX,USBRX); |
hippi345 | 1:d235193680ab | 17 | int count = 0; |
hippi345 | 1:d235193680ab | 18 | int senseValCur; |
hippi345 | 1:d235193680ab | 19 | int senseValPre = 1; |
hippi345 | 2:1a511cc48d0d | 20 | char bred=0; |
hippi345 | 2:1a511cc48d0d | 21 | char bgreen=0; |
hippi345 | 2:1a511cc48d0d | 22 | char bblue=0; |
aswild | 0:f38492690f0e | 23 | // timer used for debugging |
aswild | 0:f38492690f0e | 24 | Timer timer; |
hippi345 | 2:1a511cc48d0d | 25 | int hueToRGB2(int r, int g, int b); |
hippi345 | 2:1a511cc48d0d | 26 | |
hippi345 | 2:1a511cc48d0d | 27 | int hueToRGB2(int r, int g, int b) |
hippi345 | 2:1a511cc48d0d | 28 | { |
hippi345 | 2:1a511cc48d0d | 29 | // scale to integers and return the packed value |
hippi345 | 2:1a511cc48d0d | 30 | uint8_t R = (uint8_t)(r); |
hippi345 | 2:1a511cc48d0d | 31 | uint8_t G = (uint8_t)(g); |
hippi345 | 2:1a511cc48d0d | 32 | uint8_t B = (uint8_t)(b); |
hippi345 | 2:1a511cc48d0d | 33 | |
hippi345 | 2:1a511cc48d0d | 34 | return (R << 16) | (G << 8) | B; |
hippi345 | 2:1a511cc48d0d | 35 | } |
aswild | 0:f38492690f0e | 36 | |
aswild | 0:f38492690f0e | 37 | int hueToRGB(float h) |
aswild | 0:f38492690f0e | 38 | { |
aswild | 0:f38492690f0e | 39 | // lots of floating point magic from the internet and scratching my head |
aswild | 0:f38492690f0e | 40 | float r, g, b; |
aswild | 0:f38492690f0e | 41 | if (h > 360) |
aswild | 0:f38492690f0e | 42 | h -= 360; |
aswild | 0:f38492690f0e | 43 | if (h < 0) |
aswild | 0:f38492690f0e | 44 | h += 360; |
aswild | 0:f38492690f0e | 45 | int i = (int)(h / 60.0); |
aswild | 0:f38492690f0e | 46 | float f = (h / 60.0) - i; |
aswild | 0:f38492690f0e | 47 | float q = 1 - f; |
aswild | 0:f38492690f0e | 48 | |
aswild | 0:f38492690f0e | 49 | switch (i % 6) |
aswild | 0:f38492690f0e | 50 | { |
aswild | 0:f38492690f0e | 51 | case 0: r = 1; g = f; b = 0; break; |
aswild | 0:f38492690f0e | 52 | case 1: r = q; g = 1; b = 0; break; |
aswild | 0:f38492690f0e | 53 | case 2: r = 0; g = 1; b = f; break; |
aswild | 0:f38492690f0e | 54 | case 3: r = 0; g = q; b = 1; break; |
aswild | 0:f38492690f0e | 55 | case 4: r = f; g = 0; b = 1; break; |
aswild | 0:f38492690f0e | 56 | case 5: r = 1; g = 0; b = q; break; |
aswild | 0:f38492690f0e | 57 | default: r = 0; g = 0; b = 0; break; |
aswild | 0:f38492690f0e | 58 | } |
aswild | 0:f38492690f0e | 59 | // scale to integers and return the packed value |
aswild | 0:f38492690f0e | 60 | uint8_t R = (uint8_t)(r * 255); |
aswild | 0:f38492690f0e | 61 | uint8_t G = (uint8_t)(g * 255); |
aswild | 0:f38492690f0e | 62 | uint8_t B = (uint8_t)(b * 255); |
aswild | 0:f38492690f0e | 63 | |
aswild | 0:f38492690f0e | 64 | return (R << 16) | (G << 8) | B; |
aswild | 0:f38492690f0e | 65 | } |
aswild | 0:f38492690f0e | 66 | |
hippi345 | 2:1a511cc48d0d | 67 | void flip(){ |
hippi345 | 2:1a511cc48d0d | 68 | myled = !myled; |
hippi345 | 2:1a511cc48d0d | 69 | senseValCur = 0; |
hippi345 | 2:1a511cc48d0d | 70 | //if(senseValCur == 0) |
hippi345 | 2:1a511cc48d0d | 71 | //{ |
hippi345 | 2:1a511cc48d0d | 72 | strip.setBrightness(photocell/4); |
hippi345 | 1:d235193680ab | 73 | static float dh = 360.0 / N; |
hippi345 | 1:d235193680ab | 74 | static float x = 0; |
hippi345 | 1:d235193680ab | 75 | |
hippi345 | 1:d235193680ab | 76 | for (int i = 0; i < N; i++) |
hippi345 | 1:d235193680ab | 77 | {strip.setPixel(i, hueToRGB((dh * i) - x)); |
hippi345 | 1:d235193680ab | 78 | |
hippi345 | 1:d235193680ab | 79 | |
hippi345 | 1:d235193680ab | 80 | x += 1; |
hippi345 | 1:d235193680ab | 81 | if (x > 360) |
hippi345 | 1:d235193680ab | 82 | x = 0; |
hippi345 | 1:d235193680ab | 83 | strip.write(); |
hippi345 | 1:d235193680ab | 84 | wait(0.01);} |
hippi345 | 1:d235193680ab | 85 | |
hippi345 | 1:d235193680ab | 86 | |
hippi345 | 2:1a511cc48d0d | 87 | for (int i = 0; i < N; i++) |
hippi345 | 2:1a511cc48d0d | 88 | {strip.setPixel(i, hueToRGB2(bred,bgreen,bblue)); //blue red green |
hippi345 | 2:1a511cc48d0d | 89 | |
hippi345 | 2:1a511cc48d0d | 90 | |
hippi345 | 2:1a511cc48d0d | 91 | x += 1; |
hippi345 | 2:1a511cc48d0d | 92 | if (x > 360) |
hippi345 | 2:1a511cc48d0d | 93 | x = 0; |
hippi345 | 2:1a511cc48d0d | 94 | strip.write(); |
hippi345 | 2:1a511cc48d0d | 95 | wait(0.01);} |
hippi345 | 1:d235193680ab | 96 | count++; |
hippi345 | 1:d235193680ab | 97 | pc.printf("%i\n\r",count); |
hippi345 | 1:d235193680ab | 98 | dev.printf("%i\n\r",count); |
hippi345 | 2:1a511cc48d0d | 99 | //senseValCur = 1; |
hippi345 | 2:1a511cc48d0d | 100 | //} |
hippi345 | 2:1a511cc48d0d | 101 | senseValPre = senseValCur; |
hippi345 | 2:1a511cc48d0d | 102 | } |
hippi345 | 2:1a511cc48d0d | 103 | int main() |
hippi345 | 2:1a511cc48d0d | 104 | { |
hippi345 | 2:1a511cc48d0d | 105 | vibration.mode(PullUp); |
hippi345 | 2:1a511cc48d0d | 106 | int set = 0; |
hippi345 | 2:1a511cc48d0d | 107 | float bright = 0.2; // 20% is plenty for indoor use |
hippi345 | 2:1a511cc48d0d | 108 | //flipper.attach(&flip,5); |
hippi345 | 2:1a511cc48d0d | 109 | // while(set < 4){ |
hippi345 | 2:1a511cc48d0d | 110 | |
hippi345 | 2:1a511cc48d0d | 111 | //set++; |
hippi345 | 2:1a511cc48d0d | 112 | //} |
hippi345 | 2:1a511cc48d0d | 113 | |
hippi345 | 2:1a511cc48d0d | 114 | strip.setBrightness(bright); // set default brightness |
hippi345 | 2:1a511cc48d0d | 115 | while (true) |
hippi345 | 2:1a511cc48d0d | 116 | { |
hippi345 | 2:1a511cc48d0d | 117 | vibration.rise(&flip); |
hippi345 | 2:1a511cc48d0d | 118 | strip.setBrightness(photocell); |
hippi345 | 2:1a511cc48d0d | 119 | //myled = vibration.read(); |
hippi345 | 2:1a511cc48d0d | 120 | //myled = !myled; |
hippi345 | 2:1a511cc48d0d | 121 | //senseValCur = vibration.read(); |
hippi345 | 2:1a511cc48d0d | 122 | timer.reset(); // use a timer to measure loop execution time for debugging purposes |
hippi345 | 2:1a511cc48d0d | 123 | timer.start(); // for this application, the main loop takes approximately 3ms to run |
hippi345 | 2:1a511cc48d0d | 124 | if (dev.getc()=='!') { |
hippi345 | 2:1a511cc48d0d | 125 | if (dev.getc()=='C') { //color data packet |
hippi345 | 2:1a511cc48d0d | 126 | bred = dev.getc(); // RGB color values |
hippi345 | 2:1a511cc48d0d | 127 | bgreen = dev.getc(); |
hippi345 | 2:1a511cc48d0d | 128 | bblue = dev.getc(); |
hippi345 | 2:1a511cc48d0d | 129 | |
hippi345 | 2:1a511cc48d0d | 130 | if (dev.getc()==char(~('!' + 'C' + bred + bgreen + bblue))) { |
hippi345 | 2:1a511cc48d0d | 131 | static float x = 0; |
hippi345 | 2:1a511cc48d0d | 132 | |
hippi345 | 2:1a511cc48d0d | 133 | for (int i = 0; i < N; i++) |
hippi345 | 2:1a511cc48d0d | 134 | {strip.setPixel(i, hueToRGB2(bred,bgreen,bblue)); //blue red green |
hippi345 | 2:1a511cc48d0d | 135 | |
hippi345 | 2:1a511cc48d0d | 136 | |
hippi345 | 2:1a511cc48d0d | 137 | x += 1; |
hippi345 | 2:1a511cc48d0d | 138 | if (x > 360) |
hippi345 | 2:1a511cc48d0d | 139 | x = 0; |
hippi345 | 2:1a511cc48d0d | 140 | strip.write(); |
hippi345 | 2:1a511cc48d0d | 141 | wait(0.01);} |
hippi345 | 2:1a511cc48d0d | 142 | |
hippi345 | 2:1a511cc48d0d | 143 | } |
hippi345 | 1:d235193680ab | 144 | } |
hippi345 | 1:d235193680ab | 145 | } |
hippi345 | 2:1a511cc48d0d | 146 | |
hippi345 | 2:1a511cc48d0d | 147 | |
hippi345 | 2:1a511cc48d0d | 148 | |
hippi345 | 1:d235193680ab | 149 | } |
hippi345 | 2:1a511cc48d0d | 150 | } |
hippi345 | 1:d235193680ab | 151 | |
hippi345 | 1:d235193680ab | 152 | |
hippi345 | 1:d235193680ab | 153 |