X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Flibr2c2%2Fblock.cpp;h=386f2378a3b20011733cb54a17880cdd64a2d50b;hb=54392d65e2053d1eacb4cfcc435f1013993f2973;hp=ca404de569046f2a2824eb7641bbbbbc20425c8e;hpb=1ff06c5bc46a677fa389ef86c6b26664368f1653;p=r2c2.git diff --git a/source/libr2c2/block.cpp b/source/libr2c2/block.cpp index ca404de..386f237 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); @@ -56,9 +52,12 @@ Block::Block(Layout &l, Track &start): { unsigned path = 1<=endpoints.size()) - throw InvalidParameterValue("Endpoint index out of range"); + throw out_of_range("Block::get_endpoint"); return endpoints[i]; } @@ -104,9 +103,9 @@ 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); + TrackIter t_iter = endpoints[entry].track_iter(); float result = 0; while(t_iter && has_track(*t_iter)) @@ -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,14 +161,31 @@ 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::find_paths(TrackIter track, unsigned path) +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(const TrackIter &track, unsigned path) { unsigned mask = track.endpoint().paths; for(unsigned i=0; mask>>i; ++i) @@ -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), @@ -219,4 +254,9 @@ Block::Endpoint::Endpoint(Track *t, unsigned e): paths(0) { } +TrackIter Block::Endpoint::track_iter() const +{ + return TrackIter(track, track_ep); +} + } // namespace R2C2