imob

Dependencies:   mbedtls mbed BLE_API nRF51822 AccelSensor

Revision:
1:471d502617fe
Parent:
0:5284859bb3e8
--- a/RELAYService.h	Tue Mar 20 14:31:59 2018 +0000
+++ b/RELAYService.h	Sat Mar 20 19:09:06 2021 +0000
@@ -6,9 +6,14 @@
 #include "ble/Gap.h"
 #include "ImobStateService.h"
 
-#define RELAY_TIME 2000000 // us
+#define RELAY_TIME 120000000 // us
 #define CTR12V_TIME 100000 // us
 
+Ticker waitTicker;
+bool activation_in_progress = false;
+
+
+
 class RELAYService {
 public:
     const static uint16_t RELAY_SERVICE_UUID = 0xC000;
@@ -44,14 +49,14 @@
     
     void activate()
     {
-        Ctr12v = 1;
-        wait_us(CTR12V_TIME);
-        updateRelayState(1);
-        wait_us(RELAY_TIME);//depending of the time switching desired
-        updateRelayState(0);
-        wait_us(CTR12V_TIME);
-        Ctr12v = 0;   
+        if(!activation_in_progress)
+        {
+            flipCtr12v();       
+            waitTicker.attach(callback(this, &RELAYService::internalUpdateRelaystate), CTR12V_TIME/1000000.0);
+        }
     }
+    
+    
 
 protected:
 
@@ -60,7 +65,7 @@
         if(authenticated && activated)
         {
             activate();
-            //ISS->resetAuthenticationValues();
+            ISS->resetAuthenticationValues();
         }
     }
     
@@ -74,11 +79,39 @@
             if(!activated)
                 ISS->updateActivationValue(1);
         }
-        else
-            updateRelayState(0);
+        //else
+        //    updateRelayState(0);
     }     
 
 private:
+
+    void internalUpdateRelaystate()
+    {   
+        uint8_t aux_relayState = (relayState) ? 0: 1;
+        updateRelayState(aux_relayState);
+        
+        if(aux_relayState) 
+        {
+            waitTicker.detach();
+            waitTicker.attach(callback(this, &RELAYService::internalUpdateRelaystate), RELAY_TIME/1000000.0);
+        }
+        else
+        {
+            waitTicker.detach();
+            waitTicker.attach(callback(this, &RELAYService::flipCtr12v), CTR12V_TIME/1000000.0);
+        }
+    }
+    
+    void flipCtr12v()
+    {
+         Ctr12v = !Ctr12v;
+         if(!Ctr12v)
+         {
+             waitTicker.detach();
+             activation_in_progress = false;
+         }
+         else activation_in_progress = true;
+    }
     
     BLEDevice &ble;
     uint8_t relayState;