Changing wording of failsafe transition messages

Cleaning up and unifying the logic around displaying diagnostic
messages when failsafe transitions happen, so that the true case and
the false case run the same code path. This makes the messaging more
standard and coherent from the user point of view.

This will cause one additional logging message to appear during
startup. This is intentional: when the process is started up, this
counts as a state transition that is worth logging about, as it goes
from uncontrolled state (essentially manual mode) to either failsafe
mode or normal mode.

Tested: Installed and observed messages logged during startup

Change-Id: Ib80cc342611a78199410564f76a2c65b590ef511
Signed-off-by: Josh Lehan <krellan@google.com>
diff --git a/pid/fancontroller.cpp b/pid/fancontroller.cpp
index ddfea20..a1290c5 100644
--- a/pid/fancontroller.cpp
+++ b/pid/fancontroller.cpp
@@ -130,7 +130,16 @@
     /* If doing tuning, don't go into failsafe mode. */
     if (!tuningEnabled)
     {
-        if (_owner->getFailSafeMode())
+        bool failsafeCurrState = _owner->getFailSafeMode();
+
+        // Note when failsafe state transitions happen
+        if (failsafePrevState != failsafeCurrState)
+        {
+            failsafePrevState = failsafeCurrState;
+            failsafeTransition = true;
+        }
+
+        if (failsafeCurrState)
         {
             double failsafePercent = _owner->getFailSafePercent();
 
@@ -147,25 +156,38 @@
             {
                 percent = failsafePercent;
             }
-
-            if (failsafePrint || debugEnabled)
-            {
-                std::cerr << "Zone " << _owner->getZoneID()
-                          << " fans output failsafe pwm: " << percent << "\n";
-                failsafePrint = false;
-            }
 #endif
         }
+
+        // Always print if debug enabled
+        if (debugEnabled)
+        {
+            std::cerr << "Zone " << _owner->getZoneID() << " fans, "
+                      << (failsafeCurrState ? "failsafe" : "normal")
+                      << " mode, output pwm: " << percent << "\n";
+        }
         else
         {
-            failsafePrint = true;
-            if (debugEnabled)
+            // Only print once per transition when not debugging
+            if (failsafeTransition)
             {
-                std::cerr << "Zone " << _owner->getZoneID()
-                          << " fans output pwm: " << percent << "\n";
+                failsafeTransition = false;
+                std::cerr << "Zone " << _owner->getZoneID() << " fans, "
+                          << (failsafeCurrState ? "entering failsafe"
+                                                : "returning to normal")
+                          << " mode, output pwm: " << percent << "\n";
             }
         }
     }
+    else
+    {
+        if (debugEnabled)
+        {
+            std::cerr << "Zone " << _owner->getZoneID()
+                      << " fans, tuning mode, bypassing failsafe, output pwm: "
+                      << percent << "\n";
+        }
+    }
 
     // value and kFanFailSafeDutyCycle are 10 for 10% so let's fix that.
     percent /= 100.0;
diff --git a/pid/fancontroller.hpp b/pid/fancontroller.hpp
index 89b3d5e..447ce7b 100644
--- a/pid/fancontroller.hpp
+++ b/pid/fancontroller.hpp
@@ -47,7 +47,10 @@
   private:
     std::vector<std::string> _inputs;
     FanSpeedDirection _direction;
-    bool failsafePrint = true;
+
+    // Cosmetic only, to reduce frequency of repetitive messages
+    bool failsafeTransition = true;
+    bool failsafePrevState = false;
 };
 
 } // namespace pid_control