X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Fsensor.cpp;h=28f95482ab0b746652ed37bc0b44b31020cde22e;hb=2fe7cbcb761487bc7409b93b372da6f8ab3c581e;hp=f6913ba1b05a3173069a4cc63b5ba7f94537d097;hpb=6c61179fe09af2f5366d50f10aadbf5f83438087;p=r2c2.git diff --git a/source/libmarklin/sensor.cpp b/source/libmarklin/sensor.cpp index f6913ba..28f9548 100644 --- a/source/libmarklin/sensor.cpp +++ b/source/libmarklin/sensor.cpp @@ -1,6 +1,17 @@ +/* $Id$ + +This file is part of the MSP Märklin suite +Copyright © 2007-2008 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#include +#include #include "control.h" #include "sensor.h" +using namespace Msp; + namespace Marklin { Sensor::Sensor(Control &c, unsigned a): @@ -8,16 +19,42 @@ Sensor::Sensor(Control &c, unsigned a): addr(a), state(false) { - control.add_sensor(this); + control.add_sensor(*this); control.signal_sensor_event.connect(sigc::mem_fun(this, &Sensor::sensor_event)); } void Sensor::sensor_event(unsigned a, bool s) { - if(a==addr && s!=state) + if(a==addr) + { + if(s) + { + off_timeout = Time::TimeStamp(); + if(s!=state) + { + state = s; + signal_state_changed.emit(state); + } + } + else + off_timeout = Time::now()+0.5*Time::sec; + } +} + +void Sensor::tick() +{ + if(off_timeout) { - state=s; - signal_state_changed.emit(state); + const Time::TimeStamp t = Time::now(); + if(t>off_timeout) + { + off_timeout = Time::TimeStamp(); + if(state) + { + state = false; + signal_state_changed.emit(state); + } + } } }