![](/media/cache/profiles/IMG_0710-2.jpg.50x50_q85.jpg)
Light Show library for organic, calm, light display.
Dependencies: BLE_API mbed nRF51822
Fork of mbed_blinky by
main.cpp@10:fc61583e9e8f, 2015-10-11 (annotated)
- Committer:
- nargetdev
- Date:
- Sun Oct 11 03:12:07 2015 +0000
- Revision:
- 10:fc61583e9e8f
- Parent:
- 9:1bd0f7a2ee71
- Child:
- 11:2d3cc5459d5b
Fade out smooth -- KG
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan | 0:7dec7e9ac085 | 1 | #include "mbed.h" |
nargetdev | 8:5ed23e276c8a | 2 | |
nargetdev | 10:fc61583e9e8f | 3 | |
nargetdev | 10:fc61583e9e8f | 4 | #define HYSTERESIS_QUANTITY 1.57079632679 |
nargetdev | 9:1bd0f7a2ee71 | 5 | #define calibrationTime 10 |
nargetdev | 9:1bd0f7a2ee71 | 6 | unsigned long seed = 47; |
dan | 0:7dec7e9ac085 | 7 | |
nargetdev | 9:1bd0f7a2ee71 | 8 | typedef unsigned char byte; |
nargetdev | 9:1bd0f7a2ee71 | 9 | typedef unsigned int uint; |
nargetdev | 9:1bd0f7a2ee71 | 10 | |
nargetdev | 9:1bd0f7a2ee71 | 11 | PwmOut red(p18); |
nargetdev | 9:1bd0f7a2ee71 | 12 | PwmOut green(p19); |
nargetdev | 9:1bd0f7a2ee71 | 13 | PwmOut blue(p20); |
nargetdev | 9:1bd0f7a2ee71 | 14 | DigitalIn motion(p1); |
nargetdev | 9:1bd0f7a2ee71 | 15 | |
nargetdev | 9:1bd0f7a2ee71 | 16 | Serial pc(USBTX, USBRX); // tx, rx |
nargetdev | 9:1bd0f7a2ee71 | 17 | |
nargetdev | 9:1bd0f7a2ee71 | 18 | unsigned int hash(unsigned int x) { |
nargetdev | 9:1bd0f7a2ee71 | 19 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 20 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 21 | x = ((x >> 16) ^ x); |
nargetdev | 9:1bd0f7a2ee71 | 22 | seed*=2; |
nargetdev | 9:1bd0f7a2ee71 | 23 | seed+=17; |
nargetdev | 9:1bd0f7a2ee71 | 24 | return x%100; |
dan | 0:7dec7e9ac085 | 25 | } |
nargetdev | 8:5ed23e276c8a | 26 | |
nargetdev | 9:1bd0f7a2ee71 | 27 | void identify(unsigned int m){ |
nargetdev | 9:1bd0f7a2ee71 | 28 | unsigned int hashable; |
nargetdev | 9:1bd0f7a2ee71 | 29 | float write_me; |
nargetdev | 9:1bd0f7a2ee71 | 30 | |
nargetdev | 9:1bd0f7a2ee71 | 31 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 32 | write_me = hashable/100.0; |
nargetdev | 9:1bd0f7a2ee71 | 33 | red.write(write_me); |
nargetdev | 9:1bd0f7a2ee71 | 34 | pc.printf("r, g, b: %f\t", write_me); |
nargetdev | 9:1bd0f7a2ee71 | 35 | |
nargetdev | 9:1bd0f7a2ee71 | 36 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 37 | write_me = hashable/100.0; |
nargetdev | 9:1bd0f7a2ee71 | 38 | green.write(write_me); |
nargetdev | 9:1bd0f7a2ee71 | 39 | pc.printf("%f\t", write_me); |
nargetdev | 9:1bd0f7a2ee71 | 40 | |
nargetdev | 9:1bd0f7a2ee71 | 41 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 42 | write_me = hashable/100.0; |
nargetdev | 9:1bd0f7a2ee71 | 43 | blue.write(write_me); |
nargetdev | 9:1bd0f7a2ee71 | 44 | pc.printf("%f\n", write_me); |
nargetdev | 8:5ed23e276c8a | 45 | } |
nargetdev | 8:5ed23e276c8a | 46 | |
nargetdev | 9:1bd0f7a2ee71 | 47 | void calibrate(){ |
nargetdev | 9:1bd0f7a2ee71 | 48 | |
nargetdev | 9:1bd0f7a2ee71 | 49 | //give the sensor some time to calibrate |
nargetdev | 9:1bd0f7a2ee71 | 50 | pc.printf("calibrating sensor\n"); |
nargetdev | 9:1bd0f7a2ee71 | 51 | for(int i = 0; i < calibrationTime; i++){ |
nargetdev | 9:1bd0f7a2ee71 | 52 | pc.printf("."); |
nargetdev | 9:1bd0f7a2ee71 | 53 | identify(calibrationTime); |
nargetdev | 9:1bd0f7a2ee71 | 54 | wait(.25); |
nargetdev | 9:1bd0f7a2ee71 | 55 | } |
nargetdev | 9:1bd0f7a2ee71 | 56 | pc.printf(" done\n"); |
nargetdev | 9:1bd0f7a2ee71 | 57 | pc.printf("SENSOR ACTIVE\n"); |
nargetdev | 9:1bd0f7a2ee71 | 58 | wait(0.05); |
nargetdev | 9:1bd0f7a2ee71 | 59 | } |
nargetdev | 9:1bd0f7a2ee71 | 60 | |
nargetdev | 9:1bd0f7a2ee71 | 61 | void show(){ |
nargetdev | 9:1bd0f7a2ee71 | 62 | float in, rout, bout, gout; |
nargetdev | 9:1bd0f7a2ee71 | 63 | bool tit = true; |
nargetdev | 10:fc61583e9e8f | 64 | |
nargetdev | 10:fc61583e9e8f | 65 | unsigned int rgb = 0x0; |
nargetdev | 9:1bd0f7a2ee71 | 66 | pc.printf("showing...\n"); |
nargetdev | 8:5ed23e276c8a | 67 | |
nargetdev | 9:1bd0f7a2ee71 | 68 | float hysteresis = 0; |
nargetdev | 9:1bd0f7a2ee71 | 69 | |
nargetdev | 10:fc61583e9e8f | 70 | for (in = -1.571; in < hysteresis || (rgb ^ 0x7); in = in + 0.00628) |
nargetdev | 9:1bd0f7a2ee71 | 71 | { |
nargetdev | 9:1bd0f7a2ee71 | 72 | unsigned int i = motion; |
nargetdev | 9:1bd0f7a2ee71 | 73 | if (motion){ |
nargetdev | 10:fc61583e9e8f | 74 | hysteresis = in + HYSTERESIS_QUANTITY; |
nargetdev | 9:1bd0f7a2ee71 | 75 | pc.printf("\n-----\nextra_hysteresis\n-----\n"); |
nargetdev | 8:5ed23e276c8a | 76 | } |
nargetdev | 9:1bd0f7a2ee71 | 77 | tit = !tit; |
nargetdev | 10:fc61583e9e8f | 78 | if (!(rgb & 0x1)) |
nargetdev | 10:fc61583e9e8f | 79 | rout = sin(in) + 1.0; |
nargetdev | 10:fc61583e9e8f | 80 | else |
nargetdev | 10:fc61583e9e8f | 81 | rout = 0.0f; |
nargetdev | 10:fc61583e9e8f | 82 | if (!(rgb & 0x2)) |
nargetdev | 10:fc61583e9e8f | 83 | bout = sin(in + 1.3*(in+1.571) ) + 1.0; |
nargetdev | 10:fc61583e9e8f | 84 | else |
nargetdev | 10:fc61583e9e8f | 85 | bout = 0.0f; |
nargetdev | 10:fc61583e9e8f | 86 | if (!(rgb & 0x4)) |
nargetdev | 10:fc61583e9e8f | 87 | gout = sin(in + 4.2*(in+1.571) ) + 1.0; |
nargetdev | 10:fc61583e9e8f | 88 | else |
nargetdev | 10:fc61583e9e8f | 89 | gout = 0.0f; |
nargetdev | 10:fc61583e9e8f | 90 | |
nargetdev | 9:1bd0f7a2ee71 | 91 | if (tit) |
nargetdev | 9:1bd0f7a2ee71 | 92 | pc.printf("%f\t%f\t%f\n",rout,bout,gout); |
nargetdev | 9:1bd0f7a2ee71 | 93 | |
nargetdev | 10:fc61583e9e8f | 94 | red.write(rout/2.0); |
nargetdev | 10:fc61583e9e8f | 95 | green.write(gout/2.0); |
nargetdev | 10:fc61583e9e8f | 96 | blue.write(bout/2.0); |
nargetdev | 10:fc61583e9e8f | 97 | |
nargetdev | 10:fc61583e9e8f | 98 | if (in > hysteresis){ |
nargetdev | 10:fc61583e9e8f | 99 | if (rout < 0.01) |
nargetdev | 10:fc61583e9e8f | 100 | rgb |= 0x1; |
nargetdev | 10:fc61583e9e8f | 101 | if (gout < 0.01) |
nargetdev | 10:fc61583e9e8f | 102 | rgb |= 0x2; |
nargetdev | 10:fc61583e9e8f | 103 | if (bout < 0.01) |
nargetdev | 10:fc61583e9e8f | 104 | rgb |= 0x4; |
nargetdev | 10:fc61583e9e8f | 105 | } |
nargetdev | 9:1bd0f7a2ee71 | 106 | wait(.005); |
nargetdev | 9:1bd0f7a2ee71 | 107 | } |
nargetdev | 8:5ed23e276c8a | 108 | } |
nargetdev | 9:1bd0f7a2ee71 | 109 | |
nargetdev | 9:1bd0f7a2ee71 | 110 | int main() { |
nargetdev | 9:1bd0f7a2ee71 | 111 | red.period(0.001f); |
nargetdev | 9:1bd0f7a2ee71 | 112 | green.period(0.001f); |
nargetdev | 9:1bd0f7a2ee71 | 113 | blue.period(0.001f); |
nargetdev | 9:1bd0f7a2ee71 | 114 | |
nargetdev | 9:1bd0f7a2ee71 | 115 | pc.printf("Hello World!\n"); |
nargetdev | 9:1bd0f7a2ee71 | 116 | // Calibrate FIR and identify. |
nargetdev | 9:1bd0f7a2ee71 | 117 | calibrate(); |
nargetdev | 9:1bd0f7a2ee71 | 118 | |
nargetdev | 9:1bd0f7a2ee71 | 119 | // Clear the lights. Wait for victim. |
nargetdev | 9:1bd0f7a2ee71 | 120 | red.write(0.00f); |
nargetdev | 9:1bd0f7a2ee71 | 121 | green.write(0.00f); |
nargetdev | 9:1bd0f7a2ee71 | 122 | blue.write(0.00f); |
nargetdev | 9:1bd0f7a2ee71 | 123 | |
nargetdev | 9:1bd0f7a2ee71 | 124 | while(1){ |
nargetdev | 9:1bd0f7a2ee71 | 125 | unsigned int i = motion; |
nargetdev | 9:1bd0f7a2ee71 | 126 | if (i){ |
nargetdev | 9:1bd0f7a2ee71 | 127 | pc.printf("Motion detected.\n"); |
nargetdev | 9:1bd0f7a2ee71 | 128 | show(); |
nargetdev | 9:1bd0f7a2ee71 | 129 | pc.printf("End show.\n"); |
nargetdev | 9:1bd0f7a2ee71 | 130 | } |
nargetdev | 9:1bd0f7a2ee71 | 131 | else |
nargetdev | 9:1bd0f7a2ee71 | 132 | red.write(0.0f); |
nargetdev | 9:1bd0f7a2ee71 | 133 | green.write(0.0f); |
nargetdev | 9:1bd0f7a2ee71 | 134 | blue.write(0.0f); |
nargetdev | 9:1bd0f7a2ee71 | 135 | } |
nargetdev | 8:5ed23e276c8a | 136 | } |