Light Show library for organic, calm, light display.

Dependencies:   BLE_API mbed nRF51822

Fork of mbed_blinky by Mbed

Revision:
24:52319c0a14b8
Parent:
23:4bb74b53e112
Child:
25:d48f46d753fd
--- a/main.cpp	Fri Jan 29 20:22:02 2016 +0000
+++ b/main.cpp	Sat Jan 30 20:07:55 2016 +0000
@@ -1,14 +1,31 @@
-#include "mbed.h" 
+#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. */
+* it will have an impact on code-size and power consumption. */
 
 #if NEED_CONSOLE_OUTPUT
 #define DEBUG(...) { printf(__VA_ARGS__); }
@@ -18,18 +35,12 @@
 //#define DEBUG(...) /* nothing */
 #endif /* #if NEED_CONSOLE_OUxTPUT */
 
-//#define NRFDK
-#define MKIT
+Serial pc(USBTX, USBRX); // tx, rx
 
 #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
+
+Rgb strip(RED_PIN, GREEN_PIN, BLUE_PIN, &pc);
 
 
 //UARTService *uartServicePtr;
@@ -42,30 +53,15 @@
 
 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
+typedef unsigned char byte;
+typedef unsigned int uint;
+
+
 
-#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;
+InterruptIn motion(MOTION_PIN);
 
-Serial pc(USBTX, USBRX); // tx, rx
+
 
 
 BLEDevice  ble;
@@ -79,7 +75,8 @@
 
 
 
-unsigned int hash(unsigned int x) {
+unsigned int hash(unsigned int x)
+{
     x = ((x >> 16) ^ x) * 0x45d9f3b;
     x = ((x >> 16) ^ x) * 0x45d9f3b;
     x = ((x >> 16) ^ x);
@@ -88,74 +85,104 @@
     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){
+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);
+    strip.write(RED, r);
 
     hashable = hash(m + seed);
     write_me = hashable/100.0;
     g = hashable >= 50;
-    green.write(g);
-    
+    strip.write(GREEN, g);
+
     hashable = hash(m + seed);
     write_me = hashable/100.0;
     b = hashable >= 50;
-    blue.write(b);
-    
+    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(){
+
+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);
-      }
+    //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();
+    strip.quiet();
 
     DEBUG("SENSOR ACTIVE\n\r");
     wait(0.05);
 }
 
-void motionIRQ(){
+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() {
+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();
@@ -173,31 +200,25 @@
     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){
+
+    while(1) {
 #ifdef MKIT
         motion.rise(&motionIRQ);
-        if (motionState){
+        if (motionState) {
             pc.printf("Motion detected.\r\n");
             buttonServicePtr->updateButtonState(motionState);
-            strip.show();
+            show();
             motionState = 0;
             buttonServicePtr->updateButtonState(motionState);
-            red.write(0.0f);
-            green.write(0.0f);
-            blue.write(0.0f);
+            strip.quiet();
         }
 #endif
 #ifdef NRFDK
         motion.fall(&strip.show());
 #endif
-//        red.write(0.0f);
-//        green.write(0.0f);
-//        blue.write(0.0f);
+
         ble.waitForEvent();
     }
 }
\ No newline at end of file