]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/intellibox.cpp
Move double-address logic to drivers
[r2c2.git] / source / libr2c2 / intellibox.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <fcntl.h>
9 #include <termios.h>
10 #include <sys/poll.h>
11 #include <msp/io/print.h>
12 #include <msp/time/units.h>
13 #include <msp/time/utils.h>
14 #include "intellibox.h"
15 #include "tracktype.h"
16 #include "vehicletype.h"
17
18 using namespace std;
19 using namespace Msp;
20
21 namespace R2C2 {
22
23 Intellibox::Intellibox(const string &dev):
24         power(false),
25         halted(false),
26         update_sensors(false),
27         command_sent(false)
28 {
29         serial_fd = ::open(dev.c_str(), O_RDWR);
30         if(serial_fd<0)
31                 throw Exception("Couldn't open serial port\n");
32
33         static unsigned baud[]=
34         {
35                  2400, B2400,
36                  4800, B4800,
37                  9600, B9600,
38                 19200, B19200,
39                 0
40         };
41
42         termios attr;
43         tcgetattr(serial_fd, &attr);
44         cfmakeraw(&attr);
45         attr.c_cflag |= CSTOPB;
46
47         bool ok = false;
48         bool p50 = false;
49         for(unsigned i=0; baud[i]; i+=2)
50         {
51                 cfsetospeed(&attr, baud[i+1]);
52                 tcsetattr(serial_fd, TCSADRAIN, &attr);
53
54                 write(serial_fd, "\xC4", 1);
55
56                 pollfd pfd = { serial_fd, POLLIN, 0 };
57                 if(poll(&pfd, 1, 500)>0)
58                 {
59                         IO::print("IB detected at %d bits/s\n", baud[i]);
60                         char buf[2];
61                         p50 = (read(serial_fd, buf, 2)==2);
62                         ok = true;
63                         break;
64                 }
65         }
66
67         if(!ok)
68                 throw Exception("IB not detected");
69
70         if(p50)
71                 write(serial_fd, "xZzA1\r", 6);
72
73         command(CMD_STATUS);
74 }
75
76 void Intellibox::set_power(bool p)
77 {
78         power = p;
79         if(power)
80                 command(CMD_POWER_ON);
81         else
82                 command(CMD_POWER_OFF);
83         signal_power.emit(power);
84 }
85
86 void Intellibox::halt(bool h)
87 {
88         halted = h;
89         if(halted)
90         {
91                 for(map<unsigned, Locomotive>::iterator i=locos.begin(); i!=locos.end(); ++i)
92                         if(i->second.speed)
93                                 set_loco_speed(i->first, 0);
94         }
95
96         signal_halt.emit(halted);
97 }
98
99 const char *Intellibox::enumerate_protocols(unsigned i) const
100 {
101         ++i;
102         if(i==MM)
103                 return "MM";
104         else if(i==MM_27)
105                 return "MM-27";
106         return 0;
107 }
108
109 unsigned Intellibox::get_protocol_speed_steps(const string &proto_name) const
110 {
111         Protocol proto = map_protocol(proto_name);
112         if(proto==MM)
113                 return 14;
114         else if(proto==MM_27)
115                 return 27;
116         return 0;
117 }
118
119 void Intellibox::add_loco(unsigned addr, const string &proto_name, const VehicleType &type)
120 {
121         Protocol proto = map_protocol(proto_name);
122
123         if(!locos.count(addr))
124         {
125                 Locomotive &loco = locos[addr];
126                 loco.protocol = proto;
127                 if(type.get_max_function()>4)
128                 {
129                         loco.ext_func = true;
130                         locos[addr+1].protocol = NONE;
131                 }
132
133                 unsigned char data[2];
134                 data[0] = addr&0xFF;
135                 data[1] = (addr>>8)&0xFF;
136                 command(CMD_LOK_STATUS, addr, data, 2);
137         }
138 }
139
140 void Intellibox::set_loco_speed(unsigned addr, unsigned speed)
141 {
142         Locomotive &loco = locos[addr];
143         if(loco.protocol==NONE)
144                 return;
145
146         if(speed==loco.speed)
147         {
148                 if(loco.pending_half_step)
149                 {
150                         loco.pending_half_step = 0;
151                         loco.half_step_delay = Time::TimeStamp();
152                         signal_loco_speed.emit(addr, speed, loco.reverse);
153                 }
154                 return;
155         }
156         if(speed && halted)
157                 return;
158
159         if(loco.protocol==MM_27)
160         {
161                 if(speed>27)
162                         speed = 27;
163
164                 if(speed>loco.speed && !(speed&1))
165                 {
166                         loco.pending_half_step = -1;
167                         speed |= 1;
168                 }
169                 else if(speed<loco.speed && (speed&1))
170                 {
171                         loco.pending_half_step = 1;
172                         speed &= ~1;
173                 }
174                 else
175                         loco.pending_half_step = 0;
176                 loco.half_step_delay = Time::TimeStamp();
177
178                 loco_command(addr, (speed+1)/2, loco.reverse, loco.funcs, false);
179         }
180         else if(loco.protocol==MM)
181         {
182                 if(speed>14)
183                         speed = 14;
184
185                 loco_command(addr, speed, loco.reverse, loco.funcs, false);
186         }
187         loco.speed = speed;
188 }
189
190 void Intellibox::set_loco_reverse(unsigned addr, bool rev)
191 {
192         Locomotive &loco = locos[addr];
193         if(loco.protocol==NONE || rev==loco.reverse)
194                 return;
195
196         loco.speed = 0;
197         loco.reverse = rev;
198         loco_command(addr, 0, rev, loco.funcs, false);
199 }
200
201 void Intellibox::set_loco_function(unsigned addr, unsigned func, bool state)
202 {
203         Locomotive &loco = locos[addr];
204         if(loco.protocol==NONE)
205                 return;
206
207         if(state)
208                 loco.funcs |= 1<<func;
209         else
210                 loco.funcs &= ~(1<<func);
211         if(func<=4)
212                 loco_command(addr, loco.speed, loco.reverse, loco.funcs, true);
213         else if(loco.ext_func && func<=8)
214                 loco_command(addr+1, 0, false, (loco.funcs>>4)&0x1E, true);
215         signal_loco_function.emit(addr, func, state);
216 }
217
218 void Intellibox::add_turnout(unsigned addr, const TrackType &type)
219 {
220         if(!turnouts.count(addr))
221         {
222                 Turnout &turnout = turnouts[addr];
223                 turnout.bits = type.get_state_bits();
224
225                 unsigned char data[2];
226                 data[0] = addr&0xFF;
227                 data[1] = (addr>>8)&0xFF;
228                 command(CMD_TURNOUT_STATUS, addr, data, 2);
229                 for(unsigned i=1; i<turnout.bits; ++i)
230                 {
231                         turnouts[addr+i].bits = 0;
232
233                         ++data[0];
234                         if(!data[0])
235                                 ++data[1];
236                         command(CMD_TURNOUT_STATUS, addr+i, data, 2);
237                 }
238         }
239 }
240
241 void Intellibox::set_turnout(unsigned addr, unsigned state)
242 {
243         Turnout &turnout = turnouts[addr];
244         unsigned mask = (1<<turnout.bits)-1;
245         if(((state^turnout.state)&mask)==0 || ((state^turnout.pending)&mask)==0)
246         {
247                 turnout.state = state;
248                 turnout.pending = state;
249                 signal_turnout.emit(addr, state);
250                 return;
251         }
252
253         turnout.state = (turnout.state&mask) | (state&~mask);
254         turnout.pending = state;
255         turnout.active = true;
256         turnout.off_timeout = Time::TimeStamp();
257
258         for(unsigned i=0; i<turnout.bits; ++i)
259                 if((state^turnout.state)&(1<<i))
260                         turnout_command(addr+i, state&(1<<i), true);
261 }
262
263 unsigned Intellibox::get_turnout(unsigned addr) const
264 {
265         map<unsigned, Turnout>::const_iterator i = turnouts.find(addr);
266         if(i!=turnouts.end())
267                 return i->second.state;
268         return 0;
269 }
270
271 void Intellibox::add_sensor(unsigned addr)
272 {
273         if(!sensors.count(addr))
274         {
275                 sensors[addr];
276                 update_sensors = true;
277         }
278 }
279
280 bool Intellibox::get_sensor(unsigned addr) const
281 {
282         map<unsigned, Sensor>::const_iterator i = sensors.find(addr);
283         if(i!=sensors.end())
284                 return i->second.state;
285         return false;
286 }
287
288 void Intellibox::tick()
289 {
290         const Time::TimeStamp t = Time::now();
291
292         if(t>next_event_query)
293         {
294                 next_event_query = t+200*Time::msec;
295                 command(CMD_EVENT);
296         }
297
298         for(map<unsigned, Locomotive>::iterator i=locos.begin(); i!=locos.end(); ++i)
299                 if(i->second.protocol==MM_27 && i->second.pending_half_step && i->second.half_step_delay && t>i->second.half_step_delay)
300                 {
301                         i->second.speed += i->second.pending_half_step;
302                         i->second.pending_half_step = 0;
303                         i->second.half_step_delay = Time::TimeStamp();
304                         loco_command(i->first, (i->second.speed+1)/2, i->second.reverse, i->second.funcs, false);
305                 }
306
307         for(map<unsigned, Turnout>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
308                 if(i->second.active && i->second.off_timeout && t>i->second.off_timeout)
309                 {
310                         i->second.active = false;
311                         i->second.off_timeout = Time::TimeStamp();
312                         turnout_command(i->first, i->second.state, false);
313                 }
314
315         for(map<unsigned, Sensor>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
316                 if(i->second.off_timeout && t>i->second.off_timeout)
317                 {
318                         i->second.state = false;
319                         i->second.off_timeout = Time::TimeStamp();
320                         signal_sensor.emit(i->first, false);
321                 }
322
323         if(update_sensors)
324         {
325                 unsigned max_addr = (--sensors.end())->first;
326                 unsigned char data[2];
327                 data[0] = 0;
328                 data[1] = (max_addr+7)/8;
329                 command(CMD_SENSOR_PARAM_SET, data, 2);
330                 command(CMD_SENSOR_REPORT);
331                 update_sensors = false;
332         }
333
334         if(!queue.empty() && command_sent)
335         {
336                 pollfd pfd = { serial_fd, POLLIN, 0 };
337                 if(poll(&pfd, 1, 0)>0)
338                 {
339                         process_reply(t);
340                         queue.erase(queue.begin());
341                         command_sent = false;
342                 }
343                 else
344                         return;
345         }
346
347         if(!queue.empty())
348         {
349                 const CommandSlot &slot = queue.front();
350                 write(serial_fd, slot.data, slot.length);
351                 command_sent = true;
352         }
353 }
354
355 void Intellibox::flush()
356 {
357         Time::TimeStamp t = Time::now();
358         for(list<CommandSlot>::iterator i=queue.begin(); i!=queue.end(); ++i)
359         {
360                 write(serial_fd, i->data, i->length);
361                 pollfd pfd = { serial_fd, POLLIN, 0 };
362                 bool first = true;
363                 while(poll(&pfd, 1, (first ? -1 : 0))>0)
364                 {
365                         char data[16];
366                         read(serial_fd, data, 16);
367                         first = false;
368                 }
369         }
370
371         queue.clear();
372         command_sent = false;
373 }
374
375 Intellibox::Protocol Intellibox::map_protocol(const string &name) const
376 {
377         if(name=="MM")
378                 return MM;
379         else if(name=="MM-27")
380                 return MM_27;
381         else
382                 throw InvalidParameterValue("Unknown protocol");
383 }
384
385 void Intellibox::command(Command cmd)
386 {
387         command(cmd, 0, 0);
388 }
389
390 void Intellibox::command(Command cmd, const unsigned char *data, unsigned len)
391 {
392         command(cmd, 0, data, len);
393 }
394
395 void Intellibox::command(Command cmd, unsigned addr, const unsigned char *data, unsigned len)
396 {
397         CommandSlot slot;
398         slot.cmd = cmd;
399         slot.addr = addr;
400         slot.data[0] = cmd;
401         copy(data, data+len, slot.data+1);
402         slot.length = 1+len;
403         queue.push_back(slot);
404 }
405
406 void Intellibox::loco_command(unsigned addr, unsigned speed, bool rev, unsigned funcs, bool setf)
407 {
408         unsigned char data[4];
409         data[0] = addr&0xFF;
410         data[1] = (addr>>8)&0xFF;
411
412         if(speed==0)
413                 data[2] = 0;
414         else if(speed==1)
415                 data[2] = 2;
416         else
417                 data[2] = (speed*19-18)/2;
418         
419         data[3] = (rev ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
420
421         if(setf)
422                 data[3] |= 0x80 | ((funcs>>1)&0xF);
423
424         command(CMD_LOK, addr, data, 4);
425 }
426
427 void Intellibox::turnout_command(unsigned addr, bool state, bool active)
428 {
429         unsigned char data[2];
430         data[0] = addr&0xFF;
431         data[1] = ((addr>>8)&0x7) | (active ? 0x40 : 0) | (state ? 0x80 : 0);
432         command(CMD_TURNOUT, addr, data, 2);
433 }
434
435 void Intellibox::process_reply(const Time::TimeStamp &t)
436 {
437         Command cmd = queue.front().cmd;
438
439         if(cmd==CMD_STATUS)
440         {
441                 unsigned char status;
442                 read_all(&status, 1);
443                 power = status&0x08;
444                 signal_power.emit(power);
445         }
446         else if(cmd==CMD_EVENT)
447         {
448                 for(unsigned i=0;; ++i)
449                 {
450                         unsigned char byte;
451                         read_all(&byte, 1);
452
453                         if(i==0)
454                         {
455                                 if(byte&0x01)
456                                         command(CMD_EVENT_LOK);
457                                 if(byte&0x20)
458                                         command(CMD_EVENT_TURNOUT);
459                                 if(byte&0x04)
460                                         command(CMD_EVENT_SENSOR);
461                         }
462                         else if(i==1)
463                         {
464                                 if(byte&0x40)
465                                         command(CMD_STATUS);
466                         }
467
468                         if(!(byte&0x80))
469                                 break;
470                 }
471         }
472         else if(cmd==CMD_EVENT_LOK)
473         {
474                 while(1)
475                 {
476                         unsigned char data[5];
477                         read_all(data, 1);
478                         if(data[0]==0x80)
479                                 break;
480                         read_all(data+1, 4);
481                 }
482         }
483         else if(cmd==CMD_EVENT_TURNOUT)
484         {
485                 unsigned char count;
486                 read_all(&count, 1);
487                 for(unsigned i=0; i<count; ++i)
488                 {
489                         unsigned char data[2];
490                         read_all(data, 2);
491
492                         unsigned addr = data[0]+((data[1]&7)<<8);
493                         Turnout &turnout = turnouts[addr];
494                         turnout.state = (data[1]&0x80)!=0;
495                         turnout.pending = turnout.state;
496                         signal_turnout.emit(addr, turnout.state);
497                 }
498         }
499         else if(cmd==CMD_EVENT_SENSOR)
500         {
501                 while(1)
502                 {
503                         unsigned char mod;
504                         read_all(&mod, 1);
505                         if(!mod)
506                                 break;
507
508                         unsigned char data[2];
509                         read_all(data, 2);
510                         for(unsigned i=0; i<16; ++i)
511                         {
512                                 unsigned addr = mod*16+i-15;
513                                 bool state = (data[i/8]>>(7-i%8))&1;
514
515                                 Sensor &sensor = sensors[addr];
516                                 if(state)
517                                 {
518                                         sensor.off_timeout = Time::TimeStamp();
519                                         if(!sensor.state)
520                                         {
521                                                 sensor.state = state;
522                                                 signal_sensor(addr, state);
523                                         }
524                                 }
525                                 else if(sensor.state)
526                                         sensor.off_timeout = t+700*Time::msec;
527                         }
528                 }
529         }
530         else if(cmd==CMD_LOK)
531         {
532                 Error err;
533                 read_status(&err);
534
535                 if(err==ERR_NO_ERROR)
536                 {
537                         unsigned addr = queue.front().addr;
538                         Locomotive &loco = locos[addr];
539                         if(loco.protocol)
540                         {
541                                 signal_loco_speed.emit(addr, loco.speed+loco.pending_half_step, loco.reverse);
542                                 if(loco.pending_half_step)
543                                         loco.half_step_delay = Time::now()+500*Time::msec;
544                         }
545                 }
546                 else
547                         error(cmd, err);
548         }
549         else if(cmd==CMD_TURNOUT)
550         {
551                 Error err;
552                 read_status(&err);
553
554                 unsigned addr = queue.front().addr;
555                 unsigned mask = 1;
556                 while(!turnouts[addr].bits)
557                 {
558                         --addr;
559                         mask <<= 1;
560                 }
561                 Turnout &turnout = turnouts[addr];
562
563                 if(err==ERR_NO_ERROR)
564                 {
565                         turnout.state = (turnout.state&~mask) | (turnout.pending&mask);
566                         if(turnout.active)
567                         {
568                                 if(turnout.state==turnout.pending)
569                                         signal_turnout.emit(addr, turnout.state);
570                                 turnout.off_timeout = t+500*Time::msec;
571                         }
572                 }
573                 else if(err==ERR_NO_I2C_SPACE)
574                         queue.push_back(queue.front());
575                 else
576                 {
577                         turnout.pending = (turnout.pending&~mask) | (turnout.state&mask);
578                         error(cmd, err);
579                 }
580         }
581         else if(cmd==CMD_TURNOUT_STATUS)
582         {
583                 Error err;
584                 read_status(&err);
585
586                 if(err==ERR_NO_ERROR)
587                 {
588                         unsigned char data;
589                         read_all(&data, 1);
590
591                         unsigned addr = queue.front().addr;
592                         unsigned mask = 1;
593                         while(!turnouts[addr].bits)
594                         {
595                                 --addr;
596                                 mask <<= 1;
597                         }
598                         Turnout &turnout = turnouts[addr];
599
600                         bool state = data&0x04;
601                         if(state!=((turnout.state&mask)!=0))
602                         {
603                                 turnout.state = (turnout.state&~mask) | (state ? mask : 0);
604                                 turnout.pending = turnout.state;
605                                 signal_turnout.emit(addr, turnout.state);
606                         }
607                 }
608                 else
609                         error(cmd, err);
610         }
611         else if(cmd==CMD_LOK_STATUS)
612         {
613                 Error err;
614                 read_status(&err);
615
616                 if(err==ERR_NO_ERROR)
617                 {
618                         unsigned char data[3];
619                         read_all(data, 3);
620
621                         unsigned addr = queue.front().addr;
622                         Locomotive &loco = locos[addr];
623
624                         unsigned speed = (data[0]<=1 ? 0 : data[0]*2/19+1);
625                         bool reverse = !(data[1]&0x20);
626                         if(speed!=loco.speed || reverse!=loco.reverse)
627                         {
628                                 loco.speed = speed;
629                                 loco.reverse = reverse;
630                                 signal_loco_speed.emit(addr, loco.speed, loco.reverse);
631                         }
632
633                         unsigned funcs = (data[1]&0xF)<<1;
634                         if(data[1]&0x10)
635                                 funcs |= 1;
636                         if(funcs!=loco.funcs)
637                         {
638                                 unsigned changed = loco.funcs^funcs;
639                                 loco.funcs = funcs;
640                                 for(unsigned i=0; i<5; ++i)
641                                         if(changed&(1<<i))
642                                                 signal_loco_function.emit(addr, i, loco.funcs&(1<<i));
643                         }
644                 }
645                 else
646                         error(cmd, err);
647         }
648         else
649         {
650                 unsigned expected_bytes = 0;
651                 if(cmd==CMD_FUNC_STATUS)
652                         expected_bytes = 1;
653                 if(cmd==CMD_TURNOUT_GROUP_STATUS)
654                         expected_bytes = 2;
655                 if(cmd==CMD_LOK_CONFIG)
656                         expected_bytes = 4;
657
658                 Error err;
659                 read_status(&err);
660
661                 if(err==ERR_NO_ERROR)
662                 {
663                         unsigned char data[8];
664                         read_all(data, expected_bytes);
665                 }
666                 else
667                         error(cmd, err);
668         }
669 }
670
671 unsigned Intellibox::read_all(unsigned char *buf, unsigned len)
672 {
673         unsigned pos = 0;
674         while(pos<len)
675                 pos += read(serial_fd, buf+pos, len-pos);
676
677         return pos;
678 }
679
680 unsigned Intellibox::read_status(Error *err)
681 {
682         unsigned char c;
683         unsigned ret = read_all(&c, 1);
684         *err = static_cast<Error>(c);
685         return ret;
686 }
687
688 void Intellibox::error(Command cmd, Error err)
689 {
690         const char *cmd_str = 0;
691         switch(cmd)
692         {
693         case CMD_LOK: cmd_str = "CMD_LOK"; break;
694         case CMD_LOK_STATUS: cmd_str = "CMD_LOK_STATUS"; break;
695         case CMD_LOK_CONFIG: cmd_str = "CMD_LOK_CONFIG"; break;
696         case CMD_FUNC: cmd_str = "CMD_FUNC"; break;
697         case CMD_FUNC_STATUS: cmd_str = "CMD_FUNC_STATUS"; break;
698         case CMD_TURNOUT: cmd_str = "CMD_TURNOUT"; break;
699         case CMD_TURNOUT_FREE: cmd_str = "CMD_TURNOUT_FREE"; break;
700         case CMD_TURNOUT_STATUS: cmd_str = "CMD_TURNOUT_STATUS"; break;
701         case CMD_TURNOUT_GROUP_STATUS: cmd_str = "CMD_TURNOUT_GROUP_STATUS"; break;
702         case CMD_SENSOR_STATUS: cmd_str = "CMD_SENSOR_STATUS"; break;
703         case CMD_SENSOR_REPORT: cmd_str = "CMD_SENSOR_REPORT"; break;
704         case CMD_SENSOR_PARAM_SET: cmd_str = "CMD_SENSOR_PARAM_SET"; break;
705         case CMD_STATUS: cmd_str = "CMD_STATUS"; break;
706         case CMD_POWER_OFF: cmd_str = "CMD_POWER_OFF"; break;
707         case CMD_POWER_ON: cmd_str = "CMD_POWER_ON"; break;
708         case CMD_NOP: cmd_str = "CMD_NOP"; break;
709         case CMD_EVENT: cmd_str = "CMD_EVENT"; break;
710         case CMD_EVENT_LOK: cmd_str = "CMD_EVENT_LOK"; break;
711         case CMD_EVENT_TURNOUT: cmd_str = "CMD_EVENT_TURNOUT"; break;
712         case CMD_EVENT_SENSOR: cmd_str = "CMD_EVENT_SENSOR"; break;
713         default: cmd_str = "(unknown command)";
714         }
715
716         const char *err_str = 0;
717         switch(err)
718         {
719         case ERR_NO_ERROR: err_str = "ERR_NO_ERROR"; break;
720         case ERR_SYS_ERROR: err_str = "ERR_SYS_ERROR"; break;
721         case ERR_BAD_PARAM: err_str = "ERR_BAD_PARAM"; break;
722         case ERR_POWER_OFF: err_str = "ERR_POWER_OFF"; break;
723         case ERR_NO_LOK_SPACE: err_str = "ERR_NO_LOK_SPACE"; break;
724         case ERR_NO_TURNOUT_SPACE: err_str = "ERR_NO_TURNOUT_SPACE"; break;
725         case ERR_NO_DATA: err_str = "ERR_NO_DATA"; break;
726         case ERR_NO_SLOT: err_str = "ERR_NO_SLOT"; break;
727         case ERR_BAD_LOK_ADDR: err_str = "ERR_BAD_LOK_ADDR"; break;
728         case ERR_LOK_BUSY: err_str = "ERR_LOK_BUSY"; break;
729         case ERR_BAD_TURNOUT_ADDR: err_str = "ERR_BAD_TURNOUT_ADDR"; break;
730         case ERR_BAD_SO_VALUE: err_str = "ERR_BAD_SO_VALUE"; break;
731         case ERR_NO_I2C_SPACE: err_str = "ERR_NO_I2C_SPACE"; break;
732         case ERR_LOW_TURNOUT_SPACE: err_str = "ERR_LOW_TURNOUT_SPACE"; break;
733         case ERR_LOK_HALTED: err_str = "ERR_LOK_HALTED"; break;
734         case ERR_LOK_POWER_OFF: err_str = "ERR_LOK_POWER_OFF"; break;
735         default: cmd_str = "(unknown error)";
736         }
737
738         IO::print("Error: %s: %s\n", cmd_str, err_str);
739 }
740
741
742 Intellibox::Locomotive::Locomotive():
743         ext_func(false),
744         speed(0),
745         reverse(false),
746         funcs(0)
747 { }
748
749
750 Intellibox::Turnout::Turnout():
751         bits(1),
752         state(0),
753         active(false),
754         pending(false)
755 { }
756
757
758 Intellibox::Sensor::Sensor():
759         state(false)
760 { }
761
762 } // namespace R2C2