]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/clock.cxx
Begin a rewrite of the magnetic compass code. So far, only northerly
[flightgear.git] / src / Instrumentation / clock.cxx
1 // clock.cxx - an electric-powered turn indicator.
2 // Written by Melchior FRANZ, started 2003.
3 //
4 // This file is in the Public Domain and comes with no warranty.
5 //
6 // $Id$
7
8
9 #include "clock.hxx"
10 #include <simgear/timing/sg_time.hxx>
11 #include <Main/fg_props.hxx>
12 #include <Main/util.hxx>
13
14
15 Clock::Clock ( SGPropertyNode *node )
16     : _is_serviceable(true),
17       _gmt_time_sec(0),
18       _offset_sec(0),
19       _indicated_sec(0),
20       _standstill_offset(0),
21       name("clock"),
22       num(0)
23 {
24     _indicated_string[0] = '\0';
25
26     int i;
27     for ( i = 0; i < node->nChildren(); ++i ) {
28         SGPropertyNode *child = node->getChild(i);
29         string cname = child->getName();
30         string cval = child->getStringValue();
31         if ( cname == "name" ) {
32             name = cval;
33         } else if ( cname == "number" ) {
34             num = child->getIntValue();
35         } else {
36             SG_LOG( SG_INSTR, SG_WARN, "Error in clock config logic" );
37             if ( name.length() ) {
38                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
39             }
40         }
41     }
42 }
43
44 Clock::Clock ()
45     : _is_serviceable(true),
46       _gmt_time_sec(0),
47       _offset_sec(0),
48       _indicated_sec(0),
49       _standstill_offset(0)
50 {
51     _indicated_string[0] = '\0';
52 }
53
54 Clock::~Clock ()
55 {
56 }
57
58 void
59 Clock::init ()
60 {
61     string branch;
62     branch = "/instrumentation/" + name;
63
64     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
65     _serviceable_node = node->getChild("serviceable", 0, true);
66     _offset_node = node->getChild("offset-sec", 0, true);
67     _sec_node = node->getChild("indicated-sec", 0, true);
68     _string_node = node->getChild("indicated-string", 0, true);
69
70     _serviceable_node->setBoolValue(true);
71 }
72
73 void
74 Clock::update (double delta_time_sec)
75 {
76     if (!_serviceable_node->getBoolValue()) {
77         if (_is_serviceable) {
78             _string_node->setStringValue("");
79             _is_serviceable = false;
80         }
81         return;
82     }
83
84     struct tm *t = globals->get_time_params()->getGmt();
85     int hour = t->tm_hour;
86     int min = t->tm_min;
87     int sec = t->tm_sec;
88
89     long gmt = (hour * 60 + min) * 60 + sec;
90     int offset = _offset_node->getLongValue();
91
92     if (!_is_serviceable) {
93         _standstill_offset -= gmt - _gmt_time_sec;
94     } else if (_gmt_time_sec == gmt && _offset_sec == offset)
95         return;
96
97     _gmt_time_sec = gmt;
98     _offset_sec = offset;
99
100     _indicated_sec = _gmt_time_sec + offset + _standstill_offset;
101     _sec_node->setLongValue(_indicated_sec);
102
103     sec += offset;
104     while (sec < 0) {
105         sec += 60;
106         min--;
107     }
108     while (sec >= 60) {
109         sec -= 60;
110         min++;
111     }
112     while (min < 0) {
113         min += 60;
114         hour--;
115     }
116     while (min >= 60) {
117         min -= 60;
118         hour++;
119     }
120     while (hour < 0)
121         hour += 24;
122     while (hour >= 24)
123         hour -= 24;
124
125     sprintf(_indicated_string, "%02d:%02d:%02d", hour, min, sec);
126     _string_node->setStringValue(_indicated_string);
127     _is_serviceable = true;
128 }
129
130
131 // end of clock.cxx