Michael Koster / Mbed 2 deprecated SensorNotifyLED

Dependencies:   Chainable_RGB_LED EthernetInterface LED_Bar mbed-rtos mbed nsdl_lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LED_array.cpp Source File

LED_array.cpp

00001 // Light resource implementation
00002 
00003 #include "mbed.h"
00004 #include "nsdl_support.h"
00005 #include "ChainableLED.h"
00006 #include "LED_array.h"
00007 
00008 #define LED_ARRAY_RES_ID    "11100/0/5900"
00009 #define LED_ARRAY_RES_RT    "urn:X-mbed:LEDarray"
00010 
00011 extern Serial pc;
00012 char led_value[] = {"0000000"}; //RRGGBBII
00013 
00014 #define LED_COUNT 1
00015 
00016 ChainableLED led_chain(P0_0, P0_1, LED_COUNT);
00017 
00018 uint8_t LED_array_max_age = 0; 
00019 uint8_t LED_array_content_type = 0;
00020 
00021 void set_led_array(char *color_string)
00022 {
00023     static uint8_t red, green, blue, index;
00024     int color_int;
00025 
00026     sscanf(color_string, "%X", &color_int);
00027     
00028     index = color_int & 255;
00029     blue = color_int >> 8 & 255;
00030     green = color_int >> 16 & 255;
00031     red = color_int >> 24 & 255;
00032         
00033     if(index > 0 and index <= LED_COUNT)
00034     {
00035         led_chain.setColorRGB(index-1, red, green, blue);    
00036     }
00037     else if(index == 0)
00038     {
00039         for(int i = 0; i < LED_COUNT; i++)
00040         {
00041             led_chain.setColorRGB(i, red, green, blue);    
00042         }
00043     }    
00044 }
00045 
00046 
00047 /* Only GET and PUT method allowed */
00048 static uint8_t LED_array_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
00049 {
00050     sn_coap_hdr_s *coap_res_ptr = 0;
00051 
00052     //pc.printf("LED array callback\r\n");
00053 
00054     if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
00055     {
00056         coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
00057 
00058         coap_res_ptr->payload_len = strlen(led_value);
00059         coap_res_ptr->payload_ptr = (uint8_t*)led_value;
00060         
00061         coap_res_ptr->content_type_ptr = &LED_array_content_type;
00062         coap_res_ptr->content_type_len = sizeof(LED_array_content_type);
00063         
00064         coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s));
00065         if(!coap_res_ptr->options_list_ptr)
00066             {
00067             pc.printf("cant alloc option list for max-age\r\n");
00068             coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover
00069             }
00070         memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
00071         coap_res_ptr->options_list_ptr->max_age_ptr = &LED_array_max_age;
00072         coap_res_ptr->options_list_ptr->max_age_len = sizeof(LED_array_max_age);
00073 
00074         sn_nsdl_send_coap_message(address, coap_res_ptr);
00075         nsdl_free(coap_res_ptr->options_list_ptr);
00076         coap_res_ptr->options_list_ptr = NULL;
00077         coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory
00078 
00079     }
00080     else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
00081     {
00082         //pc.printf("PUT: %d bytes\r\n", received_coap_ptr->payload_len);
00083         if(received_coap_ptr->payload_len == 8)
00084         {
00085             memcpy(led_value, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
00086 
00087             led_value[received_coap_ptr->payload_len] = '\0';
00088             pc.printf("PUT: %s\r\n",led_value);
00089 
00090             //call LED array update function here
00091             set_led_array(led_value);
00092 
00093             coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
00094             sn_nsdl_send_coap_message(address, coap_res_ptr);
00095         }
00096     }
00097 
00098     sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
00099     return 0;
00100 }
00101 
00102 int create_LED_array_resource(sn_nsdl_resource_info_s *resource_ptr)
00103 {
00104     nsdl_create_dynamic_resource(resource_ptr, sizeof(LED_ARRAY_RES_ID)-1, (uint8_t*)LED_ARRAY_RES_ID, sizeof(LED_ARRAY_RES_RT)-1, (uint8_t*)LED_ARRAY_RES_RT, 0, &LED_array_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
00105     set_led_array("FF000000");
00106     wait(0.5);
00107     set_led_array("00FF0000");
00108     wait(0.5);
00109     set_led_array("0000FF00");
00110     wait(0.5);
00111     set_led_array("00000000");
00112 
00113             
00114     return 0;
00115 }