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 void Intellibox::add_loco(unsigned addr)
99 if(!locos.count(addr))
103 unsigned char data[2];
105 data[1] = (addr>>8)&0xFF;
106 command(CMD_LOK_STATUS, addr, data, 2);
110 void Intellibox::set_loco_speed(unsigned addr, unsigned speed)
112 Locomotive &loco = locos[addr];
113 if(speed==loco.speed)
119 loco_command(addr, speed, loco.reverse, loco.funcs|0x100);
122 void Intellibox::set_loco_reverse(unsigned addr, bool rev)
124 Locomotive &loco = locos[addr];
125 if(rev==loco.reverse)
129 loco_command(addr, loco.speed, rev, loco.funcs|0x100);
132 void Intellibox::set_loco_function(unsigned addr, unsigned func, bool state)
134 Locomotive &loco = locos[addr];
136 loco.funcs |= 1<<func;
138 loco.funcs &= ~(1<<func);
139 loco_command(addr, loco.speed, loco.reverse, loco.funcs);
140 signal_loco_function.emit(addr, func, state);
143 void Intellibox::add_turnout(unsigned addr)
145 if(!turnouts.count(addr))
149 unsigned char data[2];
151 data[1] = (addr>>8)&0xFF;
152 command(CMD_TURNOUT_STATUS, addr, data, 2);
156 void Intellibox::set_turnout(unsigned addr, bool state)
158 Turnout &turnout = turnouts[addr];
159 if(state==turnout.state || state==turnout.pending)
162 turnout.pending = state;
163 turnout.active = true;
165 turnout_command(addr, state, true);
168 bool Intellibox::get_turnout(unsigned addr) const
170 map<unsigned, Turnout>::const_iterator i = turnouts.find(addr);
171 if(i!=turnouts.end())
172 return i->second.state;
176 void Intellibox::add_sensor(unsigned addr)
178 if(!sensors.count(addr))
181 update_sensors = true;
185 bool Intellibox::get_sensor(unsigned addr) const
187 map<unsigned, Sensor>::const_iterator i = sensors.find(addr);
189 return i->second.state;
193 void Intellibox::tick()
195 const Time::TimeStamp t = Time::now();
197 if(t>next_event_query)
199 next_event_query = t+200*Time::msec;
203 for(map<unsigned, Turnout>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
204 if(i->second.active && i->second.off_timeout && t>i->second.off_timeout)
206 i->second.active = false;
207 i->second.off_timeout = Time::TimeStamp();
208 turnout_command(i->first, i->second.state, false);
211 for(map<unsigned, Sensor>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
212 if(i->second.off_timeout && t>i->second.off_timeout)
214 i->second.state = false;
215 i->second.off_timeout = Time::TimeStamp();
216 signal_sensor.emit(i->first, false);
221 unsigned max_addr = (--sensors.end())->first;
222 unsigned char data[2];
224 data[1] = (max_addr+7)/8;
225 command(CMD_SENSOR_PARAM_SET, data, 2);
226 command(CMD_SENSOR_REPORT);
227 update_sensors = false;
230 if(!queue.empty() && command_sent)
232 pollfd pfd = { serial_fd, POLLIN, 0 };
233 if(poll(&pfd, 1, 0)>0)
236 queue.erase(queue.begin());
237 command_sent = false;
245 const CommandSlot &slot = queue.front();
246 write(serial_fd, slot.data, slot.length);
251 void Intellibox::flush()
253 Time::TimeStamp t = Time::now();
254 for(list<CommandSlot>::iterator i=queue.begin(); i!=queue.end(); ++i)
256 write(serial_fd, i->data, i->length);
257 pollfd pfd = { serial_fd, POLLIN, 0 };
259 while(poll(&pfd, 1, (first ? -1 : 0))>0)
262 read(serial_fd, data, 16);
268 command_sent = false;
271 void Intellibox::command(Command cmd)
276 void Intellibox::command(Command cmd, const unsigned char *data, unsigned len)
278 command(cmd, 0, data, len);
281 void Intellibox::command(Command cmd, unsigned addr, const unsigned char *data, unsigned len)
287 copy(data, data+len, slot.data+1);
289 queue.push_back(slot);
292 void Intellibox::loco_command(unsigned addr, unsigned speed, bool rev, unsigned funcs)
294 unsigned char data[4];
296 data[1] = (addr>>8)&0xFF;
303 data[2] = (speed*19-18)/2;
305 data[3] = (rev ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
308 data[3] |= 0x80 | ((funcs>>1)&0xF);
310 command(CMD_LOK, addr, data, 4);
313 void Intellibox::turnout_command(unsigned addr, bool state, bool active)
315 unsigned char data[2];
317 data[1] = ((addr>>8)&0x7) | (active ? 0x40 : 0) | (state ? 0x80 : 0);
318 command(CMD_TURNOUT, addr, data, 2);
321 void Intellibox::process_reply(const Time::TimeStamp &t)
323 Command cmd = queue.front().cmd;
327 unsigned char status;
328 read_all(&status, 1);
330 signal_power.emit(power);
332 else if(cmd==CMD_EVENT)
334 for(unsigned i=0;; ++i)
342 command(CMD_EVENT_LOK);
344 command(CMD_EVENT_TURNOUT);
346 command(CMD_EVENT_SENSOR);
358 else if(cmd==CMD_EVENT_LOK)
362 unsigned char data[5];
369 else if(cmd==CMD_EVENT_TURNOUT)
373 for(unsigned i=0; i<count; ++i)
375 unsigned char data[2];
378 unsigned addr = data[0]+((data[1]&7)<<8);
379 signal_turnout.emit(addr, (data[1]&0x80)!=0);
382 else if(cmd==CMD_EVENT_SENSOR)
391 unsigned char data[2];
393 for(unsigned i=0; i<16; ++i)
395 unsigned addr = mod*16+i-15;
396 bool state = (data[i/8]>>(7-i%8))&1;
398 Sensor &sensor = sensors[addr];
401 sensor.off_timeout = Time::TimeStamp();
404 sensor.state = state;
405 signal_sensor(addr, state);
408 else if(sensor.state)
409 sensor.off_timeout = t+700*Time::msec;
413 else if(cmd==CMD_LOK)
418 if(err==ERR_NO_ERROR)
420 unsigned addr = queue.front().addr;
421 Locomotive &loco = locos[addr];
422 signal_loco_speed.emit(addr, loco.speed, loco.reverse);
425 else if(cmd==CMD_TURNOUT)
430 unsigned addr = queue.front().addr;
431 Turnout &turnout = turnouts[addr];
433 if(err==ERR_NO_ERROR)
435 turnout.state = turnout.pending;
438 signal_turnout.emit(addr, turnout.state);
439 turnout.off_timeout = t+500*Time::msec;
442 else if(err==ERR_NO_I2C_SPACE)
443 queue.push_back(queue.front());
445 turnout.pending = turnout.state;
447 else if(cmd==CMD_TURNOUT_STATUS)
452 if(err==ERR_NO_ERROR)
457 unsigned addr = queue.front().addr;
458 bool state = data&0x04;
460 Turnout &turnout = turnouts[addr];
461 if(state!=turnout.state)
463 turnout.state = state;
464 signal_turnout.emit(addr, turnout.state);
468 else if(cmd==CMD_LOK_STATUS)
473 if(err==ERR_NO_ERROR)
475 unsigned char data[3];
478 unsigned addr = queue.front().addr;
479 Locomotive &loco = locos[addr];
481 unsigned speed = (data[0]<=1 ? 0 : data[0]*2/19+1);
482 bool reverse = !(data[1]&0x20);
483 if(speed!=loco.speed || reverse!=loco.reverse)
486 loco.reverse = reverse;
487 signal_loco_speed.emit(addr, loco.speed, loco.reverse);
490 unsigned funcs = (data[1]&0xF)<<1;
493 if(funcs!=loco.funcs)
495 unsigned changed = loco.funcs^funcs;
497 for(unsigned i=0; i<5; ++i)
499 signal_loco_function.emit(addr, i, loco.funcs&(1<<i));
505 unsigned expected_bytes = 0;
506 if(cmd==CMD_FUNC_STATUS)
508 if(cmd==CMD_TURNOUT_GROUP_STATUS)
510 if(cmd==CMD_LOK_CONFIG)
516 if(err==ERR_NO_ERROR)
518 unsigned char data[8];
519 read_all(data, expected_bytes);
524 unsigned Intellibox::read_all(unsigned char *buf, unsigned len)
528 pos += read(serial_fd, buf+pos, len-pos);
534 Intellibox::Locomotive::Locomotive():
541 Intellibox::Turnout::Turnout():
547 Intellibox::Sensor::Sensor():
551 } // namespace Marklin