+#include <msp/strings/utils.h>
+#include <msp/time/utils.h>
#include "dummy.h"
using namespace std;
+using namespace Msp;
namespace R2C2 {
-Dummy::Dummy():
+Dummy::Dummy(const string ¶ms):
power(true)
-{ }
+{
+ vector<string> opts = split(params, ':');
+ for(vector<string>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+ {
+ string::size_type equals = i->find('=');
+ if(equals!=string::npos)
+ {
+ string name = i->substr(0, equals);
+ string value = i->substr(equals+1);
+ if(name=="turnout_delay")
+ turnout_delay = lexical_cast<unsigned>(value)*Time::msec;
+ }
+ }
+}
void Dummy::set_power(bool p)
{
void Dummy::set_turnout(unsigned addr, unsigned state)
{
- if(turnouts[addr]!=state)
+ TurnoutState &turnout = turnouts[addr];
+ if(turnout.state==state && turnout.pending==state)
+ {
+ signal_turnout.emit(addr, state);
+ return;
+ }
+
+ turnout.pending = state;
+ if(turnout_delay)
+ turnout.timeout = Time::now()+turnout_delay;
+ else
{
- turnouts[addr] = state;
+ turnout.state = state;
signal_turnout.emit(addr, state);
}
}
unsigned Dummy::get_turnout(unsigned addr) const
{
- map<unsigned, unsigned>::const_iterator i = turnouts.find(addr);
+ map<unsigned, TurnoutState>::const_iterator i = turnouts.find(addr);
if(i!=turnouts.end())
- return i->second;
- return false;
+ return i->second.state;
+ return 0;
}
void Dummy::set_loco_speed(unsigned addr, unsigned speed)
return false;
}
+void Dummy::tick()
+{
+ Time::TimeStamp t = Time::now();
+ for(map<unsigned, TurnoutState>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
+ {
+ if(i->second.timeout && t>=i->second.timeout)
+ {
+ i->second.state = i->second.pending;
+ i->second.timeout = Time::TimeStamp();
+ signal_turnout.emit(i->first, i->second.state);
+ }
+ }
+}
+
} // namespace R2C2
#define LIBR2C2_DUMMY_H_
#include <map>
+#include <msp/time/timedelta.h>
+#include <msp/time/timestamp.h>
#include "driver.h"
namespace R2C2 {
class Dummy: public Driver
{
private:
+ struct TurnoutState
+ {
+ unsigned state;
+ unsigned pending;
+ Msp::Time::TimeStamp timeout;
+ };
+
struct LocoState
{
unsigned speed;
};
bool power;
- std::map<unsigned, unsigned> turnouts;
+ std::map<unsigned, TurnoutState> turnouts;
std::map<unsigned, LocoState> locos;
std::map<unsigned, bool> sensors;
+ Msp::Time::TimeDelta turnout_delay;
public:
- Dummy();
+ Dummy(const std::string &);
virtual void set_power(bool);
virtual bool get_power() const { return power; }
virtual void set_sensor(unsigned, bool);
virtual bool get_sensor(unsigned) const;
- virtual void tick() { }
+ virtual void tick();
virtual void flush() { }
};