1
0
Fork 0

Add AbstractInstrument base class and convert DME

Base class handles some common instrument features, such as the
serviceable node and electrical supply. The electrical supply node
and minimum voltage to operate can now be set in the configuration
via <power-supply>/path/to/output/volts</> and <minimum-supply-volts>
This commit is contained in:
James Turner 2019-04-24 18:56:56 +01:00
parent b2bedfd485
commit 6261b71774
5 changed files with 133 additions and 28 deletions

View file

@ -0,0 +1,63 @@
// Copyright (C) 2019 James Turner <james@flightgear.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "config.h"
#include <simgear/debug/logstream.hxx>
#include <Instrumentation/AbstractInstrument.hxx>
#include <Main/fg_props.hxx>
void AbstractInstrument::readConfig(SGPropertyNode* config,
std::string defaultName)
{
_name = config->getStringValue("name", defaultName.c_str());
_index = config->getIntValue("number", 0);
_powerSupplyPath = "/systems/electrical/outputs/" + defaultName;
if (config->hasChild("power-supply")) {
_powerSupplyPath = config->getStringValue("power-supply");
}
// the default output values are volts, but various places have been
// treating the value as a bool,, so we default to 1.0 as our minimum
// supply volts
_minimumSupplyVolts = config->getDoubleValue("minimum-supply-volts", 1.0);
}
std::string AbstractInstrument::nodePath() const
{
return "/instrumentation/" + _name + "[" + std::to_string(_index) + "]";
}
void AbstractInstrument::initServicePowerProperties(SGPropertyNode* node)
{
_serviceableNode = node->getNode("serviceable", 0, true);
if (_serviceableNode->getType() == simgear::props::NONE)
_serviceableNode->setBoolValue(true);
_powerSupplyNode = fgGetNode(_powerSupplyPath, true);
}
bool AbstractInstrument::isServiceableAndPowered() const
{
if (!_serviceableNode->getBoolValue())
return false;
if (_powerSupplyNode->getDoubleValue() < _minimumSupplyVolts)
return false;
return true;
}

View file

@ -0,0 +1,51 @@
// Copyright (C) 2019 James Turner <james@flightgear.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef FG_ABSTRACT_INSTRUMENT_HXX
#define FG_ABSTRACT_INSTRUMENT_HXX
#include <simgear/props/props.hxx>
#include <simgear/structure/subsystem_mgr.hxx>
class AbstractInstrument : public SGSubsystem
{
public:
protected:
void readConfig(SGPropertyNode* config,
std::string defaultName);
void initServicePowerProperties(SGPropertyNode* node);
bool isServiceableAndPowered() const;
// build the path /instrumentation/<name>[number]
std::string nodePath() const;
std::string name() const { return _name; }
int number() const { return _index; }
private:
std::string _name;
int _index = 0;
std::string _powerSupplyPath;
SGPropertyNode_ptr _serviceableNode;
double _minimumSupplyVolts;
SGPropertyNode_ptr _powerSupplyNode;
};
#endif // of FG_ABSTRACT_INSTRUMENT_HXX

View file

@ -1,6 +1,7 @@
include(FlightGearComponent)
set(SOURCES
AbstractInstrument.cxx
adf.cxx
airspeed_indicator.cxx
altimeter.cxx
@ -62,6 +63,7 @@ set(SOURCES
)
set(HEADERS
AbstractInstrument.hxx
adf.hxx
airspeed_indicator.hxx
altimeter.hxx
@ -111,6 +113,6 @@ set(HEADERS
KLN89/kln89_page_usr.hxx
KLN89/kln89_page_vor.hxx
KLN89/kln89_page_alt.hxx
)
)
flightgear_component(Instruments "${SOURCES}" "${HEADERS}")

View file

@ -80,10 +80,9 @@ DME::DME ( SGPropertyNode *node )
_last_frequency_mhz(-1),
_time_before_search_sec(0),
_navrecord(NULL),
_name(node->getStringValue("name", "dme")),
_num(node->getIntValue("number", 0)),
_audioIdent(NULL)
{
readConfig(node, "dme");
}
DME::~DME ()
@ -94,13 +93,10 @@ DME::~DME ()
void
DME::init ()
{
std::string branch;
branch = "/instrumentation/" + _name;
SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
_serviceable_node = node->getChild("serviceable", 0, true);
_electrical_node = fgGetNode("/systems/electrical/outputs/dme", true);
std::string branch = nodePath();
SGPropertyNode *node = fgGetNode(branch, true );
initServicePowerProperties(node);
SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
_source_node = fnode->getChild("source", 0, true);
_frequency_node = fnode->getChild("selected-mhz", 0, true);
@ -127,9 +123,9 @@ DME::init ()
_time_string->setStringValue("--");
std::ostringstream temp;
temp << _name << "-ident-" << _num;
temp << name() << "-ident-" << number();
if( NULL == _audioIdent )
_audioIdent = new DMEAudioIdent( temp.str() );
_audioIdent = new DMEAudioIdent(temp.str());
_audioIdent->init();
reinit();
@ -148,11 +144,12 @@ DME::update (double delta_time_sec)
if( delta_time_sec < SGLimitsd::min() )
return; //paused
char tmp[16];
// Figure out the source
// Figure out the source
const char * source = _source_node->getStringValue();
if (source[0] == '\0') {
std::string branch;
branch = "/instrumentation/" + _name + "/frequencies/selected-mhz";
branch = "/instrumentation/" + name() + "/frequencies/selected-mhz";
_source_node->setStringValue(branch.c_str());
source = _source_node->getStringValue();
}
@ -176,13 +173,12 @@ DME::update (double delta_time_sec)
_navrecord = FGNavList::findByFreq(frequency_mhz, pos, &filter);
}
// If it's off, don't bother.
if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
clear();
if (!isServiceableAndPowered()) {
clear();
return;
}
// If it's on, but invalid source,don't bother.
if (nullptr == _navrecord) {
clear();

View file

@ -7,8 +7,7 @@
#ifndef __INSTRUMENTS_DME_HXX
#define __INSTRUMENTS_DME_HXX 1
#include <simgear/props/props.hxx>
#include <simgear/structure/subsystem_mgr.hxx>
#include <Instrumentation/AbstractInstrument.hxx>
// forward decls
class FGNavRecord;
@ -33,7 +32,7 @@ class FGNavRecord;
* /instrumentation/"name"/indicated-ground-speed-kt
* /instrumentation/"name"/indicated-time-kt
*/
class DME : public SGSubsystem
class DME : public AbstractInstrument
{
public:
@ -48,8 +47,6 @@ public:
private:
void clear();
SGPropertyNode_ptr _serviceable_node;
SGPropertyNode_ptr _electrical_node;
SGPropertyNode_ptr _source_node;
SGPropertyNode_ptr _frequency_node;
@ -70,10 +67,6 @@ private:
FGNavRecord * _navrecord;
std::string _name;
int _num;
class AudioIdent * _audioIdent;
};