3 This file is part of the MSP Märklin suite
4 Copyright © 2007-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
12 #include <msp/core/except.h>
13 #include <msp/time/units.h>
14 #include <msp/time/utils.h>
33 for(map<unsigned, Sensor *>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
35 for(map<unsigned, Turnout *>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
37 for(map<unsigned, Locomotive *>::iterator i=locomotives.begin(); i!=locomotives.end(); ++i)
43 void Control::open(const string &dev)
45 serial_fd = ::open(dev.c_str(), O_RDWR);
47 throw Exception("Couldn't open serial port\n");
49 static unsigned baud[]=
59 tcgetattr(serial_fd, &attr);
61 attr.c_cflag |= CSTOPB;
65 for(unsigned i=0; baud[i]; i+=2)
67 cfsetospeed(&attr, baud[i+1]);
68 tcsetattr(serial_fd, TCSADRAIN, &attr);
70 write(serial_fd, "\xC4", 1);
72 pollfd pfd = { serial_fd, POLLIN, 0 };
73 if(poll(&pfd, 1, 500)>0)
75 cout<<"IB detected at "<<baud[i]<<" bits/s\n";
77 p50 = (read(serial_fd, buf, 2)==2);
84 throw Exception("IB not detected");
87 write(serial_fd, "xZzA1\r", 6);
89 command(CMD_STATUS).signal_done.connect(sigc::mem_fun(this, &Control::status_done));
92 void Control::set_debug(bool d)
97 void Control::set_power(bool p)
101 command(CMD_POWER_ON);
103 command(CMD_POWER_OFF);
105 signal_power_event.emit(power);
108 Command &Control::command(Cmd cmd)
110 queue.push_back(Command(cmd, 0, 0));
114 Command &Control::command(Cmd cmd, unsigned char data)
116 queue.push_back(Command(cmd, &data, 1));
120 Command &Control::command(Cmd cmd, const unsigned char *data, unsigned len)
122 queue.push_back(Command(cmd, data, len));
126 void Control::add_turnout(Turnout &t)
128 turnouts[t.get_address()] = &t;
131 Turnout &Control::get_turnout(unsigned id) const
133 map<unsigned, Turnout *>::const_iterator i = turnouts.find(id);
134 if(i==turnouts.end())
135 throw KeyError("Unknown turnout");
140 void Control::add_locomotive(Locomotive &l)
142 locomotives[l.get_address()] = &l;
145 Locomotive &Control::get_locomotive(unsigned id) const
147 map<unsigned, Locomotive *>::const_iterator i = locomotives.find(id);
148 if(i==locomotives.end())
149 throw KeyError("Unknown locomotive");
154 void Control::add_sensor(Sensor &s)
156 sensors[s.get_address()] = &s;
160 Sensor &Control::get_sensor(unsigned id) const
162 map<unsigned, Sensor *>::const_iterator i = sensors.find(id);
164 throw KeyError("Unknown sensor");
171 const Time::TimeStamp t = Time::now();
173 for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
178 if(t>next_event_query)
180 next_event_query = t+200*Time::msec;
181 command(CMD_EVENT).signal_done.connect(sigc::mem_fun(this, &Control::event_query_done));
186 unsigned max_addr = (--sensors.end())->first;
187 unsigned char data[2];
189 data[1] = (max_addr+7)/8;
190 command(CMD_SENSOR_PARAM_SET, data, 2);
191 command(CMD_SENSOR_REPORT);
192 poll_sensors = false;
195 if(!queue.empty() && queue.front().is_sent())
197 pollfd pfd = { serial_fd, POLLIN, 0 };
198 if(poll(&pfd, 1, 0)>0)
200 Reply reply = Reply::read(serial_fd, queue.front().get_command());
202 cout<<"R: "<<reply<<'\n';
204 queue.front().signal_done.emit(reply);
205 queue.erase(queue.begin());
214 cout<<"W: "<<queue.front()<<'\n';
217 queue.front().send(serial_fd);
220 Reply reply = Reply::simulate(queue.front().get_command());
221 queue.front().signal_done.emit(reply);
222 queue.erase(queue.begin());
227 Time::Timer::Slot &Control::set_timer(const Time::TimeDelta &dt)
229 return timer.add(dt);
232 void Control::status_done(const Reply &reply)
234 power = ((reply.get_data()[0]&0x08)!=0);
235 signal_power_event.emit(power);
238 void Control::event_query_done(const Reply &reply)
240 const unsigned char *data = reply.get_data();
242 command(CMD_EVENT_LOK);
244 command(CMD_EVENT_TURNOUT).signal_done.connect(sigc::mem_fun(this, &Control::turnout_event_done));
246 command(CMD_EVENT_SENSOR).signal_done.connect(sigc::mem_fun(this, &Control::sensor_event_done));
247 if((data[0]&0x80) && (data[1]&0x40))
248 command(CMD_STATUS).signal_done.connect(sigc::mem_fun(this, &Control::status_done));
251 void Control::turnout_event_done(const Reply &reply)
253 const unsigned char *data = reply.get_data();
254 unsigned count = data[0];
255 for(unsigned i=0; i<count; ++i)
257 unsigned addr = (data[i*2+1])+((data[i*2+2]&7)<<8);
258 bool status = !(data[i*2+2]&0x80);
259 cout<<"Turnout "<<addr<<", status "<<status<<'\n';
260 signal_turnout_event.emit(addr, status);
264 void Control::sensor_event_done(const Reply &reply)
266 const unsigned char *data = reply.get_data();
267 for(unsigned i=0; data[i]; i+=3)
269 unsigned module = data[i];
271 cout<<"S88 module "<<module<<", status ";
272 for(unsigned j=0; j<16; ++j)
273 cout<<((data[i+1+j/8]>>(7-j%8))&1);
276 for(unsigned j=0; j<16; ++j)
277 signal_sensor_event.emit(module*16+j-15, (data[i+1+j/8]>>(7-j%8))&1);
281 } // namespace Marklin