delete next_ctrl;
}
+const char *AIControl::enumerate_controls(unsigned index) const
+{
+ if(index==0)
+ return target_speed.name.c_str();
+ else
+ {
+ for(--index;; ++index)
+ {
+ const char *ret = next_ctrl->enumerate_controls(index-1);
+ if(!ret || ret!=target_speed.name)
+ return ret;
+ }
+ }
+}
+
void AIControl::set_control(const string &n, float v)
{
- if(n=="speed")
+ if(n==target_speed.name)
{
if(v && !train.is_active())
train.set_active(true);
const Controller::Control &AIControl::get_control(const string &n) const
{
- if(n=="speed")
+ if(n==target_speed.name)
return target_speed;
else
return next_ctrl->get_control(n);
AIControl(Train &, Controller *);
virtual ~AIControl();
+ virtual const char *enumerate_controls(unsigned) const;
virtual void set_control(const std::string &, float);
virtual const Control &get_control(const std::string &) const;
public:
virtual ~Controller() { }
+ virtual const char *enumerate_controls(unsigned) const = 0;
virtual void set_control(const std::string &, float) = 0;
virtual const Control &get_control(const std::string &) const = 0;
target_speed.set(0);
}
+const char *SimpleController::enumerate_controls(unsigned index) const
+{
+ if(index==0)
+ return target_speed.name.c_str();
+ else if(index==1)
+ return reverse.name.c_str();
+ else
+ return 0;
+}
+
void SimpleController::set_control(const string &name, float v)
{
- if(name=="speed")
+ if(name==target_speed.name)
{
target_speed.set(v);
signal_control_changed.emit(target_speed);
}
- else if(name=="reverse")
+ else if(name==reverse.name)
{
if(target_speed.value || speed)
throw InvalidState("Must be stopped to change reverse");
const Controller::Control &SimpleController::get_control(const string &name) const
{
- if(name=="speed")
+ if(name==target_speed.name)
return target_speed;
- else if(name=="reverse")
+ else if(name==reverse.name)
return reverse;
else
throw KeyError("Unknown control", name);
public:
SimpleController();
+ virtual const char *enumerate_controls(unsigned) const;
virtual void set_control(const std::string &, float);
virtual const Control &get_control(const std::string &) const;
pkt.name = train.get_name();
comm.send(pkt);
}
- // XXX Need control enumeration to send control packets
+ for(unsigned j=0;; ++j)
+ {
+ const char *name = train.get_controller().enumerate_controls(j);
+ if(!name)
+ break;
+
+ TrainControlPacket pkt;
+ pkt.address = train.get_address();
+ pkt.control = name;
+ pkt.value = train.get_control(name);
+ comm.send(pkt);
+ }
{
TrainFunctionPacket pkt;
pkt.address = train.get_address();