5 #include <msp/core/except.h>
6 #include <msp/time/units.h>
7 #include <msp/time/utils.h>
26 for(map<unsigned, Sensor *>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
28 for(map<unsigned, Turnout *>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
30 for(map<unsigned, Locomotive *>::iterator i=locomotives.begin(); i!=locomotives.end(); ++i)
35 void Control::set_power(bool p)
39 command(string(1, CMD_POWER_ON));
41 command(string(1, CMD_POWER_OFF));
43 signal_power_event.emit(power);
46 void Control::set_debug(bool d)
51 void Control::open(const string &dev)
53 serial_fd=::open(dev.c_str(), O_RDWR);
55 throw Exception("Couldn't open serial port\n");
57 static unsigned baud[]=
67 tcgetattr(serial_fd, &attr);
72 for(unsigned i=0; baud[i]; i+=2)
74 cfsetospeed(&attr, baud[i+1]);
75 tcsetattr(serial_fd, TCSADRAIN, &attr);
77 write(serial_fd, "\xC4", 1);
79 pollfd pfd={serial_fd, POLLIN, 0};
80 if(poll(&pfd, 1, 500)>0)
82 cout<<"IB detected at "<<baud[i]<<" bits/s, P50 is ";
84 if(read(serial_fd, buf, 2)==2)
101 throw Exception("IB not detected");
103 command(string(1, CMD_STATUS)).signal_done.connect(sigc::mem_fun(this, &Control::status_done));
106 Command &Control::command(const string &cmd)
108 queue.push_back(Command(cmd));
112 void Control::add_turnout(Turnout &t)
114 turnouts[t.get_address()]=&t;
117 Turnout &Control::get_turnout(unsigned id) const
119 map<unsigned, Turnout *>::const_iterator i=turnouts.find(id);
120 if(i==turnouts.end())
121 throw KeyError("Unknown turnout");
126 void Control::add_locomotive(Locomotive &l)
128 locomotives[l.get_address()]=&l;
131 Locomotive &Control::get_locomotive(unsigned id) const
133 map<unsigned, Locomotive *>::const_iterator i=locomotives.find(id);
134 if(i==locomotives.end())
135 throw KeyError("Unknown locomotive");
140 void Control::add_sensor(Sensor &s)
142 sensors[s.get_address()]=&s;
146 Sensor &Control::get_sensor(unsigned id) const
148 map<unsigned, Sensor *>::const_iterator i=sensors.find(id);
150 throw KeyError("Unknown sensor");
157 const Time::TimeStamp t=Time::now();
159 for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
164 if(t>next_event_query)
166 next_event_query=t+200*Time::msec;
167 command(string(1, CMD_EVENT)).signal_done.connect(sigc::mem_fun(this, &Control::event_query_done));
172 unsigned max_addr=(--sensors.end())->second->get_address();
174 cmd[0]=CMD_SENSOR_PARAM_SET;
176 cmd[2]=(max_addr+7)/8;
178 command(string(1, CMD_SENSOR_REPORT));
182 if(!queue.empty() && queue.front().get_sent())
184 pollfd pfd={serial_fd, POLLIN, 0};
185 if(poll(&pfd, 1, 0)>0)
187 string resp=read_reply(static_cast<Cmd>(static_cast<unsigned char>(queue.front().get_command()[0])));
191 for(unsigned i=0; i<resp.size(); ++i)
192 printf("%02X ", static_cast<unsigned char>(resp[i]));
193 printf("(%d bytes)\n", resp.size());
196 queue.front().signal_done.emit(static_cast<Error>(resp[0]), resp.substr(1));
197 queue.erase(queue.begin());
205 string cmd=queue.front().get_command();
218 for(unsigned i=0; i<cmd.size(); ++i)
219 printf("%02X ", static_cast<unsigned char>(cmd[i]));
220 printf("(%d bytes)\n", cmd.size());
223 write(serial_fd, cmd.data(), cmd.size());
224 queue.front().set_sent(true);
228 Time::Timer::Slot &Control::set_timer(const Time::TimeDelta &dt)
230 return timer.add(dt);
233 void Control::read_all(int fd, char *buf, int size)
238 int len=read(fd, buf+pos, size-pos);
243 string Control::read_reply(Cmd cmd)
248 result+=ERR_NO_ERROR;
249 for(unsigned i=0; i<3; ++i)
252 read(serial_fd, &c, 1);
257 else if(cmd==CMD_EVENT_LOK)
259 result+=ERR_NO_ERROR;
261 read(serial_fd, c+4, 1);
265 read_all(serial_fd, c, 5);
269 else if(cmd==CMD_EVENT_TURNOUT)
271 result+=ERR_NO_ERROR;
273 read(serial_fd, c, 1);
274 read_all(serial_fd, c+1, c[0]*2);
275 result.append(c, c[0]*2+1);
277 else if(cmd==CMD_EVENT_SENSOR)
279 result+=ERR_NO_ERROR;
281 read(serial_fd, c+2, 1);
285 read_all(serial_fd, c, 3);
292 result+=ERR_NO_ERROR;
294 unsigned expected_bytes=1;
295 if(cmd==CMD_FUNC_STATUS || cmd==CMD_TURNOUT_STATUS)
297 if(cmd==CMD_SENSOR_STATUS || cmd==CMD_TURNOUT_GROUP_STATUS)
299 if(cmd==CMD_LOK_STATUS)
301 if(cmd==CMD_LOK_CONFIG)
304 read_all(serial_fd, c, 1);
308 read_all(serial_fd, c+1, expected_bytes-1);
309 result.append(c+1, expected_bytes-1);
316 void Control::status_done(Error, const string &resp)
318 power=((resp[0]&0x08)!=0);
319 signal_power_event.emit(power);
322 void Control::event_query_done(Error, const string &resp)
325 command(string(1, CMD_EVENT_TURNOUT)).signal_done.connect(sigc::mem_fun(this, &Control::turnout_event_done));
327 command(string(1, CMD_EVENT_SENSOR)).signal_done.connect(sigc::mem_fun(this, &Control::sensor_event_done));
328 if(resp.size()>1 && (resp[1]&0x40))
329 command(string(1, CMD_STATUS)).signal_done.connect(sigc::mem_fun(this, &Control::status_done));
332 void Control::turnout_event_done(Error, const string &resp)
334 unsigned count=resp[0];
335 for(unsigned i=0; i<count; ++i)
337 unsigned addr=static_cast<unsigned char>(resp[i*2+1])+((resp[i*2+2]&7)<<8);
338 bool status=!(resp[i*2+2]&0x80);
339 cout<<"Turnout "<<addr<<", status "<<status<<'\n';
340 signal_turnout_event.emit(addr, status);
344 void Control::sensor_event_done(Error, const string &resp)
346 for(unsigned i=0; resp[i]; i+=3)
348 unsigned module=static_cast<unsigned char>(resp[i]);
350 cout<<"S88 module "<<module<<", status ";
351 for(unsigned j=0; j<16; ++j)
352 cout<<((resp[i+1+j/8]>>(7-j%8))&1);
355 for(unsigned j=0; j<16; ++j)
356 signal_sensor_event.emit(module*16+j-15, (resp[i+1+j/8]>>(7-j%8))&1);
360 } // namespace Marklin