![](/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
Diff: main.cpp
- Revision:
- 9:1bd0f7a2ee71
- Parent:
- 8:5ed23e276c8a
- Child:
- 10:fc61583e9e8f
--- a/main.cpp Sat Oct 10 07:22:30 2015 +0000 +++ b/main.cpp Sun Oct 11 01:15:52 2015 +0000 @@ -1,48 +1,113 @@ #include "mbed.h" - typedef unsigned char byte; - -//PwmOut blue(p18); -//PwmOut green(p19); -DigitalOut red(p18); -DigitalOut green(p19); -DigitalOut blue(p20); +#define calibrationTime 10 +unsigned long seed = 47; -void error_code(){ - for(;;){ - green = !green; - blue = !blue; - red = !red; - wait(.5); - } + typedef unsigned char byte; + typedef unsigned int uint; + +PwmOut red(p18); +PwmOut green(p19); +PwmOut blue(p20); +DigitalIn motion(p1); + +Serial pc(USBTX, USBRX); // tx, rx + +unsigned int hash(unsigned int x) { + x = ((x >> 16) ^ x) * 0x45d9f3b; + x = ((x >> 16) ^ x) * 0x45d9f3b; + x = ((x >> 16) ^ x); + seed*=2; + seed+=17; + return x%100; } -void primative_channel(int rgb, byte duty){ - for (byte i = 0; i < 255; i++){ - switch (rgb){ - case 'r': - red = ( i < duty ); - break; - case 'g': - green = (i < duty); - break; - case 'b': - blue = (i < duty); - break; - default: - error_code(); - } - } +void identify(unsigned int m){ + unsigned int hashable; + float write_me; + + hashable = hash(m + seed); + write_me = hashable/100.0; + red.write(write_me); + pc.printf("r, g, b: %f\t", write_me); + + hashable = hash(m + seed); + write_me = hashable/100.0; + green.write(write_me); + pc.printf("%f\t", write_me); + + hashable = hash(m + seed); + write_me = hashable/100.0; + blue.write(write_me); + pc.printf("%f\n", write_me); } -int main() { -while(1){ + void calibrate(){ + + //give the sensor some time to calibrate + pc.printf("calibrating sensor\n"); + for(int i = 0; i < calibrationTime; i++){ + pc.printf("."); + identify(calibrationTime); + wait(.25); + } + pc.printf(" done\n"); + pc.printf("SENSOR ACTIVE\n"); + wait(0.05); +} + +void show(){ + float in, rout, bout, gout; + bool tit = true; + pc.printf("showing...\n"); - primative_channel('g',20); - primative_channel('b',150); - for (int i = 0; i < 256; i++){ - primative_channel('b',i); + float hysteresis = 0; + + for (in = -1.571; motion && in < 6.283; in = in + 0.00628) + { + unsigned int i = motion; + if (motion){ + hysteresis = in + 6.283; + pc.printf("\n-----\nextra_hysteresis\n-----\n"); } - + tit = !tit; + rout = sin(in) + 1.0; + bout = sin(in + 1.3*(in+1.571) ) + 1.0; + gout = sin(in + 4.2*(in+1.571) ) + 1.0; + if (tit) + pc.printf("%f\t%f\t%f\n",rout,bout,gout); + + red.write(rout/2.0); + green.write(gout/2.0); + blue.write(bout/2.0); + wait(.005); + } } + +int main() { + red.period(0.001f); + green.period(0.001f); + blue.period(0.001f); + + pc.printf("Hello World!\n"); +// Calibrate FIR and identify. + calibrate(); + +// Clear the lights. Wait for victim. + red.write(0.00f); + green.write(0.00f); + blue.write(0.00f); + + while(1){ + unsigned int i = motion; + if (i){ + pc.printf("Motion detected.\n"); + show(); + pc.printf("End show.\n"); + } + else + red.write(0.0f); + green.write(0.0f); + blue.write(0.0f); + } } \ No newline at end of file