cellular port
Dependencies: Beep C027_Support C12832 LM75B MMA7660 mbed-rtos mbed nsdl_lib
This is a port of the NSDL HelloWorld for cellular.
To run the example you need a C027 and the ARM mbed application shield. The example uses cellular instead of ethernet and takes the true position from the GPS instead of using a fixed position.
resources/light.cpp@10:443e7f741c8e, 2014-06-17 (annotated)
- Committer:
- mazgch
- Date:
- Tue Jun 17 07:05:29 2014 +0000
- Revision:
- 10:443e7f741c8e
- Parent:
- 8:bd9096c4784c
- Child:
- 11:fa12b9f50538
use latest rtos capable modem library, improve the led usage
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 2:7e489126fe7a | 1 | // Light resource implementation |
bogdanm | 2:7e489126fe7a | 2 | |
bogdanm | 2:7e489126fe7a | 3 | #include "mbed.h" |
bogdanm | 2:7e489126fe7a | 4 | #include "nsdl_support.h" |
bogdanm | 2:7e489126fe7a | 5 | #include "light.h" |
bogdanm | 2:7e489126fe7a | 6 | |
bogdanm | 2:7e489126fe7a | 7 | #define LIGHT_RES_ID "lt/0/dim" |
bogdanm | 2:7e489126fe7a | 8 | |
mazgch | 10:443e7f741c8e | 9 | static PwmOut led1(LED); |
mazgch | 10:443e7f741c8e | 10 | static PwmOut led2(D8); |
mazgch | 10:443e7f741c8e | 11 | static PwmOut led3(D9); |
mazgch | 10:443e7f741c8e | 12 | static PwmOut led4(D5); |
bogdanm | 2:7e489126fe7a | 13 | |
bogdanm | 2:7e489126fe7a | 14 | /* Only GET and PUT method allowed */ |
bogdanm | 2:7e489126fe7a | 15 | static uint8_t light_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) |
bogdanm | 2:7e489126fe7a | 16 | { |
bogdanm | 2:7e489126fe7a | 17 | sn_coap_hdr_s *coap_res_ptr = 0; |
bogdanm | 2:7e489126fe7a | 18 | static float led_dimm = 0; |
bogdanm | 2:7e489126fe7a | 19 | int led_state = 0; |
bogdanm | 2:7e489126fe7a | 20 | char led_dimm_temp[4]; |
bogdanm | 2:7e489126fe7a | 21 | |
mazgch | 10:443e7f741c8e | 22 | printf("light callback\r\n"); |
bogdanm | 2:7e489126fe7a | 23 | |
bogdanm | 2:7e489126fe7a | 24 | if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) |
bogdanm | 2:7e489126fe7a | 25 | { |
bogdanm | 2:7e489126fe7a | 26 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
bogdanm | 2:7e489126fe7a | 27 | |
bogdanm | 2:7e489126fe7a | 28 | led_state = led_dimm * 100; |
bogdanm | 2:7e489126fe7a | 29 | sprintf(led_dimm_temp, "%d", led_state); |
bogdanm | 2:7e489126fe7a | 30 | |
bogdanm | 2:7e489126fe7a | 31 | coap_res_ptr->payload_len = strlen(led_dimm_temp); |
bogdanm | 2:7e489126fe7a | 32 | coap_res_ptr->payload_ptr = (uint8_t*)led_dimm_temp; |
bogdanm | 2:7e489126fe7a | 33 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
bogdanm | 2:7e489126fe7a | 34 | } |
bogdanm | 2:7e489126fe7a | 35 | else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) |
bogdanm | 2:7e489126fe7a | 36 | { |
bogdanm | 2:7e489126fe7a | 37 | memcpy(led_dimm_temp, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); |
bogdanm | 2:7e489126fe7a | 38 | |
bogdanm | 2:7e489126fe7a | 39 | led_dimm_temp[received_coap_ptr->payload_len] = '\0'; |
bogdanm | 2:7e489126fe7a | 40 | |
bogdanm | 2:7e489126fe7a | 41 | led_dimm = atof(led_dimm_temp); |
bogdanm | 2:7e489126fe7a | 42 | led_dimm = led_dimm/100; |
bogdanm | 2:7e489126fe7a | 43 | |
mazgch | 8:bd9096c4784c | 44 | led1.write(led_dimm); |
mazgch | 10:443e7f741c8e | 45 | led2.write(1.f - led_dimm); |
mazgch | 10:443e7f741c8e | 46 | led3.write(1.f - led_dimm); |
mazgch | 10:443e7f741c8e | 47 | led4.write(1.f - led_dimm); |
bogdanm | 2:7e489126fe7a | 48 | |
bogdanm | 2:7e489126fe7a | 49 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); |
bogdanm | 2:7e489126fe7a | 50 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
bogdanm | 2:7e489126fe7a | 51 | } |
bogdanm | 2:7e489126fe7a | 52 | |
bogdanm | 2:7e489126fe7a | 53 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
bogdanm | 2:7e489126fe7a | 54 | return 0; |
bogdanm | 2:7e489126fe7a | 55 | } |
bogdanm | 2:7e489126fe7a | 56 | |
bogdanm | 2:7e489126fe7a | 57 | int create_light_resource(sn_nsdl_resource_info_s *resource_ptr) |
bogdanm | 2:7e489126fe7a | 58 | { |
bogdanm | 2:7e489126fe7a | 59 | 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)); |
mazgch | 10:443e7f741c8e | 60 | static float led_dimm = 0; |
mazgch | 10:443e7f741c8e | 61 | led2.write(1.f - led_dimm); |
mazgch | 10:443e7f741c8e | 62 | led3.write(1.f - led_dimm); |
mazgch | 10:443e7f741c8e | 63 | led4.write(1.f - led_dimm); |
bogdanm | 2:7e489126fe7a | 64 | return 0; |
bogdanm | 2:7e489126fe7a | 65 | } |