]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/sensor.cpp
Code reformatting: add spaces around assignment operators
[r2c2.git] / source / libmarklin / sensor.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2007-2008  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/time/utils.h>
9 #include <msp/time/units.h>
10 #include "control.h"
11 #include "sensor.h"
12
13 using namespace Msp;
14
15 namespace Marklin {
16
17 Sensor::Sensor(Control &c, unsigned a):
18         control(c),
19         addr(a),
20         state(false)
21 {
22         control.add_sensor(*this);
23         control.signal_sensor_event.connect(sigc::mem_fun(this, &Sensor::sensor_event));
24 }
25
26 void Sensor::sensor_event(unsigned a, bool s)
27 {
28         if(a==addr)
29         {
30                 if(s)
31                 {
32                         off_timeout = Time::TimeStamp();
33                         if(s!=state)
34                         {
35                                 state = s;
36                                 signal_state_changed.emit(state);
37                         }
38                 }
39                 else
40                         off_timeout = Time::now()+0.5*Time::sec;
41         }
42 }
43
44 void Sensor::tick()
45 {
46         if(off_timeout)
47         {
48                 const Time::TimeStamp t = Time::now();
49                 if(t>off_timeout)
50                 {
51                         off_timeout = Time::TimeStamp();
52                         if(state)
53                         {
54                                 state = false;
55                                 signal_state_changed.emit(state);
56                         }
57                 }
58         }
59 }
60
61 } // namespace Marklin