From a26c6712005e3477d8717938e2e413c29d8ce5c1 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Mon, 3 Jun 2019 13:49:48 +0100
Subject: [PATCH] Migrate transponder to AbstractInstrument

---
 src/Instrumentation/AbstractInstrument.cxx | 12 +++++-
 src/Instrumentation/AbstractInstrument.hxx |  4 ++
 src/Instrumentation/transponder.cxx        | 48 +++++++++++-----------
 src/Instrumentation/transponder.hxx        | 31 ++++++--------
 4 files changed, 51 insertions(+), 44 deletions(-)

diff --git a/src/Instrumentation/AbstractInstrument.cxx b/src/Instrumentation/AbstractInstrument.cxx
index 22bf28b25..330e8bcbe 100644
--- a/src/Instrumentation/AbstractInstrument.cxx
+++ b/src/Instrumentation/AbstractInstrument.cxx
@@ -70,7 +70,7 @@ void AbstractInstrument::unbind()
 
 bool AbstractInstrument::isServiceableAndPowered() const
 {
-    if (!_serviceableNode->getBoolValue() || !_powerButtonNode->getBoolValue())
+    if (!_serviceableNode->getBoolValue() || !isPowerSwitchOn())
         return false;
     
     if (_powerSupplyNode->getDoubleValue() < _minimumSupplyVolts)
@@ -78,3 +78,13 @@ bool AbstractInstrument::isServiceableAndPowered() const
     
     return true;
 }
+
+void AbstractInstrument::setMinimumSupplyVolts(double v)
+{
+    _minimumSupplyVolts = v;
+}
+
+bool AbstractInstrument::isPowerSwitchOn() const
+{
+    return _powerButtonNode->getBoolValue();
+}
diff --git a/src/Instrumentation/AbstractInstrument.hxx b/src/Instrumentation/AbstractInstrument.hxx
index 788a06374..b57e66e14 100644
--- a/src/Instrumentation/AbstractInstrument.hxx
+++ b/src/Instrumentation/AbstractInstrument.hxx
@@ -39,6 +39,10 @@ protected:
     int number() const { return _index; }
     
     void unbind() override;
+    
+    void setMinimumSupplyVolts(double v);
+    
+    virtual bool isPowerSwitchOn() const;
 private:
     std::string _name;
     int _index = 0;
diff --git a/src/Instrumentation/transponder.cxx b/src/Instrumentation/transponder.cxx
index 0753da765..4bb552609 100644
--- a/src/Instrumentation/transponder.cxx
+++ b/src/Instrumentation/transponder.cxx
@@ -45,9 +45,7 @@
 // this is /instrumentation/airspeed-indicator/indicated-speed-kt but this can be
 // changed by setting the airspeed-path property as shown above.
 
-#ifdef HAVE_CONFIG_H
-#  include <config.h>
-#endif
+#include <config.h>
 
 #include "transponder.hxx"
 
@@ -66,15 +64,17 @@ const int INVALID_ALTITUDE = -9999;
 const int INVALID_AIRSPEED = -9999;
 const int INVALID_ID = -9999;
 
