- if(!repeat_count)
- {
- tag = Tag();
- length = 0;
- repeat_count = 1;
- QueuedCommand qcmd;
- if(control.pop_command(qcmd))
- {
- length = qcmd.length;
- copy(qcmd.command, qcmd.command+length, command);
- if(qcmd.tag.type==Tag::LOCOMOTIVE)
- repeat_count = 8;
- tag = qcmd.tag;
- }
- else if(loco && phase==0)
- {
- length = loco->create_speed_func_command(control.refresh_counter%4+1, command);
- repeat_count = 2;
- ++phase;
- }
- else if(t>=next_mfx_announce)
- {
- command[0] = MFX_ANNOUNCE;
- command[1] = control.mfx_announce_serial>>8;
- command[2] = control.mfx_announce_serial;
- length = 3;
- next_mfx_announce = t+400*Time::msec;
- }
- else if(t>=next_mfx_search)
- {
- command[0] = MFX_SEARCH;
- for(unsigned i=0; i<4; ++i)
- command[1+i] = mfx_search_bits>>(24-i*8);
- command[5] = mfx_search_size;
- length = 6;
- next_mfx_search = t+200*Time::msec;
- if(control.debug>=1)
- IO::print("Search %08X/%d\n", mfx_search_bits, mfx_search_size);
- }
- else if(!s88_octets_remaining && control.n_s88_octets)
- {
- command[0] = S88_READ;
- command[1] = control.n_s88_octets;
- length = 2;
- s88_octets_remaining = control.n_s88_octets;
- }
- else if((loco = control.get_loco_to_refresh()))
- {
- if(loco->proto==MM)
- {
- length = loco->create_speed_dir_command(command);
- repeat_count = 2;
- phase = 0;
- }
- else if(loco->proto==MFX)
- {
- length = loco->create_speed_func_command(0, command);
- phase = 1;
- }
- }
- else
- {
- // Send an idle packet for the MM protocol
- command[0] = MOTOROLA_SPEED;
- command[1] = 80;
- command[2] = 0;
- command[3] = 0;
- length = 4;
- }
- }