]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/track.cpp
Add driver for my custom Arduino-based control device
[r2c2.git] / source / libr2c2 / track.cpp
index 8ecb9b9bac00833eaa70190982fcb1933b43e9ae..23f1b9e2b6da280d7179276de2d8e1d1afa1a112 100644 (file)
@@ -40,12 +40,19 @@ Track::Track(Layout &l, const TrackType &t):
        path_changing(false)
 {
        if(type.is_turnout())
+       {
                turnout_id = layout.allocate_turnout_id();
 
+               if(layout.has_driver())
+               {
+                       Driver &driver = layout.get_driver();
+                       driver.add_turnout(turnout_id, type);
+                       driver.signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
+               }
+       }
+
        layout.add(*this);
 
-       if(layout.has_driver())
-               layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
 
        for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
 }
@@ -53,6 +60,8 @@ Track::Track(Layout &l, const TrackType &t):
 Track::~Track()
 {
        break_links();
+       if(layout.has_driver() && turnout_id)
+               layout.get_driver().remove_turnout(turnout_id);
        layout.remove(*this);
 }
 
@@ -155,11 +164,15 @@ void Track::set_turnout_id(unsigned i)
        if(!i)
                throw invalid_argument("Track::set_turnout_id");
 
+       Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0);
+
+       if(driver && turnout_id)
+               driver->remove_turnout(turnout_id);
        turnout_id = i;
        layout.create_blocks(*this);
        layout.update_routes();
-       if(layout.has_driver() && turnout_id)
-               layout.get_driver().add_turnout(turnout_id, type);
+       if(driver && turnout_id)
+               driver->add_turnout(turnout_id, type);
 }
 
 void Track::set_sensor_id(unsigned i)
@@ -183,6 +196,13 @@ void Track::set_active_path(unsigned p)
        layout.get_driver().set_turnout(turnout_id, p);
 }
 
+float Track::get_path_length(int p) const
+{
+       if(p<0)
+               p = active_path;
+       return type.get_path_length(p);
+}
+
 OrientedPoint Track::get_point(unsigned epi, unsigned path, float d) const
 {
        OrientedPoint p = type.get_point(epi, path, d);
@@ -337,9 +357,14 @@ bool Track::break_link(unsigned i)
                return false;
 
        links[i] = 0;
-       other->break_link(*this);
-       // XXX Creates the blocks twice, because the other track calls this too
-       layout.create_blocks(*this);
+       if(!other->break_link(*this))
+       {
+               /* If the call doesn't succeed, it means that the other track already
+               broke the link and is calling us right now.  Recreate blocks in the inner
+               call so it occurs before any signals are emitted. */
+               layout.create_blocks(*this);
+       }
+
        signal_link_changed.emit(i, 0);
 
        return true;
@@ -382,9 +407,6 @@ void Track::save(list<DataFile::Statement> &st) const
 
 void Track::turnout_event(unsigned addr, unsigned state)
 {
-       if(!turnout_id)
-               return;
-
        if(addr==turnout_id)
        {
                active_path = state;