![](/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:
- 21:3960e3b8ca7b
- Parent:
- 20:fa23847fbfef
- Child:
- 22:b618d55e9c9b
diff -r fa23847fbfef -r 3960e3b8ca7b main.cpp --- a/main.cpp Fri Dec 04 21:29:49 2015 +0000 +++ b/main.cpp Sun Dec 06 01:21:20 2015 +0000 @@ -4,15 +4,16 @@ //#include "UARTService.h" #include <string> -#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; +#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__); } -#define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); } +#define DEBUG(...) { printf(__VA_ARGS__); } #else -#define DEBUG(...) /* nothing */ -#endif /* #if NEED_CONSOLE_OUTPUT */ +#define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); } +//#else +//#define DEBUG(...) /* nothing */ +#endif /* #if NEED_CONSOLE_OUxTPUT */ //#define NRFDK #define MKIT @@ -36,7 +37,7 @@ ButtonService *buttonServicePtr; -unsigned long seed = 1151; +unsigned long seed = 151; typedef unsigned char byte; typedef unsigned int uint; @@ -81,39 +82,54 @@ return x%100; } +void zero_out() { + red.write(0); + green.write(0); + blue.write(0); + printf("zero_out()\r\n"); +} + 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; - red.write(hashable >= 50); - char* STR; - sprintf(STR, "r, g, b: %f\t", write_me); - DEBUG(STR); + r = hashable >= 50; + red.write(r); hashable = hash(m + seed); write_me = hashable/100.0; - green.write(hashable >= 50); - pc.printf("%f\t", write_me); + g = hashable >= 50; + green.write(g); hashable = hash(m + seed); write_me = hashable/100.0; - blue.write(hashable >= 50); - pc.printf("%f\n", write_me); + b = hashable >= 50; + blue.write(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(){ //give the sensor some time to calibrate - pc.printf("calibrating sensor\n"); + pc.printf("calibrating sensor\n\r"); for(int i = 0; i < CALIBRATION_TIME; i++){ pc.printf("."); identify(CALIBRATION_TIME); wait(.5); } - DEBUG(" done\n"); - DEBUG("SENSOR ACTIVE\n"); + DEBUG(" done\n\r"); + zero_out(); + + DEBUG("SENSOR ACTIVE\n\r"); wait(0.05); } @@ -124,7 +140,7 @@ class Rgb { private: // PI macros - static const float INCREMENT = 0.00628; + static const float INCREMENT = 0.00628*4; static const float HPI = 1.571; static const float PI = 3.1416; static const float HYSTERESIS_QUANTITY = PI/4; @@ -146,38 +162,44 @@ uint8_t i; bool mov; - void random_init() { - random_init(); + void randomize_params() { float rand_seed = t.read(); - pc.printf("%f\n", rand_seed); + pc.printf("%f\n\r", rand_seed); + int rand_int = t.read() * 7919; - unsigned int seconds = time(NULL); - pc.printf("Time as seconds since January 1, 1970 = %d\n", seconds); + time_t seconds = time(NULL); + pc.printf("Time as seconds since January 1, 1970 = %d\n\r", seconds); - srand(seconds); - pc.printf("A random %i", rand() ); + srand(rand_int); + pc.printf("A random %d\r\n", rand() ); // generate random values in 0.0 - 1.0 uint8_t blah; for (int j = 0; j < 3; j++) { - srand(seconds+j); + srand(rand_int+j); blah = rand(); - SCALE[i] = (float) blah; - SCALE[i] /= (float) 0xff; - pc.printf("scale %d, %f\n", i, SCALE[i]); + 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[i] = (float) blah; - WAIT[i] /= (float) 0xff; - pc.printf("scale %d, %f\n", i, WAIT[i]); + WAIT[j] = (float) blah; + WAIT[j] /= (float) 0xff; + WAIT[j] *= HPI; + pc.printf("wait %d, %f\n\r", i, WAIT[i]); } + rgb = 0x0; + printf("Params Initialized\r\n"); } void update_rgb_values () { +// printf("Updating RGB values...\r\n"); for (i=0; i<3; i++){ if (!(rgb & (0x1 << i) )){ - if ( i > WAIT[i]){ - rgb_c[i] = -cos((in - WAIT[i])*i*WAIT[i]) + 1; + 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; @@ -187,7 +209,8 @@ rgb_c[i] = 0.0; pc.printf("%f\t",rgb_c[i]); } - pc.printf("\n"); + pc.printf("\n\r"); +// exit(0); } void write_rgb (){ @@ -197,17 +220,16 @@ } public: -Rgb() -{ - WAIT[0] = 1.3; - WAIT[1] = 0.8; - WAIT[2] = 2.1; -} +//Rgb(); void show() { + pc.printf("show\r\n"); // randomize the delay and scale values - random_init(); + randomize_params(); + printf("params initialized:\n\r"); + printf("WAIT:\t%f\t%f\t%f\n\r", WAIT[0], WAIT[1], WAIT[2]); + 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; @@ -216,7 +238,6 @@ #endif if (mov){ hysteresis = in + HYSTERESIS_QUANTITY; - pc.printf("hyst ||"); } // update rgb @@ -235,75 +256,10 @@ rgb |= 0x4; } - wait(0.0001f); - } + } } }; -//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) -// { -//#ifdef MKIT -// bool i = motion; -//#else -// bool i = !motion; -//#endif -// if (i){ -// hysteresis = in + HYSTERESIS_QUANTITY; -// pc.printf("hyst ||"); -// } -// tit = !tit; -// if (!(rgb & 0x1)){ -// if (in < RWAIT - HPI) -// rout = 0.0f; -// else -// rout = sin(in + 2.88*(in + HPI - RWAIT)) + 1.0; -// } -// else -// rout = 0.0f; -// if (!(rgb & 0x2)){ -// if (in < GWAIT - HPI) -// gout = 0.0f; -// else -// gout = sin(in + 2.61*(in+HPI - GWAIT) ) + 1.0; -// } -// else -// gout = 0.0f; -// if (!(rgb & 0x4)){ -// if (in < BWAIT - HPI) -// bout = 0.0f; -// else -// bout = sin(in + 3.92*(in+HPI - BWAIT) ) + 1.0; -// } -// else -// bout = 0.0f; -// -// 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(){ @@ -323,14 +279,17 @@ int main() { + DEBUG("Start Main.\r\n"); + // Set RTC time to Wed, 28 Oct 2009 11:35:37 set_time(1256729737); - t.start(); red.period(0.01f); green.period(0.01f); blue.period(0.01f); - identify(CALIBRATION_TIME); + + + calibrate(); ble.init(); ble.gap().onDisconnection(disconnectionCallback); @@ -346,12 +305,15 @@ ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ ble.gap().startAdvertising(); + DEBUG("Instantiate strip.\r\n"); + Rgb strip; + DEBUG("I have a strip.\r\n"); - Rgb strip; while(1){ #ifdef MKIT motion.rise(&motionIRQ); if (motionState){ + pc.printf("Motion detected.\r\n"); buttonServicePtr->updateButtonState(motionState); strip.show(); motionState = 0;