LED Demo for Tech Con 2014

Dependencies:   EthernetInterface PololuLedStripx mbed-rtos mbed nanoservice_client_1_12_X

Fork of LPC1768_LWM2M_Client by MBED_DEMOS

Committer:
michaeljkoster
Date:
Mon Nov 17 17:37:16 2014 +0000
Revision:
15:2f2b3eaa51a6
Parent:
2:4166c3e5b321
LED Strip Demo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michaeljkoster 0:9101343a70cd 1 // Light resource implementation
michaeljkoster 0:9101343a70cd 2
michaeljkoster 0:9101343a70cd 3 #include "mbed.h"
michaeljkoster 0:9101343a70cd 4 #include "nsdl_support.h"
michaeljkoster 0:9101343a70cd 5 #include "light.h"
michaeljkoster 0:9101343a70cd 6
michaeljkoster 1:09a525977925 7 #define LIGHT_RES_ID "11100/0/5900"
michaeljkoster 0:9101343a70cd 8
michaeljkoster 0:9101343a70cd 9 extern Serial pc;
michaeljkoster 1:09a525977925 10 char led_color[] = {"00000000"};
michaeljkoster 0:9101343a70cd 11
michaeljkoster 2:4166c3e5b321 12 extern void set_led_color(char* led_color);
michaeljkoster 2:4166c3e5b321 13
michaeljkoster 0:9101343a70cd 14 /* Only GET and PUT method allowed */
michaeljkoster 0:9101343a70cd 15 static uint8_t light_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
michaeljkoster 0:9101343a70cd 16 {
michaeljkoster 0:9101343a70cd 17 sn_coap_hdr_s *coap_res_ptr = 0;
michaeljkoster 0:9101343a70cd 18
michaeljkoster 2:4166c3e5b321 19 //pc.printf("LED Strip callback\r\n");
michaeljkoster 0:9101343a70cd 20
michaeljkoster 0:9101343a70cd 21 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
michaeljkoster 0:9101343a70cd 22 {
michaeljkoster 0:9101343a70cd 23 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
michaeljkoster 0:9101343a70cd 24
michaeljkoster 1:09a525977925 25 coap_res_ptr->payload_len = strlen(led_color);
michaeljkoster 1:09a525977925 26 coap_res_ptr->payload_ptr = (uint8_t*)led_color;
michaeljkoster 0:9101343a70cd 27 sn_nsdl_send_coap_message(address, coap_res_ptr);
michaeljkoster 0:9101343a70cd 28 }
michaeljkoster 0:9101343a70cd 29 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
michaeljkoster 0:9101343a70cd 30 {
michaeljkoster 2:4166c3e5b321 31 //pc.printf("PUT: %d bytes\r\n", received_coap_ptr->payload_len);
michaeljkoster 1:09a525977925 32 if(received_coap_ptr->payload_len == 8)
michaeljkoster 1:09a525977925 33 {
michaeljkoster 1:09a525977925 34 memcpy(led_color, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
michaeljkoster 0:9101343a70cd 35
michaeljkoster 1:09a525977925 36 led_color[received_coap_ptr->payload_len] = '\0';
michaeljkoster 1:09a525977925 37 pc.printf("PUT: %s\r\n",led_color);
michaeljkoster 0:9101343a70cd 38
michaeljkoster 1:09a525977925 39 //call LED strup update function here
michaeljkoster 2:4166c3e5b321 40 set_led_color(led_color);
michaeljkoster 1:09a525977925 41
michaeljkoster 1:09a525977925 42 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
michaeljkoster 1:09a525977925 43 sn_nsdl_send_coap_message(address, coap_res_ptr);
michaeljkoster 1:09a525977925 44 }
michaeljkoster 0:9101343a70cd 45 }
michaeljkoster 0:9101343a70cd 46
michaeljkoster 0:9101343a70cd 47 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
michaeljkoster 0:9101343a70cd 48 return 0;
michaeljkoster 0:9101343a70cd 49 }
michaeljkoster 0:9101343a70cd 50
michaeljkoster 0:9101343a70cd 51 int create_light_resource(sn_nsdl_resource_info_s *resource_ptr)
michaeljkoster 0:9101343a70cd 52 {
michaeljkoster 0:9101343a70cd 53 nsdl_create_dynamic_resource(resource_ptr, sizeof(LIGHT_RES_ID)-1, (uint8_t*)LIGHT_RES_ID, 0, 0, 0, &light_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
michaeljkoster 0:9101343a70cd 54 return 0;
michaeljkoster 0:9101343a70cd 55 }