MBED_DEMOS / Mbed 2 deprecated Hello-mDS-cc3000-WiFi

Dependencies:   cc3000_hostdriver_mbedsocket mbed nsdl_lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers light.cpp Source File

light.cpp

00001 // Light resource implementation
00002 
00003 #include "mbed.h"
00004 #include "nsdl_support.h"
00005 #include "light.h"
00006 
00007 #define LIGHT_RES_ID    "11100/0/5900"
00008 
00009 extern Serial pc;
00010 char leds[] = {"1111"}; //YGBR
00011 
00012 DigitalOut grn(LED1);
00013 DigitalOut red(LED2);
00014 DigitalOut blu(LED3);
00015 DigitalOut yel(LED4);
00016 
00017 void set_leds(char *leds)
00018 {
00019 int leds_int ;
00020 
00021     sscanf(leds, "%x", &leds_int);
00022     
00023     grn = ~leds_int & 1;
00024     red = ~leds_int >> 4 & 1;
00025     blu = ~leds_int >> 8 & 1;
00026     yel = ~leds_int >> 12 & 1;
00027 }
00028 
00029 
00030 /* Only GET and PUT method allowed */
00031 static uint8_t light_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
00032 {
00033     sn_coap_hdr_s *coap_res_ptr = 0;
00034 
00035     //pc.printf("LED Strip callback\r\n");
00036 
00037     if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
00038     {
00039         coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
00040 
00041         coap_res_ptr->payload_len = strlen(leds);
00042         coap_res_ptr->payload_ptr = (uint8_t*)leds;
00043         sn_nsdl_send_coap_message(address, coap_res_ptr);
00044     }
00045     else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
00046     {
00047         //pc.printf("PUT: %d bytes\r\n", received_coap_ptr->payload_len);
00048         if(received_coap_ptr->payload_len == 4)
00049         {
00050             memcpy(leds, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
00051 
00052             leds[received_coap_ptr->payload_len] = '\0';
00053             pc.printf("PUT: %s\r\n",leds);
00054 
00055             //call LED strup update function here
00056             set_leds(leds);
00057 
00058             coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
00059             sn_nsdl_send_coap_message(address, coap_res_ptr);
00060         }
00061     }
00062 
00063     sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
00064     return 0;
00065 }
00066 
00067 int create_light_resource(sn_nsdl_resource_info_s *resource_ptr)
00068 {
00069     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));
00070     return 0;
00071 }