![](/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@9:1bd0f7a2ee71, 2015-10-11 (annotated)
- Committer:
- nargetdev
- Date:
- Sun Oct 11 01:15:52 2015 +0000
- Revision:
- 9:1bd0f7a2ee71
- Parent:
- 8:5ed23e276c8a
- Child:
- 10:fc61583e9e8f
getting closer... adding continuous motion
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 | 9:1bd0f7a2ee71 | 3 | #define calibrationTime 10 |
nargetdev | 9:1bd0f7a2ee71 | 4 | unsigned long seed = 47; |
dan | 0:7dec7e9ac085 | 5 | |
nargetdev | 9:1bd0f7a2ee71 | 6 | typedef unsigned char byte; |
nargetdev | 9:1bd0f7a2ee71 | 7 | typedef unsigned int uint; |
nargetdev | 9:1bd0f7a2ee71 | 8 | |
nargetdev | 9:1bd0f7a2ee71 | 9 | PwmOut red(p18); |
nargetdev | 9:1bd0f7a2ee71 | 10 | PwmOut green(p19); |
nargetdev | 9:1bd0f7a2ee71 | 11 | PwmOut blue(p20); |
nargetdev | 9:1bd0f7a2ee71 | 12 | DigitalIn motion(p1); |
nargetdev | 9:1bd0f7a2ee71 | 13 | |
nargetdev | 9:1bd0f7a2ee71 | 14 | Serial pc(USBTX, USBRX); // tx, rx |
nargetdev | 9:1bd0f7a2ee71 | 15 | |
nargetdev | 9:1bd0f7a2ee71 | 16 | unsigned int hash(unsigned int x) { |
nargetdev | 9:1bd0f7a2ee71 | 17 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 18 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 19 | x = ((x >> 16) ^ x); |
nargetdev | 9:1bd0f7a2ee71 | 20 | seed*=2; |
nargetdev | 9:1bd0f7a2ee71 | 21 | seed+=17; |
nargetdev | 9:1bd0f7a2ee71 | 22 | return x%100; |
dan | 0:7dec7e9ac085 | 23 | } |
nargetdev | 8:5ed23e276c8a | 24 | |
nargetdev | 9:1bd0f7a2ee71 | 25 | void identify(unsigned int m){ |
nargetdev | 9:1bd0f7a2ee71 | 26 | unsigned int hashable; |
nargetdev | 9:1bd0f7a2ee71 | 27 | float write_me; |
nargetdev | 9:1bd0f7a2ee71 | 28 | |
nargetdev | 9:1bd0f7a2ee71 | 29 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 30 | write_me = hashable/100.0; |
nargetdev | 9:1bd0f7a2ee71 | 31 | red.write(write_me); |
nargetdev | 9:1bd0f7a2ee71 | 32 | pc.printf("r, g, b: %f\t", write_me); |
nargetdev | 9:1bd0f7a2ee71 | 33 | |
nargetdev | 9:1bd0f7a2ee71 | 34 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 35 | write_me = hashable/100.0; |
nargetdev | 9:1bd0f7a2ee71 | 36 | green.write(write_me); |
nargetdev | 9:1bd0f7a2ee71 | 37 | pc.printf("%f\t", write_me); |
nargetdev | 9:1bd0f7a2ee71 | 38 | |
nargetdev | 9:1bd0f7a2ee71 | 39 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 40 | write_me = hashable/100.0; |
nargetdev | 9:1bd0f7a2ee71 | 41 | blue.write(write_me); |
nargetdev | 9:1bd0f7a2ee71 | 42 | pc.printf("%f\n", write_me); |
nargetdev | 8:5ed23e276c8a | 43 | } |
nargetdev | 8:5ed23e276c8a | 44 | |
nargetdev | 9:1bd0f7a2ee71 | 45 | void calibrate(){ |
nargetdev | 9:1bd0f7a2ee71 | 46 | |
nargetdev | 9:1bd0f7a2ee71 | 47 | //give the sensor some time to calibrate |
nargetdev | 9:1bd0f7a2ee71 | 48 | pc.printf("calibrating sensor\n"); |
nargetdev | 9:1bd0f7a2ee71 | 49 | for(int i = 0; i < calibrationTime; i++){ |
nargetdev | 9:1bd0f7a2ee71 | 50 | pc.printf("."); |
nargetdev | 9:1bd0f7a2ee71 | 51 | identify(calibrationTime); |
nargetdev | 9:1bd0f7a2ee71 | 52 | wait(.25); |
nargetdev | 9:1bd0f7a2ee71 | 53 | } |
nargetdev | 9:1bd0f7a2ee71 | 54 | pc.printf(" done\n"); |
nargetdev | 9:1bd0f7a2ee71 | 55 | pc.printf("SENSOR ACTIVE\n"); |
nargetdev | 9:1bd0f7a2ee71 | 56 | wait(0.05); |
nargetdev | 9:1bd0f7a2ee71 | 57 | } |
nargetdev | 9:1bd0f7a2ee71 | 58 | |
nargetdev | 9:1bd0f7a2ee71 | 59 | void show(){ |
nargetdev | 9:1bd0f7a2ee71 | 60 | float in, rout, bout, gout; |
nargetdev | 9:1bd0f7a2ee71 | 61 | bool tit = true; |
nargetdev | 9:1bd0f7a2ee71 | 62 | pc.printf("showing...\n"); |
nargetdev | 8:5ed23e276c8a | 63 | |
nargetdev | 9:1bd0f7a2ee71 | 64 | float hysteresis = 0; |
nargetdev | 9:1bd0f7a2ee71 | 65 | |
nargetdev | 9:1bd0f7a2ee71 | 66 | for (in = -1.571; motion && in < 6.283; in = in + 0.00628) |
nargetdev | 9:1bd0f7a2ee71 | 67 | { |
nargetdev | 9:1bd0f7a2ee71 | 68 | unsigned int i = motion; |
nargetdev | 9:1bd0f7a2ee71 | 69 | if (motion){ |
nargetdev | 9:1bd0f7a2ee71 | 70 | hysteresis = in + 6.283; |
nargetdev | 9:1bd0f7a2ee71 | 71 | pc.printf("\n-----\nextra_hysteresis\n-----\n"); |
nargetdev | 8:5ed23e276c8a | 72 | } |
nargetdev | 9:1bd0f7a2ee71 | 73 | tit = !tit; |
nargetdev | 9:1bd0f7a2ee71 | 74 | rout = sin(in) + 1.0; |
nargetdev | 9:1bd0f7a2ee71 | 75 | bout = sin(in + 1.3*(in+1.571) ) + 1.0; |
nargetdev | 9:1bd0f7a2ee71 | 76 | gout = sin(in + 4.2*(in+1.571) ) + 1.0; |
nargetdev | 9:1bd0f7a2ee71 | 77 | if (tit) |
nargetdev | 9:1bd0f7a2ee71 | 78 | pc.printf("%f\t%f\t%f\n",rout,bout,gout); |
nargetdev | 9:1bd0f7a2ee71 | 79 | |
nargetdev | 9:1bd0f7a2ee71 | 80 | red.write(rout/2.0); |
nargetdev | 9:1bd0f7a2ee71 | 81 | green.write(gout/2.0); |
nargetdev | 9:1bd0f7a2ee71 | 82 | blue.write(bout/2.0); |
nargetdev | 9:1bd0f7a2ee71 | 83 | wait(.005); |
nargetdev | 9:1bd0f7a2ee71 | 84 | } |
nargetdev | 8:5ed23e276c8a | 85 | } |
nargetdev | 9:1bd0f7a2ee71 | 86 | |
nargetdev | 9:1bd0f7a2ee71 | 87 | int main() { |
nargetdev | 9:1bd0f7a2ee71 | 88 | red.period(0.001f); |
nargetdev | 9:1bd0f7a2ee71 | 89 | green.period(0.001f); |
nargetdev | 9:1bd0f7a2ee71 | 90 | blue.period(0.001f); |
nargetdev | 9:1bd0f7a2ee71 | 91 | |
nargetdev | 9:1bd0f7a2ee71 | 92 | pc.printf("Hello World!\n"); |
nargetdev | 9:1bd0f7a2ee71 | 93 | // Calibrate FIR and identify. |
nargetdev | 9:1bd0f7a2ee71 | 94 | calibrate(); |
nargetdev | 9:1bd0f7a2ee71 | 95 | |
nargetdev | 9:1bd0f7a2ee71 | 96 | // Clear the lights. Wait for victim. |
nargetdev | 9:1bd0f7a2ee71 | 97 | red.write(0.00f); |
nargetdev | 9:1bd0f7a2ee71 | 98 | green.write(0.00f); |
nargetdev | 9:1bd0f7a2ee71 | 99 | blue.write(0.00f); |
nargetdev | 9:1bd0f7a2ee71 | 100 | |
nargetdev | 9:1bd0f7a2ee71 | 101 | while(1){ |
nargetdev | 9:1bd0f7a2ee71 | 102 | unsigned int i = motion; |
nargetdev | 9:1bd0f7a2ee71 | 103 | if (i){ |
nargetdev | 9:1bd0f7a2ee71 | 104 | pc.printf("Motion detected.\n"); |
nargetdev | 9:1bd0f7a2ee71 | 105 | show(); |
nargetdev | 9:1bd0f7a2ee71 | 106 | pc.printf("End show.\n"); |
nargetdev | 9:1bd0f7a2ee71 | 107 | } |
nargetdev | 9:1bd0f7a2ee71 | 108 | else |
nargetdev | 9:1bd0f7a2ee71 | 109 | red.write(0.0f); |
nargetdev | 9:1bd0f7a2ee71 | 110 | green.write(0.0f); |
nargetdev | 9:1bd0f7a2ee71 | 111 | blue.write(0.0f); |
nargetdev | 9:1bd0f7a2ee71 | 112 | } |
nargetdev | 8:5ed23e276c8a | 113 | } |