![](/media/cache/group/arm_mbed.PNG.50x50_q85.png)
Demo starter application to connect WiGo to NSP and expose on-board sensors
Dependencies: NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed nsdl_lib TEMT6200 TSI Wi-Go_eCompass_Lib_V3 WiGo_BattCharger
This is the mbed project for the IoT World Hackathon event, June 17th and 18th in Palo Also.
The setup instructions for participants are at the Setup page of this wiki:
resources/rgbled.cpp@18:11b9d98ecae2, 2014-07-09 (annotated)
- Committer:
- michaeljkoster
- Date:
- Wed Jul 09 04:35:49 2014 +0000
- Revision:
- 18:11b9d98ecae2
- Parent:
- 12:160f28111a34
Checkpoint
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 11:9daf07113ef6 | 1 | // RGB LED resource implementation |
michaeljkoster | 11:9daf07113ef6 | 2 | |
michaeljkoster | 11:9daf07113ef6 | 3 | #include "mbed.h" |
michaeljkoster | 11:9daf07113ef6 | 4 | #include "nsdl_support.h" |
michaeljkoster | 11:9daf07113ef6 | 5 | #include "defLED.h" |
michaeljkoster | 11:9daf07113ef6 | 6 | #include "rgbled.h" |
michaeljkoster | 11:9daf07113ef6 | 7 | |
michaeljkoster | 11:9daf07113ef6 | 8 | #define RED_LED_RES_ID "3311/0/5850/0" |
michaeljkoster | 11:9daf07113ef6 | 9 | #define GRN_LED_RES_ID "3311/0/5850/1" |
michaeljkoster | 11:9daf07113ef6 | 10 | #define BLU_LED_RES_ID "3311/0/5850/2" |
michaeljkoster | 11:9daf07113ef6 | 11 | |
michaeljkoster | 11:9daf07113ef6 | 12 | extern Serial pc; |
michaeljkoster | 11:9daf07113ef6 | 13 | extern DigitalOut ledr; |
michaeljkoster | 11:9daf07113ef6 | 14 | extern DigitalOut ledg; |
michaeljkoster | 11:9daf07113ef6 | 15 | extern DigitalOut ledb; |
michaeljkoster | 11:9daf07113ef6 | 16 | |
michaeljkoster | 11:9daf07113ef6 | 17 | static uint8_t red_led_state = '0'; |
michaeljkoster | 11:9daf07113ef6 | 18 | static uint8_t grn_led_state = '0'; |
michaeljkoster | 11:9daf07113ef6 | 19 | static uint8_t blu_led_state = '0'; |
michaeljkoster | 11:9daf07113ef6 | 20 | |
michaeljkoster | 11:9daf07113ef6 | 21 | /* Only GET and PUT method allowed */ |
michaeljkoster | 11:9daf07113ef6 | 22 | static uint8_t red_led_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) |
michaeljkoster | 11:9daf07113ef6 | 23 | { |
michaeljkoster | 11:9daf07113ef6 | 24 | sn_coap_hdr_s *coap_res_ptr = 0; |
michaeljkoster | 11:9daf07113ef6 | 25 | |
michaeljkoster | 11:9daf07113ef6 | 26 | pc.printf("red_led callback\r\n"); |
michaeljkoster | 11:9daf07113ef6 | 27 | |
michaeljkoster | 11:9daf07113ef6 | 28 | if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) |
michaeljkoster | 11:9daf07113ef6 | 29 | { |
michaeljkoster | 11:9daf07113ef6 | 30 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
michaeljkoster | 11:9daf07113ef6 | 31 | |
michaeljkoster | 11:9daf07113ef6 | 32 | coap_res_ptr->payload_len = 1; |
michaeljkoster | 11:9daf07113ef6 | 33 | coap_res_ptr->payload_ptr = &red_led_state; |
michaeljkoster | 11:9daf07113ef6 | 34 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
michaeljkoster | 11:9daf07113ef6 | 35 | } |
michaeljkoster | 11:9daf07113ef6 | 36 | else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) |
michaeljkoster | 11:9daf07113ef6 | 37 | { |
michaeljkoster | 11:9daf07113ef6 | 38 | if(received_coap_ptr->payload_len) |
michaeljkoster | 11:9daf07113ef6 | 39 | { |
michaeljkoster | 11:9daf07113ef6 | 40 | if(*(received_coap_ptr->payload_ptr) == '1') |
michaeljkoster | 11:9daf07113ef6 | 41 | { |
michaeljkoster | 11:9daf07113ef6 | 42 | RED_ON; |
michaeljkoster | 11:9daf07113ef6 | 43 | red_led_state = '1'; |
michaeljkoster | 11:9daf07113ef6 | 44 | |
michaeljkoster | 11:9daf07113ef6 | 45 | } |
michaeljkoster | 11:9daf07113ef6 | 46 | else if(*(received_coap_ptr->payload_ptr) == '0') |
michaeljkoster | 11:9daf07113ef6 | 47 | { |
michaeljkoster | 11:9daf07113ef6 | 48 | RED_OFF; |
michaeljkoster | 11:9daf07113ef6 | 49 | red_led_state = '0'; |
michaeljkoster | 11:9daf07113ef6 | 50 | } |
michaeljkoster | 11:9daf07113ef6 | 51 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); |
michaeljkoster | 11:9daf07113ef6 | 52 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
michaeljkoster | 11:9daf07113ef6 | 53 | } |
michaeljkoster | 11:9daf07113ef6 | 54 | } |
michaeljkoster | 11:9daf07113ef6 | 55 | |
michaeljkoster | 11:9daf07113ef6 | 56 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
michaeljkoster | 11:9daf07113ef6 | 57 | |
michaeljkoster | 11:9daf07113ef6 | 58 | return 0; |
michaeljkoster | 11:9daf07113ef6 | 59 | } |
michaeljkoster | 11:9daf07113ef6 | 60 | |
michaeljkoster | 11:9daf07113ef6 | 61 | |
michaeljkoster | 11:9daf07113ef6 | 62 | static uint8_t grn_led_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) |
michaeljkoster | 11:9daf07113ef6 | 63 | { |
michaeljkoster | 11:9daf07113ef6 | 64 | sn_coap_hdr_s *coap_res_ptr = 0; |
michaeljkoster | 11:9daf07113ef6 | 65 | |
michaeljkoster | 11:9daf07113ef6 | 66 | pc.printf("green_led callback\r\n"); |
michaeljkoster | 11:9daf07113ef6 | 67 | |
michaeljkoster | 11:9daf07113ef6 | 68 | if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) |
michaeljkoster | 11:9daf07113ef6 | 69 | { |
michaeljkoster | 11:9daf07113ef6 | 70 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
michaeljkoster | 11:9daf07113ef6 | 71 | |
michaeljkoster | 11:9daf07113ef6 | 72 | coap_res_ptr->payload_len = 1; |
michaeljkoster | 11:9daf07113ef6 | 73 | coap_res_ptr->payload_ptr = &grn_led_state; |
michaeljkoster | 11:9daf07113ef6 | 74 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
michaeljkoster | 11:9daf07113ef6 | 75 | } |
michaeljkoster | 11:9daf07113ef6 | 76 | else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) |
michaeljkoster | 11:9daf07113ef6 | 77 | { |
michaeljkoster | 11:9daf07113ef6 | 78 | if(received_coap_ptr->payload_len) |
michaeljkoster | 11:9daf07113ef6 | 79 | { |
michaeljkoster | 11:9daf07113ef6 | 80 | if(*(received_coap_ptr->payload_ptr) == '1') |
michaeljkoster | 11:9daf07113ef6 | 81 | { |
michaeljkoster | 11:9daf07113ef6 | 82 | GREEN_ON; |
michaeljkoster | 11:9daf07113ef6 | 83 | grn_led_state = '1'; |
michaeljkoster | 11:9daf07113ef6 | 84 | |
michaeljkoster | 11:9daf07113ef6 | 85 | } |
michaeljkoster | 11:9daf07113ef6 | 86 | else if(*(received_coap_ptr->payload_ptr) == '0') |
michaeljkoster | 11:9daf07113ef6 | 87 | { |
michaeljkoster | 11:9daf07113ef6 | 88 | GREEN_OFF; |
michaeljkoster | 11:9daf07113ef6 | 89 | grn_led_state = '0'; |
michaeljkoster | 11:9daf07113ef6 | 90 | } |
michaeljkoster | 11:9daf07113ef6 | 91 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); |
michaeljkoster | 11:9daf07113ef6 | 92 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
michaeljkoster | 11:9daf07113ef6 | 93 | } |
michaeljkoster | 11:9daf07113ef6 | 94 | } |
michaeljkoster | 11:9daf07113ef6 | 95 | |
michaeljkoster | 11:9daf07113ef6 | 96 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
michaeljkoster | 11:9daf07113ef6 | 97 | |
michaeljkoster | 11:9daf07113ef6 | 98 | return 0; |
michaeljkoster | 11:9daf07113ef6 | 99 | } |
michaeljkoster | 11:9daf07113ef6 | 100 | |
michaeljkoster | 11:9daf07113ef6 | 101 | static uint8_t blu_led_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) |
michaeljkoster | 11:9daf07113ef6 | 102 | { |
michaeljkoster | 11:9daf07113ef6 | 103 | sn_coap_hdr_s *coap_res_ptr = 0; |
michaeljkoster | 11:9daf07113ef6 | 104 | |
michaeljkoster | 11:9daf07113ef6 | 105 | pc.printf("blue_led callback\r\n"); |
michaeljkoster | 11:9daf07113ef6 | 106 | |
michaeljkoster | 11:9daf07113ef6 | 107 | if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) |
michaeljkoster | 11:9daf07113ef6 | 108 | { |
michaeljkoster | 11:9daf07113ef6 | 109 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
michaeljkoster | 11:9daf07113ef6 | 110 | |
michaeljkoster | 11:9daf07113ef6 | 111 | coap_res_ptr->payload_len = 1; |
michaeljkoster | 11:9daf07113ef6 | 112 | coap_res_ptr->payload_ptr = &blu_led_state; |
michaeljkoster | 11:9daf07113ef6 | 113 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
michaeljkoster | 11:9daf07113ef6 | 114 | } |
michaeljkoster | 11:9daf07113ef6 | 115 | else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) |
michaeljkoster | 11:9daf07113ef6 | 116 | { |
michaeljkoster | 11:9daf07113ef6 | 117 | if(received_coap_ptr->payload_len) |
michaeljkoster | 11:9daf07113ef6 | 118 | { |
michaeljkoster | 11:9daf07113ef6 | 119 | if(*(received_coap_ptr->payload_ptr) == '1') |
michaeljkoster | 11:9daf07113ef6 | 120 | { |
michaeljkoster | 11:9daf07113ef6 | 121 | BLUE_ON; |
michaeljkoster | 11:9daf07113ef6 | 122 | blu_led_state = '1'; |
michaeljkoster | 11:9daf07113ef6 | 123 | |
michaeljkoster | 11:9daf07113ef6 | 124 | } |
michaeljkoster | 11:9daf07113ef6 | 125 | else if(*(received_coap_ptr->payload_ptr) == '0') |
michaeljkoster | 11:9daf07113ef6 | 126 | { |
michaeljkoster | 11:9daf07113ef6 | 127 | BLUE_OFF; |
michaeljkoster | 11:9daf07113ef6 | 128 | blu_led_state = '0'; |
michaeljkoster | 11:9daf07113ef6 | 129 | } |
michaeljkoster | 11:9daf07113ef6 | 130 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); |
michaeljkoster | 11:9daf07113ef6 | 131 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
michaeljkoster | 11:9daf07113ef6 | 132 | } |
michaeljkoster | 11:9daf07113ef6 | 133 | } |
michaeljkoster | 11:9daf07113ef6 | 134 | |
michaeljkoster | 11:9daf07113ef6 | 135 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
michaeljkoster | 11:9daf07113ef6 | 136 | |
michaeljkoster | 11:9daf07113ef6 | 137 | return 0; |
michaeljkoster | 11:9daf07113ef6 | 138 | } |
michaeljkoster | 11:9daf07113ef6 | 139 | |
michaeljkoster | 11:9daf07113ef6 | 140 | int create_rgbled_resource(sn_nsdl_resource_info_s *resource_ptr) |
michaeljkoster | 11:9daf07113ef6 | 141 | { |
michaeljkoster | 11:9daf07113ef6 | 142 | nsdl_create_dynamic_resource(resource_ptr, sizeof(RED_LED_RES_ID)-1, (uint8_t*)RED_LED_RES_ID, 0, 0, 0, &red_led_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED)); |
michaeljkoster | 12:160f28111a34 | 143 | nsdl_create_dynamic_resource(resource_ptr, sizeof(GRN_LED_RES_ID)-1, (uint8_t*)GRN_LED_RES_ID, 0, 0, 0, &grn_led_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED)); |
michaeljkoster | 12:160f28111a34 | 144 | nsdl_create_dynamic_resource(resource_ptr, sizeof(BLU_LED_RES_ID)-1, (uint8_t*)BLU_LED_RES_ID, 0, 0, 0, &blu_led_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED)); |
michaeljkoster | 11:9daf07113ef6 | 145 | //initialize state here |
michaeljkoster | 11:9daf07113ef6 | 146 | red_led_state = '0'; |
michaeljkoster | 11:9daf07113ef6 | 147 | grn_led_state = '0'; |
michaeljkoster | 11:9daf07113ef6 | 148 | blu_led_state = '0'; |
michaeljkoster | 11:9daf07113ef6 | 149 | RED_OFF; |
michaeljkoster | 11:9daf07113ef6 | 150 | GREEN_OFF; |
michaeljkoster | 11:9daf07113ef6 | 151 | BLUE_OFF; |
michaeljkoster | 11:9daf07113ef6 | 152 | |
michaeljkoster | 11:9daf07113ef6 | 153 | return 0; |
michaeljkoster | 11:9daf07113ef6 | 154 | } |