3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
11 #include <msp/io/print.h>
12 #include <msp/time/units.h>
13 #include <msp/time/utils.h>
14 #include "intellibox.h"
21 Intellibox::Intellibox(const string &dev):
24 update_sensors(false),
27 serial_fd = ::open(dev.c_str(), O_RDWR);
29 throw Exception("Couldn't open serial port\n");
31 static unsigned baud[]=
41 tcgetattr(serial_fd, &attr);
43 attr.c_cflag |= CSTOPB;
47 for(unsigned i=0; baud[i]; i+=2)
49 cfsetospeed(&attr, baud[i+1]);
50 tcsetattr(serial_fd, TCSADRAIN, &attr);
52 write(serial_fd, "\xC4", 1);
54 pollfd pfd = { serial_fd, POLLIN, 0 };
55 if(poll(&pfd, 1, 500)>0)
57 IO::print("IB detected at %d bits/s\n", baud[i]);
59 p50 = (read(serial_fd, buf, 2)==2);
66 throw Exception("IB not detected");
69 write(serial_fd, "xZzA1\r", 6);
74 void Intellibox::set_power(bool p)
78 command(CMD_POWER_ON);
80 command(CMD_POWER_OFF);
81 signal_power.emit(power);
84 void Intellibox::halt(bool h)
89 for(map<unsigned, Locomotive>::iterator i=locos.begin(); i!=locos.end(); ++i)
91 set_loco_speed(i->first, 0);
94 signal_halt.emit(halted);
97 const char *Intellibox::enumerate_protocols(unsigned i) const
104 unsigned Intellibox::get_protocol_speed_steps(const string &proto) const
109 throw InvalidParameterValue("Unknown protocol");
112 void Intellibox::add_loco(unsigned addr, const string &proto)
114 if(!locos.count(addr))
116 locos[addr].protocol = proto;
118 unsigned char data[2];
120 data[1] = (addr>>8)&0xFF;
121 command(CMD_LOK_STATUS, addr, data, 2);
125 void Intellibox::set_loco_speed(unsigned addr, unsigned speed)
127 Locomotive &loco = locos[addr];
128 if(speed==loco.speed)
134 loco_command(addr, speed, loco.reverse, loco.funcs|0x100);
137 void Intellibox::set_loco_reverse(unsigned addr, bool rev)
139 Locomotive &loco = locos[addr];
140 if(rev==loco.reverse)
144 loco_command(addr, loco.speed, rev, loco.funcs|0x100);
147 void Intellibox::set_loco_function(unsigned addr, unsigned func, bool state)
149 Locomotive &loco = locos[addr];
151 loco.funcs |= 1<<func;
153 loco.funcs &= ~(1<<func);
154 loco_command(addr, loco.speed, loco.reverse, loco.funcs);
155 signal_loco_function.emit(addr, func, state);
158 void Intellibox::add_turnout(unsigned addr)
160 if(!turnouts.count(addr))
164 unsigned char data[2];
166 data[1] = (addr>>8)&0xFF;
167 command(CMD_TURNOUT_STATUS, addr, data, 2);
171 void Intellibox::set_turnout(unsigned addr, bool state)
173 Turnout &turnout = turnouts[addr];
174 if(state==turnout.state || state==turnout.pending)
177 turnout.pending = state;
178 turnout.active = true;
179 turnout.off_timeout = Time::TimeStamp();
181 turnout_command(addr, state, true);
184 bool Intellibox::get_turnout(unsigned addr) const
186 map<unsigned, Turnout>::const_iterator i = turnouts.find(addr);
187 if(i!=turnouts.end())
188 return i->second.state;
192 void Intellibox::add_sensor(unsigned addr)
194 if(!sensors.count(addr))
197 update_sensors = true;
201 bool Intellibox::get_sensor(unsigned addr) const
203 map<unsigned, Sensor>::const_iterator i = sensors.find(addr);
205 return i->second.state;
209 void Intellibox::tick()
211 const Time::TimeStamp t = Time::now();
213 if(t>next_event_query)
215 next_event_query = t+200*Time::msec;
219 for(map<unsigned, Turnout>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
220 if(i->second.active && i->second.off_timeout && t>i->second.off_timeout)
222 i->second.active = false;
223 i->second.off_timeout = Time::TimeStamp();
224 turnout_command(i->first, i->second.state, false);
227 for(map<unsigned, Sensor>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
228 if(i->second.off_timeout && t>i->second.off_timeout)
230 i->second.state = false;
231 i->second.off_timeout = Time::TimeStamp();
232 signal_sensor.emit(i->first, false);
237 unsigned max_addr = (--sensors.end())->first;
238 unsigned char data[2];
240 data[1] = (max_addr+7)/8;
241 command(CMD_SENSOR_PARAM_SET, data, 2);
242 command(CMD_SENSOR_REPORT);
243 update_sensors = false;
246 if(!queue.empty() && command_sent)
248 pollfd pfd = { serial_fd, POLLIN, 0 };
249 if(poll(&pfd, 1, 0)>0)
252 queue.erase(queue.begin());
253 command_sent = false;
261 const CommandSlot &slot = queue.front();
262 write(serial_fd, slot.data, slot.length);
267 void Intellibox::flush()
269 Time::TimeStamp t = Time::now();
270 for(list<CommandSlot>::iterator i=queue.begin(); i!=queue.end(); ++i)
272 write(serial_fd, i->data, i->length);
273 pollfd pfd = { serial_fd, POLLIN, 0 };
275 while(poll(&pfd, 1, (first ? -1 : 0))>0)
278 read(serial_fd, data, 16);
284 command_sent = false;
287 void Intellibox::command(Command cmd)
292 void Intellibox::command(Command cmd, const unsigned char *data, unsigned len)
294 command(cmd, 0, data, len);
297 void Intellibox::command(Command cmd, unsigned addr, const unsigned char *data, unsigned len)
303 copy(data, data+len, slot.data+1);
305 queue.push_back(slot);
308 void Intellibox::loco_command(unsigned addr, unsigned speed, bool rev, unsigned funcs)
310 unsigned char data[4];
312 data[1] = (addr>>8)&0xFF;
319 data[2] = (speed*19-18)/2;
321 data[3] = (rev ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
324 data[3] |= 0x80 | ((funcs>>1)&0xF);
326 command(CMD_LOK, addr, data, 4);
329 void Intellibox::turnout_command(unsigned addr, bool state, bool active)
331 unsigned char data[2];
333 data[1] = ((addr>>8)&0x7) | (active ? 0x40 : 0) | (state ? 0x80 : 0);
334 command(CMD_TURNOUT, addr, data, 2);
337 void Intellibox::process_reply(const Time::TimeStamp &t)
339 Command cmd = queue.front().cmd;
343 unsigned char status;
344 read_all(&status, 1);
346 signal_power.emit(power);
348 else if(cmd==CMD_EVENT)
350 for(unsigned i=0;; ++i)
358 command(CMD_EVENT_LOK);
360 command(CMD_EVENT_TURNOUT);
362 command(CMD_EVENT_SENSOR);
374 else if(cmd==CMD_EVENT_LOK)
378 unsigned char data[5];
385 else if(cmd==CMD_EVENT_TURNOUT)
389 for(unsigned i=0; i<count; ++i)
391 unsigned char data[2];
394 unsigned addr = data[0]+((data[1]&7)<<8);
395 Turnout &turnout = turnouts[addr];
396 turnout.state = (data[1]&0x80)!=0;
397 turnout.pending = turnout.state;
398 signal_turnout.emit(addr, turnout.state);
401 else if(cmd==CMD_EVENT_SENSOR)
410 unsigned char data[2];
412 for(unsigned i=0; i<16; ++i)
414 unsigned addr = mod*16+i-15;
415 bool state = (data[i/8]>>(7-i%8))&1;
417 Sensor &sensor = sensors[addr];
420 sensor.off_timeout = Time::TimeStamp();
423 sensor.state = state;
424 signal_sensor(addr, state);
427 else if(sensor.state)
428 sensor.off_timeout = t+700*Time::msec;
432 else if(cmd==CMD_LOK)
437 if(err==ERR_NO_ERROR)
439 unsigned addr = queue.front().addr;
440 Locomotive &loco = locos[addr];
441 signal_loco_speed.emit(addr, loco.speed, loco.reverse);
446 else if(cmd==CMD_TURNOUT)
451 unsigned addr = queue.front().addr;
452 Turnout &turnout = turnouts[addr];
454 if(err==ERR_NO_ERROR)
456 turnout.state = turnout.pending;
459 signal_turnout.emit(addr, turnout.state);
460 turnout.off_timeout = t+500*Time::msec;
463 else if(err==ERR_NO_I2C_SPACE)
464 queue.push_back(queue.front());
467 turnout.pending = turnout.state;
471 else if(cmd==CMD_TURNOUT_STATUS)
476 if(err==ERR_NO_ERROR)
481 unsigned addr = queue.front().addr;
482 bool state = data&0x04;
484 Turnout &turnout = turnouts[addr];
485 if(state!=turnout.state)
487 turnout.state = state;
488 turnout.pending = state;
489 signal_turnout.emit(addr, turnout.state);
495 else if(cmd==CMD_LOK_STATUS)
500 if(err==ERR_NO_ERROR)
502 unsigned char data[3];
505 unsigned addr = queue.front().addr;
506 Locomotive &loco = locos[addr];
508 unsigned speed = (data[0]<=1 ? 0 : data[0]*2/19+1);
509 bool reverse = !(data[1]&0x20);
510 if(speed!=loco.speed || reverse!=loco.reverse)
513 loco.reverse = reverse;
514 signal_loco_speed.emit(addr, loco.speed, loco.reverse);
517 unsigned funcs = (data[1]&0xF)<<1;
520 if(funcs!=loco.funcs)
522 unsigned changed = loco.funcs^funcs;
524 for(unsigned i=0; i<5; ++i)
526 signal_loco_function.emit(addr, i, loco.funcs&(1<<i));
534 unsigned expected_bytes = 0;
535 if(cmd==CMD_FUNC_STATUS)
537 if(cmd==CMD_TURNOUT_GROUP_STATUS)
539 if(cmd==CMD_LOK_CONFIG)
545 if(err==ERR_NO_ERROR)
547 unsigned char data[8];
548 read_all(data, expected_bytes);
555 unsigned Intellibox::read_all(unsigned char *buf, unsigned len)
559 pos += read(serial_fd, buf+pos, len-pos);
564 unsigned Intellibox::read_status(Error *err)
567 unsigned ret = read_all(&c, 1);
568 *err = static_cast<Error>(c);
572 void Intellibox::error(Command cmd, Error err)
574 const char *cmd_str = 0;
577 case CMD_LOK: cmd_str = "CMD_LOK"; break;
578 case CMD_LOK_STATUS: cmd_str = "CMD_LOK_STATUS"; break;
579 case CMD_LOK_CONFIG: cmd_str = "CMD_LOK_CONFIG"; break;
580 case CMD_FUNC: cmd_str = "CMD_FUNC"; break;
581 case CMD_FUNC_STATUS: cmd_str = "CMD_FUNC_STATUS"; break;
582 case CMD_TURNOUT: cmd_str = "CMD_TURNOUT"; break;
583 case CMD_TURNOUT_FREE: cmd_str = "CMD_TURNOUT_FREE"; break;
584 case CMD_TURNOUT_STATUS: cmd_str = "CMD_TURNOUT_STATUS"; break;
585 case CMD_TURNOUT_GROUP_STATUS: cmd_str = "CMD_TURNOUT_GROUP_STATUS"; break;
586 case CMD_SENSOR_STATUS: cmd_str = "CMD_SENSOR_STATUS"; break;
587 case CMD_SENSOR_REPORT: cmd_str = "CMD_SENSOR_REPORT"; break;
588 case CMD_SENSOR_PARAM_SET: cmd_str = "CMD_SENSOR_PARAM_SET"; break;
589 case CMD_STATUS: cmd_str = "CMD_STATUS"; break;
590 case CMD_POWER_OFF: cmd_str = "CMD_POWER_OFF"; break;
591 case CMD_POWER_ON: cmd_str = "CMD_POWER_ON"; break;
592 case CMD_NOP: cmd_str = "CMD_NOP"; break;
593 case CMD_EVENT: cmd_str = "CMD_EVENT"; break;
594 case CMD_EVENT_LOK: cmd_str = "CMD_EVENT_LOK"; break;
595 case CMD_EVENT_TURNOUT: cmd_str = "CMD_EVENT_TURNOUT"; break;
596 case CMD_EVENT_SENSOR: cmd_str = "CMD_EVENT_SENSOR"; break;
597 default: cmd_str = "(unknown command)";
600 const char *err_str = 0;
603 case ERR_NO_ERROR: err_str = "ERR_NO_ERROR"; break;
604 case ERR_SYS_ERROR: err_str = "ERR_SYS_ERROR"; break;
605 case ERR_BAD_PARAM: err_str = "ERR_BAD_PARAM"; break;
606 case ERR_POWER_OFF: err_str = "ERR_POWER_OFF"; break;
607 case ERR_NO_LOK_SPACE: err_str = "ERR_NO_LOK_SPACE"; break;
608 case ERR_NO_TURNOUT_SPACE: err_str = "ERR_NO_TURNOUT_SPACE"; break;
609 case ERR_NO_DATA: err_str = "ERR_NO_DATA"; break;
610 case ERR_NO_SLOT: err_str = "ERR_NO_SLOT"; break;
611 case ERR_BAD_LOK_ADDR: err_str = "ERR_BAD_LOK_ADDR"; break;
612 case ERR_LOK_BUSY: err_str = "ERR_LOK_BUSY"; break;
613 case ERR_BAD_TURNOUT_ADDR: err_str = "ERR_BAD_TURNOUT_ADDR"; break;
614 case ERR_BAD_SO_VALUE: err_str = "ERR_BAD_SO_VALUE"; break;
615 case ERR_NO_I2C_SPACE: err_str = "ERR_NO_I2C_SPACE"; break;
616 case ERR_LOW_TURNOUT_SPACE: err_str = "ERR_LOW_TURNOUT_SPACE"; break;
617 case ERR_LOK_HALTED: err_str = "ERR_LOK_HALTED"; break;
618 case ERR_LOK_POWER_OFF: err_str = "ERR_LOK_POWER_OFF"; break;
619 default: cmd_str = "(unknown error)";
622 IO::print("Error: %s: %s\n", cmd_str, err_str);
626 Intellibox::Locomotive::Locomotive():
633 Intellibox::Turnout::Turnout():
640 Intellibox::Sensor::Sensor():
644 } // namespace Marklin