More advanced NanoService Demo for LPC1768 App Board using OMA Lightweight Objects

Dependencies:   Beep C12832_lcd EthernetInterface LM75B MMA7660 mbed-rtos mbed nsdl_lib

Fork of LWM2M_NanoService_Ethernet by MBED_DEMOS

Revision:
20:84ee332ba360
Parent:
17:40ce3d963495
diff -r e5c0b6553c11 -r 84ee332ba360 resources/rgb.cpp
--- a/resources/rgb.cpp	Sun Apr 06 20:11:46 2014 +0000
+++ b/resources/rgb.cpp	Tue Apr 08 01:02:06 2014 +0000
@@ -8,11 +8,36 @@
 #define RGB_UNITS_RES_ID    "308/0/5701"
 
 extern Serial pc;
-static PwmOut rout(p23);
-static PwmOut gout(p24);
-static PwmOut bout(p25);
+
 static int rgb[] = {0, 0, 0};
 
+class RGB {
+public:
+    RGB();
+    void show(float r, float g, float b);
+
+private:
+    PwmOut rout;
+    PwmOut gout;
+    PwmOut bout;
+};
+RGB::RGB() : rout(p23), gout(p24), bout(p25) {
+    pc.printf("RGB.ctor()\r\n");
+    rout.period(0.001);
+    gout.period(0.001);
+    bout.period(0.001);
+}
+
+/*
+The RGB LED is common anode, so that "0" is on, and "1" is off. For PWM, the closer to 0.0 the brighter, the closer to 1.0 the dimmer. 
+This method uses (1.0 - rgb value) to invert.
+*/
+void RGB::show(float r, float g, float b) {
+    rout = 1 - r;
+    gout = 1 - g;
+    bout = 1 - b;
+}
+
 /*
 Convert the r|g|b string into the integer parts, each in the range [0,255]
 */
@@ -40,13 +65,12 @@
 */
 static void setRGB()
 {
+    static RGB rgbLed;
     pc.printf("Changing to RGB(%d,%d,%d)\r\n", rgb[0], rgb[1], rgb[2]);
     float r = rgb[0] * RGB_SCALE;
     float g = rgb[1] * RGB_SCALE;
     float b = rgb[2] * RGB_SCALE;
-    rout = 1 - r;
-    gout = 1 - g;
-    bout = 1 - b;
+    rgbLed.show(r, g, b);
 }
     
 /* Only GET and PUT method allowed */
@@ -89,4 +113,9 @@
     nsdl_create_static_resource(resource_ptr, sizeof(RGB_UNITS_RES_ID)-1, (uint8_t*) RGB_UNITS_RES_ID, 0, 0,  (uint8_t*) "(R|G|B)0-255", sizeof("(R|G|B)0-255")-1);
     setRGB();
     return 0;
-}
\ No newline at end of file
+}
+void zero_rgb()
+{
+    memset(rgb, 0, sizeof(rgb));
+    setRGB();
+}