Skip to content

Commit 1a37e54

Browse files
committed
Refactor UTC globals and tighten debug formatting
Replace the `Global` singleton with inline helpers in the `global` namespace for UTC offset access/validation, simplifying usage while preserving behavior. Update multiple debug headers to use safer `printf` argument casts and minor const/unsigned cleanups, and remove noisy command TX/RX byte logging from the GD32 flasher while keeping timeout/error reporting.
1 parent c9b8857 commit 1a37e54

6 files changed

Lines changed: 62 additions & 89 deletions

File tree

common/include/firmware/debug/debug_dump.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file debug_dump.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
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -46,27 +46,27 @@ static inline void Dump([[maybe_unused]] const void* data, [[maybe_unused]] uint
4646
#else
4747
inline void Dump(const void* data, uint32_t size) {
4848
uint32_t chars = 0;
49-
const auto* p = reinterpret_cast<const uint8_t*>(data);
49+
const auto* ptr = reinterpret_cast<const uint8_t*>(data);
5050

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

5353
do {
5454
uint32_t chars_this_line = 0;
5555

56-
printf("%04x ", chars);
56+
printf("%04x ", static_cast<unsigned>(chars));
5757

58-
const auto* q = p;
58+
const auto* q = ptr;
5959

6060
while ((chars_this_line < dump::kCharsPerLine) && (chars < size)) {
6161
if (chars_this_line % 8 == 0) {
6262
printf(" ");
6363
}
6464

65-
printf("%02x ", *p);
65+
printf("%02x ", *ptr);
6666

6767
chars_this_line++;
6868
chars++;
69-
p++;
69+
ptr++;
7070
}
7171

7272
auto chars_dot_line = chars_this_line;

common/include/firmware/debug/debug_i2cdetect.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void Detect() {
4444
puts("\n 0 1 2 3 4 5 6 7 8 9 a b c d e f");
4545

4646
for (uint32_t i = 0; i < 128; i = (i + 16)) {
47-
printf("%02x: ", i);
47+
printf("%02x: ", static_cast<unsigned>(i));
4848
for (uint32_t j = 0; j < 16; j++) {
4949
// Skip unwanted addresses
5050
if ((i + j < kFirst) || (i + j > kLast)) {
@@ -53,7 +53,7 @@ void Detect() {
5353
}
5454

5555
if (::i2c::IsConnected(static_cast<uint8_t>(i + j))) {
56-
printf("%02x ", i + j);
56+
printf("%02x ", static_cast<unsigned>(i + j));
5757
} else {
5858
printf("-- ");
5959
}

common/include/firmware/debug/debug_stack.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ inline void Print() {
4545
assert(end > start);
4646
const auto kSize = static_cast<uint32_t>(end - start);
4747

48-
auto* p = start;
48+
const auto* ptr = start;
4949

50-
while (p < end) {
51-
if (*p != kMagicWord) {
50+
while (ptr < end) {
51+
if (*ptr != kMagicWord) {
5252
break;
5353
}
54-
p++;
54+
ptr++;
5555
}
5656

57-
const auto kUsedBytes = static_cast<uint32_t>(4 * (end - p));
58-
const auto kFreeBytes = static_cast<uint32_t>(4 * (p - start));
59-
const auto kFreePct = (static_cast<uint32_t>(p - start) * 100U) / kSize;
57+
const auto kUsedBytes = static_cast<uint32_t>(4 * (end - ptr));
58+
const auto kFreeBytes = static_cast<uint32_t>(4 * (ptr - start));
59+
const auto kFreePct = (static_cast<uint32_t>(ptr - start) * 100U) / kSize;
6060

6161
if (s_used_bytes_previous != kUsedBytes) {
6262
s_used_bytes_previous = kUsedBytes;
@@ -70,9 +70,9 @@ inline void Print() {
7070
}
7171

7272
#ifndef NDEBUG
73-
printf("Stack: Size %uKB, [%p:%p:%p], Used: %u, Free: %u [%u]", kSize / (1024 / 4), start, p, end, kUsedBytes, kFreeBytes, kFreePct);
73+
printf("Stack: Size %uKB, [%p:%p:%p], Used: %u, Free: %u [%u]", static_cast<unsigned>(kSize / (1024 / 4)), reinterpret_cast<const void *>(start), reinterpret_cast<const void *>(ptr), reinterpret_cast<const void *>(end), static_cast<unsigned>(kUsedBytes), static_cast<unsigned>(kFreeBytes), static_cast<unsigned>(kFreePct));
7474
#else
75-
printf("Stack: Size %uKB, Used: %u, Free: %u", kSize / (1024 / 4), kUsedBytes, kFreeBytes);
75+
printf("Stack: Size %uKB, Used: %u, Free: %u", static_cast<unsigned>(kSize / (1024 / 4)), static_cast<unsigned>(kUsedBytes), static_cast<unsigned>(kFreeBytes));
7676
#endif
7777
printf("\x1b[39m\n");
7878
}
@@ -81,7 +81,7 @@ inline void Print() {
8181
inline void Run() {
8282
static uint32_t s_millis_previous;
8383
const auto kMillis = timing::Millis();
84-
if (kMillis - s_millis_previous >= 1000) {
84+
if (kMillis - s_millis_previous >= 1000U) {
8585
s_millis_previous = kMillis;
8686
Print();
8787
}

common/include/firmware/pixeldmx/show.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
namespace common::firmware::pixeldmx {
3737
inline void Show(uint32_t line, pixelpatterns::Pattern pattern) {
38-
DEBUG_PRINTF("line=%u, pattern=%u", line, static_cast<uint32_t>(pattern));
38+
DEBUG_PRINTF("line=%u, pattern=%u", static_cast<unsigned>(line), static_cast<unsigned>(pattern));
3939

4040
auto& configuration = PixelDmxConfiguration::Get();
4141
auto* display = Display::Get();
@@ -44,8 +44,8 @@ inline void Show(uint32_t line, pixelpatterns::Pattern pattern) {
4444
display->ClearEndOfLine();
4545
display->Printf(line, "%s:%d G%d %s",
4646
pixel::GetTypeName(configuration.GetType()),
47-
configuration.GetCount(),
48-
configuration.GetGroupingCount(),
47+
static_cast<unsigned>(configuration.GetCount()),
48+
static_cast<unsigned>(configuration.GetGroupingCount()),
4949
pixel::GetMapName(configuration.GetMap())
5050
);
5151

@@ -55,7 +55,7 @@ inline void Show(uint32_t line, pixelpatterns::Pattern pattern) {
5555
if (pattern != pixelpatterns::Pattern::kNone) {
5656
display->Printf(6, "%s:%u",
5757
PixelPatterns::GetName(pattern),
58-
static_cast<uint32_t>(pattern)
58+
static_cast<unsigned>(pattern)
5959
);
6060
}
6161
}

common/include/global.h

Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,71 +16,51 @@
1616

1717
#include "utc.h"
1818

19+
namespace global {
20+
1921
/**
20-
* @class Global
21-
* @brief Singleton class for managing and validating UTC offsets.
22-
*
23-
* The Global class provides methods to set and get the UTC offset in seconds.
24-
* It also includes validation logic for standard UTC time zones.
22+
* @brief Gets the current UTC offset in seconds.
23+
* @return UTC offset in seconds.
2524
*/
26-
class Global {
27-
public:
28-
/**
29-
* @brief Get the singleton instance of the Global class.
30-
* @return Reference to the singleton Global object.
31-
*/
32-
static Global& Instance() {
33-
static Global instance;
34-
return instance;
35-
}
25+
inline int32_t GetUtcOffset() {
26+
return global::g_utc_offset;
27+
}
3628

37-
/**
38-
* @brief Gets the current UTC offset in seconds.
39-
* @return UTC offset in seconds.
40-
*/
41-
int32_t GetUtcOffset() const { return global::g_utc_offset; }
42-
43-
/**
44-
* @brief Gets the current UTC offset as (hours, minutes).
45-
* @param[out] hours Signed hour component.
46-
* @param[out] minutes Unsigned minute component.
47-
*/
48-
void GetUtcOffset(int32_t& hours, uint32_t& minutes) { utc::SplitOffset(global::g_utc_offset, hours, minutes); }
29+
/**
30+
* @brief Gets the current UTC offset as (hours, minutes).
31+
* @param[out] hours Signed hour component.
32+
* @param[out] minutes Unsigned minute component.
33+
*/
34+
inline void GetUtcOffset(int32_t& hours, uint32_t& minutes) {
35+
utc::SplitOffset(global::g_utc_offset, hours, minutes);
36+
}
4937

50-
/**
51-
* @brief Sets the global UTC offset if the value is valid.
52-
* @param utc_offset_seconds Offset in seconds
53-
* @return true if successfully set; false otherwise
54-
*/
55-
bool SetUtcOffsetIfValid(int32_t utc_offset_seconds) {
56-
if (utc::IsValidOffset(utc_offset_seconds)) {
57-
::global::g_utc_offset = utc_offset_seconds;
58-
return true;
59-
}
60-
return false;
38+
/**
39+
* @brief Sets the global UTC offset if the value is valid.
40+
* @param utc_offset_seconds Offset in seconds
41+
* @return true if successfully set; false otherwise
42+
*/
43+
inline bool SetUtcOffsetIfValid(int32_t utc_offset_seconds) {
44+
if (utc::IsValidOffset(utc_offset_seconds)) {
45+
::global::g_utc_offset = utc_offset_seconds;
46+
return true;
6147
}
48+
return false;
49+
}
6250

63-
/**
64-
* @brief Sets the global UTC offset from (hours, minutes) if valid.
65-
* @param hours Signed hour component
66-
* @param minutes Unsigned minute component
67-
* @return true if valid and set; false otherwise
68-
*/
69-
bool SetUtcOffsetIfValid(int32_t hours, uint32_t minutes) {
70-
int32_t offset_seconds;
71-
if (utc::ValidateOffset(hours, minutes, offset_seconds)) {
72-
return SetUtcOffsetIfValid(offset_seconds);
73-
}
74-
return false;
51+
/**
52+
* @brief Sets the global UTC offset from (hours, minutes) if valid.
53+
* @param hours Signed hour component
54+
* @param minutes Unsigned minute component
55+
* @return true if valid and set; false otherwise
56+
*/
57+
inline bool SetUtcOffsetIfValid(int32_t hours, uint32_t minutes) {
58+
int32_t offset_seconds;
59+
if (utc::ValidateOffset(hours, minutes, offset_seconds)) {
60+
return SetUtcOffsetIfValid(offset_seconds);
7561
}
76-
77-
private:
78-
Global() = default;
79-
// Delete copy/move constructors and assignment operators
80-
Global(const Global&) = delete;
81-
Global& operator=(const Global&) = delete;
82-
Global(Global&&) = delete;
83-
Global& operator=(Global&&) = delete;
84-
};
62+
return false;
63+
}
64+
} // namespace global
8565

8666
#endif // GLOBAL_H_

common/scripts/gd32/flash.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,6 @@ def _try_sync(self, attempts=3):
177177
def _send_command(self, cmd):
178178
command = bytes([cmd, cmd ^ 0xFF])
179179

180-
print(
181-
f" Command TX: "
182-
f"0x{command[0]:02X} 0x{command[1]:02X}"
183-
)
184-
185180
self.port.write(command)
186181
self.port.flush()
187182

@@ -191,8 +186,6 @@ def _send_command(self, cmd):
191186
print(f" Command 0x{cmd:02X}: timeout")
192187
return False
193188

194-
print(f" Command RX: 0x{response[0]:02X}")
195-
196189
if response[0] == self.ACK:
197190
return True
198191

0 commit comments

Comments
 (0)