Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
Fork of mbed_blinky by
Diff: rgb_led.cpp
- Revision:
- 23:4bb74b53e112
- Child:
- 24:52319c0a14b8
--- /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
