![](/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
main.cpp
- Committer:
- nargetdev
- Date:
- 2016-01-30
- Revision:
- 24:52319c0a14b8
- Parent:
- 23:4bb74b53e112
- Child:
- 25:d48f46d753fd
File content as of revision 24:52319c0a14b8:
#include "mbed.h" #include "BLE.h" #include "ButtonService.h" //#include "UARTService.h" #include <string> #include "rgb_led.h" #define MKIT #ifdef NRFDK #define MOTION_PIN p20 #define RED_PIN p21 #define GREEN_PIN p22 #define BLUE_PIN p23 #endif #ifdef MKIT #define MOTION_PIN p1 #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. */ #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}; uint8_t motionState = 0; 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() { //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; } 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; } } } int main() { DEBUG("Start Main.\r\n"); calibrate(); 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(); while(1) { #ifdef MKIT motion.rise(&motionIRQ); if (motionState) { pc.printf("Motion detected.\r\n"); buttonServicePtr->updateButtonState(motionState); show(); motionState = 0; buttonServicePtr->updateButtonState(motionState); strip.quiet(); } #endif #ifdef NRFDK motion.fall(&strip.show()); #endif ble.waitForEvent(); } }