![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Fork of KL46Z Wifi version. Moved to KL25Z as umbrella stand now requires 2 SPIs, thus requires 46z.
Dependencies: WIZnet_Library_ASE mbed nanoservice_client_1_12
Fork of Trenton_Doormat_FRDM-KL25Z_ETH by
Diff: pressure_mat.cpp
- Revision:
- 17:8ca4a5801430
- Child:
- 18:c1a2c0c738b2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pressure_mat.cpp Mon Apr 07 09:29:11 2014 +0000 @@ -0,0 +1,106 @@ +// Pressure Mat resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "pressure_mat.h" + +#define PRESSURE_MAT_RES_ID "sen/pressure_mat" + +//DigitalIn pressure_mat_in(PTD3); +InterruptIn pressure_mat_in(PTD3); +DigitalOut led1(LED1); +Timer debounce; +/* stored data for observable resource */ +static uint8_t obs_number = 0; +static uint8_t *obs_token_ptr = NULL; +static uint8_t obs_token_len = 0; +static uint8_t current_pressure_mat = 0; +static uint8_t last_reported_pressure_mat = 99; +static char pressure_mat_val[2]; + + +/* Interrupt handler for pressure mat pin */ +/* Handles Interrupt, sets state for main polling thread to send update message. */ +void pressure_mat_interrupt(){ + if(debounce.read_ms() > 200) { + led1 = pressure_mat_in; + current_pressure_mat = pressure_mat_in; + debounce.reset(); + } +} + +//This is to be called from main program loop... it only sends report if the pressure mat has changed. +void pressure_mat_report() { + if(last_reported_pressure_mat != current_pressure_mat) { + if(obs_number != 0){// && obs_token_ptr != NULL){ + obs_number++; + snprintf(pressure_mat_val,2,"%d" ,current_pressure_mat); + if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)pressure_mat_val, 1, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) + printf("Pressure Observation Sending Failed\r\n"); + else + printf("Pressure Observation Sent\r\n"); + + //Set last reported to current regardless... + last_reported_pressure_mat = current_pressure_mat; + } + } +} + +/* Only GET method allowed */ +/* Observable resource */ +static uint8_t pressure_mat_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + uint8_t pressure_mat_reading = pressure_mat_in; + snprintf(pressure_mat_val,2,"%d" ,pressure_mat_reading); + sn_coap_hdr_s *coap_res_ptr = 0; + + printf("pressure mat callback\r\n"); + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); + + coap_res_ptr->payload_len = 1; + coap_res_ptr->payload_ptr = (uint8_t*)pressure_mat_val; + + if(received_coap_ptr->token_ptr) + { + printf(" Token included\r\n"); + if(obs_token_ptr) + { + free(obs_token_ptr); + obs_token_ptr = 0; + } + obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); + if(obs_token_ptr) + { + memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); + obs_token_len = received_coap_ptr->token_len; + } + } + + if(received_coap_ptr->options_list_ptr->observe) + { + coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); + memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); + coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; + coap_res_ptr->options_list_ptr->observe_len = 1; + obs_number++; + } + printf(" Send observation %d... \r\n", obs_number); + sn_nsdl_send_coap_message(address, coap_res_ptr); + + coap_res_ptr->options_list_ptr->observe_ptr = 0; + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + +int create_pressure_mat_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(PRESSURE_MAT_RES_ID)-1, (uint8_t*)PRESSURE_MAT_RES_ID, 0, 0, 1, &pressure_mat_resource_cb, SN_GRS_GET_ALLOWED); + obs_number++; + + //Attach interrupt handler and start debounce... + debounce.start(); + pressure_mat_in.rise(&pressure_mat_interrupt); + pressure_mat_in.fall(&pressure_mat_interrupt); + + return 0; +} \ No newline at end of file