X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Fblock.cpp;h=8bab92c1f87f63159ebda4f221cc0e1535de2d39;hb=eab144208f380f638295efac3c5ef234dc39ac54;hp=ca404de569046f2a2824eb7641bbbbbc20425c8e;hpb=1ff06c5bc46a677fa389ef86c6b26664368f1653;p=r2c2.git diff --git a/source/libr2c2/block.cpp b/source/libr2c2/block.cpp index ca404de..8bab92c 100644 --- a/source/libr2c2/block.cpp +++ b/source/libr2c2/block.cpp @@ -1,12 +1,7 @@ -/* $Id$ - -This file is part of R²C² -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - #include +#include #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); } @@ -87,7 +86,7 @@ bool Block::has_track(Track &t) const const Block::Endpoint &Block::get_endpoint(unsigned i) const { if(i>=endpoints.size()) - throw InvalidParameterValue("Endpoint index out of range"); + throw out_of_range("Block::get_endpoint"); return endpoints[i]; } @@ -104,7 +103,7 @@ int Block::get_endpoint_by_link(Block &other) const float Block::get_path_length(unsigned entry, const Route *route) const { if(entry>=endpoints.size()) - throw InvalidParameterValue("Endpoint index out of range"); + throw out_of_range("Block::get_path_length"); TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep); @@ -153,7 +152,7 @@ void Block::break_link(Block &other) Block *Block::get_link(unsigned epi) const { if(epi>=endpoints.size()) - throw InvalidParameterValue("Endpoint index out of range"); + throw out_of_range("Block::get_link"); return endpoints[epi].link; } @@ -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 && stateMAYBE_INACTIVE) + { + state = MAYBE_INACTIVE; + state_confirm_timeout = 700*Time::msec; + signal_state_changed.emit(state); + } + } +} + Block::Endpoint::Endpoint(Track *t, unsigned e): track(t),