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):
23 update_sensors(false),
26 serial_fd = ::open(dev.c_str(), O_RDWR);
28 throw Exception("Couldn't open serial port\n");
30 static unsigned baud[]=
40 tcgetattr(serial_fd, &attr);
42 attr.c_cflag |= CSTOPB;
46 for(unsigned i=0; baud[i]; i+=2)
48 cfsetospeed(&attr, baud[i+1]);
49 tcsetattr(serial_fd, TCSADRAIN, &attr);
51 write(serial_fd, "\xC4", 1);
53 pollfd pfd = { serial_fd, POLLIN, 0 };
54 if(poll(&pfd, 1, 500)>0)
56 IO::print("IB detected at %d bits/s\n", baud[i]);
58 p50 = (read(serial_fd, buf, 2)==2);
65 throw Exception("IB not detected");
68 write(serial_fd, "xZzA1\r", 6);
73 void Intellibox::set_power(bool p)
77 command(CMD_POWER_ON);
79 command(CMD_POWER_OFF);
80 signal_power.emit(power);
83 void Intellibox::add_loco(unsigned addr)
85 if(!locos.count(addr))
89 unsigned char data[2];
91 data[1] = (addr>>8)&0xFF;
92 command(CMD_LOK_STATUS, addr, data, 2);
96 void Intellibox::set_loco_speed(unsigned addr, unsigned speed)
98 Locomotive &loco = locos[addr];
103 loco_command(addr, speed, loco.reverse, loco.funcs|0x100);
106 void Intellibox::set_loco_reverse(unsigned addr, bool rev)
108 Locomotive &loco = locos[addr];
109 if(rev==loco.reverse)
113 loco_command(addr, loco.speed, rev, loco.funcs|0x100);
116 void Intellibox::set_loco_function(unsigned addr, unsigned func, bool state)
118 Locomotive &loco = locos[addr];
120 loco.funcs |= 1<<func;
122 loco.funcs &= ~(1<<func);
123 loco_command(addr, loco.speed, loco.reverse, loco.funcs);
124 signal_loco_function.emit(addr, func, state);
127 void Intellibox::add_turnout(unsigned addr)
129 if(!turnouts.count(addr))
133 unsigned char data[2];
135 data[1] = (addr>>8)&0xFF;
136 command(CMD_TURNOUT_STATUS, addr, data, 2);
140 void Intellibox::set_turnout(unsigned addr, bool state)
142 Turnout &turnout = turnouts[addr];
143 if(state==turnout.state || state==turnout.pending)
146 turnout.pending = state;
147 turnout.active = true;
149 turnout_command(addr, state, true);
152 bool Intellibox::get_turnout(unsigned addr) const
154 map<unsigned, Turnout>::const_iterator i = turnouts.find(addr);
155 if(i!=turnouts.end())
156 return i->second.state;
160 void Intellibox::add_sensor(unsigned addr)
162 if(!sensors.count(addr))
165 update_sensors = true;
169 bool Intellibox::get_sensor(unsigned addr) const
171 map<unsigned, Sensor>::const_iterator i = sensors.find(addr);
173 return i->second.state;
177 void Intellibox::tick()
179 const Time::TimeStamp t = Time::now();
181 if(t>next_event_query)
183 next_event_query = t+200*Time::msec;
187 for(map<unsigned, Turnout>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
188 if(i->second.active && i->second.off_timeout && t>i->second.off_timeout)
190 i->second.active = false;
191 i->second.off_timeout = Time::TimeStamp();
192 turnout_command(i->first, i->second.state, false);
195 for(map<unsigned, Sensor>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
196 if(i->second.off_timeout && t>i->second.off_timeout)
198 i->second.state = false;
199 i->second.off_timeout = Time::TimeStamp();
200 signal_sensor.emit(i->first, false);
205 unsigned max_addr = (--sensors.end())->first;
206 unsigned char data[2];
208 data[1] = (max_addr+7)/8;
209 command(CMD_SENSOR_PARAM_SET, data, 2);
210 command(CMD_SENSOR_REPORT);
211 update_sensors = false;
214 if(!queue.empty() && command_sent)
216 pollfd pfd = { serial_fd, POLLIN, 0 };
217 if(poll(&pfd, 1, 0)>0)
220 queue.erase(queue.begin());
221 command_sent = false;
229 const CommandSlot &slot = queue.front();
230 write(serial_fd, slot.data, slot.length);
235 void Intellibox::flush()
237 Time::TimeStamp t = Time::now();
238 for(list<CommandSlot>::iterator i=queue.begin(); i!=queue.end(); ++i)
240 write(serial_fd, i->data, i->length);
241 pollfd pfd = { serial_fd, POLLIN, 0 };
243 while(poll(&pfd, 1, (first ? -1 : 0))>0)
246 read(serial_fd, data, 16);
252 command_sent = false;
255 void Intellibox::command(Command cmd)
260 void Intellibox::command(Command cmd, const unsigned char *data, unsigned len)
262 command(cmd, 0, data, len);
265 void Intellibox::command(Command cmd, unsigned addr, const unsigned char *data, unsigned len)
271 copy(data, data+len, slot.data+1);
273 queue.push_back(slot);
276 void Intellibox::loco_command(unsigned addr, unsigned speed, bool rev, unsigned funcs)
278 unsigned char data[4];
280 data[1] = (addr>>8)&0xFF;
287 data[2] = (speed*19-18)/2;
289 data[3] = (rev ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
292 data[3] |= 0x80 | ((funcs>>1)&0xF);
294 command(CMD_LOK, addr, data, 4);
297 void Intellibox::turnout_command(unsigned addr, bool state, bool active)
299 unsigned char data[2];
301 data[1] = ((addr>>8)&0x7) | (active ? 0x40 : 0) | (state ? 0x80 : 0);
302 command(CMD_TURNOUT, addr, data, 2);
305 void Intellibox::process_reply(const Time::TimeStamp &t)
307 Command cmd = queue.front().cmd;
311 unsigned char status;
312 read_all(&status, 1);
314 signal_power.emit(power);
316 else if(cmd==CMD_EVENT)
318 for(unsigned i=0;; ++i)
326 command(CMD_EVENT_LOK);
328 command(CMD_EVENT_TURNOUT);
330 command(CMD_EVENT_SENSOR);
342 else if(cmd==CMD_EVENT_LOK)
346 unsigned char data[5];
353 else if(cmd==CMD_EVENT_TURNOUT)
357 for(unsigned i=0; i<count; ++i)
359 unsigned char data[2];
362 unsigned addr = data[0]+((data[1]&7)<<8);
363 signal_turnout.emit(addr, (data[1]&0x80)!=0);
366 else if(cmd==CMD_EVENT_SENSOR)
375 unsigned char data[2];
377 for(unsigned i=0; i<16; ++i)
379 unsigned addr = mod*16+i-15;
380 bool state = (data[i/8]>>(7-i%8))&1;
382 Sensor &sensor = sensors[addr];
385 sensor.off_timeout = Time::TimeStamp();
388 sensor.state = state;
389 signal_sensor(addr, state);
392 else if(sensor.state)
393 sensor.off_timeout = t+700*Time::msec;
397 else if(cmd==CMD_LOK)
402 if(err==ERR_NO_ERROR)
404 unsigned addr = queue.front().addr;
405 Locomotive &loco = locos[addr];
406 signal_loco_speed.emit(addr, loco.speed, loco.reverse);
409 else if(cmd==CMD_TURNOUT)
414 if(err==ERR_NO_ERROR)
416 unsigned addr = queue.front().addr;
417 Turnout &turnout = turnouts[addr];
418 turnout.state = turnout.pending;
421 signal_turnout.emit(addr, turnout.state);
422 turnout.off_timeout = t+500*Time::msec;
425 else if(err==ERR_NO_I2C_SPACE)
426 queue.push_back(queue.front());
428 else if(cmd==CMD_TURNOUT_STATUS)
433 if(err==ERR_NO_ERROR)
438 unsigned addr = queue.front().addr;
439 bool state = data&0x04;
441 Turnout &turnout = turnouts[addr];
442 if(state!=turnout.state)
444 turnout.state = state;
445 signal_turnout.emit(addr, turnout.state);
449 else if(cmd==CMD_LOK_STATUS)
454 if(err==ERR_NO_ERROR)
456 unsigned char data[3];
459 unsigned addr = queue.front().addr;
460 Locomotive &loco = locos[addr];
462 unsigned speed = (data[0]<=1 ? 0 : data[0]*2/19+1);
463 bool reverse = !(data[1]&0x20);
464 if(speed!=loco.speed || reverse!=loco.reverse)
467 loco.reverse = reverse;
468 signal_loco_speed.emit(addr, loco.speed, loco.reverse);
471 unsigned funcs = (data[1]&0xF)<<1;
474 if(funcs!=loco.funcs)
476 unsigned changed = loco.funcs^funcs;
478 for(unsigned i=0; i<5; ++i)
480 signal_loco_function.emit(addr, i, loco.funcs&(1<<i));
486 unsigned expected_bytes = 0;
487 if(cmd==CMD_FUNC_STATUS)
489 if(cmd==CMD_TURNOUT_GROUP_STATUS)
491 if(cmd==CMD_LOK_CONFIG)
497 if(err==ERR_NO_ERROR)
499 unsigned char data[8];
500 read_all(data, expected_bytes);
505 unsigned Intellibox::read_all(unsigned char *buf, unsigned len)
509 pos += read(serial_fd, buf+pos, len-pos);
515 Intellibox::Locomotive::Locomotive():
522 Intellibox::Turnout::Turnout():
528 Intellibox::Sensor::Sensor():
532 } // namespace Marklin