]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/clock.cxx
do also silence the marker sounds on --disable-sound
[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
71 void
72 Clock::update (double delta_time_sec)
73 {
74     if (!_serviceable_node->getBoolValue()) {
75         if (_is_serviceable) {
76             _string_node->setStringValue("");
77             _is_serviceable = false;
78         }
79         return;
80     }
81
82     struct tm *t = globals->get_time_params()->getGmt();
83     int hour = t->tm_hour;
84     int min = t->tm_min;
85     int sec = t->tm_sec;
86
87     long gmt = (hour * 60 + min) * 60 + sec;
88     int offset = _offset_node->getLongValue();
89
90     if (!_is_serviceable) {
91         _standstill_offset -= gmt - _gmt_time_sec;
92     } else if (_gmt_time_sec == gmt && _offset_sec == offset)
93         return;
94
95     _gmt_time_sec = gmt;
96     _offset_sec = offset;
97
98     _indicated_sec = _gmt_time_sec + offset + _standstill_offset;
99     _sec_node->setLongValue(_indicated_sec);
100
101     sec += offset;
102     while (sec < 0) {
103         sec += 60;
104         min--;
105     }
106     while (sec >= 60) {
107         sec -= 60;
108         min++;
109     }
110     while (min < 0) {
111         min += 60;
112         hour--;
113     }
114     while (min >= 60) {
115         min -= 60;
116         hour++;
117     }
118     while (hour < 0)
119         hour += 24;
120     while (hour >= 24)
121         hour -= 24;
122
123     sprintf(_indicated_string, "%02d:%02d:%02d", hour, min, sec);
124     _string_node->setStringValue(_indicated_string);
125     _is_serviceable = true;
126 }
127
128
129 // end of clock.cxx