-Transponder::Transponder(SGPropertyNode *node)
-    :
-    _identMode(false),
-    _name(node->getStringValue("name", "transponder")),
-    _num(node->getIntValue("number", 0)),
-    _mode((Mode) node->getIntValue("mode", 1)),
-    _listener_active(0)
+Transponder::Transponder(SGPropertyNode *node) :
+    _mode((Mode) node->getIntValue("mode", 1))
 {
-    _requiredBusVolts = node->getDoubleValue("bus-volts", 8.0);
+    readConfig(node, "transponder");
+
+    // ensure older power-supply name works
+    if (node->hasChild("bus-volts")) {
+        SG_LOG(SG_INSTR, SG_DEV_WARN, "Transponder: use new supply-volatge prop: 'minimum-supply-volts' instead of 'bus-volts'");
+        setMinimumSupplyVolts(node->getDoubleValue("bus-volts", 8.0));
+    }
+    
     _altitudeSourcePath = node->getStringValue("encoder-path", "/instrumentation/altimeter");
     _autoGroundPath = node->getStringValue("auto-ground");
     _airspeedSourcePath = node->getStringValue("airspeed-path", "/instrumentation/airspeed-indicator/indicated-speed-kt");
@@ -89,10 +89,10 @@ Transponder::~Transponder()
 
 void Transponder::init()
 {
-    SGPropertyNode *node = fgGetNode("/instrumentation/" + _name, _num, true );
+    SGPropertyNode *node = fgGetNode(nodePath(), true);
+    initServicePowerProperties(node);
 
     // Inputs
-    _busPower_node = fgGetNode("/systems/electrical/outputs/transponder", true);
     _pressureAltitude_node = fgGetNode(_altitudeSourcePath, true);
     _autoGround_node = fgGetNode(_autoGroundPath, true);
     _airspeedIndicator_node = fgGetNode(_airspeedSourcePath, true);
@@ -123,9 +123,6 @@ void Transponder::init()
     _identBtn_node->setBoolValue(false);
     _identBtn_node->addChangeListener(this);
     
-    _serviceable_node = node->getChild("serviceable", 0, true);
-    _serviceable_node->setBoolValue(true);
-    
     _idCode_node = node->getChild("id-code", 0, true);
     _idCode_node->addChangeListener(this);
     // set default, but don't overwrite value from defaults.xml or -set.xml
@@ -153,7 +150,7 @@ void Transponder::init()
 void Transponder::bind()
 {
     if (_kt70Compat) {
-        SGPropertyNode *node = fgGetNode("/instrumentation/" + _name, _num, true );
+        SGPropertyNode *node = fgGetNode(nodePath(), true );
         _tiedProperties.setRoot(node);
         
         _tiedProperties.Tie("annunciators/fl", this,
@@ -174,12 +171,12 @@ void Transponder::bind()
 void Transponder::unbind()
 {
     _tiedProperties.Untie();
+    AbstractInstrument::unbind();
 }
 
-
 void Transponder::update(double dt)
 {
-    if (has_power() && _serviceable_node->getBoolValue())
+    if (isServiceableAndPowered())
     {
         // Mode C & S send also altitude
         Mode effectiveMode = (_knob == KNOB_ALT || _knob == KNOB_GROUND) ? _mode : MODE_A;
@@ -243,7 +240,7 @@ void Transponder::update(double dt)
         }
     }
     else
-    {
+    { // un-powered or u/s
       _altitude_node->setIntValue(INVALID_ALTITUDE);
       _altitudeValid_node->setBoolValue(false);
       _ident_node->setBoolValue(false);
@@ -314,11 +311,6 @@ void Transponder::valueChanged(SGPropertyNode *prop)
     _listener_active--;
 }
 
-bool Transponder::has_power() const
-{
-    return (_knob_node->getIntValue() > KNOB_STANDBY) && (_busPower_node->getDoubleValue() > _requiredBusVolts);
-}
-
 bool Transponder::getFLAnnunciator() const
 {
     return (_knob == KNOB_ALT) || (_knob == KNOB_GROUND) || (_knob == KNOB_TEST);
@@ -349,3 +341,9 @@ bool Transponder::getReplyAnnunciator() const
     return _identMode || (_knob == KNOB_TEST);
 }
 
+bool Transponder::isPowerSwitchOn() const
+{
+    return (_knob_node->getIntValue() > KNOB_STANDBY);
+}
+
+
diff --git a/src/Instrumentation/transponder.hxx b/src/Instrumentation/transponder.hxx
index 4b97a8cf6..acd7cae75 100644
--- a/src/Instrumentation/transponder.hxx
+++ b/src/Instrumentation/transponder.hxx
@@ -22,25 +22,24 @@
 #ifndef TRANSPONDER_HXX
 #define TRANSPONDER_HXX 1
 
-#ifndef __cplusplus
-# error This library requires C++
-#endif
-
-#include <Main/fg_props.hxx>
+#include <Instrumentation/AbstractInstrument.hxx>
 
 #include <simgear/structure/subsystem_mgr.hxx>
 #include <simgear/props/tiedpropertylist.hxx>
 
-class Transponder : public SGSubsystem, public SGPropertyChangeListener
+class Transponder : public AbstractInstrument, public SGPropertyChangeListener
 {
 public:
     Transponder(SGPropertyNode *node);
     virtual ~Transponder();
 
-    virtual void init ();
-    virtual void update (double dt);
-    virtual void bind();
-    virtual void unbind();
+    void init () override;
+    void update (double dt) override;
+    void bind() override;
+    void unbind() override;
+    
+protected:
+    bool isPowerSwitchOn() const override;
     
 private:
     enum Mode
@@ -73,8 +72,6 @@ private:
     SGPropertyNode_ptr _pressureAltitude_node;
     SGPropertyNode_ptr _autoGround_node;
     SGPropertyNode_ptr _airspeedIndicator_node;
-    SGPropertyNode_ptr _busPower_node;
-    SGPropertyNode_ptr _serviceable_node;
 
     SGPropertyNode_ptr _mode_node;
     SGPropertyNode_ptr _knob_node;
@@ -84,7 +81,7 @@ private:
     simgear::TiedPropertyList _tiedProperties;
     
     SGPropertyNode_ptr _identBtn_node;
-    bool _identMode;
+    bool _identMode = false;
     bool _kt70Compat;
     
     // Outputs
@@ -96,20 +93,18 @@ private:
     SGPropertyNode_ptr _airspeed_node;
 
     // Internal
-    std::string _name;
-    int _num;
     Mode _mode;
     KnobPosition _knob;
     double _identTime;
-    int _listener_active;
+    int _listener_active = 0;
     double _requiredBusVolts;
     std::string _altitudeSourcePath;
     std::string _autoGroundPath;
     std::string _airspeedSourcePath;
     
-    void valueChanged (SGPropertyNode *);
+    void valueChanged (SGPropertyNode *) override;
+    
     int setMinMax(int val);
-    bool has_power() const;
 };
 
 #endif // TRANSPONDER_HXX