]> git.tdb.fi Git - r2c2.git/commitdiff
Save turnout states and restore them on startup
authorMikko Rasa <tdb@tdb.fi>
Tue, 23 Nov 2010 21:54:33 +0000 (21:54 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 23 Nov 2010 21:54:33 +0000 (21:54 +0000)
Don't send turnout commands before establishing sync with the device

source/engineer/engineer.cpp
source/libr2c2/intellibox.cpp
source/libr2c2/intellibox.h
source/libr2c2/layout.cpp
source/libr2c2/layout.h

index 1f3b7dc4accc617e56eb8c587f4064c0d9e091d2..8db0be6c2465433f23cef8ea3610fb3fb9da0aaf 100644 (file)
@@ -131,7 +131,7 @@ Engineer::~Engineer()
        layout.get_driver().flush();
 
        if(!options.simulate)
-               layout.save_trains(options.state_fn);
+               layout.save_dynamic(options.state_fn);
 
        delete overlay;
        delete root;
index 7702ed6b1bdde49b9309c2da9ef72f54fd3e2ae6..84d576ba99ac8821ae797b62d13380adffb38dbb 100644 (file)
@@ -242,7 +242,7 @@ void Intellibox::set_turnout(unsigned addr, unsigned state)
 {
        Turnout &turnout = turnouts[addr];
        unsigned mask = (1<<turnout.bits)-1;
-       if(((state^turnout.state)&mask)==0 || ((state^turnout.pending)&mask)==0)
+       if(((state^turnout.state)&mask)==0 || ((state^turnout.pending)&mask)==0 || !turnout.synced)
        {
                turnout.state = state;
                turnout.pending = state;
@@ -601,6 +601,8 @@ void Intellibox::process_reply(const Time::TimeStamp &t)
                                turnout.pending = turnout.state;
                                signal_turnout.emit(addr, turnout.state);
                        }
+
+                       turnout.synced = true;
                }
                else
                        error(cmd, err);
@@ -748,7 +750,8 @@ Intellibox::Turnout::Turnout():
        bits(1),
        state(0),
        active(false),
-       pending(false)
+       synced(false),
+       pending(0)
 { }
 
 
index a4eed30d4bcf0cda32e28c80ee2748c786d7872a..e5f63de6c728dca7597d78887864aaffba68aa89 100644 (file)
@@ -96,6 +96,7 @@ private:
                unsigned bits;
                unsigned state;
                bool active;
+               bool synced;
                unsigned pending;
                Msp::Time::TimeStamp off_timeout;
 
index cbe226a684c31ffcf6229fd5d1ead978530ae269..d6bd9ee909dee6f11c7016559ccdd24acbf7386a 100644 (file)
@@ -312,11 +312,15 @@ void Layout::save(const string &fn) const
        }
 }
 
-void Layout::save_trains(const string &fn) const
+void Layout::save_dynamic(const string &fn) const
 {
        IO::BufferedFile out(fn, IO::M_WRITE);
        DataFile::Writer writer(out);
 
+       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               if(unsigned tid = (*i)->get_turnout_id())
+                       writer.write((DataFile::Statement("turnout"), tid, (*i)->get_active_path()));
+
        for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
        {
                DataFile::Statement st("train");
@@ -351,6 +355,7 @@ Layout::Loader::Loader(Layout &l):
        add("route", static_cast<void (Loader::*)()>(&Loader::route));
        add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
        add("train", static_cast<void (Loader::*)(ArticleNumber, unsigned, const std::string &)>(&Loader::train));
+       add("turnout", &Loader::turnout);
        add("zone",  &Loader::zone);
 
        // Deprecated aliases
@@ -404,6 +409,12 @@ void Layout::Loader::train(ArticleNumber art_nr, unsigned addr, const std::strin
        load_sub(*trn);
 }
 
+void Layout::Loader::turnout(unsigned addr, unsigned path)
+{
+       if(obj.driver)
+               obj.driver->set_turnout(addr, path);
+}
+
 void Layout::Loader::zone()
 {
        Zone *zne = new Zone(obj);
index 6ef7e0900b517e4015e58874de66be306d193b26..8c143ac3d79b0a6508ce8fbba5ed50cda37eb4bd 100644 (file)
@@ -43,6 +43,7 @@ public:
                void track(ArticleNumber);
                void train(unsigned, unsigned, const std::string &);
                void train(ArticleNumber, unsigned, const std::string &);
+               void turnout(unsigned, unsigned);
                void zone();
        };
 
@@ -120,7 +121,7 @@ public:
        void emergency(const std::string &);
 
        void save(const std::string &) const;
-       void save_trains(const std::string &) const;
+       void save_dynamic(const std::string &) const;
 private:
        void sensor_event(unsigned, bool);
 };