3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include <msp/net/resolve.h>
11 #include <msp/io/print.h>
15 using namespace Marklin;
17 Application::RegApp<Serial> Serial::reg;
19 Serial::Serial(int, char **argv):
26 DataFile::load(catalogue, "locos.dat");
28 client.use_event_dispatcher(event_disp);
29 client.signal_train_added.connect(sigc::mem_fun(this, &Serial::train_added));
30 client.signal_error.connect(sigc::mem_fun(this, &Serial::error));
32 string addr_str = argv[1];
33 if(addr_str.find(':')==string::npos)
35 Net::SockAddr *addr = Net::resolve(addr_str);
36 client.connect(*addr);
39 serial_port.set_parameters("9600,8N1");
40 event_disp.add(serial_port);
41 serial_port.signal_data_available.connect(sigc::mem_fun(this, &Serial::data_available));
49 void Serial::train_added(NetTrain &t)
55 void Serial::error(const string &e)
60 void Serial::set_train(NetTrain *t)
63 serial_port.write(format("A%02d", train->get_address()));
66 void Serial::next_train()
68 const map<unsigned, NetTrain *> &trains = client.get_trains();
70 map<unsigned, NetTrain *>::const_iterator i = trains.find(train->get_address());
78 void Serial::prev_train()
80 const map<unsigned, NetTrain *> &trains = client.get_trains();
82 map<unsigned, NetTrain *>::const_iterator i = trains.find(train->get_address());
90 void Serial::data_available()
93 serial_port.read(&c, 1);
96 if(c=='S' || c=='H' || c=='L')
97 rx_buf[rx_fill++] = c;
100 IO::print("Reverse\n");
102 train->set_control("speed", 0);
111 rx_buf[rx_fill++] = c;
112 if(rx_buf[0]=='H' && rx_fill==2)
114 train->set_function(rx_buf[1]-'0', true);
115 IO::print("Func %d on\n", rx_buf[1]-'0');
118 else if(rx_buf[0]=='L' && rx_fill==2)
120 train->set_function(rx_buf[1]-'0', false);
121 IO::print("Func %d off\n", rx_buf[1]-'0');
124 else if(rx_buf[0]=='S' && rx_fill==3)
126 // XXX The firmware is still coded for speed step based control
127 float speed = ((rx_buf[1]-'0')*10+(rx_buf[2]-'0'))*10/3.6*catalogue.get_scale();
128 IO::print("Set speed %g\n", speed);
129 train->set_control("speed", speed);