![](/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@14:e8a1fe69b048, 2015-10-11 (annotated)
- Committer:
- nargetdev
- Date:
- Sun Oct 11 06:49:34 2015 +0000
- Revision:
- 14:e8a1fe69b048
- Parent:
- 13:ed92d45dc9ab
- Child:
- 15:ee94af1f90a0
KG -- start working on bluetooth you ass...
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 | 11:2d3cc5459d5b | 4 | |
nargetdev | 11:2d3cc5459d5b | 5 | #define calibrationTime 3 |
nargetdev | 11:2d3cc5459d5b | 6 | |
nargetdev | 11:2d3cc5459d5b | 7 | #define HPI 1.571 |
nargetdev | 11:2d3cc5459d5b | 8 | #define PI 3.1416 |
nargetdev | 14:e8a1fe69b048 | 9 | #define HYSTERESIS_QUANTITY PI |
nargetdev | 11:2d3cc5459d5b | 10 | |
nargetdev | 9:1bd0f7a2ee71 | 11 | unsigned long seed = 47; |
dan | 0:7dec7e9ac085 | 12 | |
nargetdev | 9:1bd0f7a2ee71 | 13 | typedef unsigned char byte; |
nargetdev | 9:1bd0f7a2ee71 | 14 | typedef unsigned int uint; |
nargetdev | 9:1bd0f7a2ee71 | 15 | |
nargetdev | 9:1bd0f7a2ee71 | 16 | PwmOut red(p18); |
nargetdev | 9:1bd0f7a2ee71 | 17 | PwmOut green(p19); |
nargetdev | 9:1bd0f7a2ee71 | 18 | PwmOut blue(p20); |
nargetdev | 9:1bd0f7a2ee71 | 19 | DigitalIn motion(p1); |
nargetdev | 9:1bd0f7a2ee71 | 20 | |
nargetdev | 9:1bd0f7a2ee71 | 21 | Serial pc(USBTX, USBRX); // tx, rx |
nargetdev | 9:1bd0f7a2ee71 | 22 | |
nargetdev | 9:1bd0f7a2ee71 | 23 | unsigned int hash(unsigned int x) { |
nargetdev | 9:1bd0f7a2ee71 | 24 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 25 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 26 | x = ((x >> 16) ^ x); |
nargetdev | 9:1bd0f7a2ee71 | 27 | seed*=2; |
nargetdev | 9:1bd0f7a2ee71 | 28 | seed+=17; |
nargetdev | 9:1bd0f7a2ee71 | 29 | return x%100; |
dan | 0:7dec7e9ac085 | 30 | } |
nargetdev | 8:5ed23e276c8a | 31 | |
nargetdev | 9:1bd0f7a2ee71 | 32 | void identify(unsigned int m){ |
nargetdev | 9:1bd0f7a2ee71 | 33 | unsigned int hashable; |
nargetdev | 9:1bd0f7a2ee71 | 34 | float 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 | red.write(write_me); |
nargetdev | 9:1bd0f7a2ee71 | 39 | pc.printf("r, g, b: %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 | green.write(write_me); |
nargetdev | 9:1bd0f7a2ee71 | 44 | pc.printf("%f\t", write_me); |
nargetdev | 9:1bd0f7a2ee71 | 45 | |
nargetdev | 9:1bd0f7a2ee71 | 46 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 47 | write_me = hashable/100.0; |
nargetdev | 9:1bd0f7a2ee71 | 48 | blue.write(write_me); |
nargetdev | 9:1bd0f7a2ee71 | 49 | pc.printf("%f\n", write_me); |
nargetdev | 8:5ed23e276c8a | 50 | } |
nargetdev | 8:5ed23e276c8a | 51 | |
nargetdev | 9:1bd0f7a2ee71 | 52 | void calibrate(){ |
nargetdev | 9:1bd0f7a2ee71 | 53 | |
nargetdev | 9:1bd0f7a2ee71 | 54 | //give the sensor some time to calibrate |
nargetdev | 9:1bd0f7a2ee71 | 55 | pc.printf("calibrating sensor\n"); |
nargetdev | 9:1bd0f7a2ee71 | 56 | for(int i = 0; i < calibrationTime; i++){ |
nargetdev | 9:1bd0f7a2ee71 | 57 | pc.printf("."); |
nargetdev | 9:1bd0f7a2ee71 | 58 | identify(calibrationTime); |
nargetdev | 9:1bd0f7a2ee71 | 59 | wait(.25); |
nargetdev | 9:1bd0f7a2ee71 | 60 | } |
nargetdev | 9:1bd0f7a2ee71 | 61 | pc.printf(" done\n"); |
nargetdev | 9:1bd0f7a2ee71 | 62 | pc.printf("SENSOR ACTIVE\n"); |
nargetdev | 9:1bd0f7a2ee71 | 63 | wait(0.05); |
nargetdev | 9:1bd0f7a2ee71 | 64 | } |
nargetdev | 9:1bd0f7a2ee71 | 65 | |
nargetdev | 9:1bd0f7a2ee71 | 66 | void show(){ |
nargetdev | 9:1bd0f7a2ee71 | 67 | float in, rout, bout, gout; |
nargetdev | 9:1bd0f7a2ee71 | 68 | bool tit = true; |
nargetdev | 10:fc61583e9e8f | 69 | |
nargetdev | 10:fc61583e9e8f | 70 | unsigned int rgb = 0x0; |
nargetdev | 9:1bd0f7a2ee71 | 71 | pc.printf("showing...\n"); |
nargetdev | 8:5ed23e276c8a | 72 | |
nargetdev | 9:1bd0f7a2ee71 | 73 | float hysteresis = 0; |
nargetdev | 9:1bd0f7a2ee71 | 74 | |
nargetdev | 11:2d3cc5459d5b | 75 | for (in = -HPI; in < hysteresis || rgb != 0x7; in = in + 0.00628) |
nargetdev | 9:1bd0f7a2ee71 | 76 | { |
nargetdev | 9:1bd0f7a2ee71 | 77 | unsigned int i = motion; |
nargetdev | 9:1bd0f7a2ee71 | 78 | if (motion){ |
nargetdev | 10:fc61583e9e8f | 79 | hysteresis = in + HYSTERESIS_QUANTITY; |
nargetdev | 11:2d3cc5459d5b | 80 | pc.printf("hyst ||"); |
nargetdev | 8:5ed23e276c8a | 81 | } |
nargetdev | 9:1bd0f7a2ee71 | 82 | tit = !tit; |
nargetdev | 10:fc61583e9e8f | 83 | if (!(rgb & 0x1)) |
nargetdev | 13:ed92d45dc9ab | 84 | rout = sin(in + 2.88*(in + HPI)) + 1.0; |
nargetdev | 10:fc61583e9e8f | 85 | else |
nargetdev | 10:fc61583e9e8f | 86 | rout = 0.0f; |
nargetdev | 10:fc61583e9e8f | 87 | if (!(rgb & 0x2)) |
nargetdev | 13:ed92d45dc9ab | 88 | gout = sin(in + 2.61*(in+HPI) ) + 1.0; |
nargetdev | 13:ed92d45dc9ab | 89 | else |
nargetdev | 13:ed92d45dc9ab | 90 | gout = 0.0f; |
nargetdev | 13:ed92d45dc9ab | 91 | if (!(rgb & 0x4)) |
nargetdev | 13:ed92d45dc9ab | 92 | bout = sin(in + 3.92*(in+HPI) ) + 1.0; |
nargetdev | 10:fc61583e9e8f | 93 | else |
nargetdev | 10:fc61583e9e8f | 94 | bout = 0.0f; |
nargetdev | 10:fc61583e9e8f | 95 | |
nargetdev | 9:1bd0f7a2ee71 | 96 | if (tit) |
nargetdev | 13:ed92d45dc9ab | 97 | pc.printf("%f\t%f\t%f\n",rout,gout, bout); |
nargetdev | 9:1bd0f7a2ee71 | 98 | |
nargetdev | 10:fc61583e9e8f | 99 | red.write(rout/2.0); |
nargetdev | 10:fc61583e9e8f | 100 | green.write(gout/2.0); |
nargetdev | 10:fc61583e9e8f | 101 | blue.write(bout/2.0); |
nargetdev | 10:fc61583e9e8f | 102 | |
nargetdev | 10:fc61583e9e8f | 103 | if (in > hysteresis){ |
nargetdev | 10:fc61583e9e8f | 104 | if (rout < 0.01) |
nargetdev | 10:fc61583e9e8f | 105 | rgb |= 0x1; |
nargetdev | 10:fc61583e9e8f | 106 | if (gout < 0.01) |
nargetdev | 10:fc61583e9e8f | 107 | rgb |= 0x2; |
nargetdev | 10:fc61583e9e8f | 108 | if (bout < 0.01) |
nargetdev | 10:fc61583e9e8f | 109 | rgb |= 0x4; |
nargetdev | 10:fc61583e9e8f | 110 | } |
nargetdev | 13:ed92d45dc9ab | 111 | wait(0.0001f); |
nargetdev | 9:1bd0f7a2ee71 | 112 | } |
nargetdev | 11:2d3cc5459d5b | 113 | pc.printf("\n%X\n", rgb); |
nargetdev | 8:5ed23e276c8a | 114 | } |
nargetdev | 9:1bd0f7a2ee71 | 115 | |
nargetdev | 14:e8a1fe69b048 | 116 | void channel_check(){ |
nargetdev | 14:e8a1fe69b048 | 117 | |
nargetdev | 14:e8a1fe69b048 | 118 | red.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 119 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 120 | red.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 121 | |
nargetdev | 14:e8a1fe69b048 | 122 | green.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 123 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 124 | green.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 125 | |
nargetdev | 14:e8a1fe69b048 | 126 | blue.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 127 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 128 | blue.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 129 | } |
nargetdev | 14:e8a1fe69b048 | 130 | |
nargetdev | 9:1bd0f7a2ee71 | 131 | int main() { |
nargetdev | 9:1bd0f7a2ee71 | 132 | red.period(0.001f); |
nargetdev | 9:1bd0f7a2ee71 | 133 | green.period(0.001f); |
nargetdev | 9:1bd0f7a2ee71 | 134 | blue.period(0.001f); |
nargetdev | 14:e8a1fe69b048 | 135 | |
nargetdev | 14:e8a1fe69b048 | 136 | channel_check(); |
nargetdev | 9:1bd0f7a2ee71 | 137 | pc.printf("Hello World!\n"); |
nargetdev | 9:1bd0f7a2ee71 | 138 | // Calibrate FIR and identify. |
nargetdev | 9:1bd0f7a2ee71 | 139 | calibrate(); |
nargetdev | 9:1bd0f7a2ee71 | 140 | |
nargetdev | 9:1bd0f7a2ee71 | 141 | // Clear the lights. Wait for victim. |
nargetdev | 9:1bd0f7a2ee71 | 142 | red.write(0.00f); |
nargetdev | 9:1bd0f7a2ee71 | 143 | green.write(0.00f); |
nargetdev | 9:1bd0f7a2ee71 | 144 | blue.write(0.00f); |
nargetdev | 9:1bd0f7a2ee71 | 145 | |
nargetdev | 9:1bd0f7a2ee71 | 146 | while(1){ |
nargetdev | 9:1bd0f7a2ee71 | 147 | unsigned int i = motion; |
nargetdev | 9:1bd0f7a2ee71 | 148 | if (i){ |
nargetdev | 9:1bd0f7a2ee71 | 149 | pc.printf("Motion detected.\n"); |
nargetdev | 9:1bd0f7a2ee71 | 150 | show(); |
nargetdev | 9:1bd0f7a2ee71 | 151 | pc.printf("End show.\n"); |
nargetdev | 9:1bd0f7a2ee71 | 152 | } |
nargetdev | 9:1bd0f7a2ee71 | 153 | else |
nargetdev | 9:1bd0f7a2ee71 | 154 | red.write(0.0f); |
nargetdev | 9:1bd0f7a2ee71 | 155 | green.write(0.0f); |
nargetdev | 9:1bd0f7a2ee71 | 156 | blue.write(0.0f); |
nargetdev | 9:1bd0f7a2ee71 | 157 | } |
nargetdev | 8:5ed23e276c8a | 158 | } |