Light Show library for organic, calm, light display.

Dependencies:   BLE_API mbed nRF51822

Fork of mbed_blinky by Mbed

Files at this revision

API Documentation at this revision

Comitter:
nargetdev
Date:
Fri Jan 29 20:22:02 2016 +0000
Parent:
22:b618d55e9c9b
Child:
24:52319c0a14b8
Commit message:
uggh

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
rgb_led.cpp Show annotated file Show diff for this revision Revisions of this file
rgb_led.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Jan 29 19:39:07 2016 +0000
+++ b/main.cpp	Fri Jan 29 20:22:02 2016 +0000
@@ -4,6 +4,9 @@
 //#include "UARTService.h"
 #include <string>
 
+
+#include "rgb_led.h"
+
 #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. */
 
@@ -140,144 +143,7 @@
     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);
-}
 
 
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rgb_led.cpp	Fri Jan 29 20:22:02 2016 +0000
@@ -0,0 +1,117 @@
+#include "rgb_led.h"
+
+
+void Rgb::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 Rgb::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 Rgb::write_rgb ()
+{
+    red.write(rgb_c[0]/2.0);
+    green.write(rgb_c[1]/2.0);
+    blue.write(rgb_c[2]/2.0);
+}
+
+//public:
+
+void Rgb::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);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rgb_led.h	Fri Jan 29 20:22:02 2016 +0000
@@ -0,0 +1,56 @@
+
+
+class Rgb
+{
+private:
+
+//#define NRFDK
+#define MKIT
+// Handles for actually writing the appropriate PWM regs
+#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
+
+
+    // 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;
+
+    // Arbitrary params for the show
+    static const float RWAIT = 0;
+    static const float GWAIT = PI/8;
+    static const float BWAIT = PI/4;
+    float WAIT [3];
+    float SCALE [3];
+
+    // channel operators
+    float rgb_c[3];
+    float in;
+    uint8_t rgb;
+    float hysteresis;
+
+    uint8_t i;
+    bool mov;
+
+    void randomize_params();
+    void update_rgb_values ();
+    void write_rgb ();
+
+public:
+    void show();
+};
\ No newline at end of file