Light Show library for organic, calm, light display.

Dependencies:   BLE_API mbed nRF51822

Fork of mbed_blinky by Mbed

main.cpp

Committer:
nargetdev
Date:
2016-01-29
Revision:
22:b618d55e9c9b
Parent:
21:3960e3b8ca7b
Child:
23:4bb74b53e112

File content as of revision 22:b618d55e9c9b:

#include "mbed.h" 
#include "BLE.h"
#include "ButtonService.h"
//#include "UARTService.h"
#include <string>

#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 */

//#define NRFDK
#define MKIT

#define CALIBRATION_TIME 3

//#define HPI 1.571
//#define PI 3.1416
//#define HYSTERESIS_QUANTITY  PI/4
//
//#define RWAIT 0
//#define GWAIT PI/8
//#define BWAIT PI/4


//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;
 
#ifdef NRFDK
InterruptIn motion(p20);
PwmOut red(p21);
PwmOut green(p22);
PwmOut blue(p23);
#endif

#ifdef MKIT
//PwmOut red(p18);
//PwmOut green(p19);
//PwmOut blue(p20);
PwmOut red(p6);
PwmOut green(p22);
PwmOut blue(p30);
InterruptIn motion(p1);
#endif

// get some randomness
Timer t;

Serial pc(USBTX, USBRX); // tx, rx


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 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;
    r = hashable >= 50;
    red.write(r);

    hashable = hash(m + seed);
    write_me = hashable/100.0;
    g = hashable >= 50;
    green.write(g);
    
    hashable = hash(m + seed);
    write_me = hashable/100.0;
    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\r");
    for(int i = 0; i < CALIBRATION_TIME; i++){
      pc.printf(".");
      identify(CALIBRATION_TIME);
      wait(.5);
      }
    DEBUG(" done\n\r");
    zero_out();

    DEBUG("SENSOR ACTIVE\n\r");
    wait(0.05);
}

void motionIRQ(){
    motionState = 1;
}

class Rgb {
private:
    // PI macros
    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;

    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 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);
        
        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(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]);
        }
        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 ( 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);
    }

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();


void show() {
    pc.printf("show\r\n");
    // randomize the delay and scale values
    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;
        #else
            bool mov = !motion;
        #endif
        if (mov){
            hysteresis = in + HYSTERESIS_QUANTITY;
        }
        
        // update rgb
        update_rgb_values();
        
        // write values
        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;
        }
        
      }
    }
};


void 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);
}


    
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);


    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();
    
    DEBUG("Instantiate strip.\r\n");
    Rgb strip;
    DEBUG("I have a strip.\r\n");

    while(1){
#ifdef MKIT
        motion.rise(&motionIRQ);
        if (motionState){
            pc.printf("Motion detected.\r\n");
            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(&strip.show());
#endif
//        red.write(0.0f);
//        green.write(0.0f);
//        blue.write(0.0f);
        ble.waitForEvent();
    }
}