]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/control.h
Rewrite command/reply system
[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 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, Sensor *> &get_sensors() const { return sensors; }
51         unsigned   get_queue_length() const { return queue.size(); }
52         void       open(const std::string &);
53         Command    &command(Cmd);
54         Command    &command(Cmd, unsigned char);
55         Command    &command(Cmd, const unsigned char *, unsigned);
56
57         void       add_turnout(Turnout &);
58         Turnout    &get_turnout(unsigned) const;
59         void       add_locomotive(Locomotive &);
60         Locomotive &get_locomotive(unsigned) const;
61         void       add_sensor(Sensor &);
62         Sensor     &get_sensor(unsigned) const;
63
64         void       tick();
65         Msp::Time::Timer::Slot &set_timer(const Msp::Time::TimeDelta &);
66 private:
67         void read_all(int, char *, int);
68         std::string read_reply(Cmd);
69         void status_done(const Reply &);
70         void event_query_done(const Reply &);
71         void turnout_event_done(const Reply &);
72         void sensor_event_done(const Reply &);
73 };
74
75 } // namespace Marklin
76
77 #endif