]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/control.h
a91186059b033ed1052ea80408f9a7c0e474e8db
[r2c2.git] / source / libmarklin / control.h
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 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
24 class Control
25 {
26 private:
27         int  serial_fd;
28         bool p50_enabled;
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, Sensor *> &get_sensors() const { return sensors; }
51         unsigned   get_queue_length() const { return queue.size(); }
52         void       open(const std::string &);
53         Command    &command(const std::string &);
54
55         void       add_turnout(Turnout &);
56         Turnout    &get_turnout(unsigned) const;
57         void       add_locomotive(Locomotive &);
58         Locomotive &get_locomotive(unsigned) const;
59         void       add_sensor(Sensor &);
60         Sensor     &get_sensor(unsigned) const;
61
62         void       tick();
63         Msp::Time::Timer::Slot &set_timer(const Msp::Time::TimeDelta &);
64 private:
65         void read_all(int, char *, int);
66         std::string read_reply(Cmd);
67         void status_done(Error, const std::string &);
68         void event_query_done(Error, const std::string &);
69         void turnout_event_done(Error, const std::string &);
70         void sensor_event_done(Error, const std::string &);
71 };
72
73 } // namespace Marklin
74
75 #endif