Light Show library for organic, calm, light display.
Dependencies: BLE_API mbed nRF51822
Fork of mbed_blinky by
Revision 25:d48f46d753fd, committed 2016-02-01
- Comitter:
- nargetdev
- Date:
- Mon Feb 01 01:02:46 2016 +0000
- Parent:
- 24:52319c0a14b8
- Child:
- 26:8bc9984c4600
- Commit message:
- alpha done refactoring to object oriented
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config.h Mon Feb 01 01:02:46 2016 +0000 @@ -0,0 +1,22 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define CALIBRATION_TIME 3 + +#define MKIT + +#ifdef NRFDK + #define MOTION_PIN p20 + + #define RED_PIN p21 + #define GREEN_PIN p22 + #define BLUE_PIN p23 +#elif defined MKIT + #define MOTION_PIN p1 + + #define RED_PIN p6 + #define GREEN_PIN p22 + #define BLUE_PIN p30 +#endif + +#endif \ No newline at end of file
--- a/light_show.cpp Sat Jan 30 20:07:55 2016 +0000 +++ b/light_show.cpp Mon Feb 01 01:02:46 2016 +0000 @@ -1,52 +1,111 @@ #include "light_show.h" -void Light_show::drive () -{ -// printf("Updating RGB values...\r\n"); - for (i=0; i<3; i++) { - if (!(rgb & (0x1 << i) )) { - if ( in > WAIT[i]) { -// printf("%d, %d, result: %d\r\n", rgb, (0x1 << i), (!(rgb & (0x1 << i)) ) ); - rgb_c[i] = -cos((in - WAIT[i])*SCALE[i]) + 1; - } else { - rgb_c[i] = 0.0; - } - } else - rgb_c[i] = 0.0; - pc->printf("%f\t",rgb_c[i]); - } - pc->printf("\n\r"); -// exit(0); + +extern InterruptIn motion; + +LightShow::LightShow (Rgb* strip): + strip(strip), hysteresis(HYSTERESIS_QUANTITY) { + // Set RTC time to Wed, 28 Oct 2009 11:35:37 + set_time(1256729737); + t.start(); } -void Light_show::randomize_params() +void LightShow::show() +{ + printf("show\r\n"); + // randomize the delay and scale values + randomize_params(); + printf("params initialized:\n\r"); + + for (time = 0; time < hysteresis || finished != 0x7; time = time + INCREMENT) { +#ifdef MKIT + bool mov = motion; +#else + bool mov = !motion; +#endif + if (mov) { + hysteresis = time + HYSTERESIS_QUANTITY; + } + + // update rgb + update_rgb(); + + printf("t:%f\t", time); + + if (time > hysteresis) { + if (rgb_c[0] < 0.01) + finished |= 0x1; + if (rgb_c[1] < 0.01) + finished |= 0x2; + if (rgb_c[2] < 0.01) + finished |= 0x4; + } + } +} + + +void LightShow::simple_show() +{ + finished = 0; + printf("simple_show()\r\n"); + // randomize the delay and scale values + randomize_params(); + printf("params initialized:\n\r"); + + for (time = 0; time < 10*PI; time = time + INCREMENT) { + printf("t:%f\t", time); + + // update rgb + update_rgb(); + } + strip->quiet(); +} + +void LightShow::update_rgb() +{ +// printf("\n\rupdate_rgb()\n\r"); + for (int i = 0; i < 3; i++) { + if (finished & (0x1 << i) ) { + printf("FINISHED\t"); + strip->write(i, 0.0f); + } else { + strip->write(i, rgb_c[i] = sine_waves[i].get_y(time)); + printf("%f\t", rgb_c[i]); + } + } + printf("\n\r"); +} + + + +void LightShow::randomize_params() { float rand_seed = t.read(); - pc->printf("%f\n\r", rand_seed); - int rand_int = t.read() * 7919; - - time_t seconds = time(NULL); - pc->printf("Time as seconds since January 1, 1970 = %d\n\r", seconds); + pc.printf("float: %f\n\r", rand_seed); + + int rand_int = (int) t.read() * 7919; + pc.printf("int: %d\n\r", rand_int); srand(rand_int); - pc->printf("A random %d\r\n", rand() ); + pc.printf("A random %d\r\n", rand() ); - // generate random values in 0.0 - 1.0 - uint8_t blah; + // generate random values time 0.0 - 1.0 + uint8_t bitmask; for (int j = 0; j < 3; j++) { - srand(rand_int+j); - blah = rand(); - SCALE[j] = (float) blah; - SCALE[j] /= (float) 0xff; - SCALE[j] = SCALE[j]*HPI + 1; - pc->printf("scale %d, %f\n\r", i, SCALE[i]); - srand(blah+j % 17 + 7); - blah = rand(); - WAIT[j] = (float) blah; - WAIT[j] /= (float) 0xff; - WAIT[j] *= HPI; - pc->printf("wait %d, %f\n\r", i, WAIT[i]); + float freq = (float) (bitmask = rand()&0xff) / 0xff; + sine_waves[j].set_frequency(freq*PI); + printf("freq: %f\t", freq); + //srand(bitmask*j % 17 + 7); + float wait = (float) (rand()&0xff) / 0xff; + printf("wait_time: %f\n\r", wait); + sine_waves[j].set_wait_time(wait); } - rgb = 0x0; + finished = 0x0; printf("Params Initialized\r\n"); -} \ No newline at end of file +} + +#ifdef NO +void show_params() + 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]); +#endif \ No newline at end of file
--- a/light_show.h Sat Jan 30 20:07:55 2016 +0000 +++ b/light_show.h Mon Feb 01 01:02:46 2016 +0000 @@ -1,29 +1,42 @@ #ifndef LIGHT_SHOW_H #define LIGHT_SHOW_H +#include "macros.h" +#include "config.h" +#include "utility.h" + +#include "rgb_led.h" +#include "sinusoid.h" + +extern Serial pc; + +const float HYSTERESIS_QUANTITY = HPI; +const float INCREMENT = 0.05f; + class LightShow { public: + LightShow(Rgb*); void randomize_params(); - void drive(); + + void show(); + void simple_show(); /** sanity check **/ + +private: + Rgb* strip; + Sinusoid sine_waves[3]; -private: + // get some randomness + Timer t; + static const float HYSTERESIS_QUANTITY = PI/4; - // Arbitrary params for the show - static const float RWAIT = 0; - static const float GWAIT = PI/8; - static const float BWAIT = PI/4; - float WAIT [3]; - float SCALE [3]; + float time; // phase + uint8_t finished; // bit mask for smooth fade out + float hysteresis; + float rgb_c[3]; - // channel operators - float rgb_c[3]; - float in; // phase - uint8_t rgb; - float hysteresis; - - uint8_t i; + void update_rgb(); }; #endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/macros.h Mon Feb 01 01:02:46 2016 +0000 @@ -0,0 +1,14 @@ +#ifndef MACROS_H +#define MACROS_H + +#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. */ +#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 */ + +#endif /* #ifndef MACROS_H */ \ No newline at end of file
--- 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); +} + + +
--- a/rgb_led.cpp Sat Jan 30 20:07:55 2016 +0000 +++ b/rgb_led.cpp Mon Feb 01 01:02:46 2016 +0000 @@ -8,12 +8,6 @@ red.period(0.01f); green.period(0.01f); blue.period(0.01f); - - - // Set RTC time to Wed, 28 Oct 2009 11:35:37 - set_time(1256729737); - t.start(); - } void Rgb::write(int channel, float intensity) @@ -35,11 +29,11 @@ -void Rgb::write_rgb () +void Rgb::write_rgb (float r, float g, float b) { - red.write(rgb_c[0]/2.0); - green.write(rgb_c[1]/2.0); - blue.write(rgb_c[2]/2.0); + red.write(r); + green.write(g); + blue.write(b); } //public: @@ -56,16 +50,17 @@ void Rgb::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); + write_rgb(1, 0, 0); + wait(.1f); + write_rgb(0, 1, 0); + wait(.1f); + write_rgb(0, 0, 1); + wait(.1f); + write_rgb(1, 1, 0); + wait(.1f); + write_rgb(1, 0, 1); + wait(.1f); + write_rgb(0, 1, 1); + wait(.1f); + write_rgb(1, 1, 1); } \ No newline at end of file
--- a/rgb_led.h Sat Jan 30 20:07:55 2016 +0000 +++ b/rgb_led.h Mon Feb 01 01:02:46 2016 +0000 @@ -15,13 +15,20 @@ **/ Rgb(PinName r, PinName g, PinName b, Serial* pc); void init(); - + + /* write an RGB value */ + void write_rgb (float red, float green, float blue); + + /** write a single value **/ void write(int channel, float intensity); /* turn off all chanels */ void quiet(); + + /** Check all 3 channels for heartbeat **/ + void channel_check(); private: PwmOut red; @@ -29,8 +36,6 @@ PwmOut blue; - // get some randomness - Timer t; Serial* pc; @@ -42,7 +47,9 @@ void randomize_params(); void update_rgb_values (); - void write_rgb (); + + + };
--- a/sinusoid.cpp Sat Jan 30 20:07:55 2016 +0000 +++ b/sinusoid.cpp Mon Feb 01 01:02:46 2016 +0000 @@ -1,14 +1,18 @@ #include "sinusoid.h" +#include "mbed.h" + +//extern Serial pc; Sinusoid::Sinusoid(float wait, float frequency): wait(wait),frequency(2*PI*frequency) {} -float Sinusoid::get_y() +float Sinusoid::get_y(float time) { - if ( in >= WAIT[i]) { + if ( time >= wait) { // printf("%d, %d, result: %d\r\n", rgb, (0x1 << i), (!(rgb & (0x1 << i)) ) ); return (-cos( (time - wait)*frequency ) + 1) / 2.0; } else { - return = 0.0; + printf ("WAITING...\n\r"); + return 0.0; } } \ No newline at end of file
--- a/sinusoid.h Sat Jan 30 20:07:55 2016 +0000 +++ b/sinusoid.h Mon Feb 01 01:02:46 2016 +0000 @@ -1,6 +1,9 @@ #ifndef SINUSOID_H #define SINUSOID_H +const float HPI = 1.571; +const float PI = 3.1416; + class Sinusoid { public: @@ -8,16 +11,18 @@ * Returns a float between 0 and 1 * - This value is zero if time is < wait **/ - float get_y(); - -private: + float get_y(float); + /** default constructor **/ + Sinusoid():wait(0), frequency(1){} Sinusoid(float wait, float frequency); - long int time; + void set_frequency(float f) {frequency = f;} + void set_wait_time(float w) {wait = w;} +private: + // PI macros static const float INCREMENT = 0.00628*4; - static const float HPI = 1.571; - static const float PI = 3.1416; + float wait; // before this y is zero float frequency;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/typedef.h Mon Feb 01 01:02:46 2016 +0000 @@ -0,0 +1,7 @@ +#ifndef TYPEDEF_H +#define TYPEDEF_H + +typedef unsigned char byte; +typedef unsigned int uint; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utility.cpp Mon Feb 01 01:02:46 2016 +0000 @@ -0,0 +1,46 @@ +#include "utility.h" +#include "macros.h" + + +/** random seed for unique identification **/ +unsigned long seed = 151; + +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, Rgb& strip) +{ + 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); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utility.h Mon Feb 01 01:02:46 2016 +0000 @@ -0,0 +1,11 @@ +#ifndef UTILITY_H +#define UTILITY_H + +#include "rgb_led.h" + +//extern Serial pc(USBTX, USBRX); // tx, rx + +unsigned int hash(unsigned int x); +void identify(unsigned int m, Rgb&); + +#endif \ No newline at end of file