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));
44 void Control::set_debug(bool d)
49 void Control::open(const string &dev)
51 serial_fd=::open(dev.c_str(), O_RDWR);
53 throw Exception("Couldn't open serial port\n");
55 static unsigned baud[]=
65 tcgetattr(serial_fd, &attr);
70 for(unsigned i=0; baud[i]; i+=2)
72 cfsetospeed(&attr, baud[i+1]);
73 tcsetattr(serial_fd, TCSADRAIN, &attr);
75 write(serial_fd, "\xC4", 1);
77 pollfd pfd={serial_fd, POLLIN, 0};
78 if(poll(&pfd, 1, 500)>0)
80 cout<<"IB detected at "<<baud[i]<<" bits/s, P50 is ";
82 if(read(serial_fd, buf, 2)==2)
98 throw Exception("IB not detected");
101 Command &Control::command(const string &cmd)
103 queue.push_back(Command(cmd));
107 void Control::add_turnout(Turnout &t)
109 turnouts[t.get_address()]=&t;
112 Turnout &Control::get_turnout(unsigned id) const
114 map<unsigned, Turnout *>::const_iterator i=turnouts.find(id);
115 if(i==turnouts.end())
116 throw KeyError("Unknown turnout");
121 void Control::add_locomotive(Locomotive &l)
123 locomotives[l.get_address()]=&l;
126 Locomotive &Control::get_locomotive(unsigned id) const
128 map<unsigned, Locomotive *>::const_iterator i=locomotives.find(id);
129 if(i==locomotives.end())
130 throw KeyError("Unknown locomotive");
135 void Control::add_sensor(Sensor &s)
137 sensors[s.get_address()]=&s;
141 Sensor &Control::get_sensor(unsigned id) const
143 map<unsigned, Sensor *>::const_iterator i=sensors.find(id);
145 throw KeyError("Unknown sensor");
152 const Time::TimeStamp t=Time::now();
154 for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
159 if(t>next_event_query)
161 next_event_query=t+300*Time::msec;
162 command(string(1, CMD_EVENT)).signal_done.connect(sigc::mem_fun(this, &Control::event_query_done));
167 unsigned max_addr=(--sensors.end())->second->get_address();
169 cmd[0]=CMD_SENSOR_PARAM_SET;
171 cmd[2]=(max_addr+7)/8;
173 command(string(1, CMD_SENSOR_REPORT));
177 if(!queue.empty() && queue.front().get_sent())
179 pollfd pfd={serial_fd, POLLIN, 0};
180 if(poll(&pfd, 1, 0)>0)
182 string resp=read_reply(static_cast<Cmd>(static_cast<unsigned char>(queue.front().get_command()[0])));
186 for(unsigned i=0; i<resp.size(); ++i)
187 printf("%02X ", static_cast<unsigned char>(resp[i]));
188 printf("(%d bytes)\n", resp.size());
191 queue.front().signal_done.emit(static_cast<Error>(resp[0]), resp.substr(1));
192 queue.erase(queue.begin());
200 string cmd=queue.front().get_command();
213 for(unsigned i=0; i<cmd.size(); ++i)
214 printf("%02X ", static_cast<unsigned char>(cmd[i]));
215 printf("(%d bytes)\n", cmd.size());
218 write(serial_fd, cmd.data(), cmd.size());
219 queue.front().set_sent(true);
223 Time::Timer::Slot &Control::set_timer(const Time::TimeDelta &dt)
225 return timer.add(dt);
228 void Control::read_all(int fd, char *buf, int size)
233 int len=read(fd, buf+pos, size-pos);
238 string Control::read_reply(Cmd cmd)
243 result+=ERR_NO_ERROR;
244 for(unsigned i=0; i<3; ++i)
247 read(serial_fd, &c, 1);
252 else if(cmd==CMD_EVENT_LOK)
254 result+=ERR_NO_ERROR;
256 read(serial_fd, c+4, 1);
260 read_all(serial_fd, c, 5);
264 else if(cmd==CMD_EVENT_TURNOUT)
266 result+=ERR_NO_ERROR;
268 read(serial_fd, c, 1);
269 read_all(serial_fd, c+1, c[0]*2);
270 result.append(c, c[0]*2+1);
272 else if(cmd==CMD_EVENT_SENSOR)
274 result+=ERR_NO_ERROR;
276 read(serial_fd, c+2, 1);
280 read_all(serial_fd, c, 3);
286 unsigned expected_bytes=1;
287 if(cmd==CMD_FUNC_STATUS || cmd==CMD_TURNOUT_STATUS)
289 if(cmd==CMD_SENSOR_STATUS || cmd==CMD_TURNOUT_GROUP_STATUS)
291 if(cmd==CMD_LOK_STATUS)
293 if(cmd==CMD_LOK_CONFIG)
296 read_all(serial_fd, c, 1);
300 read_all(serial_fd, c+1, expected_bytes-1);
301 result.append(c+1, expected_bytes-1);
308 void Control::event_query_done(Error, const string &resp)
311 command(string(1, CMD_EVENT_TURNOUT)).signal_done.connect(sigc::mem_fun(this, &Control::turnout_event_done));
313 command(string(1, CMD_EVENT_SENSOR)).signal_done.connect(sigc::mem_fun(this, &Control::sensor_event_done));
316 void Control::turnout_event_done(Error, const string &resp)
318 unsigned count=resp[0];
319 for(unsigned i=0; i<count; ++i)
321 unsigned addr=static_cast<unsigned char>(resp[i*2+1])+((resp[i*2+2]&7)<<8);
322 bool status=!(resp[i*2+2]&0x80);
323 cout<<"Turnout "<<addr<<", status "<<status<<'\n';
324 signal_turnout_event.emit(addr, status);
328 void Control::sensor_event_done(Error, const string &resp)
330 for(unsigned i=0; resp[i]; i+=3)
332 unsigned module=static_cast<unsigned char>(resp[i]);
334 cout<<"S88 module "<<module<<", status ";
335 for(unsigned j=0; j<16; ++j)
336 cout<<((resp[i+1+j/8]>>(7-j%8))&1);
339 for(unsigned j=0; j<16; ++j)
340 signal_sensor_event.emit(module*16+j-15, (resp[i+1+j/8]>>(7-j%8))&1);
344 } // namespace Marklin