Skip to content

Commit bd254d1

Browse files
committed
Refactor discovery; add Bit helper & panel LED
Replace direct bit-shifts with a Bit(index) helper and use explicit uint8_t casts for masks; fix kPorts constant name. Add conditional hal_panelled includes and turn on panel LEDs during discovery/unmute/quickfind states (gated by CONFIG_PANELLED_RDM_PORT/CONFIG_PANELLED_RDM_NO_PORT). Apply style and formatting cleanups (brace/line formatting, enum formatting, IWYU pragmas) and small safety/clarity changes across rdm_discovery.h and rdm_discovery_statemachine.cpp (e.g. kBackgroundIntervalMinutes placement, function formatting, and minor snprintf formatting). These changes improve readability, correctness of byte-sized masks, and add visual feedback for discovery activity.
1 parent d79d535 commit bd254d1

2 files changed

Lines changed: 173 additions & 256 deletions

File tree

lib-rdm/include/rdm_discovery.h

Lines changed: 71 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,20 @@
3535
#include "dmx.h" // IWYU pragma: keep
3636
#include "softwaretimers.h" // IWYU pragma: keep
3737

38-
namespace rdm
39-
{
40-
namespace discovery
41-
{
42-
enum class Type
43-
{
44-
kFull,
45-
kIncremental
46-
};
38+
namespace rdm {
39+
namespace discovery {
40+
enum class Type { kFull, kIncremental };
4741

4842
void Starting(uint32_t port_index, Type type);
4943
void Finished(uint32_t port_index, Type type);
5044
} // namespace discovery
5145
inline static constexpr uint32_t kBackgroundIntervalMinutes = 1;
5246

53-
class Discovery : rdm::discovery::StateMachine
54-
{
55-
static constexpr auto kPorts = dmx::config::max::PORTS;
47+
class Discovery : rdm::discovery::StateMachine {
48+
static constexpr auto kPorts = dmx::config::max::kPorts;
5649

5750
public:
58-
Discovery() : rdm::discovery::StateMachine(rdm::device::Base::Instance().GetUID())
59-
{
51+
Discovery() : rdm::discovery::StateMachine(rdm::device::Base::Instance().GetUID()) {
6052
assert(s_this == nullptr);
6153
s_this = this;
6254
}
@@ -68,133 +60,111 @@ class Discovery : rdm::discovery::StateMachine
6860

6961
void Print() { rdm::device::Base::Instance().Print(); }
7062

71-
void Enable(uint32_t port_index)
72-
{
63+
void Enable(uint32_t port_index) {
7364
assert(port_index < kPorts);
74-
enabled_ |= (1U << port_index);
65+
enabled_ |= Bit(port_index);
7566
}
7667

77-
bool IsEnabled(uint32_t port_index) const { return (((1U << port_index) & enabled_) == (1U << port_index)); }
68+
bool IsEnabled(uint32_t port_index) const { return ((Bit(port_index) & enabled_) == Bit(port_index)); }
7869

79-
void Disable(uint32_t port_index)
80-
{
70+
void Disable(uint32_t port_index) {
8171
assert(port_index < kPorts);
82-
enabled_ &= ~(1U << port_index);
72+
enabled_ &= static_cast<uint8_t>(~Bit(port_index));
8373
}
8474

85-
void EnableBackground(uint32_t port_index)
86-
{
75+
void EnableBackground(uint32_t port_index) {
8776
assert(port_index < kPorts);
88-
if (((1U << port_index) & enabled_) == (1U << port_index))
89-
{
90-
s_bg_discovery |= (1U << port_index);
77+
if ((Bit(port_index) & enabled_) == Bit(port_index)) {
78+
s_bg_discovery |= Bit(port_index);
9179
}
9280

93-
if (s_timer_id < 0)
94-
{
81+
if (s_timer_id < 0) {
9582
s_timer_id = SoftwareTimerAdd((1000U * 60U) * kBackgroundIntervalMinutes, TimerBackGround);
9683
printf("s_timer_id=%d\n", s_timer_id);
9784
}
9885
}
9986

100-
void DisableBackground(uint32_t port_index)
101-
{
87+
void DisableBackground(uint32_t port_index) {
10288
assert(port_index < kPorts);
103-
if (((1U << port_index) & enabled_) == (1U << port_index))
104-
{
105-
s_bg_discovery &= ~(1U << port_index);
89+
if ((Bit(port_index) & enabled_) == Bit(port_index)) {
90+
s_bg_discovery &= static_cast<uint8_t>(~Bit(port_index));
10691
Stop(port_index);
10792
}
10893

109-
if (s_bg_discovery == 0)
110-
{
94+
if (s_bg_discovery == 0) {
11195
SoftwareTimerDelete(s_timer_id);
11296
}
11397
}
11498

115-
bool IsEnabledBackground(uint32_t port_index) const { return (((1U << port_index) & s_bg_discovery) == (1U << port_index)); }
99+
bool IsEnabledBackground(uint32_t port_index) const { return ((Bit(port_index) & s_bg_discovery) == Bit(port_index)); }
116100

117-
void Full(uint32_t port_index)
118-
{
101+
void Full(uint32_t port_index) {
119102
assert(port_index < kPorts);
120-
if (((1U << port_index) & enabled_) == (1U << port_index))
121-
{
122-
waiting_ |= (1U << port_index);
123-
type_ |= (1U << port_index);
103+
if ((Bit(port_index) & enabled_) == Bit(port_index)) {
104+
waiting_ |= Bit(port_index);
105+
type_ |= Bit(port_index);
124106
running_ = true;
125107
}
126108
}
127109

128-
void Incremental(uint32_t port_index)
129-
{
110+
void Incremental(uint32_t port_index) {
130111
assert(port_index < kPorts);
131-
if (((1U << port_index) & enabled_) == (1U << port_index))
132-
{
133-
waiting_ |= (1U << port_index);
134-
type_ &= ~(1U << port_index);
112+
if ((Bit(port_index) & enabled_) == Bit(port_index)) {
113+
waiting_ |= Bit(port_index);
114+
type_ &= static_cast<uint8_t>(~Bit(port_index));
135115
running_ = true;
136116
}
137117
}
138118

139-
void Stop(uint32_t port_index)
140-
{
119+
void Stop(uint32_t port_index) {
141120
assert(port_index < kPorts);
142-
if (((1U << port_index) & enabled_) == (1U << port_index))
143-
{
121+
if ((Bit(port_index) & enabled_) == Bit(port_index)) {
144122
bool is_incremental;
145123
uint32_t index;
146-
if (rdm::discovery::StateMachine::IsRunning(index, is_incremental))
147-
{
148-
if (index == port_index)
149-
{
124+
if (rdm::discovery::StateMachine::IsRunning(index, is_incremental)) {
125+
if (index == port_index) {
150126
rdm::discovery::StateMachine::Stop();
151-
waiting_ &= ~(1U << port_index);
127+
waiting_ &= static_cast<uint8_t>(~Bit(port_index));
152128
}
153129
}
154130
}
155131
}
156132

157133
bool IsRunning(uint32_t portindex, bool& is_incremental) { return rdm::discovery::StateMachine::IsRunning(portindex, is_incremental); }
158134

159-
bool IsRunning(uint32_t port_index)
160-
{
135+
bool IsRunning(uint32_t port_index) {
161136
assert(port_index < kPorts);
162137

163138
uint32_t portindex;
164139
bool is_incremental;
165140

166-
if (rdm::discovery::StateMachine::IsRunning(portindex, is_incremental))
167-
{
141+
if (rdm::discovery::StateMachine::IsRunning(portindex, is_incremental)) {
168142
return port_index == portindex;
169143
}
170144

171145
return false;
172146
}
173147

174-
void GetStatus(uint8_t data[5])
175-
{
148+
void GetStatus(uint8_t data[5]) {
176149
data[0] = enabled_;
177150
data[1] = waiting_;
178151
data[2] = type_;
179152
data[3] = s_bg_discovery;
180153
data[4] = 0;
181-
for (uint32_t port_index = 0; port_index < kPorts; port_index++)
182-
{
183-
data[4] |= (IsRunning(port_index) ? (1U << port_index) : 0);
154+
155+
for (uint32_t port_index = 0; port_index < kPorts; port_index++) {
156+
if (IsRunning(port_index)) {
157+
data[4] |= Bit(port_index);
158+
}
184159
}
185160
}
186161

187-
uint32_t CopyWorkingQueue(char* out_buffer, uint32_t out_buffer_size)
188-
{
189-
return rdm::discovery::StateMachine::CopyWorkingQueue(out_buffer, out_buffer_size);
190-
}
162+
uint32_t CopyWorkingQueue(char* out_buffer, uint32_t out_buffer_size) { return rdm::discovery::StateMachine::CopyWorkingQueue(out_buffer, out_buffer_size); }
191163

192-
void Run()
193-
{
164+
void Run() {
194165
rdm::discovery::StateMachine::Run();
195166

196-
if (__builtin_expect((!running_), 1))
197-
{
167+
if (__builtin_expect((!running_), 1)) {
198168
return;
199169
}
200170

@@ -203,44 +173,34 @@ class Discovery : rdm::discovery::StateMachine
203173
bool is_incremental;
204174

205175
uint32_t port_index;
206-
if (rdm::discovery::StateMachine::IsFinished(port_index, is_incremental))
207-
{
176+
if (rdm::discovery::StateMachine::IsFinished(port_index, is_incremental)) {
208177
assert(port_index_ == port_index);
209178
printf("Finished:%u\n", port_index_);
210179
rdm::discovery::Finished(port_index_, is_incremental ? rdm::discovery::Type::kIncremental : rdm::discovery::Type::kFull);
211180

212181
port_index_++;
213182
if (port_index_ == kPorts) port_index_ = 0;
214183

215-
if (waiting_ == 0)
216-
{
184+
if (waiting_ == 0) {
217185
running_ = false;
218186
port_index_ = 0;
219187
}
220188
}
221189

222-
if (waiting_)
223-
{
224-
if (((1U << port_index_) & waiting_) == (1U << port_index_))
225-
{
226-
if (((1U << port_index_) & type_) == (1U << port_index_))
227-
228-
{
190+
if (waiting_) {
191+
if ((Bit(port_index_) & waiting_) == Bit(port_index_)) {
192+
if ((Bit(port_index_) & type_) == Bit(port_index_)) {
229193
rdm::discovery::Starting(port_index_, rdm::discovery::Type::kFull);
230194
rdm::discovery::StateMachine::Full(port_index_, &s_tod[port_index_]);
231195
printf("Full:%u\n", port_index_);
232-
}
233-
else
234-
{
196+
} else {
235197
rdm::discovery::Starting(port_index_, rdm::discovery::Type::kIncremental);
236198
rdm::discovery::StateMachine::Incremental(port_index_, &s_tod[port_index_]);
237199
printf("Incremental:%u\n", port_index_);
238200
}
239201

240-
waiting_ &= ~(1U << port_index_);
241-
}
242-
else
243-
{
202+
waiting_ &= static_cast<uint8_t>(~Bit(port_index));
203+
} else {
244204
port_index_++;
245205
if (port_index_ == kPorts) port_index_ = 0;
246206
}
@@ -249,95 +209,82 @@ class Discovery : rdm::discovery::StateMachine
249209
}
250210
}
251211

252-
uint32_t TodUidCount(uint32_t port_index)
253-
{
212+
uint32_t TodUidCount(uint32_t port_index) {
254213
assert(port_index < kPorts);
255214
return s_tod[port_index].UidCount();
256215
}
257216

258-
bool TodCopyUidEntry(uint32_t port_index, uint32_t index, uint8_t uid[RDM_UID_SIZE])
259-
{
217+
bool TodCopyUidEntry(uint32_t port_index, uint32_t index, uint8_t uid[RDM_UID_SIZE]) {
260218
assert(port_index < kPorts);
261219
return s_tod[port_index].CopyUidEntry(index, uid);
262220
}
263221

264-
void TodCopy(uint32_t port_index, uint8_t* tod)
265-
{
222+
void TodCopy(uint32_t port_index, uint8_t* tod) {
266223
assert(port_index < kPorts);
267224
s_tod[port_index].Copy(tod);
268225
}
269226

270-
void TodReset(uint32_t port_index)
271-
{
227+
void TodReset(uint32_t port_index) {
272228
assert(port_index < kPorts);
273229
s_tod[port_index].Reset();
274230
}
275231

276-
bool TodAddUid(uint32_t port_index, const uint8_t* uid)
277-
{
232+
bool TodAddUid(uint32_t port_index, const uint8_t* uid) {
278233
assert(port_index < kPorts);
279234
return s_tod[port_index].AddUid(uid);
280235
}
281236

282-
bool TodExist(uint32_t port_index, const uint8_t* uid)
283-
{
237+
bool TodExist(uint32_t port_index, const uint8_t* uid) {
284238
assert(port_index < kPorts);
285239
return s_tod[port_index].Exist(uid);
286240
}
287241

288-
const uint8_t* TodNext(uint32_t port_index)
289-
{
242+
const uint8_t* TodNext(uint32_t port_index) {
290243
assert(port_index < kPorts);
291244
return s_tod[port_index].Next();
292245
}
293246

294-
bool TodIsMuted(uint32_t port_index)
295-
{
247+
bool TodIsMuted(uint32_t port_index) {
296248
assert(port_index < kPorts);
297249
return s_tod[port_index].IsMuted();
298250
}
299251

300-
void TodMute(uint32_t port_index)
301-
{
252+
void TodMute(uint32_t port_index) {
302253
assert(port_index < kPorts);
303254
s_tod[port_index].Mute();
304255
}
305256

306-
void TodUnMute(uint32_t port_index)
307-
{
257+
void TodUnMute(uint32_t port_index) {
308258
assert(port_index < kPorts);
309259
s_tod[port_index].UnMute();
310260
}
311261

312-
void TodUnMuteAll(uint32_t port_index)
313-
{
262+
void TodUnMuteAll(uint32_t port_index) {
314263
assert(port_index < kPorts);
315264
s_tod[port_index].UnMuteAll();
316265
}
317266

318-
void TodDump(uint32_t port_index)
319-
{
267+
void TodDump(uint32_t port_index) {
320268
assert(port_index < kPorts);
321269
s_tod[port_index].Dump();
322270
}
323271

324-
static Discovery& Instance()
325-
{
272+
static Discovery& Instance() {
326273
assert(s_this != nullptr);
327274
return *s_this;
328275
}
329276

330-
static void TimerBackGround([[maybe_unused]] TimerHandle_t handle)
331-
{
332-
for (uint32_t port_index = 0; port_index < kPorts; port_index++)
333-
{
334-
if (((1U << port_index) & s_bg_discovery) == (1U << port_index))
335-
{
277+
static void TimerBackGround([[maybe_unused]] TimerHandle_t handle) {
278+
for (uint32_t port_index = 0; port_index < kPorts; port_index++) {
279+
if ((Bit(port_index) & s_bg_discovery) == Bit(port_index)) {
336280
Discovery::Instance().Incremental(port_index);
337281
}
338282
}
339283
}
340284

285+
private:
286+
static constexpr uint8_t Bit(uint32_t index) { return static_cast<uint8_t>(1U << index); }
287+
341288
private:
342289
uint8_t port_index_{0};
343290
uint8_t enabled_{0};

0 commit comments

Comments
 (0)