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):
19 update_sensors(false),
22 serial_fd = ::open(dev.c_str(), O_RDWR);
24 throw Exception("Couldn't open serial port\n");
26 static unsigned baud[]=
36 tcgetattr(serial_fd, &attr);
38 attr.c_cflag |= CSTOPB;
42 for(unsigned i=0; baud[i]; i+=2)
44 cfsetospeed(&attr, baud[i+1]);
45 tcsetattr(serial_fd, TCSADRAIN, &attr);
47 write(serial_fd, "\xC4", 1);
49 pollfd pfd = { serial_fd, POLLIN, 0 };
50 if(poll(&pfd, 1, 500)>0)
52 IO::print("IB detected at %d bits/s\n", baud[i]);
54 p50 = (read(serial_fd, buf, 2)==2);
61 throw Exception("IB not detected");
64 write(serial_fd, "xZzA1\r", 6);
69 void Intellibox::set_power(bool p)
73 command(CMD_POWER_ON);
75 command(CMD_POWER_OFF);
76 signal_power.emit(power);
79 void Intellibox::halt(bool h)
84 for(map<unsigned, Locomotive>::iterator i=locos.begin(); i!=locos.end(); ++i)
86 set_loco_speed(i->first, 0);
89 signal_halt.emit(halted);
92 const char *Intellibox::enumerate_protocols(unsigned i) const
102 unsigned Intellibox::get_protocol_speed_steps(const string &proto_name) const
104 Protocol proto = map_protocol(proto_name);
107 else if(proto==MM_27)
112 void Intellibox::add_loco(unsigned addr, const string &proto_name, const VehicleType &type)
114 Protocol proto = map_protocol(proto_name);
116 if(!locos.count(addr))
118 Locomotive &loco = locos[addr];
119 loco.protocol = proto;
120 if(type.get_max_function()>4)
122 loco.ext_func = true;
123 locos[addr+1].protocol = NONE;
126 unsigned char data[2];
128 data[1] = (addr>>8)&0xFF;
129 command(CMD_LOK_STATUS, addr, data, 2);
133 void Intellibox::set_loco_speed(unsigned addr, unsigned speed)
135 Locomotive &loco = locos[addr];
136 if(loco.protocol==NONE)
139 if(speed==loco.speed)
141 if(loco.pending_half_step)
143 loco.pending_half_step = 0;
144 loco.half_step_delay = Time::TimeStamp();
145 signal_loco_speed.emit(addr, speed, loco.reverse);
152 if(loco.protocol==MM_27)
157 if(speed>loco.speed && !(speed&1))
159 loco.pending_half_step = -1;
162 else if(speed<loco.speed && (speed&1))
164 loco.pending_half_step = 1;
168 loco.pending_half_step = 0;
169 loco.half_step_delay = Time::TimeStamp();
171 loco_command(addr, (speed+1)/2, loco.reverse, loco.funcs, false);
173 else if(loco.protocol==MM)
178 loco_command(addr, speed, loco.reverse, loco.funcs, false);
183 void Intellibox::set_loco_reverse(unsigned addr, bool rev)
185 Locomotive &loco = locos[addr];
186 if(loco.protocol==NONE || rev==loco.reverse)
191 loco_command(addr, 0, rev, loco.funcs, false);
194 void Intellibox::set_loco_function(unsigned addr, unsigned func, bool state)
196 Locomotive &loco = locos[addr];
197 if(loco.protocol==NONE)
201 loco.funcs |= 1<<func;
203 loco.funcs &= ~(1<<func);
205 loco_command(addr, loco.speed, loco.reverse, loco.funcs, true);
206 else if(loco.ext_func && func<=8)
207 loco_command(addr+1, 0, false, (loco.funcs>>4)&0x1E, true);
208 signal_loco_function.emit(addr, func, state);
211 void Intellibox::add_turnout(unsigned addr, const TrackType &type)
213 if(!turnouts.count(addr))
215 Turnout &turnout = turnouts[addr];
216 turnout.bits = type.get_state_bits();
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::set_turnout(unsigned addr, unsigned state)
236 Turnout &turnout = turnouts[addr];
237 unsigned mask = (1<<turnout.bits)-1;
238 if(((state^turnout.state)&mask)==0 || ((state^turnout.pending)&mask)==0 || !turnout.synced)
240 turnout.state = state;
241 turnout.pending = state;
242 signal_turnout.emit(addr, state);
246 turnout.state = (turnout.state&mask) | (state&~mask);
247 turnout.pending = state;
248 turnout.active = true;
249 turnout.off_timeout = Time::TimeStamp();
251 for(unsigned i=0; i<turnout.bits; ++i)
252 if((state^turnout.state)&(1<<i))
253 turnout_command(addr+i, !(state&(1<<i)), true);
256 unsigned Intellibox::get_turnout(unsigned addr) const
258 map<unsigned, Turnout>::const_iterator i = turnouts.find(addr);
259 if(i!=turnouts.end())
260 return i->second.state;
264 void Intellibox::add_sensor(unsigned addr)
266 if(!sensors.count(addr))
269 update_sensors = true;
273 bool Intellibox::get_sensor(unsigned addr) const
275 map<unsigned, Sensor>::const_iterator i = sensors.find(addr);
277 return i->second.state;
281 void Intellibox::tick()
283 const Time::TimeStamp t = Time::now();
285 if(t>next_event_query)
287 next_event_query = t+200*Time::msec;
291 for(map<unsigned, Locomotive>::iterator i=locos.begin(); i!=locos.end(); ++i)
292 if(i->second.protocol==MM_27 && i->second.pending_half_step && i->second.half_step_delay && t>i->second.half_step_delay)
294 i->second.speed += i->second.pending_half_step;
295 i->second.pending_half_step = 0;
296 i->second.half_step_delay = Time::TimeStamp();
297 loco_command(i->first, (i->second.speed+1)/2, i->second.reverse, i->second.funcs, false);
300 for(map<unsigned, Turnout>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
301 if(i->second.active && i->second.off_timeout && t>i->second.off_timeout)
303 i->second.active = false;
304 i->second.off_timeout = Time::TimeStamp();
305 for(unsigned j=0; j<i->second.bits; ++j)
306 turnout_command(i->first+j, !(i->second.state&(1<<j)), false);
309 for(map<unsigned, Sensor>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
310 if(i->second.off_timeout && t>i->second.off_timeout)
312 i->second.state = false;
313 i->second.off_timeout = Time::TimeStamp();
314 signal_sensor.emit(i->first, false);
319 unsigned max_addr = (--sensors.end())->first;
320 unsigned char data[2];
322 data[1] = (max_addr+7)/8;
323 command(CMD_SENSOR_PARAM_SET, data, 2);
324 command(CMD_SENSOR_REPORT);
325 update_sensors = false;
328 if(!queue.empty() && command_sent)
330 pollfd pfd = { serial_fd, POLLIN, 0 };
331 if(poll(&pfd, 1, 0)>0)
334 queue.erase(queue.begin());
335 command_sent = false;
343 const CommandSlot &slot = queue.front();
344 write(serial_fd, slot.data, slot.length);
349 void Intellibox::flush()
351 for(list<CommandSlot>::iterator i=queue.begin(); i!=queue.end(); ++i)
353 write(serial_fd, i->data, i->length);
354 pollfd pfd = { serial_fd, POLLIN, 0 };
356 while(poll(&pfd, 1, (first ? -1 : 0))>0)
359 read(serial_fd, data, 16);
365 command_sent = false;
368 Intellibox::Protocol Intellibox::map_protocol(const string &name) const
372 else if(name=="MM-27")
375 throw InvalidParameterValue("Unknown protocol");
378 void Intellibox::command(Command cmd)
383 void Intellibox::command(Command cmd, const unsigned char *data, unsigned len)
385 command(cmd, 0, data, len);
388 void Intellibox::command(Command cmd, unsigned addr, const unsigned char *data, unsigned len)
394 copy(data, data+len, slot.data+1);
396 queue.push_back(slot);
399 void Intellibox::loco_command(unsigned addr, unsigned speed, bool rev, unsigned funcs, bool setf)
401 unsigned char data[4];
403 data[1] = (addr>>8)&0xFF;
410 data[2] = (speed*19-18)/2;
412 data[3] = (rev ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
415 data[3] |= 0x80 | ((funcs>>1)&0xF);
417 command(CMD_LOK, addr, data, 4);
420 void Intellibox::turnout_command(unsigned addr, bool state, bool active)
422 unsigned char data[2];
424 data[1] = ((addr>>8)&0x7) | (active ? 0x40 : 0) | (state ? 0x80 : 0);
425 command(CMD_TURNOUT, addr, data, 2);
428 void Intellibox::process_reply(const Time::TimeStamp &t)
430 Command cmd = queue.front().cmd;
434 unsigned char status;
435 read_all(&status, 1);
437 signal_power.emit(power);
439 else if(cmd==CMD_EVENT)
441 for(unsigned i=0;; ++i)
449 command(CMD_EVENT_LOK);
451 command(CMD_EVENT_TURNOUT);
453 command(CMD_EVENT_SENSOR);
465 else if(cmd==CMD_EVENT_LOK)
469 unsigned char data[5];
476 else if(cmd==CMD_EVENT_TURNOUT)
480 for(unsigned i=0; i<count; ++i)
482 unsigned char data[2];
485 unsigned addr = data[0]+((data[1]&7)<<8);
487 for(; !turnouts[addr].bits; --addr, mask<<=1) ;
488 Turnout &turnout = turnouts[addr];
490 unsigned bit = !(data[1]&0x80);
491 turnout.state = (turnout.state&~mask) | (bit*mask);
492 turnout.pending = turnout.state;
493 signal_turnout.emit(addr, turnout.state);
496 else if(cmd==CMD_EVENT_SENSOR)
505 unsigned char data[2];
507 for(unsigned i=0; i<16; ++i)
509 unsigned addr = mod*16+i-15;
510 bool state = (data[i/8]>>(7-i%8))&1;
512 Sensor &sensor = sensors[addr];
515 sensor.off_timeout = Time::TimeStamp();
518 sensor.state = state;
519 signal_sensor(addr, state);
522 else if(sensor.state)
523 sensor.off_timeout = t+700*Time::msec;
527 else if(cmd==CMD_LOK)
532 if(err==ERR_NO_ERROR)
534 unsigned addr = queue.front().addr;
535 Locomotive &loco = locos[addr];
538 signal_loco_speed.emit(addr, loco.speed+loco.pending_half_step, loco.reverse);
539 if(loco.pending_half_step)
540 loco.half_step_delay = Time::now()+500*Time::msec;
546 else if(cmd==CMD_TURNOUT)
551 unsigned addr = queue.front().addr;
553 for(; !turnouts[addr].bits; --addr, mask<<=1) ;
554 Turnout &turnout = turnouts[addr];
556 if(err==ERR_NO_ERROR)
558 turnout.state = (turnout.state&~mask) | (turnout.pending&mask);
561 if(turnout.state==turnout.pending)
562 signal_turnout.emit(addr, turnout.state);
563 turnout.off_timeout = t+500*Time::msec;
566 else if(err==ERR_NO_I2C_SPACE)
567 queue.push_back(queue.front());
570 turnout.pending = (turnout.pending&~mask) | (turnout.state&mask);
574 else if(cmd==CMD_TURNOUT_STATUS)
579 if(err==ERR_NO_ERROR)
584 unsigned addr = queue.front().addr;
586 for(; !turnouts[addr].bits; --addr, mask<<=1) ;
587 Turnout &turnout = turnouts[addr];
589 bool bit = !(data&0x04);
590 if(bit!=((turnout.state&mask)!=0))
592 turnout.state = (turnout.state&~mask) | (bit*mask);
593 turnout.pending = turnout.state;
594 signal_turnout.emit(addr, turnout.state);
597 turnout.synced = true;
602 else if(cmd==CMD_LOK_STATUS)
607 if(err==ERR_NO_ERROR)
609 unsigned char data[3];
612 unsigned addr = queue.front().addr;
613 Locomotive &loco = locos[addr];
615 unsigned speed = (data[0]<=1 ? 0 : data[0]*2/19+1);
616 bool reverse = !(data[1]&0x20);
617 bool speed_changed = (speed!=loco.speed || reverse!=loco.reverse);
620 loco.reverse = reverse;
622 unsigned funcs = (data[1]&0xF)<<1;
625 unsigned funcs_changed = loco.funcs^funcs;
629 signal_loco_speed.emit(addr, loco.speed, loco.reverse);
630 for(unsigned i=0; i<5; ++i)
631 if(funcs_changed&(1<<i))
632 signal_loco_function.emit(addr, i, loco.funcs&(1<<i));
639 unsigned expected_bytes = 0;
640 if(cmd==CMD_FUNC_STATUS)
642 if(cmd==CMD_TURNOUT_GROUP_STATUS)
644 if(cmd==CMD_LOK_CONFIG)
650 if(err==ERR_NO_ERROR)
652 unsigned char data[8];
653 read_all(data, expected_bytes);
660 unsigned Intellibox::read_all(unsigned char *buf, unsigned len)
664 pos += read(serial_fd, buf+pos, len-pos);
669 unsigned Intellibox::read_status(Error *err)
672 unsigned ret = read_all(&c, 1);
673 *err = static_cast<Error>(c);
677 void Intellibox::error(Command cmd, Error err)
679 const char *cmd_str = 0;
682 case CMD_LOK: cmd_str = "CMD_LOK"; break;
683 case CMD_LOK_STATUS: cmd_str = "CMD_LOK_STATUS"; break;
684 case CMD_LOK_CONFIG: cmd_str = "CMD_LOK_CONFIG"; break;
685 case CMD_FUNC: cmd_str = "CMD_FUNC"; break;
686 case CMD_FUNC_STATUS: cmd_str = "CMD_FUNC_STATUS"; break;
687 case CMD_TURNOUT: cmd_str = "CMD_TURNOUT"; break;
688 case CMD_TURNOUT_FREE: cmd_str = "CMD_TURNOUT_FREE"; break;
689 case CMD_TURNOUT_STATUS: cmd_str = "CMD_TURNOUT_STATUS"; break;
690 case CMD_TURNOUT_GROUP_STATUS: cmd_str = "CMD_TURNOUT_GROUP_STATUS"; break;
691 case CMD_SENSOR_STATUS: cmd_str = "CMD_SENSOR_STATUS"; break;
692 case CMD_SENSOR_REPORT: cmd_str = "CMD_SENSOR_REPORT"; break;
693 case CMD_SENSOR_PARAM_SET: cmd_str = "CMD_SENSOR_PARAM_SET"; break;
694 case CMD_STATUS: cmd_str = "CMD_STATUS"; break;
695 case CMD_POWER_OFF: cmd_str = "CMD_POWER_OFF"; break;
696 case CMD_POWER_ON: cmd_str = "CMD_POWER_ON"; break;
697 case CMD_NOP: cmd_str = "CMD_NOP"; break;
698 case CMD_EVENT: cmd_str = "CMD_EVENT"; break;
699 case CMD_EVENT_LOK: cmd_str = "CMD_EVENT_LOK"; break;
700 case CMD_EVENT_TURNOUT: cmd_str = "CMD_EVENT_TURNOUT"; break;
701 case CMD_EVENT_SENSOR: cmd_str = "CMD_EVENT_SENSOR"; break;
702 default: cmd_str = "(unknown command)";
705 const char *err_str = 0;
708 case ERR_NO_ERROR: err_str = "ERR_NO_ERROR"; break;
709 case ERR_SYS_ERROR: err_str = "ERR_SYS_ERROR"; break;
710 case ERR_BAD_PARAM: err_str = "ERR_BAD_PARAM"; break;
711 case ERR_POWER_OFF: err_str = "ERR_POWER_OFF"; break;
712 case ERR_NO_LOK_SPACE: err_str = "ERR_NO_LOK_SPACE"; break;
713 case ERR_NO_TURNOUT_SPACE: err_str = "ERR_NO_TURNOUT_SPACE"; break;
714 case ERR_NO_DATA: err_str = "ERR_NO_DATA"; break;
715 case ERR_NO_SLOT: err_str = "ERR_NO_SLOT"; break;
716 case ERR_BAD_LOK_ADDR: err_str = "ERR_BAD_LOK_ADDR"; break;
717 case ERR_LOK_BUSY: err_str = "ERR_LOK_BUSY"; break;
718 case ERR_BAD_TURNOUT_ADDR: err_str = "ERR_BAD_TURNOUT_ADDR"; break;
719 case ERR_BAD_SO_VALUE: err_str = "ERR_BAD_SO_VALUE"; break;
720 case ERR_NO_I2C_SPACE: err_str = "ERR_NO_I2C_SPACE"; break;
721 case ERR_LOW_TURNOUT_SPACE: err_str = "ERR_LOW_TURNOUT_SPACE"; break;
722 case ERR_LOK_HALTED: err_str = "ERR_LOK_HALTED"; break;
723 case ERR_LOK_POWER_OFF: err_str = "ERR_LOK_POWER_OFF"; break;
724 default: cmd_str = "(unknown error)";
727 IO::print("Error: %s: %s\n", cmd_str, err_str);
731 Intellibox::Locomotive::Locomotive():
739 Intellibox::Turnout::Turnout():
748 Intellibox::Sensor::Sensor():