1 #include <msp/strings/utils.h>
2 #include <msp/time/utils.h>
10 Dummy::Dummy(const string ¶ms):
13 vector<string> opts = split(params, ':');
14 for(vector<string>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
16 string::size_type equals = i->find('=');
17 if(equals!=string::npos)
19 string name = i->substr(0, equals);
20 string value = i->substr(equals+1);
21 if(name=="turnout_delay")
22 turnout_delay = lexical_cast<unsigned>(value)*Time::msec;
27 void Dummy::set_power(bool p)
30 signal_power.emit(power);
33 const char *Dummy::enumerate_protocols(unsigned i) const
40 unsigned Dummy::get_protocol_speed_steps(const string &) const
45 unsigned Dummy::add_turnout(unsigned addr, const TrackType &)
51 void Dummy::set_turnout(unsigned addr, unsigned state)
53 TurnoutState &turnout = turnouts[addr];
54 if(turnout.state==state && turnout.pending==state)
56 signal_turnout.emit(addr, state);
60 turnout.pending = state;
62 turnout.timeout = Time::now()+turnout_delay;
65 turnout.state = state;
66 signal_turnout.emit(addr, state);
70 unsigned Dummy::get_turnout(unsigned addr) const
72 map<unsigned, TurnoutState>::const_iterator i = turnouts.find(addr);
74 return i->second.state;
78 void Dummy::set_loco_speed(unsigned addr, unsigned speed)
80 LocoState &loco = locos[addr];
82 signal_loco_speed.emit(addr, speed, loco.reverse);
85 void Dummy::set_loco_reverse(unsigned addr, bool rev)
87 LocoState &loco = locos[addr];
89 signal_loco_speed.emit(addr, loco.speed, rev);
92 void Dummy::set_loco_function(unsigned addr, unsigned func, bool state)
94 signal_loco_function.emit(addr, func, state);
97 void Dummy::set_sensor(unsigned addr, bool state)
99 if(sensors[addr]!=state)
101 sensors[addr] = state;
102 signal_sensor.emit(addr, state);
106 bool Dummy::get_sensor(unsigned addr) const
108 map<unsigned, bool>::const_iterator i = sensors.find(addr);
116 Time::TimeStamp t = Time::now();
117 for(map<unsigned, TurnoutState>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
119 if(i->second.timeout && t>=i->second.timeout)
121 i->second.state = i->second.pending;
122 i->second.timeout = Time::TimeStamp();
123 signal_turnout.emit(i->first, i->second.state);