serial(dev),
debug(1),
power(false),
- next_refresh(refresh_cycle.end()),
- refresh_counter(0),
active_accessory(0),
- n_s88_octets(0),
- mfx_announce_serial(0),
- next_mfx_address(1),
+ s88(*this),
+ mfx_search(*this),
thread(*this)
{
PendingCommand cmd;
void ArduControl::remove_loco(unsigned id)
{
Locomotive &loco = get_item(locomotives, id);
- remove_loco_from_refresh(loco);
+ refresh.remove_loco(loco);
locomotives.erase(id);
}
PendingCommand cmd(loco, Locomotive::SPEED);
push_command(cmd);
- add_loco_to_refresh(loco);
+ refresh.add_loco(loco);
}
}
PendingCommand cmd(loco, Locomotive::REVERSE);
push_command(cmd);
- add_loco_to_refresh(loco);
+ refresh.add_loco(loco);
}
}
push_command(cmd);
}
- add_loco_to_refresh(loco);
- }
-}
-
-void ArduControl::add_loco_to_refresh(Locomotive &loco)
-{
- MutexLock lock(mutex);
- refresh_cycle.push_back(&loco);
- if(refresh_cycle.size()>15)
- {
- LocomotivePtrList::iterator oldest = refresh_cycle.begin();
- for(LocomotivePtrList::iterator i=refresh_cycle.begin(); ++i!=refresh_cycle.end(); )
- if((*i)->last_change_age>(*oldest)->last_change_age)
- oldest = i;
- if(oldest==next_refresh)
- advance_next_refresh();
- refresh_cycle.erase(oldest);
- }
- if(next_refresh==refresh_cycle.end())
- next_refresh = refresh_cycle.begin();
-}
-
-void ArduControl::remove_loco_from_refresh(Locomotive &loco)
-{
- MutexLock lock(mutex);
- for(LocomotivePtrList::iterator i=refresh_cycle.begin(); i!=refresh_cycle.end(); ++i)
- if(*i==&loco)
- {
- if(i==next_refresh)
- {
- if(refresh_cycle.size()>1)
- advance_next_refresh();
- else
- next_refresh = refresh_cycle.end();
- }
- refresh_cycle.erase(i);
- return;
- }
-}
-
-ArduControl::Locomotive *ArduControl::get_loco_to_refresh()
-{
- MutexLock lock(mutex);
- if(refresh_cycle.empty())
- return 0;
-
- Locomotive *loco = *next_refresh;
- advance_next_refresh();
- return loco;
-}
-
-void ArduControl::advance_next_refresh()
-{
- ++next_refresh;
- if(next_refresh==refresh_cycle.end())
- {
- next_refresh = refresh_cycle.begin();
- ++refresh_counter;
+ refresh.add_loco(loco);
}
}
throw invalid_argument("ArduControl::add_sensor");
insert_unique(sensors, addr, Sensor(addr));
- unsigned octet_index = (addr-1)/8;
- if(octet_index>=n_s88_octets)
- n_s88_octets = octet_index+1;
+ s88.grow_n_octets((addr+7)/8);
return addr;
}
void ArduControl::remove_sensor(unsigned addr)
{
remove_existing(sensors, addr);
- // TODO update n_s88_octets
+ // TODO update s88.n_octets
}
bool ArduControl::get_sensor(unsigned addr) const
}
-ArduControl::RefreshTask::RefreshTask(ArduControl &c):
- control(c),
+ArduControl::RefreshTask::RefreshTask():
+ next(cycle.end()),
+ round(0),
loco(0),
phase(0)
{ }
{
if(loco && loco->proto==MM && phase==0)
{
- cmd.length = loco->create_speed_func_command(control.refresh_counter%4+1, cmd.command);
+ cmd.length = loco->create_speed_func_command(round%4+1, cmd.command);
cmd.repeat_count = 2;
++phase;
return true;
}
- loco = control.get_loco_to_refresh();
+ loco = get_next_loco();
if(!loco)
return false;
return true;
}
+void ArduControl::RefreshTask::add_loco(Locomotive &l)
+{
+ MutexLock lock(mutex);
+ cycle.push_back(&l);
+ if(cycle.size()>15)
+ {
+ LocomotivePtrList::iterator oldest = cycle.begin();
+ for(LocomotivePtrList::iterator i=cycle.begin(); ++i!=cycle.end(); )
+ if((*i)->last_change_age>(*oldest)->last_change_age)
+ oldest = i;
+ if(oldest==next)
+ advance();
+ cycle.erase(oldest);
+ }
+ if(next==cycle.end())
+ next = cycle.begin();
+}
+
+void ArduControl::RefreshTask::remove_loco(Locomotive &l)
+{
+ MutexLock lock(mutex);
+ for(LocomotivePtrList::iterator i=cycle.begin(); i!=cycle.end(); ++i)
+ if(*i==&l)
+ {
+ if(i==next)
+ {
+ if(cycle.size()>1)
+ advance();
+ else
+ next = cycle.end();
+ }
+ cycle.erase(i);
+ return;
+ }
+}
+
+ArduControl::Locomotive *ArduControl::RefreshTask::get_next_loco()
+{
+ MutexLock lock(mutex);
+ if(cycle.empty())
+ return 0;
+
+ Locomotive *l = *next;
+ advance();
+ return l;
+}
+
+void ArduControl::RefreshTask::advance()
+{
+ ++next;
+ if(next==cycle.end())
+ {
+ next= cycle.begin();
+ ++round;
+ }
+}
+
ArduControl::S88Task::S88Task(ArduControl &c):
control(c),
+ n_octets(0),
octets_remaining(0)
{ }
bool ArduControl::S88Task::get_work(PendingCommand &cmd)
{
- if(octets_remaining || !control.n_s88_octets)
+ if(octets_remaining || !n_octets)
return false;
+ octets_remaining = n_octets;
cmd.command[0] = S88_READ;
- cmd.command[1] = control.n_s88_octets;
+ cmd.command[1] = octets_remaining;
cmd.length = 2;
- octets_remaining = control.n_s88_octets;
return true;
}
}
}
+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(ArduControl &c):
- control(c)
+
+ArduControl::MfxAnnounceTask::MfxAnnounceTask():
+ serial(0)
{ }
bool ArduControl::MfxAnnounceTask::get_work(PendingCommand &cmd)
return false;
cmd.command[0] = MFX_ANNOUNCE;
- cmd.command[1] = control.mfx_announce_serial>>8;
- cmd.command[2] = control.mfx_announce_serial;
+ cmd.command[1] = serial>>8;
+ cmd.command[2] = serial;
cmd.length = 3;
next = t+400*Time::msec;
return true;
}
+void ArduControl::MfxAnnounceTask::set_serial(unsigned s)
+{
+ serial = s;
+}
+
ArduControl::MfxSearchTask::MfxSearchTask(ArduControl &c):
control(c),
+ next_address(1),
size(0),
bits(0),
pending(false)
if(size==32)
{
if(control.debug>=1)
- IO::print("Assigning MFX address %d to decoder %08X\n", control.next_mfx_address, bits);
+ IO::print("Assigning MFX address %d to decoder %08X\n", next_address, bits);
cmd.command[0] = MFX_ASSIGN_ADDRESS;
- cmd.command[1] = control.next_mfx_address>>8;
- cmd.command[2] = control.next_mfx_address;
+ cmd.command[1] = next_address>>8;
+ cmd.command[2] = next_address;
for(unsigned i=0; i<4; ++i)
cmd.command[3+i] = bits>>(24-i*8);
cmd.length = 7;
size = 0;
bits = 0;
- ++control.next_mfx_address;
+ ++next_address;
return true;
}
control(c),
done(false)
{
- tasks.push_back(new MfxAnnounceTask(control));
- tasks.push_back(new MfxSearchTask(control));
- tasks.push_back(new S88Task(control));
- tasks.push_back(new RefreshTask(control));
+ tasks.push_back(&control.mfx_announce);
+ tasks.push_back(&control.mfx_search);
+ tasks.push_back(&control.s88);
+ tasks.push_back(&control.refresh);
launch();
}
class RefreshTask: public Task
{
private:
- ArduControl &control;
+ typedef std::list<Locomotive *> LocomotivePtrList;
+
+ LocomotivePtrList cycle;
+ LocomotivePtrList::iterator next;
+ unsigned round;
Locomotive *loco;
unsigned phase;
+ Msp::Mutex mutex;
public:
- RefreshTask(ArduControl &);
+ RefreshTask();
virtual bool get_work(PendingCommand &);
+
+ void add_loco(Locomotive &);
+ void remove_loco(Locomotive &);
+ private:
+ Locomotive *get_next_loco();
+ void advance();
};
class S88Task: public Task
{
private:
ArduControl &control;
+ unsigned n_octets;
unsigned octets_remaining;
public:
virtual bool get_work(PendingCommand &);
virtual void process_reply(const char *, unsigned);
+
+ void set_n_octets(unsigned);
+ void grow_n_octets(unsigned);
};
class MfxAnnounceTask: public Task
{
private:
- ArduControl &control;
+ unsigned serial;
Msp::Time::TimeStamp next;
public:
- MfxAnnounceTask(ArduControl &);
+ MfxAnnounceTask();
virtual bool get_work(PendingCommand &);
+
+ void set_serial(unsigned);
};
class MfxSearchTask: public Task
{
private:
ArduControl &control;
+ unsigned next_address;
Msp::Time::TimeStamp next;
unsigned size;
unsigned bits;
};
typedef std::map<unsigned, Locomotive> LocomotiveMap;
- typedef std::list<Locomotive *> LocomotivePtrList;
typedef std::map<unsigned, Accessory> AccessoryMap;
typedef std::list<Accessory *> AccessoryPtrList;
typedef std::map<unsigned, Sensor> SensorMap;
ControlledVariable<bool> power;
LocomotiveMap locomotives;
- LocomotivePtrList refresh_cycle;
- LocomotivePtrList::iterator next_refresh;
- unsigned refresh_counter;
AccessoryMap accessories;
AccessoryPtrList accessory_queue;
Accessory *active_accessory;
std::list<Tag> completed_commands;
SensorMap sensors;
- unsigned n_s88_octets;
-
- unsigned mfx_announce_serial;
- unsigned next_mfx_address;
Msp::Mutex mutex;
+ RefreshTask refresh;
+ S88Task s88;
+ MfxAnnounceTask mfx_announce;
+ MfxSearchTask mfx_search;
ControlThread thread;
static ProtocolInfo protocol_info[2];
virtual void set_loco_speed(unsigned, unsigned);
virtual void set_loco_reverse(unsigned, bool);
virtual void set_loco_function(unsigned, unsigned, bool);
-private:
- void add_loco_to_refresh(Locomotive &);
- void remove_loco_from_refresh(Locomotive &);
- Locomotive *get_loco_to_refresh();
- void advance_next_refresh();
-public:
virtual unsigned add_turnout(unsigned, const TrackType &);
virtual void remove_turnout(unsigned);
virtual void set_turnout(unsigned, unsigned);