Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Revision:
260:fe726583ba1d
Parent:
253:ae850c19cf81
Child:
261:4e9a588c938e
--- a/src/ConfigurationHandler/Controls/FailsafeControl.cpp	Fri Oct 21 19:13:21 2016 +0000
+++ b/src/ConfigurationHandler/Controls/FailsafeControl.cpp	Mon Oct 24 13:07:52 2016 +0000
@@ -86,69 +86,68 @@
 //
 void FailsafeControl::update(void)
 {
-    switch ( this->currentState ) {
+    switch (this->currentState) {
         case STATE_INIT:
-            // do nothing
+            // do nothing - must be invoked
             break;
         case STATE_START:
+            // control has been started, do some checking
             if ( this->aboveHighFailsafe() ) {
-                currentState = STATE_CONTROL_HFS_ON;
+                this->currentState = STATE_CONTROL_HFS_ON;
             } else if ( this->belowLowFailsafe() ) {
-                currentState = STATE_CONTROL_LFS_ON;
+                this->currentState = STATE_CONTROL_LFS_ON;
             } else {
-                currentState = STATE_CONTROL_OFF;
+                this->currentState = STATE_CONTROL_OFF;
             }
             break;
         case STATE_CONTROL_OFF:
+            // control is acting normal, within bounds
             if ( this->aboveHighFailsafe() ) {
-                currentState = STATE_CONTROL_HFS_ON;
-                if ( hfs_data.dutyCycle ) {
-                    this->startHfsTimer();
-                    // send an ON request to the output thread
-                    sendMail(ACTION_CONTROL_ON);
-                } else {
-                    // fixed off
-                    sendMail(ACTION_CONTROL_OFF);
-                    // fixed off
-                }
+                this->currentState = STATE_CONTROL_HFS_ON;
             } else if ( this->belowLowFailsafe() ) {
-                currentState = STATE_CONTROL_LFS_ON;
+                this->currentState = STATE_CONTROL_LFS_ON;
             } else {
                 // do nothing
             }
             break;
         case STATE_CONTROL_LFS_ON:
+            // control is in low-failsafe with duty cycle ON
             if ( !this->belowLowFailsafe() ) {
-                currentState = STATE_CONTROL_OFF;
+                this->currentState = STATE_CONTROL_OFF;
+            } else if ( this->dutyOnExpired() ) {
+                this->currentState = STATE_CONTROL_LFS_OFF;
             } else {
-                // check the time interval
+                // do nothing
             }
             break;
         case STATE_CONTROL_LFS_OFF:
+            // control is in low-failsafe with duty cycle OFF
             if ( !this->belowLowFailsafe() ) {
-                currentState = STATE_CONTROL_OFF;
+                this->currentState = STATE_CONTROL_OFF;
+            } else if ( this->dutyOffExpired() ) {
+                this->currentState = STATE_CONTROL_LFS_ON;
             } else {
-
+                // do nothing
             }
             break;
         case STATE_CONTROL_HFS_ON:
-            // the control is in HFS with the output ON
+            // control is in high-failsafe with duty cycle ON
             if ( !this->aboveHighFailsafe() ) {
-                currentState = STATE_CONTROL_OFF;
-                // clear the timer
-                this->stopHfsTimer();
-            } else if ( this->hfsTimerElapsed() ) {
-                // duty cyle
-                printf("\rInitial timer has expired\n");
-                currentState = STATE_CONTROL_HFS_OFF;
+                this->currentState = STATE_CONTROL_OFF;
+            } else if ( this->dutyOnExpired() ) {
+                this->currentState = STATE_CONTROL_HFS_OFF;
+            } else {
+                // do nothing
             }
             break;
         case STATE_CONTROL_HFS_OFF:
-            // the control is in HFS with the output OFF
+            // control is in high-failsafe with cuty cycle OFF
             if ( !this->aboveHighFailsafe() ) {
-                currentState = STATE_CONTROL_OFF;
-            } else {
-
+                this->currentState = STATE_CONTROL_OFF;
+            } else if ( this->dutyOffExpired() ) {
+                this->currentState = STATE_CONTROL_LFS_ON;
+            } else { 
+                // do nothing
             }
             break;
         default:
@@ -185,42 +184,25 @@
     return ( value.value >= hfs_data.value );
 }
 
-//
-// method:          startHfsTimer
-// description:
-void FailsafeControl::startHfsTimer(void)
+bool FailsafeControl::dutyOnExpired() 
 {
-    printf("%s invoked\n", __func__);
-    unsigned long currentTime = time(NULL);
-    unsigned long period = hfs_data.interval * 60;
-
-    double totalOnTime = (double)period * ((double)hfs_data.dutyCycle/(double)100.0);
-    duty_timer.offTime = (unsigned long) totalOnTime;
-    duty_timer.expirationTime = currentTime + period;
-
-    printf("\rnext off time = %lu\n", duty_timer.offTime);
-    printf("\rexpiration time = %lu\n", duty_timer.expirationTime);
+    return false;
 }
 
-void FailsafeControl::stopHfsTimer(void)
+bool FailsafeControl::dutyOffExpired()
 {
-    memset(&duty_timer, 0, sizeof(duty_timer));
-}
-
-bool FailsafeControl::hfsTimerElapsed(void)
-{
-    return ( duty_timer.offTime >= time(NULL) );
+    return false;
 }
 
 //
-// method:      sendMail
+// method:      sendMailToOutput
 // description: send mail to the output task
 //
 // @param       io_tag  -> input/output tag
 // @param       action  -> ON, OFF, UNREGISTER
 // @return      none
 //
-void FailsafeControl::sendMail(OutputAction action)
+void FailsafeControl::sendMailToOutput(OutputAction action)
 {
     logInfo("%s: failsafe control attempting to send action %d\n",
             __func__, action);
@@ -241,6 +223,30 @@
 
 
 //
+// method:          unregisterControl
+// description:     unregister this control with the output task
+//
+// @param           none
+// @return          none
+//
+void FailsafeControl::unregisterControl(void)
+{
+    logInfo("%s: %s attempting to unregister %s\n",
+            __func__, controlFile.c_str());
+
+    OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
+    memset(output_mail, 0, sizeof(OutputControlMsg_t));
+
+    output_mail->action      = ACTION_CONTROL_UNREGISTER;
+    output_mail->controlType = CONTROL_FAILSAFE;
+    output_mail->priority     = this->priority;
+    strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
+    strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
+
+    OutputMasterMailBox.put(output_mail);
+}
+
+//
 // method:      display
 // description: display the pertinents
 //