5 #include <msp/core/except.h>
6 #include <msp/time/units.h>
7 #include <msp/time/utils.h>
24 void Control::set_power(bool p)
28 command(string(1, CMD_POWER_ON));
30 command(string(1, CMD_POWER_OFF));
33 void Control::open(const string &dev)
35 serial_fd=::open(dev.c_str(), O_RDWR);
37 throw Exception("Couldn't open serial port\n");
39 static unsigned baud[]=
49 tcgetattr(serial_fd, &attr);
54 for(unsigned i=0; baud[i]; i+=2)
56 cfsetospeed(&attr, baud[i+1]);
57 tcsetattr(serial_fd, TCSADRAIN, &attr);
59 write(serial_fd, "\xC4", 1);
61 pollfd pfd={serial_fd, POLLIN, 0};
62 if(poll(&pfd, 1, 500)>0)
64 cout<<"IB detected at "<<baud[i]<<" bits/s, P50 is ";
66 if(read(serial_fd, buf, 2)==2)
82 throw Exception("IB not detected");
85 Command &Control::command(const string &cmd)
87 queue.push_back(Command(cmd));
91 void Control::add_turnout(Turnout &t)
93 turnouts[t.get_address()]=&t;
96 Turnout &Control::get_turnout(unsigned id) const
98 map<unsigned, Turnout *>::const_iterator i=turnouts.find(id);
100 throw KeyError("Unknown turnout");
105 void Control::add_locomotive(Locomotive &l)
107 locomotives[l.get_address()]=&l;
110 Locomotive &Control::get_locomotive(unsigned id) const
112 map<unsigned, Locomotive *>::const_iterator i=locomotives.find(id);
113 if(i==locomotives.end())
114 throw KeyError("Unknown locomotive");
119 void Control::add_sensor(Sensor &s)
121 sensors[s.get_address()]=&s;
125 Sensor &Control::get_sensor(unsigned id) const
127 map<unsigned, Sensor *>::const_iterator i=sensors.find(id);
129 throw KeyError("Unknown sensor");
136 const Time::TimeStamp t=Time::now();
138 for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
141 if(t>next_event_query)
143 next_event_query=t+300*Time::msec;
144 command(string(1, CMD_EVENT)).signal_done.connect(sigc::mem_fun(this, &Control::event_query_done));
149 unsigned max_addr=(--sensors.end())->second->get_address();
151 cmd[0]=CMD_SENSOR_PARAM_SET;
153 cmd[2]=(max_addr+7)/8;
155 command(string(1, CMD_SENSOR_REPORT));
159 if(queue.size() && queue.front().get_sent())
161 pollfd pfd={serial_fd, POLLIN, 0};
162 if(poll(&pfd, 1, 0)>0)
164 string resp=read_reply((Cmd)(unsigned char)queue.front().get_command()[0]);
168 for(unsigned i=0; i<resp.size(); ++i)
169 printf("%02X ", (unsigned char)resp[i]);
170 printf("(%d bytes)\n", resp.size());
173 queue.front().signal_done.emit((Error)resp[0], resp.substr(1));
174 queue.erase(queue.begin());
182 string cmd=queue.front().get_command();
195 for(unsigned i=0; i<cmd.size(); ++i)
196 printf("%02X ",(unsigned char)cmd[i]);
197 printf("(%d bytes)\n",cmd.size());
200 write(serial_fd, cmd.data(), cmd.size());
201 queue.front().set_sent(true);
205 Time::Timer::Slot &Control::set_timer(const Time::TimeDelta &dt)
207 return timer.add(dt);
212 for(map<unsigned, Sensor *>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
214 for(map<unsigned, Turnout *>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
216 for(map<unsigned, Locomotive *>::iterator i=locomotives.begin(); i!=locomotives.end(); ++i)
223 void Control::read_all(int fd, char *buf, int size)
228 int len=read(fd, buf+pos, size-pos);
233 string Control::read_reply(Cmd cmd)
238 result+=ERR_NO_ERROR;
239 for(unsigned i=0; i<3; ++i)
242 read(serial_fd, &c, 1);
247 else if(cmd==CMD_EVENT_LOK)
249 result+=ERR_NO_ERROR;
251 read(serial_fd, c+4, 1);
255 read_all(serial_fd, c, 5);
259 else if(cmd==CMD_EVENT_TURNOUT)
261 result+=ERR_NO_ERROR;
263 read(serial_fd, c, 1);
264 read_all(serial_fd, c+1, c[0]*2);
265 result.append(c, c[0]*2+1);
267 else if(cmd==CMD_EVENT_SENSOR)
269 result+=ERR_NO_ERROR;
271 read(serial_fd, c+2, 1);
275 read_all(serial_fd, c, 3);
281 unsigned expected_bytes=1;
282 if(cmd==CMD_FUNC_STATUS || cmd==CMD_TURNOUT_STATUS)
284 if(cmd==CMD_SENSOR_STATUS || cmd==CMD_TURNOUT_GROUP_STATUS)
286 if(cmd==CMD_LOK_STATUS)
288 if(cmd==CMD_LOK_CONFIG)
291 read_all(serial_fd, c, 1);
295 read_all(serial_fd, c+1, expected_bytes-1);
296 result.append(c+1, expected_bytes-1);
303 void Control::event_query_done(Error, const string &resp)
306 command(string(1, CMD_EVENT_TURNOUT)).signal_done.connect(sigc::mem_fun(this, &Control::turnout_event_done));
308 command(string(1, CMD_EVENT_SENSOR)).signal_done.connect(sigc::mem_fun(this, &Control::sensor_event_done));
311 void Control::turnout_event_done(Error, const string &resp)
313 unsigned count=resp[0];
314 for(unsigned i=0; i<count; ++i)
316 unsigned addr=(unsigned char)resp[i*2+1]+((resp[i*2+2]&7)<<8);
317 bool status=!(resp[i*2+2]&0x80);
318 cout<<"Turnout "<<addr<<", status "<<status<<'\n';
319 signal_turnout_event.emit(addr, status);
323 void Control::sensor_event_done(Error, const string &resp)
325 for(unsigned i=0; resp[i]; i+=3)
327 unsigned module=(unsigned char)resp[i];
328 cout<<"S88 module "<<module<<", status ";
329 for(unsigned j=0; j<16; ++j)
331 bool status=(resp[i+1+j/8]>>(7-j%8))&1;
333 signal_sensor_event.emit(module*16+j-15, status);
339 } // namespace Marklin