]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/clock.cxx
reduce noise
[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       _indicated_min(0),
21       _indicated_hour(0),
22       _standstill_offset(0),
23       name("clock"),
24       num(0)
25 {
26     _indicated_string[0] = '\0';
27
28     int i;
29     for ( i = 0; i < node->nChildren(); ++i ) {
30         SGPropertyNode *child = node->getChild(i);
31         string cname = child->getName();
32         string cval = child->getStringValue();
33         if ( cname == "name" ) {
34             name = cval;
35         } else if ( cname == "number" ) {
36             num = child->getIntValue();
37         } else {
38             SG_LOG( SG_INSTR, SG_WARN, "Error in clock config logic" );
39             if ( name.length() ) {
40                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
41             }
42         }
43     }
44 }
45
46 Clock::Clock ()
47     : _is_serviceable(true),
48       _gmt_time_sec(0),
49       _offset_sec(0),
50       _indicated_sec(0),
51       _standstill_offset(0)
52 {
53     _indicated_string[0] = '\0';
54     _indicated_short_string[0] = '\0';
55 }
56
57 Clock::~Clock ()
58 {
59 }
60
61 void
62 Clock::init ()
63 {
64     string branch;
65     branch = "/instrumentation/" + name;
66
67     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
68     _serviceable_node = node->getChild("serviceable", 0, true);
69     _offset_node = node->getChild("offset-sec", 0, true);
70     _sec_node = node->getChild("indicated-sec", 0, true);
71     _min_node = node->getChild("indicated-min", 0, true);
72     _hour_node = node->getChild("indicated-hour", 0, true);
73     _string_node = node->getChild("indicated-string", 0, true);
74     _string_node1 = node->getChild("indicated-short-string", 0, true);
75 }
76
77 void
78 Clock::update (double delta_time_sec)
79 {
80     if (!_serviceable_node->getBoolValue()) {
81         if (_is_serviceable) {
82             _string_node->setStringValue("");
83             _is_serviceable = false;
84         }
85         return;
86     }
87
88     struct tm *t = globals->get_time_params()->getGmt();
89     int hour = t->tm_hour;
90     int min = t->tm_min;
91     int sec = t->tm_sec;
92
93     long gmt = (hour * 60 + min) * 60 + sec;
94     int offset = _offset_node->getLongValue();
95
96     if (!_is_serviceable) {
97         _standstill_offset -= gmt - _gmt_time_sec;
98     } else if (_gmt_time_sec == gmt && _offset_sec == offset)
99         return;
100
101     _gmt_time_sec = gmt;
102     _offset_sec = offset;
103
104     _indicated_sec = _gmt_time_sec + offset + _standstill_offset;
105     _sec_node->setLongValue(_indicated_sec);
106
107     sec += offset;
108     while (sec < 0) {
109         sec += 60;
110         min--;
111     }
112     while (sec >= 60) {
113         sec -= 60;
114         min++;
115     }
116     while (min < 0) {
117         min += 60;
118         hour--;
119     }
120     while (min >= 60) {
121         min -= 60;
122         hour++;
123     }
124     while (hour < 0)
125         hour += 24;
126     while (hour >= 24)
127         hour -= 24;
128
129     sprintf(_indicated_string, "%02d:%02d:%02d", hour, min, sec);
130     _string_node->setStringValue(_indicated_string);
131     sprintf(_indicated_short_string, "%02d:%02d", hour, min);
132     _string_node1->setStringValue(_indicated_short_string);
133     _is_serviceable = true;
134
135    _indicated_min = min;
136     _min_node->setLongValue(_indicated_min);
137    _indicated_hour = hour;
138     _hour_node->setLongValue(_indicated_hour);
139 }
140
141
142 // end of clock.cxx