Light Show library for organic, calm, light display.

Dependencies:   BLE_API mbed nRF51822

Fork of mbed_blinky by Mbed

main.cpp

Committer:
nargetdev
Date:
2015-10-11
Revision:
10:fc61583e9e8f
Parent:
9:1bd0f7a2ee71
Child:
11:2d3cc5459d5b

File content as of revision 10:fc61583e9e8f:

#include "mbed.h"
 
 
#define HYSTERESIS_QUANTITY  1.57079632679
#define calibrationTime 10
unsigned long seed = 47;

 typedef unsigned char byte;
 typedef unsigned int uint;
 
PwmOut red(p18);
PwmOut green(p19);
PwmOut blue(p20);
DigitalIn motion(p1);

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

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){
    unsigned int hashable;
    float write_me;
    
    hashable = hash(m + seed);
    write_me = hashable/100.0;
    red.write(write_me);
    pc.printf("r, g, b: %f\t", write_me);

    hashable = hash(m + seed);
    write_me = hashable/100.0;
    green.write(write_me);
    pc.printf("%f\t", write_me);
    
    hashable = hash(m + seed);
    write_me = hashable/100.0;
    blue.write(write_me);
    pc.printf("%f\n", write_me);
}
 
 void calibrate(){

  //give the sensor some time to calibrate
  pc.printf("calibrating sensor\n");
    for(int i = 0; i < calibrationTime; i++){
      pc.printf(".");
      identify(calibrationTime);
      wait(.25);
      }
    pc.printf(" done\n");
    pc.printf("SENSOR ACTIVE\n");
    wait(0.05);
}

void show(){
  float in, rout, bout, gout;
  bool tit = true;

  unsigned int rgb = 0x0;
    pc.printf("showing...\n");
    
    float hysteresis = 0;

  for (in = -1.571; in < hysteresis || (rgb ^ 0x7); in = in + 0.00628)
  {
    unsigned int i = motion;
    if (motion){
        hysteresis = in + HYSTERESIS_QUANTITY;
        pc.printf("\n-----\nextra_hysteresis\n-----\n");
    }
    tit = !tit;
    if (!(rgb & 0x1))
        rout = sin(in) + 1.0;
    else
        rout = 0.0f;
    if (!(rgb & 0x2))
        bout = sin(in + 1.3*(in+1.571) ) + 1.0;
    else
        bout = 0.0f;
    if (!(rgb & 0x4))
        gout = sin(in + 4.2*(in+1.571) ) + 1.0;
    else
        gout = 0.0f;
        
    if (tit)
        pc.printf("%f\t%f\t%f\n",rout,bout,gout);

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

int main() {
    red.period(0.001f);
    green.period(0.001f);
    blue.period(0.001f);

    pc.printf("Hello World!\n");
// Calibrate FIR and identify.
    calibrate();
    
//    Clear the lights.  Wait for victim.
    red.write(0.00f);
    green.write(0.00f);   
    blue.write(0.00f); 

    while(1){
        unsigned int i = motion;
        if (i){
            pc.printf("Motion detected.\n");
            show();
            pc.printf("End show.\n");
        }
        else
            red.write(0.0f);
            green.write(0.0f);
            blue.write(0.0f);
    }
}