![](/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:
- 25:d48f46d753fd
- Parent:
- 24:52319c0a14b8
- Child:
- 26:8bc9984c4600
--- a/main.cpp Sat Jan 30 20:07:55 2016 +0000 +++ b/main.cpp Mon Feb 01 01:02:46 2016 +0000 @@ -5,44 +5,20 @@ #include <string> #include "rgb_led.h" - -#define MKIT - -#ifdef NRFDK -#define MOTION_PIN p20 +#include "sinusoid.h" +#include "light_show.h" -#define RED_PIN p21 -#define GREEN_PIN p22 -#define BLUE_PIN p23 -#endif - -#ifdef MKIT -#define MOTION_PIN p1 +#include "utility.h" -#define RED_PIN p6 -#define GREEN_PIN p22 -#define BLUE_PIN p30 -#endif - -#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; -* it will have an impact on code-size and power consumption. */ +#include "macros.h" +#include "config.h" +#include "typedef.h" -#if NEED_CONSOLE_OUTPUT -#define DEBUG(...) { printf(__VA_ARGS__); } -#else -#define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); } -//#else -//#define DEBUG(...) /* nothing */ -#endif /* #if NEED_CONSOLE_OUxTPUT */ Serial pc(USBTX, USBRX); // tx, rx -#define CALIBRATION_TIME 3 - - Rgb strip(RED_PIN, GREEN_PIN, BLUE_PIN, &pc); - //UARTService *uartServicePtr; const static char DEVICE_NAME[] = "Bathroom"; static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; @@ -51,141 +27,37 @@ ButtonService *buttonServicePtr; -unsigned long seed = 151; - -typedef unsigned char byte; -typedef unsigned int uint; - - - - InterruptIn motion(MOTION_PIN); - - - BLEDevice ble; -void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) -{ - DEBUG("Disconnected!\n\r"); - DEBUG("Restarting the advertising process\n\r"); - ble.startAdvertising(); -} - - - -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) -{ - DEBUG("IDENTIFYING as: "); - unsigned int hashable; - float write_me; - - int r, g, b; - - hashable = hash(m + seed); - write_me = hashable/100.0; - r = hashable >= 50; - strip.write(RED, r); - - hashable = hash(m + seed); - write_me = hashable/100.0; - g = hashable >= 50; - strip.write(GREEN, g); - - hashable = hash(m + seed); - write_me = hashable/100.0; - b = hashable >= 50; - strip.write(BLUE, b); - - char* STR; -// sprintf(STR, "r, g, b: %f\t\r\n", write_me); -// DEBUG(STR); - DEBUG("%d%d%d\r\n",r,g,b); -} - -void calibrate() -{ +/** Reconnect if bluetooth disconnects **/ +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params); - //give the sensor some time to calibrate - pc.printf("calibrating sensor\n\r"); - for(int i = 0; i < CALIBRATION_TIME; i++) { - pc.printf("."); - identify(CALIBRATION_TIME); - wait(.5); - } - DEBUG(" done\n\r"); - strip.quiet(); - - DEBUG("SENSOR ACTIVE\n\r"); - wait(0.05); -} - -void motionIRQ() -{ - motionState = 1; -} - - +/** chill for 10 seconds for FIR sensor calibration **/ +void calibrate(); -void show() -{ - printf("show\r\n"); - // randomize the delay and scale values - strip.randomize_params(); - printf("params initialized:\n\r"); - pc.printf("WAIT:\t%f\t%f\t%f\n\r", WAIT[0], WAIT[1], WAIT[2]); - pc.printf("SCALE:\t%f\t%f\t%f\n\r", SCALE[0], SCALE[1], SCALE[2]); - for (in = 0; in < hysteresis || rgb != 0x7; in = in + INCREMENT) { -#ifdef MKIT - bool mov = motion; -#else - bool mov = !motion; -#endif - if (mov) { - strip.hysteresis = in + HYSTERESIS_QUANTITY; - } - - // update rgb - strip.update_rgb_values(); - - // write values - strip.write_rgb(); - - - if (in > hysteresis) { - if (rgb_c[0] < 0.01) - rgb |= 0x1; - if (rgb_c[1] < 0.01) - rgb |= 0x2; - if (rgb_c[2] < 0.01) - rgb |= 0x4; - } - } -} - - +/** Light weight interrupt handler **/ +void motionIRQ(); int main() { DEBUG("Start Main.\r\n"); - + strip.init(); + strip.channel_check(); + strip.quiet(); + +// Sinusoid s(0.0, 1.0); +// for (float ff;; ff+=0.00005) +// strip.write(RED, s.get_y(ff)); + + LightShow show_obj(&strip); /* object that runs the light show */ + +#define BLE +#ifdef BLE - - calibrate(); + //calibrate(); ble.init(); ble.gap().onDisconnection(disconnectionCallback); @@ -209,16 +81,49 @@ if (motionState) { pc.printf("Motion detected.\r\n"); buttonServicePtr->updateButtonState(motionState); - show(); + show_obj.show(); motionState = 0; buttonServicePtr->updateButtonState(motionState); strip.quiet(); } -#endif -#ifdef NRFDK +#elif defined NRFDK motion.fall(&strip.show()); #endif ble.waitForEvent(); } -} \ No newline at end of file +#endif//BLE +} // END main() + + +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) +{ + DEBUG("Disconnected!\n\r"); + DEBUG("Restarting the advertising process\n\r"); + ble.startAdvertising(); +} + +void motionIRQ() +{ + motionState = 1; +} + +void calibrate() +{ + + //give the sensor some time to calibrate + pc.printf("calibrating sensor\n\r"); + for(int i = 0; i < CALIBRATION_TIME; i++) { + pc.printf("."); + identify(CALIBRATION_TIME, strip); + wait(.5); + } + DEBUG(" done\n\r"); + strip.quiet(); + + DEBUG("SENSOR ACTIVE\n\r"); + wait(0.05); +} + + +