1 #include <msp/net/resolve.h>
4 #include <msp/io/print.h>
10 Serial::Serial(int, char **argv):
17 DataFile::load(catalogue, "locos.dat");
19 client.use_event_dispatcher(event_disp);
20 client.signal_train_added.connect(sigc::mem_fun(this, &Serial::train_added));
21 client.signal_error.connect(sigc::mem_fun(this, &Serial::error));
23 string addr_str = argv[1];
24 if(addr_str.find(':')==string::npos)
26 Net::SockAddr *addr = Net::resolve(addr_str);
27 client.connect(*addr);
30 serial_port.set_parameters("9600,8N1");
31 event_disp.add(serial_port);
32 serial_port.signal_data_available.connect(sigc::mem_fun(this, &Serial::data_available));
40 void Serial::train_added(NetTrain &t)
46 void Serial::error(const string &e)
51 void Serial::set_train(NetTrain *t)
54 serial_port.write(format("A%02d", train->get_address()));
57 void Serial::next_train()
59 const map<unsigned, NetTrain *> &trains = client.get_trains();
61 map<unsigned, NetTrain *>::const_iterator i = trains.find(train->get_address());
69 void Serial::prev_train()
71 const map<unsigned, NetTrain *> &trains = client.get_trains();
73 map<unsigned, NetTrain *>::const_iterator i = trains.find(train->get_address());
81 void Serial::data_available()
84 serial_port.read(&c, 1);
87 if(c=='S' || c=='H' || c=='L')
88 rx_buf[rx_fill++] = c;
91 IO::print("Reverse\n");
93 train->set_control("speed", 0);
102 rx_buf[rx_fill++] = c;
103 if(rx_buf[0]=='H' && rx_fill==2)
105 train->set_function(rx_buf[1]-'0', true);
106 IO::print("Func %d on\n", rx_buf[1]-'0');
109 else if(rx_buf[0]=='L' && rx_fill==2)
111 train->set_function(rx_buf[1]-'0', false);
112 IO::print("Func %d off\n", rx_buf[1]-'0');
115 else if(rx_buf[0]=='S' && rx_fill==3)
117 // XXX The firmware is still coded for speed step based control
118 float speed = ((rx_buf[1]-'0')*10+(rx_buf[2]-'0'))*10/3.6*catalogue.get_scale();
119 IO::print("Set speed %g\n", speed);
120 train->set_control("speed", speed);