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/strings/formatter.h>
9 #include <msp/time/units.h>
11 #include "catalogue.h"
14 #include "timetable.h"
22 Timetable::Timetable(Train &t):
30 train.signal_arrived.connect(sigc::mem_fun(this, &Timetable::train_arrived));
31 train.get_layout().get_driver().signal_sensor.connect(sigc::mem_fun(this, &Timetable::sensor_event));
34 void Timetable::set_enabled(bool e)
39 void Timetable::reset()
42 wait_timeout = Time::TimeStamp();
47 void Timetable::clear()
53 void Timetable::append(const Row &row)
58 void Timetable::insert(unsigned i, const Row &row)
61 throw InvalidParameterValue("Insert position out of range");
63 rows.insert(rows.begin()+i, row);
68 const Timetable::Row &Timetable::get_row(unsigned i) const
71 throw InvalidParameterValue("Row index out of range");
75 void Timetable::tick(const Time::TimeStamp &t)
77 if(rows.empty() || !enabled)
80 if(wait_timeout && t>=wait_timeout)
82 wait_timeout = Time::TimeStamp();
83 current_row = (current_row+1)%rows.size();
89 Row &row = rows[current_row];
93 if(!train.go_to(**parse_location(row.get_param<string>(0)).get_tracks().begin()))
97 pending_block = &parse_location(row.get_param<string>(0));
98 pending_train = &train;
102 wait_timeout = t+row.get_param<unsigned>(0)*Time::sec;
106 pending_train = &train.get_layout().get_train(row.get_param<unsigned>(0));
107 pending_block = &parse_location(row.get_param<string>(1));
114 train.set_control("speed", row.get_param<unsigned>(0)/3.6*train.get_layout().get_catalogue().get_scale());
117 train.set_control("reverse", !train.get_control("reverse"));
120 if(!train.set_route(&train.get_layout().get_route(row.get_param<string>(0))))
126 current_row = (current_row+1)%rows.size();
130 void Timetable::save(list<DataFile::Statement> &st) const
132 for(vector<Row>::const_iterator i=rows.begin(); i!=rows.end(); ++i)
133 st.push_back(i->save());
136 Block &Timetable::parse_location(const string &loc)
138 if(!loc.compare(0, 7, "sensor "))
139 return train.get_layout().get_block(lexical_cast<unsigned>(loc.substr(7))|0x1000);
140 throw Exception("Named blocks are not supported yet");
143 void Timetable::sensor_event(unsigned addr, bool state)
145 if(pending_block && pending_block->get_train()==pending_train && addr==pending_block->get_sensor_id() && state)
148 current_row = (current_row+1)%rows.size();
153 void Timetable::train_arrived()
155 Row &row = rows[current_row];
158 current_row = (current_row+1)%rows.size();
164 Timetable::Row::Row(RowType t):
169 Timetable::Row::Row(RowType t, const T &p):
176 const T &Timetable::Row::get_param(unsigned i) const
179 throw InvalidParameterValue("Parameter index out of range");
180 return params[i].value<T>();
183 string Timetable::Row::str() const
188 return "set route to "+get_param<string>(0);
190 return "travel to "+get_param<string>(0);
192 return format("wait for %d seconds", get_param<unsigned>(0));
194 return format("wait for train %d at %s", get_param<unsigned>(0), get_param<string>(1));
196 return "travel until arrival";
198 return format("set speed %d km/h", get_param<unsigned>(0));
202 return "set route "+get_param<string>(0);
204 return "invalid row";
208 DataFile::Statement Timetable::Row::save() const
213 return DataFile::Statement("goto"), get_param<string>(0);
215 return DataFile::Statement("travel"), get_param<string>(0);
217 return DataFile::Statement("wait"), get_param<unsigned>(0);
219 return DataFile::Statement("wait_train"), get_param<unsigned>(0), get_param<string>(1);
221 return DataFile::Statement("arrive");
223 return DataFile::Statement("speed"), get_param<unsigned>(0);
225 return DataFile::Statement("reverse");
227 return DataFile::Statement("route"), get_param<string>(0);
229 return DataFile::Statement();
233 Timetable::Row Timetable::Row::parse(const string &s)
235 if(!s.compare(0, 7, "travel "))
237 if(!s.compare(7, 3, "to "))
238 return Row(TRAVEL, s.substr(10));
239 else if(!s.compare(7, string::npos, "until arrival"))
242 else if(!s.compare(0, 9, "wait for "))
246 unsigned nondigit = 10;
247 while(nondigit<s.size() && isdigit(s[nondigit]))
249 return Row(WAIT_TIME, lexical_cast<unsigned>(s.substr(9, nondigit-9)));
251 else if(!s.compare(9, 6, "train "))
253 string::size_type at = s.find(" at ", 15);
256 Row row(WAIT_TRAIN, lexical_cast<unsigned>(s.substr(15, at-15)));
257 row.params.push_back(s.substr(at+4));
262 else if(!s.compare(0, 10, "set speed "))
264 unsigned nondigit = 11;
265 while(nondigit<s.size() && (isdigit(s[nondigit]) || s[nondigit]=='-'))
267 return Row(SPEED, lexical_cast<unsigned>(s.substr(10, nondigit-10)));
269 else if(s=="reverse")
271 else if(!s.compare(0, 10, "set route "))
273 if(!s.compare(10, 3, "to "))
274 return Row(GOTO, s.substr(13));
275 return Row(ROUTE, s.substr(10));
278 throw InvalidParameterValue("Invalid row");
282 Timetable::Loader::Loader(Timetable &tt):
283 DataFile::ObjectLoader<Timetable>(tt)
285 add("arrive", &Loader::arrive);
286 add("goto", &Loader::go_to);
287 add("route", &Loader::route);
288 add("speed", &Loader::speed);
289 add("reverse", &Loader::reverse);
290 add("travel", &Loader::travel);
291 add("wait", &Loader::wait);
292 add("wait_train", &Loader::wait_train);
295 void Timetable::Loader::arrive()
297 obj.rows.push_back(Row(ARRIVE));
300 void Timetable::Loader::go_to(const string &t)
302 obj.rows.push_back(Row(GOTO, t));
305 void Timetable::Loader::route(const string &r)
307 obj.rows.push_back(Row(ROUTE, r));
310 void Timetable::Loader::reverse()
312 obj.rows.push_back(Row(REVERSE));
315 void Timetable::Loader::speed(unsigned s)
317 obj.rows.push_back(Row(SPEED, s));
320 void Timetable::Loader::travel(const string &t)
322 obj.rows.push_back(Row(TRAVEL, t));
325 void Timetable::Loader::wait(unsigned t)
327 obj.rows.push_back(Row(WAIT_TIME, t));
330 void Timetable::Loader::wait_train(unsigned t, const string &b)
332 Row row(WAIT_TRAIN, t);
333 row.params.push_back(b);
334 obj.rows.push_back(row);
337 } // namespace Marklin