]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/block.cpp
Strip Id tags and copyright notices from files
[r2c2.git] / source / libr2c2 / block.cpp
index ca404de569046f2a2824eb7641bbbbbc20425c8e..ec313d9d415240dab9114a27be838f27111112e4 100644 (file)
@@ -1,12 +1,7 @@
-/* $Id$
-
-This file is part of R²C²
-Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
 #include <algorithm>
+#include <msp/time/units.h>
 #include "block.h"
+#include "driver.h"
 #include "layout.h"
 #include "route.h"
 #include "trackiter.h"
@@ -22,6 +17,7 @@ Block::Block(Layout &l, Track &start):
        id(0),
        sensor_id(start.get_sensor_id()),
        turnout_id(start.get_turnout_id()),
+       state(INACTIVE),
        train(0)
 {
        tracks.insert(&start);
@@ -59,6 +55,9 @@ Block::Block(Layout &l, Track &start):
                find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path);
        }
 
+       if(sensor_id && layout.has_driver())
+               layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Block::sensor_event));
+
        layout.add_block(*this);
 }
 
@@ -162,13 +161,30 @@ bool Block::reserve(Train *t)
        if(!t || !train)
        {
                train = t;
-               layout.signal_block_reserved.emit(*this, train);
+               signal_reserved.emit(train);
                return true;
        }
        else
                return false;
 }
 
+void Block::tick(const Time::TimeDelta &dt)
+{
+       if(state_confirm_timeout)
+       {
+               state_confirm_timeout -= dt;
+               if(state_confirm_timeout<=Time::zero)
+               {
+                       if(state==MAYBE_INACTIVE)
+                               state = INACTIVE;
+                       else if(state==MAYBE_ACTIVE)
+                               state = ACTIVE;
+                       state_confirm_timeout = Time::zero;
+                       signal_state_changed.emit(state);
+               }
+       }
+}
+
 void Block::find_paths(TrackIter track, unsigned path)
 {
        unsigned mask = track.endpoint().paths;
@@ -211,6 +227,25 @@ void Block::determine_id()
        }
 }
 
+void Block::sensor_event(unsigned addr, bool s)
+{
+       if(addr==sensor_id)
+       {
+               if(s && state<MAYBE_ACTIVE)
+               {
+                       state = MAYBE_ACTIVE;
+                       state_confirm_timeout = 300*Time::msec;
+                       signal_state_changed.emit(state);
+               }
+               else if(!s && state>MAYBE_INACTIVE)
+               {
+                       state = MAYBE_INACTIVE;
+                       state_confirm_timeout = 700*Time::msec;
+                       signal_state_changed.emit(state);
+               }
+       }
+}
+
 
 Block::Endpoint::Endpoint(Track *t, unsigned e):
        track(t),