v1.19 Release

Dependencies:   nRF51822

Revision:
50:8dca54c1e3fd
Parent:
49:d2bd9056426e
Child:
51:53fe9aff625a
Child:
54:bc0199198178
diff -r d2bd9056426e -r 8dca54c1e3fd Source/main.cpp
--- a/Source/main.cpp	Thu Sep 03 17:17:59 2020 +0000
+++ b/Source/main.cpp	Fri Sep 11 19:27:20 2020 +0000
@@ -52,17 +52,19 @@
 
 bool log_enable = true;     // turn off event logging when false (engineering function) – DEW
 
-bool package_open_detected = false;
-bool is_cap_off = false; // 0=cap on, 1=cap off
-bool last_cap_off = false;
+volatile bool package_open_detected = false;
+volatile bool is_cap_off = false; // 0=cap on, 1=cap off
+volatile bool last_cap_off = false;
+volatile bool adaptive_active = false;
+
 uint32_t cap_off_time;
 
-int16_t off_reading;
-int16_t on_reading;
-int16_t on_reading_filtered = 0;
-int16_t on_reading_peak = 0;
+volatile int16_t off_reading;
+volatile int16_t on_reading;
+volatile int16_t on_reading_filtered = 0;
+volatile int16_t on_reading_peak = 0;
 
-uint16_t cap_threshold_on = CAP_THRESHOLD_OFF_INITIAL;
+volatile uint16_t cap_threshold_on = CAP_THRESHOLD_ON_INITIAL;
 
 typedef  enum {
     INIT,
@@ -114,15 +116,6 @@
             #if ENABLE_LED        
                 led = 0;
             #endif  
-            
-            int16_t diff = on_reading - off_reading;
-            on_reading_filtered = (256-CAP_THRESHOLD_ADAPT_RATE)*(int32_t)on_reading_filtered/256 + (CAP_THRESHOLD_ADAPT_RATE)*(int32_t)diff/256;
-            
-            if(on_reading_filtered > on_reading_peak)
-            { // new higher diff value found
-                on_reading_peak = on_reading_filtered;
-                cap_threshold_on = CAP_THRESHOLD_PERCENT_OF_PEAK*on_reading_peak/256;             
-            }
         }
     }
     else
@@ -140,6 +133,21 @@
             #endif 
         }
     }
+    
+    // if adapting enabled and cap is on then adapt
+    if(adaptive_active && !is_cap_off)
+    {
+        // adaptive cap on threshold processing
+        int16_t diff = on_reading - off_reading;
+        on_reading_filtered = (256-CAP_THRESHOLD_ADAPT_RATE)*(int32_t)on_reading_filtered/256 + (CAP_THRESHOLD_ADAPT_RATE)*(int32_t)diff/256;
+        
+        if(on_reading_filtered > on_reading_peak)
+        { // new higher diff value found
+            on_reading_peak = on_reading_filtered;
+            cap_threshold_on = CAP_THRESHOLD_PERCENT_OF_PEAK*on_reading_peak/256;             
+        }
+    }
+    
     last_cap_off = is_cap_off;
 }
 
@@ -280,6 +288,18 @@
                 // set up and enable the Light Sense Interrupt
                 // go to lowest power state
                 
+                // before going to sleep, adapt the cap_on threshold (iff the cap is on)
+                {
+                    adaptive_active = true;
+                    int i = 20; // multiple calls aallow it to adapt slowly to eliminate potencial noise.
+                    while(i--) 
+                    {
+                        test_cap();
+                    }
+                    adaptive_active = false;
+                }
+                
+                // now go to sleep
                 debug("Going SHIP MODE\n");
                 is_package_open.disable_irq();
                 stop_periodic_tick();