+ if(count>octets_remaining)
+ octets_remaining = 0;
+ else
+ octets_remaining -= count;
+ }
+}
+
+void ArduControl::S88Task::set_n_octets(unsigned n)
+{
+ n_octets = n;
+}
+
+void ArduControl::S88Task::grow_n_octets(unsigned n)
+{
+ if(n>n_octets)
+ n_octets = n;
+}
+
+
+ArduControl::MfxAnnounceTask::MfxAnnounceTask():
+ Task("MfxAnnounce", 1),
+ serial(0)
+{ }
+
+bool ArduControl::MfxAnnounceTask::get_work(PendingCommand &cmd)
+{
+ cmd.command[0] = MFX_ANNOUNCE;
+ cmd.command[1] = serial>>8;
+ cmd.command[2] = serial;
+ cmd.length = 3;
+
+ sleep(400*Time::msec);
+
+ return true;
+}
+
+void ArduControl::MfxAnnounceTask::set_serial(unsigned s)
+{
+ serial = s;
+}
+
+
+ArduControl::MfxSearchTask::MfxSearchTask(ArduControl &c):
+ Task("MfxSearch", 1),
+ control(c),
+ next_address(1),
+ size(0),
+ bits(0),
+ misses(0),
+ pending_info(0),
+ read_array(0),
+ read_offset(0),
+ read_length(0),
+ block_size(0)
+{ }
+
+bool ArduControl::MfxSearchTask::get_work(PendingCommand &cmd)
+{
+ if(read_length>0)
+ {
+ cmd.command[0] = MFX_READ;
+ cmd.command[1] = pending_info->address>>8;
+ cmd.command[2] = pending_info->address;
+ unsigned index = read_array*0x40+read_offset;
+ cmd.command[3] = index>>8;
+ cmd.command[4] = index;
+ unsigned length = (read_length>=4 ? 4 : read_length>=2 ? 2 : 1);
+ cmd.command[5] = length;
+ cmd.length = 6;
+
+ sleep(100*Time::msec);
+
+ return true;
+ }
+ else if(pending_info)
+ {
+ queue.push(*pending_info);
+ Tag tag;
+ tag.type = Tag::GENERAL;
+ tag.command = NEW_LOCO;
+ tag.id = pending_info->id;
+ control.completed_commands.push(tag);
+
+ if(control.debug>=1)
+ IO::print("Completed processing locomotive %s at address %d\n", pending_info->name, pending_info->address);
+
+ delete pending_info;
+ pending_info = 0;
+ }
+
+ 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", address, bits);
+
+ pending_info = new MfxInfo;
+ pending_info->protocol = "MFX";
+ pending_info->address = address;
+ pending_info->name = format("%08X", bits);
+ pending_info->id = bits;