4 #include <msp/io/print.h>
5 #include <msp/time/units.h>
6 #include <msp/time/utils.h>
7 #include "intellibox.h"
9 #include "vehicletype.h"
16 Intellibox::Intellibox(const string &dev):
20 update_sensors(false),
23 static unsigned baud[]= { 2400, 4800, 9600, 19200, 0 };
25 serial.set_stop_bits(2);
29 for(unsigned i=0; baud[i]; ++i)
31 serial.set_baud_rate(baud[i]);
34 if(IO::poll(serial, IO::P_INPUT, 500*Time::msec))
36 IO::print("IB detected at %d bits/s\n", baud[i]);
38 p50 = (serial.read(buf, 2)==2);
45 throw runtime_error("IB not detected");
48 serial.write("xZzA1\r", 6);
53 void Intellibox::set_power(bool p)
57 command(CMD_POWER_ON);
59 command(CMD_POWER_OFF);
60 signal_power.emit(power);
63 void Intellibox::halt(bool h)
68 for(map<unsigned, Locomotive>::iterator i=locos.begin(); i!=locos.end(); ++i)
70 set_loco_speed(i->first, 0);
73 signal_halt.emit(halted);
76 const char *Intellibox::enumerate_protocols(unsigned i) const
86 unsigned Intellibox::get_protocol_speed_steps(const string &proto_name) const
88 Protocol proto = map_protocol(proto_name);
96 void Intellibox::add_loco(unsigned addr, const string &proto_name, const VehicleType &type)
98 Protocol proto = map_protocol(proto_name);
100 if(!locos.count(addr))
102 Locomotive &loco = locos[addr];
103 loco.protocol = proto;
104 if(type.get_max_function()>4)
106 loco.ext_func = true;
107 locos[addr+1].protocol = NONE;
110 unsigned char data[2];
112 data[1] = (addr>>8)&0xFF;
113 command(CMD_LOK_STATUS, addr, data, 2);
117 void Intellibox::remove_loco(unsigned addr)
122 void Intellibox::set_loco_speed(unsigned addr, unsigned speed)
124 Locomotive &loco = locos[addr];
125 if(loco.protocol==NONE)
128 if(speed==loco.speed)
130 if(loco.pending_half_step)
132 loco.pending_half_step = 0;
133 loco.half_step_delay = Time::TimeStamp();
134 signal_loco_speed.emit(addr, speed, loco.reverse);
141 if(loco.protocol==MM_27)
146 if(speed>loco.speed && !(speed&1))
148 loco.pending_half_step = -1;
151 else if(speed<loco.speed && (speed&1))
153 loco.pending_half_step = 1;
157 loco.pending_half_step = 0;
158 loco.half_step_delay = Time::TimeStamp();
160 loco_command(addr, (speed+1)/2, loco.reverse, loco.funcs, false);
162 else if(loco.protocol==MM)
167 loco_command(addr, speed, loco.reverse, loco.funcs, false);
172 void Intellibox::set_loco_reverse(unsigned addr, bool rev)
174 Locomotive &loco = locos[addr];
175 if(loco.protocol==NONE || rev==loco.reverse)
180 loco_command(addr, 0, rev, loco.funcs, false);
183 void Intellibox::set_loco_function(unsigned addr, unsigned func, bool state)
185 Locomotive &loco = locos[addr];
186 if(loco.protocol==NONE)
190 loco.funcs |= 1<<func;
192 loco.funcs &= ~(1<<func);
194 loco_command(addr, loco.speed, loco.reverse, loco.funcs, true);
195 else if(loco.ext_func && func<=8)
196 loco_command(addr+1, 0, false, (loco.funcs>>4)&0x1E, true);
197 signal_loco_function.emit(addr, func, state);
200 void Intellibox::add_turnout(unsigned addr, const TrackType &type)
202 add_turnout(addr, type.get_state_bits(), false);
205 void Intellibox::remove_turnout(unsigned addr)
207 turnouts.erase(addr);
210 void Intellibox::add_turnout(unsigned addr, unsigned bits, bool signal)
212 if(!turnouts.count(addr))
214 Turnout &turnout = turnouts[addr];
216 turnout.signal = signal;
218 unsigned char data[2];
220 data[1] = (addr>>8)&0xFF;
221 command(CMD_TURNOUT_STATUS, addr, data, 2);
222 for(unsigned i=1; i<turnout.bits; ++i)
224 turnouts[addr+i].bits = 0;
229 command(CMD_TURNOUT_STATUS, addr+i, data, 2);
234 void Intellibox::turnout_state_changed(unsigned addr, const Turnout &turnout) const
237 signal_signal.emit(addr, turnout.state);
239 signal_turnout.emit(addr, turnout.state);
242 void Intellibox::set_turnout(unsigned addr, unsigned state)
244 Turnout &turnout = turnouts[addr];
245 unsigned mask = (1<<turnout.bits)-1;
246 if(((state^turnout.state)&mask)==0 || ((state^turnout.pending)&mask)==0 || !turnout.synced)
248 turnout.state = state;
249 turnout.pending = state;
250 turnout_state_changed(addr, turnout);
254 turnout.state = (turnout.state&mask) | (state&~mask);
255 turnout.pending = state;
256 turnout.active = true;
257 turnout.off_timeout = Time::TimeStamp();
259 for(unsigned i=0; i<turnout.bits; ++i)
260 if((state^turnout.state)&(1<<i))
261 turnout_command(addr+i, !(state&(1<<i)), true);
264 unsigned Intellibox::get_turnout(unsigned addr) const
266 map<unsigned, Turnout>::const_iterator i = turnouts.find(addr);
267 if(i!=turnouts.end())
268 return i->second.state;
272 void Intellibox::add_signal(unsigned addr, const SignalType &)
274 add_turnout(addr, 1, true);
277 void Intellibox::remove_signal(unsigned addr)
279 remove_turnout(addr);
282 void Intellibox::set_signal(unsigned addr, unsigned state)
284 set_turnout(addr, state);
287 unsigned Intellibox::get_signal(unsigned addr) const
289 return get_turnout(addr);
292 void Intellibox::add_sensor(unsigned addr)
294 if(!sensors.count(addr))
297 update_sensors = true;
301 void Intellibox::remove_sensor(unsigned addr)
304 update_sensors = true;
307 bool Intellibox::get_sensor(unsigned addr) const
309 map<unsigned, Sensor>::const_iterator i = sensors.find(addr);
311 return i->second.state;
315 void Intellibox::tick()
317 const Time::TimeStamp t = Time::now();
319 if(t>next_event_query)
321 next_event_query = t+200*Time::msec;
325 for(map<unsigned, Locomotive>::iterator i=locos.begin(); i!=locos.end(); ++i)
326 if(i->second.protocol==MM_27 && i->second.pending_half_step && i->second.half_step_delay && t>i->second.half_step_delay)
328 i->second.speed += i->second.pending_half_step;
329 i->second.pending_half_step = 0;
330 i->second.half_step_delay = Time::TimeStamp();
331 loco_command(i->first, (i->second.speed+1)/2, i->second.reverse, i->second.funcs, false);
334 for(map<unsigned, Turnout>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
335 if(i->second.active && i->second.off_timeout && t>i->second.off_timeout)
337 i->second.active = false;
338 i->second.off_timeout = Time::TimeStamp();
339 for(unsigned j=0; j<i->second.bits; ++j)
340 turnout_command(i->first+j, !(i->second.state&(1<<j)), false);
343 for(map<unsigned, Sensor>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
344 if(i->second.off_timeout && t>i->second.off_timeout)
346 i->second.state = false;
347 i->second.off_timeout = Time::TimeStamp();
348 signal_sensor.emit(i->first, false);
353 unsigned max_addr = (--sensors.end())->first;
354 unsigned char data[2];
356 data[1] = (max_addr+7)/8;
357 command(CMD_SENSOR_PARAM_SET, data, 2);
358 command(CMD_SENSOR_REPORT);
359 update_sensors = false;
362 if(!queue.empty() && command_sent)
364 if(IO::poll(serial, IO::P_INPUT, Time::zero))
367 queue.erase(queue.begin());
368 command_sent = false;
376 const CommandSlot &slot = queue.front();
377 serial.write(reinterpret_cast<const char *>(slot.data), slot.length);
382 void Intellibox::flush()
384 for(list<CommandSlot>::iterator i=queue.begin(); i!=queue.end(); ++i)
386 serial.write(reinterpret_cast<const char *>(i->data), i->length);
388 while(first ? IO::poll(serial, IO::P_INPUT) : IO::poll(serial, IO::P_INPUT, Time::zero))
391 serial.read(data, 16);
397 command_sent = false;
400 Intellibox::Protocol Intellibox::map_protocol(const string &name) const
404 else if(name=="MM-27")
407 throw invalid_argument("Intellibox::map_protocol");
410 void Intellibox::command(Command cmd)
415 void Intellibox::command(Command cmd, const unsigned char *data, unsigned len)
417 command(cmd, 0, data, len);
420 void Intellibox::command(Command cmd, unsigned addr, const unsigned char *data, unsigned len)
426 copy(data, data+len, slot.data+1);
428 queue.push_back(slot);
431 void Intellibox::loco_command(unsigned addr, unsigned speed, bool rev, unsigned funcs, bool setf)
433 unsigned char data[4];
435 data[1] = (addr>>8)&0xFF;
442 data[2] = (speed*19-18)/2;
444 data[3] = (rev ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
447 data[3] |= 0x80 | ((funcs>>1)&0xF);
449 command(CMD_LOK, addr, data, 4);
452 void Intellibox::turnout_command(unsigned addr, bool state, bool active)
454 unsigned char data[2];
456 data[1] = ((addr>>8)&0x7) | (active ? 0x40 : 0) | (state ? 0x80 : 0);
457 command(CMD_TURNOUT, addr, data, 2);
460 void Intellibox::process_reply(const Time::TimeStamp &t)
462 Command cmd = queue.front().cmd;
466 unsigned char status;
467 read_all(&status, 1);
469 signal_power.emit(power);
471 else if(cmd==CMD_EVENT)
473 for(unsigned i=0;; ++i)
481 command(CMD_EVENT_LOK);
483 command(CMD_EVENT_TURNOUT);
485 command(CMD_EVENT_SENSOR);
497 else if(cmd==CMD_EVENT_LOK)
501 unsigned char data[5];
508 else if(cmd==CMD_EVENT_TURNOUT)
512 for(unsigned i=0; i<count; ++i)
514 unsigned char data[2];
517 unsigned addr = data[0]+((data[1]&7)<<8);
519 for(; !turnouts[addr].bits; --addr, mask<<=1) ;
520 Turnout &turnout = turnouts[addr];
522 unsigned bit = !(data[1]&0x80);
523 turnout.state = (turnout.state&~mask) | (bit*mask);
524 turnout.pending = turnout.state;
525 turnout_state_changed(addr,turnout);
528 else if(cmd==CMD_EVENT_SENSOR)
537 unsigned char data[2];
539 for(unsigned i=0; i<16; ++i)
541 unsigned addr = mod*16+i-15;
542 bool state = (data[i/8]>>(7-i%8))&1;
544 Sensor &sensor = sensors[addr];
547 sensor.off_timeout = Time::TimeStamp();
550 sensor.state = state;
551 signal_sensor(addr, state);
554 else if(sensor.state)
555 sensor.off_timeout = t+700*Time::msec;
559 else if(cmd==CMD_LOK)
564 if(err==ERR_NO_ERROR)
566 unsigned addr = queue.front().addr;
567 Locomotive &loco = locos[addr];
570 signal_loco_speed.emit(addr, loco.speed+loco.pending_half_step, loco.reverse);
571 if(loco.pending_half_step)
572 loco.half_step_delay = Time::now()+500*Time::msec;
578 else if(cmd==CMD_TURNOUT)
583 unsigned addr = queue.front().addr;
585 for(; !turnouts[addr].bits; --addr, mask<<=1) ;
586 Turnout &turnout = turnouts[addr];
588 if(err==ERR_NO_ERROR)
590 turnout.state = (turnout.state&~mask) | (turnout.pending&mask);
593 if(turnout.state==turnout.pending)
594 turnout_state_changed(addr, turnout);
595 turnout.off_timeout = t+500*Time::msec;
598 else if(err==ERR_NO_I2C_SPACE)
599 queue.push_back(queue.front());
602 turnout.pending = (turnout.pending&~mask) | (turnout.state&mask);
606 else if(cmd==CMD_TURNOUT_STATUS)
611 if(err==ERR_NO_ERROR)
616 unsigned addr = queue.front().addr;
618 for(; !turnouts[addr].bits; --addr, mask<<=1) ;
619 Turnout &turnout = turnouts[addr];
621 bool bit = !(data&0x04);
622 if(bit!=((turnout.state&mask)!=0))
624 turnout.state = (turnout.state&~mask) | (bit*mask);
625 turnout.pending = turnout.state;
626 turnout_state_changed(addr, turnout);
629 turnout.synced = true;
634 else if(cmd==CMD_LOK_STATUS)
639 if(err==ERR_NO_ERROR)
641 unsigned char data[3];
644 unsigned addr = queue.front().addr;
645 Locomotive &loco = locos[addr];
647 unsigned speed = (data[0]<=1 ? 0 : data[0]*2/19+1);
648 bool reverse = !(data[1]&0x20);
649 bool speed_changed = (speed!=loco.speed || reverse!=loco.reverse);
652 loco.reverse = reverse;
654 unsigned funcs = (data[1]&0xF)<<1;
657 unsigned funcs_changed = loco.funcs^funcs;
661 signal_loco_speed.emit(addr, loco.speed, loco.reverse);
662 for(unsigned i=0; i<5; ++i)
663 if(funcs_changed&(1<<i))
664 signal_loco_function.emit(addr, i, loco.funcs&(1<<i));
671 unsigned expected_bytes = 0;
672 if(cmd==CMD_FUNC_STATUS)
674 if(cmd==CMD_TURNOUT_GROUP_STATUS)
676 if(cmd==CMD_LOK_CONFIG)
682 if(err==ERR_NO_ERROR)
684 unsigned char data[8];
685 read_all(data, expected_bytes);
692 unsigned Intellibox::read_all(unsigned char *buf, unsigned len)
696 pos += serial.read(reinterpret_cast<char *>(buf+pos), len-pos);
701 unsigned Intellibox::read_status(Error *err)
704 unsigned ret = read_all(&c, 1);
705 *err = static_cast<Error>(c);
709 void Intellibox::error(Command cmd, Error err)
711 const char *cmd_str = 0;
714 case CMD_LOK: cmd_str = "CMD_LOK"; break;
715 case CMD_LOK_STATUS: cmd_str = "CMD_LOK_STATUS"; break;
716 case CMD_LOK_CONFIG: cmd_str = "CMD_LOK_CONFIG"; break;
717 case CMD_FUNC: cmd_str = "CMD_FUNC"; break;
718 case CMD_FUNC_STATUS: cmd_str = "CMD_FUNC_STATUS"; break;
719 case CMD_TURNOUT: cmd_str = "CMD_TURNOUT"; break;
720 case CMD_TURNOUT_FREE: cmd_str = "CMD_TURNOUT_FREE"; break;
721 case CMD_TURNOUT_STATUS: cmd_str = "CMD_TURNOUT_STATUS"; break;
722 case CMD_TURNOUT_GROUP_STATUS: cmd_str = "CMD_TURNOUT_GROUP_STATUS"; break;
723 case CMD_SENSOR_STATUS: cmd_str = "CMD_SENSOR_STATUS"; break;
724 case CMD_SENSOR_REPORT: cmd_str = "CMD_SENSOR_REPORT"; break;
725 case CMD_SENSOR_PARAM_SET: cmd_str = "CMD_SENSOR_PARAM_SET"; break;
726 case CMD_STATUS: cmd_str = "CMD_STATUS"; break;
727 case CMD_POWER_OFF: cmd_str = "CMD_POWER_OFF"; break;
728 case CMD_POWER_ON: cmd_str = "CMD_POWER_ON"; break;
729 case CMD_NOP: cmd_str = "CMD_NOP"; break;
730 case CMD_EVENT: cmd_str = "CMD_EVENT"; break;
731 case CMD_EVENT_LOK: cmd_str = "CMD_EVENT_LOK"; break;
732 case CMD_EVENT_TURNOUT: cmd_str = "CMD_EVENT_TURNOUT"; break;
733 case CMD_EVENT_SENSOR: cmd_str = "CMD_EVENT_SENSOR"; break;
734 default: cmd_str = "(unknown command)";
737 const char *err_str = 0;
740 case ERR_NO_ERROR: err_str = "ERR_NO_ERROR"; break;
741 case ERR_SYS_ERROR: err_str = "ERR_SYS_ERROR"; break;
742 case ERR_BAD_PARAM: err_str = "ERR_BAD_PARAM"; break;
743 case ERR_POWER_OFF: err_str = "ERR_POWER_OFF"; break;
744 case ERR_NO_LOK_SPACE: err_str = "ERR_NO_LOK_SPACE"; break;
745 case ERR_NO_TURNOUT_SPACE: err_str = "ERR_NO_TURNOUT_SPACE"; break;
746 case ERR_NO_DATA: err_str = "ERR_NO_DATA"; break;
747 case ERR_NO_SLOT: err_str = "ERR_NO_SLOT"; break;
748 case ERR_BAD_LOK_ADDR: err_str = "ERR_BAD_LOK_ADDR"; break;
749 case ERR_LOK_BUSY: err_str = "ERR_LOK_BUSY"; break;
750 case ERR_BAD_TURNOUT_ADDR: err_str = "ERR_BAD_TURNOUT_ADDR"; break;
751 case ERR_BAD_SO_VALUE: err_str = "ERR_BAD_SO_VALUE"; break;
752 case ERR_NO_I2C_SPACE: err_str = "ERR_NO_I2C_SPACE"; break;
753 case ERR_LOW_TURNOUT_SPACE: err_str = "ERR_LOW_TURNOUT_SPACE"; break;
754 case ERR_LOK_HALTED: err_str = "ERR_LOK_HALTED"; break;
755 case ERR_LOK_POWER_OFF: err_str = "ERR_LOK_POWER_OFF"; break;
756 default: cmd_str = "(unknown error)";
759 IO::print("Error: %s: %s\n", cmd_str, err_str);
763 Intellibox::Locomotive::Locomotive():
773 Intellibox::Turnout::Turnout():
783 Intellibox::Sensor::Sensor():