]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/control.h
df8ea1bc3c76ad9cc51b65c0c29c81481be87644
[r2c2.git] / source / libmarklin / control.h
1 #ifndef LIBMARKLIN_CONTROL_H_
2 #define LIBMARKLIN_CONTROL_H_
3
4 #include <list>
5 #include <string>
6 #include <msp/time/timer.h>
7 #include <msp/time/timestamp.h>
8 #include "constants.h"
9 #include "sensor.h"
10 #include "locomotive.h"
11 #include "turnout.h"
12
13 namespace Marklin {
14
15 class Command;
16
17 class Control
18 {
19 private:
20         int  serial_fd;
21         bool p50_enabled;
22         bool power;
23         std::list<Command> queue;
24         std::map<unsigned, Turnout *> turnouts;
25         std::map<unsigned, Locomotive *> locomotives;
26         std::map<unsigned, Sensor *> sensors;
27         Msp::Time::TimeStamp next_event_query;
28         bool poll_sensors;
29         bool debug;
30         Msp::Time::Timer timer;
31
32 public:
33         sigc::signal<void, bool> signal_power_event;
34         sigc::signal<void, unsigned, bool> signal_turnout_event;
35         sigc::signal<void, unsigned, bool> signal_sensor_event;
36
37         Control();
38         ~Control();
39
40         void       set_power(bool);
41         bool       get_power() const { return power; }
42         void       set_debug(bool);
43         const std::map<unsigned, Sensor *> &get_sensors() const { return sensors; }
44         unsigned   get_queue_length() const { return queue.size(); }
45         void       open(const std::string &);
46         Command    &command(const std::string &);
47
48         void       add_turnout(Turnout &);
49         Turnout    &get_turnout(unsigned) const;
50         void       add_locomotive(Locomotive &);
51         Locomotive &get_locomotive(unsigned) const;
52         void       add_sensor(Sensor &);
53         Sensor     &get_sensor(unsigned) const;
54
55         void       tick();
56         Msp::Time::Timer::Slot &set_timer(const Msp::Time::TimeDelta &);
57 private:
58         void read_all(int, char *, int);
59         std::string read_reply(Cmd);
60         void status_done(Error, const std::string &);
61         void event_query_done(Error, const std::string &);
62         void turnout_event_done(Error, const std::string &);
63         void sensor_event_done(Error, const std::string &);
64 };
65
66 } // namespace Marklin
67
68 #endif