]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/control.h
Allow intercepting and denying turnout route and locomotive speed changes
[r2c2.git] / source / libmarklin / control.h
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #ifndef LIBMARKLIN_CONTROL_H_
9 #define LIBMARKLIN_CONTROL_H_
10
11 #include <list>
12 #include <string>
13 #include <msp/time/timer.h>
14 #include <msp/time/timestamp.h>
15 #include "constants.h"
16 #include "sensor.h"
17 #include "locomotive.h"
18 #include "turnout.h"
19
20 namespace Marklin {
21
22 class Command;
23 class Reply;
24
25 class Control
26 {
27 private:
28         int  serial_fd;
29         bool power;
30         std::list<Command> queue;
31         std::map<unsigned, Turnout *> turnouts;
32         std::map<unsigned, Locomotive *> locomotives;
33         std::map<unsigned, Sensor *> sensors;
34         Msp::Time::TimeStamp next_event_query;
35         bool poll_sensors;
36         bool debug;
37         Msp::Time::Timer timer;
38
39 public:
40         sigc::signal<void, bool> signal_power_event;
41         sigc::signal<void, unsigned, bool> signal_turnout_event;
42         sigc::signal<void, unsigned, bool> signal_sensor_event;
43
44         Control();
45         ~Control();
46
47         void       set_power(bool);
48         bool       get_power() const { return power; }
49         void       set_debug(bool);
50         const std::map<unsigned, Turnout *> &get_turnouts() const { return turnouts; }
51         const std::map<unsigned, Sensor *> &get_sensors() const { return sensors; }
52         unsigned   get_queue_length() const { return queue.size(); }
53         void       open(const std::string &);
54         Command    &command(Cmd);
55         Command    &command(Cmd, unsigned char);
56         Command    &command(Cmd, const unsigned char *, unsigned);
57
58         void       add_turnout(Turnout &);
59         Turnout    &get_turnout(unsigned) const;
60         void       add_locomotive(Locomotive &);
61         Locomotive &get_locomotive(unsigned) const;
62         void       add_sensor(Sensor &);
63         Sensor     &get_sensor(unsigned) const;
64
65         void       tick();
66         Msp::Time::Timer::Slot &set_timer(const Msp::Time::TimeDelta &);
67 private:
68         void read_all(int, char *, int);
69         std::string read_reply(Cmd);
70         void status_done(const Reply &);
71         void event_query_done(const Reply &);
72         void turnout_event_done(const Reply &);
73         void sensor_event_done(const Reply &);
74 };
75
76 } // namespace Marklin
77
78 #endif