Skip to content

Commit 6ebf59b

Browse files
committed
Replace DMX magic flags with constants
Introduces named constants for DMX/RDM packet-complete flags and replaces hardcoded bit masks throughout receive handling, improving readability and reducing error-prone magic numbers. Also adds shared DMX protocol constants (`kSlotsMax`, `kSlotTime`, `kBaudRate`), performs small naming cleanups in buffer code, and simplifies a few helper declarations/control-flow paths without changing behavior.
1 parent cad9372 commit 6ebf59b

3 files changed

Lines changed: 44 additions & 30 deletions

File tree

lib-dmx/include/dmxconst.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,29 @@
2929
#include <cstdint>
3030

3131
namespace dmx {
32-
enum class Direction { kInput, kOutput, kDisable };
32+
enum class Direction {
33+
kInput, ///<
34+
kOutput, ///<
35+
kDisable ///<
36+
};
3337

3438
enum class OutputStyle {
3539
kDelta, ///< DMX frame is triggered
3640
kConstant ///< DMX output is continuous
3741
};
3842

39-
enum class SendStyle { kDirect, kSync };
43+
enum class SendStyle {
44+
kDirect, ///<
45+
kSync ///<
46+
};
4047

4148
inline constexpr uint32_t kStartCode = 0; ///< The start code for DMX512 data. This is often referred to as NSC for "Null Start Code".
4249
inline constexpr uint32_t kChannelsMin = 2;
4350
inline constexpr uint32_t kChannelsMax = 512;
51+
inline constexpr uint32_t kSlotsMax = 1 + kChannelsMax; ///< Start code + channels
52+
inline constexpr uint32_t kSlotTime = 44; ///< 40us + 4us space
53+
inline constexpr uint32_t kBaudRate = 250000;
54+
4455

4556
namespace transmit {
4657
inline constexpr uint32_t kBreakTimeMin = 92; ///< 92 us

lib-dmx/src/gd32/dmx.cpp

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ static_assert(dmx::buffer::kSize % 4 == 0); // multiple of uint32_t
6060
extern struct HwTimersSeconds gv_seconds;
6161

6262
namespace dmx {
63+
static constexpr uint32_t kDmxSlotsCompleteFlag = 0x8000;
64+
static constexpr uint32_t kRdmSlotsCompleteFlag = 0x4000;
65+
6366
enum class TxRxState {
6467
kIdle,
6568
kDmxBreak,
@@ -207,13 +210,13 @@ void IrqHandlerDmxRdmInput() {
207210

208211
if (rx_buffer.state == dmx::TxRxState::kDmxData) {
209212
rx_buffer.state = dmx::TxRxState::kIdle;
210-
rx_buffer.dmx.current.slots_in_packet |= 0x8000;
213+
rx_buffer.dmx.current.slots_in_packet |= dmx::kDmxSlotsCompleteFlag;
211214
return;
212215
}
213216

214217
if (rx_buffer.state == dmx::TxRxState::kRdmdisc) {
215218
rx_buffer.state = dmx::TxRxState::kIdle;
216-
rx_buffer.rdm.index |= 0x4000;
219+
rx_buffer.rdm.index |= dmx::kRdmSlotsCompleteFlag;
217220
return;
218221
}
219222

@@ -275,7 +278,7 @@ void IrqHandlerDmxRdmInput() {
275278
rx_buffer.dmx.current.slots_in_packet = index;
276279

277280
if (index > dmx::kChannelsMax) {
278-
index |= 0x8000;
281+
index |= dmx::kDmxSlotsCompleteFlag;
279282
rx_buffer.dmx.current.slots_in_packet = index;
280283
rx_buffer.state = dmx::TxRxState::kIdle;
281284
break;
@@ -308,7 +311,7 @@ void IrqHandlerDmxRdmInput() {
308311
case dmx::TxRxState::kRdmChecksuml: {
309312
auto index = rx_buffer.rdm.index;
310313
rx_buffer.rdm.data[index] = kData;
311-
index |= 0x4000;
314+
index |= dmx::kRdmSlotsCompleteFlag;
312315
rx_buffer.rdm.index = index;
313316
rx_buffer.state = dmx::TxRxState::kIdle;
314317
gsv_rdm_data_receive_end[port_index] = DWT->CYCCNT;
@@ -1479,9 +1482,9 @@ void Dmx::DataDisable(uint32_t port_index) {
14791482
void Dmx::ClearData(uint32_t port_index) {
14801483
assert(port_index < dmx::config::max::kPorts);
14811484

1482-
auto* p = &s_DmxTxBuffer[port_index].dmx.data[0];
1483-
p->length = 513; // Including START Code
1484-
__builtin_memset(p->data, 0, dmx::buffer::kSize);
1485+
auto* data = &s_DmxTxBuffer[port_index].dmx.data[0];
1486+
data->length = 513; // Including START Code
1487+
__builtin_memset(data->data, 0, dmx::buffer::kSize);
14851488
}
14861489

14871490
#if !defined(CONFIG_DMX_DISABLE_STATISTICS)
@@ -1512,15 +1515,15 @@ void Dmx::FullOn() {
15121515
if (port_direction_[port_index] == dmx::Direction::kOutput) {
15131516
DataDisable(port_index);
15141517

1515-
auto* __restrict__ p = &s_DmxTxBuffer[port_index].dmx.data[0];
1516-
auto* __restrict__ p32 = reinterpret_cast<uint32_t*>(p->data);
1518+
auto* __restrict__ data = &s_DmxTxBuffer[port_index].dmx.data[0];
1519+
auto* __restrict__ p32 = reinterpret_cast<uint32_t*>(data->data);
15171520

15181521
for (auto i = 0; i < dmx::buffer::kSize / 4; i++) {
15191522
*p32++ = UINT32_MAX;
15201523
}
15211524

1522-
p->data[0] = dmx::kStartCode;
1523-
p->length = 513;
1525+
data->data[0] = dmx::kStartCode;
1526+
data->length = 513;
15241527

15251528
DataEnable(port_index);
15261529
}
@@ -1991,11 +1994,11 @@ const uint8_t* Dmx::GetDmxAvailable([[maybe_unused]] uint32_t port_index) {
19911994
#if !defined(CONFIG_DMX_TRANSMIT_ONLY)
19921995
auto slots_in_packet = sv_rx_buffer[port_index].dmx.current.slots_in_packet;
19931996

1994-
if ((slots_in_packet & 0x8000) != 0x8000) {
1997+
if ((slots_in_packet & dmx::kDmxSlotsCompleteFlag) != dmx::kDmxSlotsCompleteFlag) {
19951998
return nullptr;
19961999
}
19972000

1998-
slots_in_packet &= static_cast<uint32_t>(~0x8000);
2001+
slots_in_packet &= ~dmx::kDmxSlotsCompleteFlag;
19992002
slots_in_packet--; // Remove SC from length
20002003
sv_rx_buffer[port_index].dmx.current.slots_in_packet = slots_in_packet;
20012004

@@ -2059,47 +2062,47 @@ void Dmx::RdmTransmitDiscoveryRespondMessage(uint32_t port_index, const uint8_t*
20592062
const uint8_t* Dmx::RdmReceive(uint32_t port_index) {
20602063
DMX_CHECK_PORT_INDEX_PTR(port_index);
20612064

2062-
if ((sv_rx_buffer[port_index].rdm.index & 0x4000) != 0x4000) {
2065+
if ((sv_rx_buffer[port_index].rdm.index & dmx::kRdmSlotsCompleteFlag) != dmx::kRdmSlotsCompleteFlag) {
20632066
return nullptr;
20642067
}
20652068

20662069
sv_rx_buffer[port_index].rdm.index = 0;
20672070

2068-
const auto* p = const_cast<const uint8_t*>(sv_rx_buffer[port_index].rdm.data);
2071+
const auto* data = const_cast<const uint8_t*>(sv_rx_buffer[port_index].rdm.data);
20692072

2070-
if (p[0] == E120_SC_RDM) {
2071-
const auto* rdm_command = reinterpret_cast<const struct TRdmMessage*>(p);
2073+
if (data[0] == E120_SC_RDM) {
2074+
const auto* rdm_command = reinterpret_cast<const struct TRdmMessage*>(data);
20722075

20732076
uint32_t i;
20742077
uint16_t checksum = 0;
20752078

20762079
for (i = 0; i < 24; i++) {
2077-
checksum = static_cast<uint16_t>(checksum + p[i]);
2080+
checksum = static_cast<uint16_t>(checksum + data[i]);
20782081
}
20792082

20802083
for (; i < rdm_command->message_length; i++) {
2081-
checksum = static_cast<uint16_t>(checksum + p[i]);
2084+
checksum = static_cast<uint16_t>(checksum + data[i]);
20822085
}
20832086

2084-
if (p[i++] == static_cast<uint8_t>(checksum >> 8)) {
2085-
if (p[i] == static_cast<uint8_t>(checksum)) {
2087+
if (data[i++] == static_cast<uint8_t>(checksum >> 8)) {
2088+
if (data[i] == static_cast<uint8_t>(checksum)) {
20862089
#if !defined(CONFIG_DMX_DISABLE_STATISTICS)
20872090
sv_total_statistics[port_index].rdm.received.good = sv_total_statistics[port_index].rdm.received.good + 1;
20882091
#endif
2089-
return p;
2092+
return data;
20902093
}
20912094
}
20922095
#if !defined(CONFIG_DMX_DISABLE_STATISTICS)
20932096
sv_total_statistics[port_index].rdm.received.bad = sv_total_statistics[port_index].rdm.received.bad + 1;
20942097
#endif
20952098
return nullptr;
2096-
} else {
2099+
}
2100+
20972101
#if !defined(CONFIG_DMX_DISABLE_STATISTICS)
2098-
sv_total_statistics[port_index].rdm.received.discovery_response = sv_total_statistics[port_index].rdm.received.discovery_response + 1;
2102+
sv_total_statistics[port_index].rdm.received.discovery_response = sv_total_statistics[port_index].rdm.received.discovery_response + 1;
20992103
#endif
2100-
}
21012104

2102-
return p;
2105+
return data;
21032106
}
21042107

21052108
// RDM Receive with timeout

lib-dmx/src/gd32/dmx_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
#define SECTION_DMA_BUFFER
5454
#endif
5555

56-
inline constexpr uint32_t DmxPortToUart(uint32_t port) {
56+
constexpr uint32_t DmxPortToUart(uint32_t port) {
5757
switch (port) {
5858
#if defined(DMX_USE_USART0)
5959
case dmx::config::kUsart0Port:
@@ -105,7 +105,7 @@ inline constexpr uint32_t DmxPortToUart(uint32_t port) {
105105
}
106106

107107
#if defined(GD32F4XX) || defined(GD32H7XX)
108-
inline constexpr uint32_t GetUsartAf(uint32_t usart_periph) {
108+
constexpr uint32_t GetUsartAf(uint32_t usart_periph) {
109109
switch (usart_periph) {
110110
#if defined(DMX_USE_USART0)
111111
case USART0:

0 commit comments

Comments
 (0)