]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/sensor.cpp
Store routing information in TrainRoutePlanner to avoid threading problems
[r2c2.git] / source / libr2c2 / sensor.cpp
1 #include "driver.h"
2 #include "layout.h"
3 #include "sensor.h"
4
5 using namespace Msp;
6
7 namespace R2C2 {
8
9 Sensor::Sensor(Layout &l):
10         layout(l),
11         address(0),
12         id(0),
13         invert(false),
14         state(INACTIVE)
15 {
16         if(layout.has_driver())
17                 layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Sensor::event));
18
19         layout.add(*this);
20 }
21
22 Sensor::~Sensor()
23 {
24         if(layout.has_driver() && address)
25                 layout.get_driver().remove_sensor(address);
26         layout.remove(*this);
27 }
28
29 void Sensor::set_address(unsigned a)
30 {
31         Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0);
32         if(driver && id)
33                 driver->remove_sensor(id);
34         address = a;
35         if(driver && address)
36                 id = driver->add_sensor(address);
37         else
38                 id = 0;
39 }
40
41 void Sensor::tick(const Time::TimeDelta &dt)
42 {
43         if(state_confirm_timeout)
44         {
45                 state_confirm_timeout -= dt;
46                 if(state_confirm_timeout<=Time::zero)
47                 {
48                         if(state==MAYBE_INACTIVE)
49                                 state = INACTIVE;
50                         else if(state==MAYBE_ACTIVE)
51                                 state = ACTIVE;
52                         state_confirm_timeout = Time::zero;
53                         signal_state_changed.emit(state);
54                 }
55         }
56 }
57
58 void Sensor::event(unsigned i, bool s)
59 {
60         if(i==id)
61         {
62                 if(s!=invert && state<MAYBE_ACTIVE)
63                 {
64                         state = MAYBE_ACTIVE;
65                         state_confirm_timeout = 300*Time::msec;
66                         signal_state_changed.emit(state);
67                 }
68                 else if(s==invert && state>MAYBE_INACTIVE)
69                 {
70                         state = MAYBE_INACTIVE;
71                         state_confirm_timeout = 700*Time::msec;
72                         signal_state_changed.emit(state);
73                 }
74         }
75 }
76
77 } // namespace R2C2