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:
- 16:810a1bbb320a
- Parent:
- 15:ee94af1f90a0
- Child:
- 17:76c74851442f
File content as of revision 16:810a1bbb320a:
#include "mbed.h" #define calibrationTime 3 #define HPI 1.571 #define PI 3.1416 #define HYSTERESIS_QUANTITY PI unsigned long seed = 156; 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(hashable >= 50); pc.printf("r, g, b: %f\t", write_me); hashable = hash(m + seed); write_me = hashable/100.0; green.write(hashable >= 50); pc.printf("%f\t", write_me); hashable = hash(m + seed); write_me = hashable/100.0; blue.write(hashable >= 50); 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(.5); } pc.printf(" done\n"); pc.printf("SENSOR ACTIVE\n"); wait(0.05); } void show(){ float in, rout, bout, gout; bool tit = true; unsigned int rgb = 0x0; pc.printf("showing...\n"); float hysteresis = 0; for (in = -HPI; in < hysteresis || rgb != 0x7; in = in + 0.00628) { unsigned int i = motion; if (motion){ hysteresis = in + HYSTERESIS_QUANTITY; pc.printf("hyst ||"); } tit = !tit; if (!(rgb & 0x1)) rout = sin(in + 2.88*(in + HPI)) + 1.0; else rout = 0.0f; if (!(rgb & 0x2)) gout = sin(in + 2.61*(in+HPI) ) + 1.0; else gout = 0.0f; if (!(rgb & 0x4)) bout = sin(in + 3.92*(in+HPI) ) + 1.0; else bout = 0.0f; if (tit) pc.printf("%f\t%f\t%f\n",rout,gout, bout); red.write(rout/2.0); green.write(gout/2.0); blue.write(bout/2.0); if (in > hysteresis){ if (rout < 0.01) rgb |= 0x1; if (gout < 0.01) rgb |= 0x2; if (bout < 0.01) rgb |= 0x4; } wait(0.0001f); } pc.printf("\n%X\n", rgb); } void channel_check(){ red.write(1.0f); wait(.5); red.write(0.0f); green.write(1.0f); wait(.5); green.write(0.0f); blue.write(1.0f); wait(.5); blue.write(0.0f); } int main() { red.period(0.001f); green.period(0.001f); blue.period(0.001f); // channel_check(); 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); } }