Project with example IPSO resources for LED bar, Gas Sensor, and Light Sensor
Dependencies: EthernetInterface LED_Bar mbed-rtos mbed nsdl_lib
resources/LEDbar.cpp
- Committer:
- michaeljkoster
- Date:
- 2014-10-20
- Revision:
- 0:93cc592ffdb5
- Child:
- 2:7afc089e3bf3
File content as of revision 0:93cc592ffdb5:
// Light resource implementation #include "mbed.h" #include "nsdl_support.h" #include "LEDbar.h" #include "LED_Bar.h" #define LEDBAR_RES_ID "11101/0/5901" #define LEDBAR_RES_RT "urn:X-mbed:LEDbar" extern Serial pc; char leds[] = {"0000000000"}; //GGGGGGGGYR LED_Bar bar(D6, D5); void set_leds(char *leds) { int i; for (i=0; i<=9; i++) { if(leds[i] == '1') bar.setSingleLed((9-i),1); else if(leds[i] == '0') bar.setSingleLed((9-i),0); } } /* Only GET and PUT method allowed */ static uint8_t LEDbar_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; //pc.printf("LED Strip 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); coap_res_ptr->payload_len = strlen(leds); coap_res_ptr->payload_ptr = (uint8_t*)leds; sn_nsdl_send_coap_message(address, coap_res_ptr); } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) { //pc.printf("PUT: %d bytes\r\n", received_coap_ptr->payload_len); if(received_coap_ptr->payload_len == 10) { memcpy(leds, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); leds[received_coap_ptr->payload_len] = '\0'; pc.printf("PUT: %s\r\n",leds); //call LED strup update function here set_leds(leds); 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_LEDbar_resource(sn_nsdl_resource_info_s *resource_ptr) { nsdl_create_dynamic_resource(resource_ptr, sizeof(LEDBAR_RES_ID)-1, (uint8_t*)LEDBAR_RES_ID, sizeof(LEDBAR_RES_RT)-1, (uint8_t*)LEDBAR_RES_RT, 0, &LEDbar_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED)); return 0; }