]> git.mxchange.org Git - flightgear.git/commitdiff
Melchior FRANZ:
authorehofman <ehofman>
Fri, 27 Jun 2003 08:46:57 +0000 (08:46 +0000)
committerehofman <ehofman>
Fri, 27 Jun 2003 08:46:57 +0000 (08:46 +0000)
These patches add a clock instrument, which allows to model failure ("serviceable") and to adjust the time independently of the system time (defaults to GMT). The main incentive is to make the p51d clock work and adjustable via the knob.

 o Offers a time string ("12:03:15") for the LCD or for LED
   clocks, or an empty string in case of failure/power off. The
   instrument assumes that digital clocks are battery buffered,
   so they will be updated even if there's nothing on the display.

 o Offers the number of seconds since midnight for analog
   clocks, like in the p51d. This number is not increased
   if !serviceable. So the clock will stand still and continue
   where it stopped when it's serviceable again.

I did not consider voltage yet, because the Mustang's clock will need a lot more current than the LCD clock. The instrument is updated 4 times per second but returns immediately if neither time nor offset changed. The function getGMTString() in fg_props.cxx could be removed after applying these patches.

src/Instrumentation/Makefile.am
src/Instrumentation/clock.cxx [new file with mode: 0644]
src/Instrumentation/clock.hxx [new file with mode: 0644]
src/Instrumentation/instrument_mgr.cxx

index 0829c1ced2ac07108b7c4d0e0412b2a130b7e675..e07c264017be4babca7d211579b1228eae621f0b 100644 (file)
@@ -13,6 +13,7 @@ libInstrumentation_a_SOURCES = \
         slip_skid_ball.cxx slip_skid_ball.hxx \
         heading_indicator.cxx heading_indicator.hxx \
         vertical_speed_indicator.cxx vertical_speed_indicator.hxx \
-        mag_compass.cxx mag_compass.hxx
+        mag_compass.cxx mag_compass.hxx \
+        clock.cxx clock.hxx
 
 INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/src
diff --git a/src/Instrumentation/clock.cxx b/src/Instrumentation/clock.cxx
new file mode 100644 (file)
index 0000000..994c357
--- /dev/null
@@ -0,0 +1,96 @@
+// clock.cxx - an electric-powered turn indicator.
+// Written by Melchior FRANZ, started 2003.
+//
+// This file is in the Public Domain and comes with no warranty.
+//
+// $Id$
+
+
+#include "clock.hxx"
+#include <simgear/timing/sg_time.hxx>
+#include <Main/fg_props.hxx>
+#include <Main/util.hxx>
+
+
+Clock::Clock ()
+    : _is_serviceable(true),
+      _gmt_time_sec(0),
+      _offset_sec(0),
+      _indicated_sec(0),
+      _standstill_offset(0)
+{
+    _indicated_string[0] = '\0';
+}
+
+Clock::~Clock ()
+{
+}
+
+void
+Clock::init ()
+{
+    _serviceable_node = fgGetNode("/instrumentation/clock/serviceable", true);
+    _offset_node      = fgGetNode("/instrumentation/clock/offset-sec", true);
+    _sec_node         = fgGetNode("/instrumentation/clock/indicated-sec", true);
+    _string_node      = fgGetNode("/instrumentation/clock/indicated-string", true);
+}
+
+void
+Clock::update (double delta_time_sec)
+{
+    if (!_serviceable_node->getBoolValue()) {
+        if (_is_serviceable) {
+            _string_node->setStringValue("");
+            _is_serviceable = false;
+        }
+        return;
+    }
+
+    struct tm *t = globals->get_time_params()->getGmt();
+    int hour = t->tm_hour;
+    int min = t->tm_min;
+    int sec = t->tm_sec;
+
+    long gmt = (hour * 60 + min) * 60 + sec;
+    int offset = _offset_node->getLongValue();
+
+    if (!_is_serviceable) {
+        _standstill_offset -= gmt - _gmt_time_sec;
+    } else if (_gmt_time_sec == gmt && _offset_sec == offset)
+        return;
+
+    _gmt_time_sec = gmt;
+    _offset_sec = offset;
+
+    _indicated_sec = _gmt_time_sec + offset + _standstill_offset;
+    _sec_node->setLongValue(_indicated_sec);
+
+    sec += offset;
+    while (sec < 0) {
+        sec += 60;
+        min--;
+    }
+    while (sec >= 60) {
+        sec -= 60;
+        min++;
+    }
+    while (min < 0) {
+        min += 60;
+        hour--;
+    }
+    while (min >= 60) {
+        min -= 60;
+        hour++;
+    }
+    while (hour < 0)
+        hour += 24;
+    while (hour >= 24)
+        hour -= 24;
+
+    sprintf(_indicated_string, "%02d:%02d:%02d", hour, min, sec);
+    _string_node->setStringValue(_indicated_string);
+    _is_serviceable = true;
+}
+
+
+// end of clock.cxx
diff --git a/src/Instrumentation/clock.hxx b/src/Instrumentation/clock.hxx
new file mode 100644 (file)
index 0000000..25fcd65
--- /dev/null
@@ -0,0 +1,61 @@
+// clock.hxx.
+// Written by Melchior FRANZ, started 2003.
+//
+// This file is in the Public Domain and comes with no warranty.
+//
+// $Id$
+
+
+#ifndef __INSTRUMENTS_CLOCK_HXX
+#define __INSTRUMENTS_CLOCK_HXX 1
+
+#ifndef __cplusplus
+# error This library requires C++
+#endif
+
+#include <simgear/props/props.hxx>
+
+#include <Main/fgfs.hxx>
+
+
+/**
+ * Model a clock.
+ *
+ * Input properties:
+ *
+ * /instrumentation/clock/serviceable
+ * /instrumentation/clock/offset-sec
+ *
+ * Output properties:
+ *
+ * /instrumentation/clock/indicated-sec
+ * /instrumentation/clock/indicated-string
+ */
+class Clock : public FGSubsystem
+{
+
+public:
+
+    Clock ();
+    virtual ~Clock ();
+
+    virtual void init ();
+    virtual void update (double dt);
+
+private:
+
+    bool _is_serviceable;
+    long _gmt_time_sec;
+    long _offset_sec;
+    long _indicated_sec;
+    char _indicated_string[16];
+    long _standstill_offset;
+
+    SGPropertyNode_ptr _serviceable_node;
+    SGPropertyNode_ptr _offset_node;
+    SGPropertyNode_ptr _sec_node;
+    SGPropertyNode_ptr _string_node;
+
+};
+
+#endif // __INSTRUMENTS_CLOCK_HXX
index 6a0ea21244c429ffb01e948960381a2fea05007d..1e77a9ec6312d03c168b68976c321c94848182a9 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "dme.hxx"
 #include "gps.hxx"
+#include "clock.hxx"
 
 
 FGInstrumentMgr::FGInstrumentMgr ()
@@ -32,6 +33,7 @@ FGInstrumentMgr::FGInstrumentMgr ()
     set_subsystem("compass", new MagCompass);
     set_subsystem("dme", new DME, 1.0);
     set_subsystem("gps", new GPS, 0.45);
+    set_subsystem("clock", new Clock, 0.25);
 }
 
 FGInstrumentMgr::~FGInstrumentMgr ()