![](/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:
- 19:fabe9521b0a1
- Parent:
- 18:7c05ef6a639c
- Child:
- 20:fa23847fbfef
--- a/main.cpp Fri Oct 30 02:41:18 2015 +0000 +++ b/main.cpp Fri Dec 04 21:01:55 2015 +0000 @@ -1,9 +1,10 @@ #include "mbed.h" #include "BLE.h" -#include "UARTService.h" +#include "ButtonService.h" +//#include "UARTService.h" #include <string> -#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; +#define NEED_CONSOLE_OUTPUT 0 /* 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 @@ -16,19 +17,26 @@ //#define NRFDK #define MKIT -#define calibrationTime 2 +#define CALIBRATION_TIME 3 -#define HPI 1.571 -#define PI 3.1416 -#define HYSTERESIS_QUANTITY PI/4 +//#define HPI 1.571 +//#define PI 3.1416 +//#define HYSTERESIS_QUANTITY PI/4 +// +//#define RWAIT 0 +//#define GWAIT PI/8 +//#define BWAIT PI/4 -#define RWAIT 0 -#define GWAIT 0 -#define BWAIT 0 + +//UARTService *uartServicePtr; +const static char DEVICE_NAME[] = "Bathroom"; +static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; -UARTService *uartServicePtr; +uint8_t motionState = 0; +ButtonService *buttonServicePtr; -unsigned long seed = 113; + +unsigned long seed = 1151; typedef unsigned char byte; typedef unsigned int uint; @@ -47,6 +55,8 @@ InterruptIn motion(p1); #endif +// get some randomness +Timer t; Serial pc(USBTX, USBRX); // tx, rx @@ -97,9 +107,9 @@ //give the sensor some time to calibrate pc.printf("calibrating sensor\n"); - for(int i = 0; i < calibrationTime; i++){ + for(int i = 0; i < CALIBRATION_TIME; i++){ pc.printf("."); - identify(calibrationTime); + identify(CALIBRATION_TIME); wait(.5); } DEBUG(" done\n"); @@ -107,59 +117,173 @@ wait(0.05); } -void show(){ - float in, rout, bout, gout; - bool tit = true; +void motionIRQ(){ + motionState = 1; +} - unsigned int rgb = 0x0; - pc.printf("showing...\n"); - - float hysteresis = 0; +class Rgb { +private: + // PI macros + static const float INCREMENT = 0.00628; + static const float HPI = 1.571; + static const float PI = 3.1416; + static const float HYSTERESIS_QUANTITY = PI/4; - 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 ||"); + static const float RWAIT = 0; + static const float GWAIT = PI/8; + static const float BWAIT = PI/4; + float WAIT [3]; + float SCALE [3]; + +// static const float SCALE[3] = { + + // channel operators + float rgb_c[3]; + float in; + uint8_t rgb; + float hysteresis; + + uint8_t i; + bool mov; + + void random_init() { + float rand_seed = t.read(); + pc.printf("%f\n", rand_seed); + + time_t seconds = time(NULL); + printf("Time as seconds since January 1, 1970 = %d\n", seconds); } - 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; - - pc.printf("%f\t%f\t%f\n",rout,gout, bout); + + void update_rgb_values () { + 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; + } + else { + rgb_c[i] = 0.0; + } + } + else + rgb_c[i] = 0.0; + pc.printf("%f\t",rgb_c[i]); + } + pc.printf("\n"); + } + +void write_rgb (){ + red.write(rgb_c[0]/2.0); + green.write(rgb_c[1]/2.0); + blue.write(rgb_c[2]/2.0); +} + +public: +Rgb() +{ + WAIT[0] = 1.3; + WAIT[1] = 0.8; + WAIT[2] = 2.1; +} - red.write(rout/2.0); - green.write(gout/2.0); - blue.write(bout/2.0); +void show() { + random_init(); + for (in = 0; in < hysteresis || rgb != 0x7; in = in + INCREMENT){ + #ifdef MKIT + bool mov = motion; + #else + bool mov = !motion; + #endif + if (mov){ + hysteresis = in + HYSTERESIS_QUANTITY; + pc.printf("hyst ||"); + } + + // update rgb + update_rgb_values(); + + // write values + write_rgb(); - if (in > hysteresis){ - if (rout < 0.01) - rgb |= 0x1; - if (gout < 0.01) - rgb |= 0x2; - if (bout < 0.01) - rgb |= 0x4; + + 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; + } + + wait(0.0001f); + } } - wait(0.0001f); - } - pc.printf("\n%X\n", rgb); -} +}; +//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(){ @@ -176,36 +300,49 @@ blue.write(0.0f); } + + int main() { + // 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); -// -// ble.init(); -// ble.onDisconnection(disconnectionCallback); -// -// uartServicePtr = new UARTService(ble); -// -// /* setup advertising */ -// ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); -// ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); -// ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, -// (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); -// ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, -// (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); -// -// ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ -// ble.startAdvertising(); -// + identify(CALIBRATION_TIME); + + ble.init(); + ble.gap().onDisconnection(disconnectionCallback); + + ButtonService buttonService(ble, false /* initial value for button pressed */); + buttonServicePtr = &buttonService; + + /* setup advertising */ + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); + ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ + ble.gap().startAdvertising(); - + Rgb strip; while(1){ #ifdef MKIT - motion.rise(&show); + motion.rise(&motionIRQ); + if (motionState){ + buttonServicePtr->updateButtonState(motionState); + strip.show(); + motionState = 0; + buttonServicePtr->updateButtonState(motionState); + red.write(0.0f); + green.write(0.0f); + blue.write(0.0f); + } #endif #ifdef NRFDK - motion.fall(&show); + motion.fall(&strip.show()); #endif // red.write(0.0f); // green.write(0.0f);