adding resources firmware and 1/0/8
Dependencies: Beep C12832_lcd EthernetInterface EthernetNetIf HTTPClient LM75B MMA7660 mbed-rtos mbed nsdl_lib
Fork of LWM2M_NanoService_Ethernet by
Diff: resources/rgb.cpp
- Revision:
- 17:40ce3d963495
- Child:
- 20:84ee332ba360
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/rgb.cpp Sat Apr 05 06:39:08 2014 +0000 @@ -0,0 +1,92 @@ +// Light resource implementation + +#include "mbed.h" +#include "rgb.h" + +#define RGB_COLOUR_RES_ID "308/0/0" +#define RGB_SETPT_RES_ID "308/0/5900" +#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}; + +/* +Convert the r|g|b string into the integer parts, each in the range [0,255] +*/ +static void decode_rgb(char *rgbstr) +{ + char tmp[4]; + memset(tmp, 0, sizeof(tmp)); + char *next; + char *token = rgbstr; + int index = 0; + while((next = strchr(token, '|')) != NULL) { + int len = next - token; + strncpy(tmp, token, len); + rgb[index ++] = atoi(tmp); + token = next + 1; + } + rgb[index] = atoi(token); + pc.printf("decode_rgb(%s) = %d,%d,%d\r\n", rgbstr, rgb[0], rgb[1], rgb[2]); +} + +static float RGB_SCALE = 1.0 / 255.0; +/* +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. +*/ +static void setRGB() +{ + 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; +} + +/* Only GET and PUT method allowed */ +static uint8_t rgb_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + sn_coap_hdr_s *coap_res_ptr = 0; + char rgbstr[16]; + + pc.printf("rgb callback\r\n"); + + if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) + { + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); + + sprintf(rgbstr, "%d|%d|%d", rgb[0], rgb[1], rgb[2]); + + coap_res_ptr->payload_len = strlen(rgbstr); + coap_res_ptr->payload_ptr = (uint8_t*)rgbstr; + sn_nsdl_send_coap_message(address, coap_res_ptr); + } + else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) + { + memcpy(rgbstr, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); + rgbstr[received_coap_ptr->payload_len] = '\0'; + decode_rgb(rgbstr); + setRGB(); + + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); + sn_nsdl_send_coap_message(address, coap_res_ptr); + } + + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + +int create_rgb_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + 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)); + 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); + 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