]> git.tdb.fi Git - r2c2.git/commitdiff
Retain addresses of previously seen locomotives
authorMikko Rasa <tdb@tdb.fi>
Mon, 2 Mar 2015 21:16:20 +0000 (23:16 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 2 Mar 2015 21:16:20 +0000 (23:16 +0200)
It may happen that a locomotive gets re-enumerated, e.g. after visiting
another layout.  Since they are identified by their address, it's useful
to keep the same address.

source/libr2c2/arducontrol.cpp
source/libr2c2/arducontrol.h

index 3d49fabf4784c6b02ac278d67d4086b5f6d0e918..4135df3b54585e26ccd293f7f0101d309d1676a4 100644 (file)
@@ -154,6 +154,14 @@ ArduControl::MfxInfoArray::iterator ArduControl::add_mfx_info(const MfxInfo &inf
        return i;
 }
 
+ArduControl::MfxInfo *ArduControl::find_mfx_info(unsigned id)
+{
+       for(MfxInfoArray::iterator i=mfx_info.begin(); i!=mfx_info.end(); ++i)
+               if(i->id==id)
+                       return &*i;
+       return 0;
+}
+
 void ArduControl::remove_loco(unsigned id)
 {
        Locomotive &loco = get_item(locomotives, id);
@@ -1002,18 +1010,24 @@ bool ArduControl::MfxSearchTask::get_work(PendingCommand &cmd)
 
        if(size>32)
        {
+               unsigned address = 0;
+               if(MfxInfo *existing = control.find_mfx_info(bits))
+                       address = existing->address;
+               else
+                       address = next_address++;
+
                if(control.debug>=1)
-                       IO::print("Assigning MFX address %d to decoder %08X\n", next_address, bits);
+                       IO::print("Assigning MFX address %d to decoder %08X\n", address, bits);
 
                pending_info = new MfxInfo;
                pending_info->protocol = "MFX";
-               pending_info->address = next_address;
+               pending_info->address = address;
                pending_info->name = format("%08X", bits);
                pending_info->id = bits;
 
                cmd.command[0] = MFX_ASSIGN_ADDRESS;
-               cmd.command[1] = next_address>>8;
-               cmd.command[2] = next_address;
+               cmd.command[1] = address>>8;
+               cmd.command[2] = address;
                for(unsigned i=0; i<4; ++i)
                        cmd.command[3+i] = bits>>(24-i*8);
                cmd.length = 7;
@@ -1021,7 +1035,6 @@ bool ArduControl::MfxSearchTask::get_work(PendingCommand &cmd)
                size = 0;
                bits = 0;
                misses = 0;
-               ++next_address;
 
                read_array = 0;
                read_offset = 0;
index b75d415b5fb5a1e73b1b834a1d58989cb6cb426a..78b360018de17c245629059b789ab6be55c9ba4d 100644 (file)
@@ -446,6 +446,7 @@ public:
        virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
 private:
        MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
+       MfxInfo *find_mfx_info(unsigned);
 public:
        virtual void remove_loco(unsigned);
        virtual void set_loco_speed(unsigned, unsigned);