Red Hat Summit 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

Committer:
sam_grove
Date:
Tue Apr 29 00:41:05 2014 +0000
Revision:
32:7f3f1ef700e3
Parent:
20:84ee332ba360
mbed emt node

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sstark 17:40ce3d963495 1 // Light resource implementation
sstark 17:40ce3d963495 2
sstark 17:40ce3d963495 3 #include "mbed.h"
sstark 17:40ce3d963495 4 #include "rgb.h"
sstark 17:40ce3d963495 5
sstark 17:40ce3d963495 6 #define RGB_COLOUR_RES_ID "308/0/0"
sstark 17:40ce3d963495 7 #define RGB_SETPT_RES_ID "308/0/5900"
sstark 17:40ce3d963495 8 #define RGB_UNITS_RES_ID "308/0/5701"
sstark 17:40ce3d963495 9
sstark 17:40ce3d963495 10 extern Serial pc;
sstark 20:84ee332ba360 11
sstark 17:40ce3d963495 12 static int rgb[] = {0, 0, 0};
sstark 17:40ce3d963495 13
sstark 20:84ee332ba360 14 class RGB {
sstark 20:84ee332ba360 15 public:
sstark 20:84ee332ba360 16 RGB();
sstark 20:84ee332ba360 17 void show(float r, float g, float b);
sstark 20:84ee332ba360 18
sstark 20:84ee332ba360 19 private:
sstark 20:84ee332ba360 20 PwmOut rout;
sstark 20:84ee332ba360 21 PwmOut gout;
sstark 20:84ee332ba360 22 PwmOut bout;
sstark 20:84ee332ba360 23 };
sstark 20:84ee332ba360 24 RGB::RGB() : rout(p23), gout(p24), bout(p25) {
sstark 20:84ee332ba360 25 pc.printf("RGB.ctor()\r\n");
sstark 20:84ee332ba360 26 rout.period(0.001);
sstark 20:84ee332ba360 27 gout.period(0.001);
sstark 20:84ee332ba360 28 bout.period(0.001);
sstark 20:84ee332ba360 29 }
sstark 20:84ee332ba360 30
sstark 20:84ee332ba360 31 /*
sstark 20:84ee332ba360 32 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.
sstark 20:84ee332ba360 33 This method uses (1.0 - rgb value) to invert.
sstark 20:84ee332ba360 34 */
sstark 20:84ee332ba360 35 void RGB::show(float r, float g, float b) {
sstark 20:84ee332ba360 36 rout = 1 - r;
sstark 20:84ee332ba360 37 gout = 1 - g;
sstark 20:84ee332ba360 38 bout = 1 - b;
sstark 20:84ee332ba360 39 }
sstark 20:84ee332ba360 40
sstark 17:40ce3d963495 41 /*
sstark 17:40ce3d963495 42 Convert the r|g|b string into the integer parts, each in the range [0,255]
sstark 17:40ce3d963495 43 */
sstark 17:40ce3d963495 44 static void decode_rgb(char *rgbstr)
sstark 17:40ce3d963495 45 {
sstark 17:40ce3d963495 46 char tmp[4];
sstark 17:40ce3d963495 47 memset(tmp, 0, sizeof(tmp));
sstark 17:40ce3d963495 48 char *next;
sstark 17:40ce3d963495 49 char *token = rgbstr;
sstark 17:40ce3d963495 50 int index = 0;
sstark 17:40ce3d963495 51 while((next = strchr(token, '|')) != NULL) {
sstark 17:40ce3d963495 52 int len = next - token;
sstark 17:40ce3d963495 53 strncpy(tmp, token, len);
sstark 17:40ce3d963495 54 rgb[index ++] = atoi(tmp);
sstark 17:40ce3d963495 55 token = next + 1;
sstark 17:40ce3d963495 56 }
sstark 17:40ce3d963495 57 rgb[index] = atoi(token);
sstark 17:40ce3d963495 58 pc.printf("decode_rgb(%s) = %d,%d,%d\r\n", rgbstr, rgb[0], rgb[1], rgb[2]);
sstark 17:40ce3d963495 59 }
sstark 17:40ce3d963495 60
sstark 17:40ce3d963495 61 static float RGB_SCALE = 1.0 / 255.0;
sstark 17:40ce3d963495 62 /*
sstark 17:40ce3d963495 63 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.
sstark 17:40ce3d963495 64 This method uses (1.0 - rgb value) to invert.
sstark 17:40ce3d963495 65 */
sstark 17:40ce3d963495 66 static void setRGB()
sstark 17:40ce3d963495 67 {
sstark 20:84ee332ba360 68 static RGB rgbLed;
sstark 17:40ce3d963495 69 pc.printf("Changing to RGB(%d,%d,%d)\r\n", rgb[0], rgb[1], rgb[2]);
sstark 17:40ce3d963495 70 float r = rgb[0] * RGB_SCALE;
sstark 17:40ce3d963495 71 float g = rgb[1] * RGB_SCALE;
sstark 17:40ce3d963495 72 float b = rgb[2] * RGB_SCALE;
sstark 20:84ee332ba360 73 rgbLed.show(r, g, b);
sstark 17:40ce3d963495 74 }
sstark 17:40ce3d963495 75
sstark 17:40ce3d963495 76 /* Only GET and PUT method allowed */
sstark 17:40ce3d963495 77 static uint8_t rgb_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
sstark 17:40ce3d963495 78 {
sstark 17:40ce3d963495 79 sn_coap_hdr_s *coap_res_ptr = 0;
sstark 17:40ce3d963495 80 char rgbstr[16];
sstark 17:40ce3d963495 81
sstark 17:40ce3d963495 82 pc.printf("rgb callback\r\n");
sstark 17:40ce3d963495 83
sstark 17:40ce3d963495 84 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
sstark 17:40ce3d963495 85 {
sstark 17:40ce3d963495 86 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
sstark 17:40ce3d963495 87
sstark 17:40ce3d963495 88 sprintf(rgbstr, "%d|%d|%d", rgb[0], rgb[1], rgb[2]);
sstark 17:40ce3d963495 89
sstark 17:40ce3d963495 90 coap_res_ptr->payload_len = strlen(rgbstr);
sstark 17:40ce3d963495 91 coap_res_ptr->payload_ptr = (uint8_t*)rgbstr;
sstark 17:40ce3d963495 92 sn_nsdl_send_coap_message(address, coap_res_ptr);
sstark 17:40ce3d963495 93 }
sstark 17:40ce3d963495 94 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
sstark 17:40ce3d963495 95 {
sstark 17:40ce3d963495 96 memcpy(rgbstr, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
sstark 17:40ce3d963495 97 rgbstr[received_coap_ptr->payload_len] = '\0';
sstark 17:40ce3d963495 98 decode_rgb(rgbstr);
sstark 17:40ce3d963495 99 setRGB();
sstark 17:40ce3d963495 100
sstark 17:40ce3d963495 101 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
sstark 17:40ce3d963495 102 sn_nsdl_send_coap_message(address, coap_res_ptr);
sstark 17:40ce3d963495 103 }
sstark 17:40ce3d963495 104
sstark 17:40ce3d963495 105 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
sstark 17:40ce3d963495 106 return 0;
sstark 17:40ce3d963495 107 }
sstark 17:40ce3d963495 108
sstark 17:40ce3d963495 109 int create_rgb_resource(sn_nsdl_resource_info_s *resource_ptr)
sstark 17:40ce3d963495 110 {
sstark 17:40ce3d963495 111 nsdl_create_dynamic_resource(resource_ptr, sizeof(RGB_SETPT_RES_ID)-1, (uint8_t*)RGB_SETPT_RES_ID, 0, 0, 0, &rgb_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
sstark 17:40ce3d963495 112 nsdl_create_static_resource(resource_ptr, sizeof(RGB_COLOUR_RES_ID)-1, (uint8_t*) RGB_COLOUR_RES_ID, 0, 0, (uint8_t*) "RBG LED Colour", sizeof("RBG LED Colour")-1);
sstark 17:40ce3d963495 113 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);
sstark 17:40ce3d963495 114 setRGB();
sstark 17:40ce3d963495 115 return 0;
sstark 20:84ee332ba360 116 }
sstark 20:84ee332ba360 117 void zero_rgb()
sstark 20:84ee332ba360 118 {
sstark 20:84ee332ba360 119 memset(rgb, 0, sizeof(rgb));
sstark 20:84ee332ba360 120 setRGB();
sstark 20:84ee332ba360 121 }