#include <msp/strings/formatter.h>
#include <msp/strings/lexicalcast.h>
#include <msp/strings/regex.h>
+#include "libmarklin/except.h"
#include "libmarklin/tracktype.h"
#include "engineer.h"
#include "mainpanel.h"
Engineer::~Engineer()
{
- trfc_mgr->save("engineer.state");
+ if(!simulate)
+ trfc_mgr->save("engineer.state");
delete trfc_mgr;
}
if(unsigned tid=track->get_track().get_turnout_id())
{
Turnout &turnout=control.get_turnout(tid);
- turnout.set_route((turnout.get_route()+1)%track->get_track().get_type().get_n_routes());
+ try
+ {
+ turnout.set_route((turnout.get_route()+1)%track->get_track().get_type().get_n_routes());
+ main_panel->set_status_text(format("Turnout %d switched", turnout.get_address()));
+ }
+ catch(const TurnoutBusy &e)
+ {
+ main_panel->set_status_text(e.what());
+ }
}
else if(simulate)
{
trfc_mgr(tm),
id(next_id++),
sensor_id(start.get_sensor_id()),
+ turnout_id(start.get_turnout_id()),
train(0)
{
tracks.insert(&start);
for(unsigned i=0; i<links.size(); ++i)
if(links[i] && !tracks.count(links[i]))
{
- if(links[i]->get_sensor_id()==sensor_id && !links[i]->get_turnout_id() && !track->get_turnout_id())
+ if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
{
queue.push_back(links[i]);
tracks.insert(links[i]);
/* $Id$
This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
TrafficManager &trfc_mgr;
unsigned id;
unsigned sensor_id;
+ unsigned turnout_id;
std::set<Track *> tracks;
std::vector<Endpoint> endpoints;
const Train *train;
Block(TrafficManager &, Track &);
unsigned get_sensor_id() const { return sensor_id; }
+ unsigned get_turnout_id() const { return turnout_id; }
const std::set<Track *> &get_tracks() const { return tracks; }
const std::vector<Endpoint> &get_endpoints() const { return endpoints; }
int get_endpoint_by_link(const Block &) const;
/* $Id$
This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
void set_power(bool);
bool get_power() const { return power; }
void set_debug(bool);
+ const std::map<unsigned, Turnout *> &get_turnouts() const { return turnouts; }
const std::map<unsigned, Sensor *> &get_sensors() const { return sensors; }
unsigned get_queue_length() const { return queue.size(); }
void open(const std::string &);
--- /dev/null
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2009 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#ifndef LIBMARKLIN_EXCEPT_H_
+#define LIBMARKLIN_EXCEPT_H_
+
+#include <msp/core/except.h>
+
+namespace Marklin {
+
+class Train;
+
+class TurnoutBusy: public Msp::Exception
+{
+private:
+ Train *train;
+
+public:
+ TurnoutBusy(Train *t): Exception("Turnout is busy"), train(t) { }
+ virtual ~TurnoutBusy() throw() { }
+
+ Train *get_train() const throw() { return train; }
+};
+
+}
+
+#endif
/* $Id$
This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
void Locomotive::set_speed(unsigned spd)
{
- speed=min(spd, 14U);
+ spd=min(spd, 14U);
+ signal_speed_changing.emit(spd);
+ speed=spd;
send_command(false);
signal_speed_changed.emit(speed);
/* $Id$
This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
unsigned funcs;
public:
+ sigc::signal<void, unsigned> signal_speed_changing;
sigc::signal<void, unsigned> signal_speed_changed;
sigc::signal<void, unsigned, bool> signal_function_changed;
#include <msp/time/units.h>
#include <msp/time/utils.h>
#include "control.h"
+#include "except.h"
#include "tracktype.h"
#include "trafficmanager.h"
#include "train.h"
const map<unsigned, Sensor *> &sensors=trfc_mgr.get_control().get_sensors();
for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::sensor_event), i->second));
+
+ const map<unsigned, Turnout *> &turnouts=trfc_mgr.get_control().get_turnouts();
+ for(map<unsigned, Turnout *>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
+ {
+ i->second->signal_route_changing.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_route_changing), i->second));
+ i->second->signal_route_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_route_changed), i->second));
+ }
}
void Train::set_name(const string &n)
i->block->reserve(0);
rsv_blocks.clear();
}
+ else
+ reserve_more();
update_speed();
pure_speed=false;
bool Train::free_block(Block *block)
{
+ unsigned nsens=0;
for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+ {
if(i->block==block)
{
+ if(nsens<1)
+ return false;
while(i!=rsv_blocks.end())
{
i->block->reserve(0);
i=rsv_blocks.erase(i);
}
+ update_speed();
return true;
}
+ else if(i->block->get_sensor_id())
+ ++nsens;
+ }
return false;
}
void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
{
if(try_reserve && t>try_reserve)
+ {
+ reserve_more();
update_speed();
+ }
if(cur_track)
{
set_position(i->block->get_endpoints()[i->entry]);
if(target_speed)
+ {
+ reserve_more();
update_speed();
+ }
}
else
{
}
}
+void Train::turnout_route_changing(unsigned, Turnout *turnout)
+{
+ unsigned tid=turnout->get_address();
+ for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
+ if(i->block->get_turnout_id()==tid)
+ throw TurnoutBusy(this);
+
+ unsigned nsens=0;
+ for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+ {
+ if(i->block->get_turnout_id()==tid)
+ {
+ if(nsens<1)
+ throw TurnoutBusy(this);
+ break;
+ }
+ else if(i->block->get_sensor_id())
+ ++nsens;
+ }
+}
+
+void Train::turnout_route_changed(unsigned, Turnout *turnout)
+{
+ unsigned tid=turnout->get_address();
+ for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+ if(i->block->get_turnout_id()==tid)
+ {
+ while(i!=rsv_blocks.end())
+ {
+ i->block->reserve(0);
+ i=rsv_blocks.erase(i);
+ }
+ reserve_more();
+ update_speed();
+ return;
+ }
+}
+
unsigned Train::reserve_more()
{
BlockRef *last=0;
}
else
{
- unsigned n=reserve_more();
- if(n==0)
+ unsigned nsens=0;
+ for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+ if(i->block->get_sensor_id())
+ ++nsens;
+
+ if(nsens==0)
{
loco.set_speed(0);
pure_speed=false;
try_reserve=Time::now()+2*Time::sec;
set_status("Blocked");
}
- else if(n==1)
+ else if(nsens==1 && target_speed>3)
{
loco.set_speed(3);
pure_speed=false;
void save(std::list<Msp::DataFile::Statement> &) const;
private:
void sensor_event(bool, Sensor *);
+ void turnout_route_changing(unsigned, Turnout *);
+ void turnout_route_changed(unsigned, Turnout *);
unsigned reserve_more();
void update_speed();
void set_status(const std::string &);
void Turnout::set_route(unsigned r)
{
- route=r;
+ signal_route_changing.emit(r);
+ route=r;
command(true);
control.set_timer(200*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Turnout::switch_timeout));
bool dual;
public:
+ sigc::signal<void, unsigned> signal_route_changing;
sigc::signal<void, unsigned> signal_route_changed;
Turnout(Control &, unsigned, bool =false);