Light Show library for organic, calm, light display.

Dependencies:   BLE_API mbed nRF51822

Fork of mbed_blinky by Mbed

Revision:
25:d48f46d753fd
Parent:
24:52319c0a14b8
Child:
26:8bc9984c4600
--- a/light_show.cpp	Sat Jan 30 20:07:55 2016 +0000
+++ b/light_show.cpp	Mon Feb 01 01:02:46 2016 +0000
@@ -1,52 +1,111 @@
 #include "light_show.h"
 
-void Light_show::drive ()
-{
-//        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);
+
+extern InterruptIn motion;
+
+LightShow::LightShow (Rgb* strip):
+    strip(strip), hysteresis(HYSTERESIS_QUANTITY)  {    
+    // Set RTC time to Wed, 28 Oct 2009 11:35:37
+    set_time(1256729737);
+    t.start();
 }
 
-void Light_show::randomize_params()
+void LightShow::show()
+{
+    printf("show\r\n");
+    // randomize the delay and scale values
+    randomize_params();
+    printf("params initialized:\n\r");
+
+    for (time = 0; time < hysteresis || finished != 0x7; time = time + INCREMENT) {
+#ifdef MKIT
+        bool mov = motion;
+#else
+        bool mov = !motion;
+#endif
+        if (mov) {
+            hysteresis = time + HYSTERESIS_QUANTITY;
+        }
+
+        // update rgb
+        update_rgb();
+        
+        printf("t:%f\t", time);
+
+        if (time > hysteresis) {
+            if (rgb_c[0] < 0.01)
+                finished |= 0x1;
+            if (rgb_c[1] < 0.01)
+                finished |= 0x2;
+            if (rgb_c[2] < 0.01)
+                finished |= 0x4;
+        }
+    }
+}
+
+
+void LightShow::simple_show()
+{
+    finished = 0;
+    printf("simple_show()\r\n");
+    // randomize the delay and scale values
+    randomize_params();
+    printf("params initialized:\n\r");
+
+    for (time = 0; time < 10*PI; time = time + INCREMENT) {
+        printf("t:%f\t", time);
+
+        // update rgb
+        update_rgb();
+    }
+    strip->quiet();
+}
+
+void LightShow::update_rgb()
+{
+//    printf("\n\rupdate_rgb()\n\r");
+    for (int i = 0; i < 3; i++) {
+        if (finished & (0x1 << i) ) {
+            printf("FINISHED\t");
+            strip->write(i, 0.0f);
+        } else {
+            strip->write(i, rgb_c[i] = sine_waves[i].get_y(time));
+            printf("%f\t", rgb_c[i]);
+        }
+    }
+    printf("\n\r");
+}
+
+
+
+void LightShow::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);
+    pc.printf("float: %f\n\r", rand_seed);
+    
+    int rand_int = (int) t.read() * 7919;
+    pc.printf("int: %d\n\r", rand_int);
 
     srand(rand_int);
-    pc->printf("A random %d\r\n", rand() );
+    pc.printf("A random %d\r\n", rand() );
 
-    // generate random values in 0.0 - 1.0
-    uint8_t blah;
+    // generate random values time 0.0 - 1.0
+    uint8_t bitmask;
     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]);
+        float freq = (float) (bitmask = rand()&0xff) / 0xff;
+        sine_waves[j].set_frequency(freq*PI);
+        printf("freq: %f\t", freq);
+        //srand(bitmask*j % 17 + 7);
+        float wait = (float) (rand()&0xff) / 0xff;
+        printf("wait_time: %f\n\r", wait);
+        sine_waves[j].set_wait_time(wait);
     }
-    rgb = 0x0;
+    finished = 0x0;
     printf("Params Initialized\r\n");
-}
\ No newline at end of file
+}
+
+#ifdef NO
+void show_params()
+    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]);
+#endif
\ No newline at end of file