Skip to content

Commit 49756be

Browse files
committed
Refine debug macros and timer API cleanup
Modernizes debug utilities by switching DEBUG_* location reporting to `std::source_location`, cleaning up formatting, and tightening dump/bit-print helpers. It also improves type safety in `printf` calls with explicit unsigned casts, adds extra status LED debug traces, and performs a small SoftwareTimers API/implementation cleanup (using aliases, clearer parameter names, and clearer local variable naming) without changing behaviour.
1 parent 8df86d6 commit 49756be

9 files changed

Lines changed: 91 additions & 121 deletions

File tree

common/include/firmware/debug/debug_debug.h

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,54 @@
22
* @file debug_debug.h
33
*
44
*/
5-
/* Copyright (C) 2018-2025 by Arjan van Vught mailto:info@gd32-dmx.org */
5+
/* Copyright (C) 2018-2026 by Arjan van Vught mailto:info@gd32-dmx.org */
66

77
#ifndef FIRMWARE_DEBUG_DEBUG_H_
88
#define FIRMWARE_DEBUG_DEBUG_H_
99

1010
#if !defined(NDEBUG)
1111
#include <cstdio>
12+
#include <source_location>
1213

13-
#define DEBUG_ENTRY() \
14-
do \
15-
{ \
16-
printf("-> %s:%s:%d\n", __FILE__, __func__, __LINE__); \
14+
#define DEBUG_ENTRY() \
15+
do { \
16+
const std::source_location loc = std::source_location::current(); \
17+
printf("-> %s(%u):%s\n", loc.file_name(), static_cast<unsigned>(loc.line()), loc.function_name()); \
1718
} while (0)
1819

19-
#define DEBUG_EXIT() \
20-
do \
21-
{ \
22-
printf("<- %s:%s:%d\n", __FILE__, __func__, __LINE__); \
20+
#define DEBUG_EXIT() \
21+
do { \
22+
const std::source_location loc = std::source_location::current(); \
23+
printf("<- %s(%u):%s\n", loc.file_name(), static_cast<unsigned>(loc.line()), loc.function_name()); \
2324
} while (0)
2425

25-
#define DEBUG_PRINTF(fmt, ...) \
26-
do \
27-
{ \
28-
printf("%s() %s:%d: " fmt "\n", __func__, __FILE__, __LINE__ __VA_OPT__(, ) __VA_ARGS__); \
26+
#define DEBUG_PRINTF(fmt, ...) \
27+
do { \
28+
const std::source_location loc = std::source_location::current(); \
29+
printf(" %s(%u):%s: " fmt "\n", loc.file_name(), static_cast<unsigned>(loc.line()), loc.function_name() __VA_OPT__(, ) __VA_ARGS__); \
2930
} while (0)
3031

3132
#define DEBUG_PUTS(msg) \
32-
do \
33-
{ \
33+
do { \
3434
DEBUG_PRINTF("%s", (msg)); \
3535
} while (0)
3636

3737
#else
3838

3939
#define DEBUG_ENTRY() \
40-
do \
41-
{ \
40+
do { \
4241
} while (0)
4342

4443
#define DEBUG_EXIT() \
45-
do \
46-
{ \
44+
do { \
4745
} while (0)
4846

4947
#define DEBUG_PRINTF(...) \
50-
do \
51-
{ \
48+
do { \
5249
} while (0)
5350

5451
#define DEBUG_PUTS(...) \
55-
do \
56-
{ \
52+
do { \
5753
} while (0)
5854

5955
#endif

common/include/firmware/debug/debug_dump.h

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,26 @@
3030
#include <cstdio>
3131
#include <ctype.h>
3232

33-
#if defined(H3)
34-
namespace uart0 {
35-
int Printf(const char* fmt, ...);
36-
}
37-
#define printf uart0::Printf // NOLINT
38-
#endif
39-
4033
namespace debug {
41-
namespace dump {
42-
inline constexpr uint32_t kCharsPerLine = 16;
43-
}
4434
#ifdef NDEBUG
4535
static inline void Dump([[maybe_unused]] const void* data, [[maybe_unused]] uint32_t size) {}
4636
#else
4737
inline void Dump(const void* data, uint32_t size) {
48-
uint32_t chars = 0;
38+
constexpr uint32_t kCharsPerLine = 16;
39+
constexpr uint32_t kBytesPerGroup = 8; // Visual separator every 8 bytes
4940
const auto* ptr = reinterpret_cast<const uint8_t*>(data);
41+
uint32_t chars = 0;
5042

51-
printf("%p:%u\n", data, static_cast<int>(size));
43+
printf("%p:%u\n", data, static_cast<unsigned>(size));
5244

5345
do {
54-
uint32_t chars_this_line = 0;
55-
5646
printf("%04x ", static_cast<unsigned>(chars));
5747

58-
const auto* q = ptr;
48+
uint32_t chars_this_line = 0;
49+
const auto* line_start_ptr = ptr;
5950

60-
while ((chars_this_line < dump::kCharsPerLine) && (chars < size)) {
61-
if (chars_this_line % 8 == 0) {
51+
while ((chars_this_line < kCharsPerLine) && (chars < size)) {
52+
if (chars_this_line % kBytesPerGroup == 0) {
6253
printf(" ");
6354
}
6455

@@ -71,8 +62,8 @@ inline void Dump(const void* data, uint32_t size) {
7162

7263
auto chars_dot_line = chars_this_line;
7364

74-
for (; chars_this_line < dump::kCharsPerLine; chars_this_line++) {
75-
if (chars_this_line % 8 == 0) {
65+
for (; chars_this_line < kCharsPerLine; chars_this_line++) {
66+
if (chars_this_line % kBytesPerGroup == 0) {
7667
printf(" ");
7768
}
7869
printf(" ");
@@ -81,19 +72,19 @@ inline void Dump(const void* data, uint32_t size) {
8172
chars_this_line = 0;
8273

8374
while (chars_this_line < chars_dot_line) {
84-
if (chars_this_line % 8 == 0) {
75+
if (chars_this_line % kBytesPerGroup == 0) {
8576
printf(" ");
8677
}
8778

88-
int ch = *q;
89-
if (isprint(ch)) {
90-
printf("%c", ch);
79+
int character = *line_start_ptr;
80+
if (0 != isprint(character)) {
81+
printf("%c", character);
9182
} else {
9283
printf(".");
9384
}
9485

9586
chars_this_line++;
96-
q++;
87+
line_start_ptr++;
9788
}
9889

9990
puts("");

common/include/firmware/debug/debug_printbits.h

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,43 @@
22
* @file debug_printbits.h
33
*
44
*/
5-
/* Copyright (C) 2025 by Arjan van Vught mailto:info@gd32-dmx.org
6-
*
7-
* Permission is hereby granted, free of charge, to any person obtaining a copy
8-
* of this software and associated documentation files (the "Software"), to deal
9-
* in the Software without restriction, including without limitation the rights
10-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
* copies of the Software, and to permit persons to whom the Software is
12-
* furnished to do so, subject to the following conditions:
13-
14-
* The above copyright notice and this permission notice shall be included in
15-
* all copies or substantial portions of the Software.
16-
17-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
* THE SOFTWARE.
24-
*/
5+
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
2525

2626
#ifndef COMMON_DEBUG_DEBUG_PRINTBITS_H_
2727
#define COMMON_DEBUG_DEBUG_PRINTBITS_H_
2828

2929
#include <cstdio>
3030
#include <cstdint>
3131

32-
#if defined(H3)
33-
namespace uart0
34-
{
35-
int Printf(const char* fmt, ...);
36-
}
37-
#define printf uart0::Printf // NOLINT
38-
#endif
39-
40-
namespace debug
41-
{
32+
namespace debug {
4233
#ifdef NDEBUG
4334
inline void PrintBits([[maybe_unused]] uint32_t u) {}
4435
#else
4536
inline void PrintBits(uint32_t u) {
46-
printf("%.8x ", u);
37+
printf("%.8x ", u);
4738
uint32_t b = 1U << 31;
4839

49-
for (uint32_t i = 0; i < 32; i++)
50-
{
51-
if ((b & u) == b)
52-
{
40+
for (uint32_t i = 0; i < 32; i++) {
41+
if ((b & u) == b) {
5342
uint32_t bit_number = 31 - i;
5443
printf("%-2d ", bit_number);
5544
}

lib-board/src/board_statusled.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* THE SOFTWARE.
2323
*/
2424

25+
#include <concepts>
2526
#if defined(DEBUG_HAL)
2627
#undef NDEBUG
2728
#endif
@@ -43,10 +44,12 @@ enum class ModeToFrequency { kOffOff = 0, kNormal = 1, kData = 3, kFast = 5, kRe
4344
#if !defined(CONFIG_HAL_USE_MINIMUM)
4445

4546
void __attribute__((weak)) Event([[maybe_unused]] Mode mode) {
46-
DEBUG_PRINTF("mode=%u", static_cast<uint32_t>(mode));
47+
DEBUG_PRINTF("mode=%u", static_cast<unsigned>(mode));
4748
}
4849

4950
void SetModeWithLock(Mode mode, bool do_lock) {
51+
DEBUG_PRINTF("mode=%u, do_lock=%c", static_cast<unsigned>(mode), do_lock ? 'Y' : 'N');
52+
5053
s_do_lock = false;
5154
SetMode(mode);
5255
s_do_lock = do_lock;
@@ -56,6 +59,8 @@ void SetMode(board::statusled::Mode mode) {
5659
if (s_do_lock || (global::g_status_led_mode == mode)) {
5760
return;
5861
}
62+
63+
DEBUG_PRINTF("mode=%u", static_cast<unsigned>(mode));
5964

6065
global::g_status_led_mode = mode;
6166

@@ -85,7 +90,7 @@ void SetMode(board::statusled::Mode mode) {
8590

8691
board::statusled::Event(global::g_status_led_mode);
8792

88-
DEBUG_PRINTF("global::g_status_led_mode=%u", static_cast<uint32_t>(global::g_status_led_mode));
93+
DEBUG_PRINTF("global::g_status_led_mode=%u", static_cast<unsigned>(global::g_status_led_mode));
8994
}
9095
#endif
9196
} // namespace board::statusled

lib-ddp/src/ddpdisplay.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ void DdpDisplay::Input(const uint8_t* buffer, uint32_t size, [[maybe_unused]] ui
295295

296296
void DdpDisplay::Print() {
297297
puts("DDP Display");
298-
printf(" Count : %u\n", count_);
299-
printf(" Channels per pixel: %u\n", GetChannelsPerPixel());
300-
printf(" Active ports : %u\n", active_ports_);
298+
printf(" Count : %u\n", static_cast<unsigned>(count_));
299+
printf(" Channels per pixel: %u\n", static_cast<unsigned>(GetChannelsPerPixel()));
300+
printf(" Active ports : %u\n", static_cast<unsigned>(active_ports_));
301301
}

lib-remoteconfig/http/content/modern/config_ltc.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,6 @@ function validateLtcForm(values, root) {
184184
return false;
185185
}
186186

187-
const fps = Number(values.fps);
188-
const start = timecodeTotal(values.start_hour, values.start_minute, values.start_second, values.start_frame, fps);
189-
const stop = timecodeTotal(values.stop_hour, values.stop_minute, values.stop_second, values.stop_frame, fps);
190-
191-
if (stop <= start) {
192-
const stopFrame = root.querySelector("[data-key='stop_frame']");
193-
stopFrame.setCustomValidity("Stop timecode must be greater than start timecode.");
194-
stopFrame.reportValidity();
195-
return false;
196-
}
197-
198187
return true;
199188
}
200189

lib-remoteconfig/src/httpd/httpdhandlerequest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ http::Status HttpDeamonHandleRequest::HandlePostUpload() {
564564
auto part_uri = &uri_[7];
565565

566566
if (memcmp(part_uri, "_start", 7) == 0) {
567-
printf("Firmware: %s -> %u bytes\n", upload_filename_, upload_size_);
567+
printf("Firmware: %s -> %u bytes\n", upload_filename_, static_cast<unsigned>(upload_size_));
568568

569569
if (strncmp(upload_filename_, firmware::kFileName, sizeof(upload_filename_)) != 0) {
570570
puts("Wrong firmware file name.");
@@ -598,7 +598,7 @@ http::Status HttpDeamonHandleRequest::HandlePostUpload() {
598598
Display::Get()->Progress();
599599

600600
uint32_t data_written;
601-
printf("%u\n", request_data_length_);
601+
printf("%u\n", static_cast<unsigned>(request_data_length_));
602602
if (!(FlashCodeInstall::Get()->WriteChunk(reinterpret_cast<uint8_t*>(file_data_), request_data_length_, data_written))) {
603603
DEBUG_PRINTF("WriteChunk failed. Data written:%u bytes", data_written);
604604
DEBUG_EXIT();
@@ -620,7 +620,7 @@ http::Status HttpDeamonHandleRequest::HandlePostUpload() {
620620
return http::Status::kInternalServerError;
621621
}
622622

623-
printf("Written bytes -> %u [%s]\n", write_count, write_count == upload_size_ ? "Ok" : "Wrong");
623+
printf("Written bytes -> %u [%s]\n", static_cast<unsigned>(write_count), write_count == upload_size_ ? "Ok" : "Wrong");
624624

625625
content_size_ = static_cast<uint32_t>(snprintf(dynamic_content_, sizeof(dynamic_content_), "{\"status\":\"ok\"}"));
626626
content_ = reinterpret_cast<uint8_t*>(dynamic_content_);

lib-superloop/include/superloop/softwaretimers.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ static constexpr uint32_t kSoftwareTimersMax =
3535
12;
3636
#endif
3737

38-
typedef int32_t TimerHandle_t;
39-
typedef void (*TimerCallbackFunction_t)(TimerHandle_t);
38+
using TimerHandle_t = int32_t;
39+
using TimerCallbackFunction_t = void (*)(TimerHandle_t);
4040

4141
inline constexpr TimerHandle_t kTimerIdNone = -1;
4242

43-
TimerHandle_t SoftwareTimerAdd(uint32_t interval_millis, const TimerCallbackFunction_t kCallback);
44-
bool SoftwareTimerDelete(TimerHandle_t& id);
45-
bool SoftwareTimerChange(TimerHandle_t id, uint32_t interval_millis);
43+
TimerHandle_t SoftwareTimerAdd(uint32_t interval_millis, TimerCallbackFunction_t k_callback);
44+
bool SoftwareTimerDelete(TimerHandle_t& handle);
45+
bool SoftwareTimerChange(TimerHandle_t handle, uint32_t interval_millis);
4646

4747
void SoftwareTimerRun();
4848

0 commit comments

Comments
 (0)