Light Show library for organic, calm, light display.
Dependencies: BLE_API mbed nRF51822
Fork of mbed_blinky by
main.cpp
- Committer:
- nargetdev
- Date:
- 2015-10-11
- Revision:
- 9:1bd0f7a2ee71
- Parent:
- 8:5ed23e276c8a
- Child:
- 10:fc61583e9e8f
File content as of revision 9:1bd0f7a2ee71:
#include "mbed.h" #define calibrationTime 10 unsigned long seed = 47; 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 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); } 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"); 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); } }