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 Eric Gowland

Revision:
18:c1a2c0c738b2
Parent:
17:8ca4a5801430
Child:
24:ad4d74ed0e4b
--- a/pressure_mat.cpp	Mon Apr 07 09:29:11 2014 +0000
+++ b/pressure_mat.cpp	Fri Jun 27 13:29:54 2014 +0000
@@ -6,10 +6,15 @@
 
 #define PRESSURE_MAT_RES_ID     "sen/pressure_mat"
 
-//DigitalIn pressure_mat_in(PTD3);
+#define MINIMUM_POLL_PERIOD       1  //Seconds
+#define MINIMUM_REPORT_PERIOD    10  //Seconds
+
+//InterruptIn pressure_mat_in(PTD3);
 InterruptIn pressure_mat_in(PTD3);
 DigitalOut led1(LED1);
 Timer debounce;
+Timer reportTimer;
+Timer pollTimer;
 /* stored data for observable resource */
 static uint8_t obs_number = 0;
 static uint8_t *obs_token_ptr = NULL;
@@ -23,14 +28,31 @@
 /* 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;
+        current_pressure_mat = !pressure_mat_in;
+        led1 = !current_pressure_mat;
         debounce.reset();
     }
 }
 
+void pressure_mat_resetTokens() {
+    obs_number = 0;
+    *obs_token_ptr = NULL;
+    obs_token_len = 0;    
+}
+
 //This is to be called from main program loop... it only sends report if the pressure mat has changed.
 void pressure_mat_report() {
+    //Poll mat anyways...
+    if(pollTimer.read() > MINIMUM_POLL_PERIOD) {
+        current_pressure_mat = !pressure_mat_in;
+        led1 = !current_pressure_mat;
+        pollTimer.reset();
+    }
+    if(reportTimer.read() > MINIMUM_REPORT_PERIOD) {
+        //We haven't reported for minimum period, so take a reading and report.
+        last_reported_pressure_mat = current_pressure_mat + 10; //ensure different values to force report...
+        reportTimer.reset();
+    }
     if(last_reported_pressure_mat != current_pressure_mat) {
         if(obs_number != 0){// && obs_token_ptr != NULL){
             obs_number++;
@@ -50,7 +72,8 @@
 /* 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;
+    uint8_t pressure_mat_reading = !pressure_mat_in;
+    led1 = !pressure_mat_reading;
     snprintf(pressure_mat_val,2,"%d" ,pressure_mat_reading);
     sn_coap_hdr_s *coap_res_ptr = 0;
 
@@ -96,11 +119,14 @@
 {
     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++;
-    
+    pressure_mat_in.mode(PullUp);
     //Attach interrupt handler and start debounce...
     debounce.start();
     pressure_mat_in.rise(&pressure_mat_interrupt);
     pressure_mat_in.fall(&pressure_mat_interrupt);
     
+    reportTimer.start();
+    pollTimer.start();
+    
     return 0;
 }
\ No newline at end of file