| 1 | // digitalcomponent.cxx - Base class for digital autopilot components |
| 2 | // |
| 3 | // Written by Torsten Dreyer |
| 4 | // Based heavily on work created by Curtis Olson, started January 2004. |
| 5 | // |
| 6 | // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt |
| 7 | // Copyright (C) 2010 Torsten Dreyer - Torsten (at) t3r (dot) de |
| 8 | // |
| 9 | // This program is free software; you can redistribute it and/or |
| 10 | // modify it under the terms of the GNU General Public License as |
| 11 | // published by the Free Software Foundation; either version 2 of the |
| 12 | // License, or (at your option) any later version. |
| 13 | // |
| 14 | // This program is distributed in the hope that it will be useful, but |
| 15 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | // General Public License for more details. |
| 18 | // |
| 19 | // You should have received a copy of the GNU General Public License |
| 20 | // along with this program; if not, write to the Free Software |
| 21 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 | // |
| 23 | |
| 24 | #include "digitalcomponent.hxx" |
| 25 | #include <Main/fg_props.hxx> |
| 26 | |
| 27 | #include <simgear/misc/strutils.hxx> |
| 28 | |
| 29 | using std::string; |
| 30 | using namespace FGXMLAutopilot; |
| 31 | |
| 32 | DigitalComponent::DigitalComponent() : |
| 33 | _inverted(false) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | bool DigitalComponent::InputMap::get_value( const std::string & name ) const |
| 38 | { |
| 39 | // can't use map::operator[] here since it's not const |
| 40 | const_iterator __i = lower_bound( name ); |
| 41 | if (__i == end() || key_comp()(name, (*__i).first)) |
| 42 | return false; // does not exist, return false |
| 43 | |
| 44 | return (*__i).second->test(); |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | <input> |
| 49 | <name>Foo</name> |
| 50 | <condition> |
| 51 | <and>...</and> |
| 52 | </condition> |
| 53 | </input> |
| 54 | <output> |
| 55 | <name>Bar</name> |
| 56 | <property>/foo/bar</property> |
| 57 | <inverted>true</inverted> |
| 58 | </output> |
| 59 | <output>/some/property</output> |
| 60 | */ |
| 61 | bool DigitalComponent::configure( SGPropertyNode& cfg_node, |
| 62 | const std::string& cfg_name, |
| 63 | SGPropertyNode& prop_root ) |
| 64 | { |
| 65 | if (cfg_name == "input") { |
| 66 | SGPropertyNode_ptr nameNode = cfg_node.getNode("name"); |
| 67 | string name; |
| 68 | if( nameNode != NULL ) { |
| 69 | name = nameNode->getStringValue(); |
| 70 | } else { |
| 71 | std::ostringstream buf; |
| 72 | buf << "Input" << _input.size(); |
| 73 | name = buf.str(); |
| 74 | } |
| 75 | _input[name] = sgReadCondition(&prop_root, &cfg_node); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | if (cfg_name == "output") { |
| 80 | SGPropertyNode_ptr n = cfg_node.getNode("name"); |
| 81 | string name; |
| 82 | if( n != NULL ) { |
| 83 | name = n->getStringValue(); |
| 84 | } else { |
| 85 | std::ostringstream buf; |
| 86 | buf << "Output" << _output.size(); |
| 87 | name = buf.str(); |
| 88 | } |
| 89 | |
| 90 | DigitalOutput_ptr o = new DigitalOutput(); |
| 91 | _output[name] = o; |
| 92 | |
| 93 | if( (n = cfg_node.getNode("inverted")) != NULL ) |
| 94 | o->setInverted( n->getBoolValue() ); |
| 95 | |
| 96 | if( (n = cfg_node.getNode("property")) != NULL ) { |
| 97 | const auto trimmed = simgear::strutils::strip(n->getStringValue()); |
| 98 | o->setProperty( prop_root.getNode(trimmed, true) ); |
| 99 | } |
| 100 | |
| 101 | if( cfg_node.nChildren() == 0 ) { |
| 102 | const auto trimmed = simgear::strutils::strip(cfg_node.getStringValue()); |
| 103 | o->setProperty( prop_root.getNode(trimmed, true) ); |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | if (cfg_name == "inverted") { |
| 110 | _inverted = cfg_node.getBoolValue(); |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | return Component::configure(cfg_node, cfg_name, prop_root); |
| 115 | } |