Debian Codegraph @

DataModel/Feedback.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 #include <mutex>
24 #include <string>
25
26 #include "DataTypes.h"
27 #include "DataModel/ObjectIdentifier.h"
28 #include "DataModel/LayoutItem.h"
29 #include "Hardware/FeedbackCache.h"
30 #include "Languages.h"
31
32 class Manager;
33
34 namespace DataModel
35 {
36 class Track;
37
38 enum FeedbackType : unsigned char
39 {
40 FeedbackTypeDefault = 0,
41 FeedbackTypeStraight = 1,
42 FeedbackTypeTurn = 2
43 };
44
45 class Feedback : public LayoutItem
46 {
47 public:
48 enum FeedbackState : bool
49 {
50 FeedbackStateFree = false,
51 FeedbackStateOccupied = true
52 };
53
54 inline Feedback(Manager* manager,
55 const FeedbackID feedbackID)
56 : LayoutItem(feedbackID),
57 controlID(ControlIdNone),
58 pin(FeedbackPinNone),
59 manager(manager),
60 feedbackType(FeedbackTypeDefault),
61 routeId(RouteNone),
62 inverted(false),
63 track(nullptr),
64 stateCounter(0)
65 {
66 }
67
68 inline Feedback(Manager* manager, const std::string& serialized)
69 : manager(manager),
70 track(nullptr)
71 {
72 Deserialize(serialized);
73 }
74
75 inline ObjectType GetObjectType() const override
76 {
77 return ObjectTypeFeedback;
78 }
79
80 std::string Serialize() const override;
81
•82 void Deserialize(const std::string& serialized) override;
83
84 inline bool IsInUse() const
85 {
86 return track != nullptr;
87 }
88
89 inline std::string GetLayoutType() const override
90 {
91 return Languages::GetText(Languages::TextFeedback);
92 }
93
94 inline FeedbackType GetFeedbackType() const
95 {
96 return feedbackType;
97 }
98
99 inline void SetFeedbackType(const FeedbackType type)
100 {
101 this->feedbackType = type;
102 }
103
104 inline RouteID GetRouteId() const
105 {
106 return routeId;
107 }
108
109 inline void SetRouteId(const RouteID routeId)
110 {
111 this->routeId = routeId;
112 }
113
114 inline void SetInverted(const bool inverted)
115 {
116 this->inverted = inverted;
117 }
118
119 inline bool GetInverted() const
120 {
121 return inverted;
122 }
123
124 void SetState(const FeedbackState state);
125
126 inline FeedbackState GetState() const
127 {
128 return static_cast<FeedbackState>(stateCounter > 0);
129 }
130
131 void Debounce();
132
133 inline void SetControlID(const ControlID controlID)
134 {
135 this->controlID = controlID;
136 }
137
138 inline ControlID GetControlID() const
139 {
140 return controlID;
141 }
142
143 inline void SetPin(const FeedbackPin pin)
144 {
145 this->pin = pin;
146 }
147
148 inline FeedbackPin GetPin() const
149 {
150 return pin;
151 }
152
153 inline void SetTrack(DataModel::Track* track = nullptr)
154 {
155 this->track = track;
156 }
157
158 inline Track* GetTrack()
159 {
160 return track;
161 }
162
163 inline void SetMatchKey(const std::string& matchKey)
164 {
165 this->matchKey = matchKey;
166 }
167
168 inline void ClearMatchKey()
169 {
170 matchKey.clear();
171 }
172
173 inline std::string GetMatchKey() const
174 {
175 return matchKey;
176 }
177
178 Feedback& operator=(const Hardware::FeedbackCacheEntry& feedback);
179
180 private:
181 void UpdateTrackState(const FeedbackState state);
182
183 ControlID controlID;
184 FeedbackPin pin;
185
186 Manager* manager;
187 FeedbackType feedbackType;
188 RouteID routeId;
189 bool inverted;
190 Track* track;
191 unsigned char stateCounter;
192 static const unsigned char MaxStateCounter = 10;
193 mutable std::mutex updateMutex;
194 std::string matchKey;
195 };
196
197 } // namespace DataModel
198