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:
sstark
Date:
Sat Apr 05 06:39:08 2014 +0000
Revision:
17:40ce3d963495
Child:
20:84ee332ba360
Added RGB LED resource

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 17:40ce3d963495 11 static PwmOut rout(p23);
sstark 17:40ce3d963495 12 static PwmOut gout(p24);
sstark 17:40ce3d963495 13 static PwmOut bout(p25);
sstark 17:40ce3d963495 14 static int rgb[] = {0, 0, 0};
sstark 17:40ce3d963495 15
sstark 17:40ce3d963495 16 /*
sstark 17:40ce3d963495 17 Convert the r|g|b string into the integer parts, each in the range [0,255]
sstark 17:40ce3d963495 18 */
sstark 17:40ce3d963495 19 static void decode_rgb(char *rgbstr)
sstark 17:40ce3d963495 20 {
sstark 17:40ce3d963495 21 char tmp[4];
sstark 17:40ce3d963495 22 memset(tmp, 0, sizeof(tmp));
sstark 17:40ce3d963495 23 char *next;
sstark 17:40ce3d963495 24 char *token = rgbstr;
sstark 17:40ce3d963495 25 int index = 0;
sstark 17:40ce3d963495 26 while((next = strchr(token, '|')) != NULL) {
sstark 17:40ce3d963495 27 int len = next - token;
sstark 17:40ce3d963495 28 strncpy(tmp, token, len);
sstark 17:40ce3d963495 29 rgb[index ++] = atoi(tmp);
sstark 17:40ce3d963495 30 token = next + 1;
sstark 17:40ce3d963495 31 }
sstark 17:40ce3d963495 32 rgb[index] = atoi(token);
sstark 17:40ce3d963495 33 pc.printf("decode_rgb(%s) = %d,%d,%d\r\n", rgbstr, rgb[0], rgb[1], rgb[2]);
sstark 17:40ce3d963495 34 }
sstark 17:40ce3d963495 35
sstark 17:40ce3d963495 36 static float RGB_SCALE = 1.0 / 255.0;
sstark 17:40ce3d963495 37 /*
sstark 17:40ce3d963495 38 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 39 This method uses (1.0 - rgb value) to invert.
sstark 17:40ce3d963495 40 */
sstark 17:40ce3d963495 41 static void setRGB()
sstark 17:40ce3d963495 42 {
sstark 17:40ce3d963495 43 pc.printf("Changing to RGB(%d,%d,%d)\r\n", rgb[0], rgb[1], rgb[2]);
sstark 17:40ce3d963495 44 float r = rgb[0] * RGB_SCALE;
sstark 17:40ce3d963495 45 float g = rgb[1] * RGB_SCALE;
sstark 17:40ce3d963495 46 float b = rgb[2] * RGB_SCALE;
sstark 17:40ce3d963495 47 rout = 1 - r;
sstark 17:40ce3d963495 48 gout = 1 - g;
sstark 17:40ce3d963495 49 bout = 1 - b;
sstark 17:40ce3d963495 50 }
sstark 17:40ce3d963495 51
sstark 17:40ce3d963495 52 /* Only GET and PUT method allowed */
sstark 17:40ce3d963495 53 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 54 {
sstark 17:40ce3d963495 55 sn_coap_hdr_s *coap_res_ptr = 0;
sstark 17:40ce3d963495 56 char rgbstr[16];
sstark 17:40ce3d963495 57
sstark 17:40ce3d963495 58 pc.printf("rgb callback\r\n");
sstark 17:40ce3d963495 59
sstark 17:40ce3d963495 60 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
sstark 17:40ce3d963495 61 {
sstark 17:40ce3d963495 62 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
sstark 17:40ce3d963495 63
sstark 17:40ce3d963495 64 sprintf(rgbstr, "%d|%d|%d", rgb[0], rgb[1], rgb[2]);
sstark 17:40ce3d963495 65
sstark 17:40ce3d963495 66 coap_res_ptr->payload_len = strlen(rgbstr);
sstark 17:40ce3d963495 67 coap_res_ptr->payload_ptr = (uint8_t*)rgbstr;
sstark 17:40ce3d963495 68 sn_nsdl_send_coap_message(address, coap_res_ptr);
sstark 17:40ce3d963495 69 }
sstark 17:40ce3d963495 70 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
sstark 17:40ce3d963495 71 {
sstark 17:40ce3d963495 72 memcpy(rgbstr, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
sstark 17:40ce3d963495 73 rgbstr[received_coap_ptr->payload_len] = '\0';
sstark 17:40ce3d963495 74 decode_rgb(rgbstr);
sstark 17:40ce3d963495 75 setRGB();
sstark 17:40ce3d963495 76
sstark 17:40ce3d963495 77 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
sstark 17:40ce3d963495 78 sn_nsdl_send_coap_message(address, coap_res_ptr);
sstark 17:40ce3d963495 79 }
sstark 17:40ce3d963495 80
sstark 17:40ce3d963495 81 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
sstark 17:40ce3d963495 82 return 0;
sstark 17:40ce3d963495 83 }
sstark 17:40ce3d963495 84
sstark 17:40ce3d963495 85 int create_rgb_resource(sn_nsdl_resource_info_s *resource_ptr)
sstark 17:40ce3d963495 86 {
sstark 17:40ce3d963495 87 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 88 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 89 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 90 setRGB();
sstark 17:40ce3d963495 91 return 0;
sstark 17:40ce3d963495 92 }