From: Mikko Rasa Date: Mon, 2 Mar 2015 21:16:20 +0000 (+0200) Subject: Retain addresses of previously seen locomotives X-Git-Url: http://git.tdb.fi/?p=r2c2.git;a=commitdiff_plain;h=c1f45677ffbe9ded079df99f998d8e2abfe548de Retain addresses of previously seen locomotives 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. --- diff --git a/source/libr2c2/arducontrol.cpp b/source/libr2c2/arducontrol.cpp index 3d49fab..4135df3 100644 --- a/source/libr2c2/arducontrol.cpp +++ b/source/libr2c2/arducontrol.cpp @@ -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; diff --git a/source/libr2c2/arducontrol.h b/source/libr2c2/arducontrol.h index b75d415..78b3600 100644 --- a/source/libr2c2/arducontrol.h +++ b/source/libr2c2/arducontrol.h @@ -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);