Skip to content

Commit c460478

Browse files
committed
Refactor shared utils and cleanup warnings
Moves common error-printing and string constants into shared utils headers, then updates network, remoteconfig, EMAC, and timer code to use them. Also tightens several conditional compilation checks, replaces magic values with named constants, and cleans up a few small naming and formatting issues.
1 parent b32afed commit c460478

9 files changed

Lines changed: 139 additions & 78 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @file utils_print.h
3+
*
4+
*/
5+
/* Copyright (C) 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+
*/
25+
26+
#ifndef COMMON_UTILS_UTILS_PRINT_H_
27+
#define COMMON_UTILS_UTILS_PRINT_H_
28+
29+
#include <cstdio>
30+
31+
#include "firmware/ansi_colour.h"
32+
33+
namespace common::print {
34+
inline void Error(const char* func, const char* string) {
35+
printf("%s%s: %s%s\n", ansi::Colours::Fg::kRed, func, string, ansi::Colours::Fg::kDefault);
36+
}
37+
} // namespace common::print
38+
39+
#define ERROR(s) \
40+
do { \
41+
common::print::Error(__func__, (s)); \
42+
} while (false)
43+
44+
#endif // COMMON_UTILS_UTILS_PRINT_H_

common/include/common/utils/utils_string.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
#include <cstdint>
3030

3131
namespace common {
32+
inline constexpr char kWarning[] = "Warning";
33+
inline constexpr char kError[] = "Error";
34+
inline constexpr char kSuccess[] = "Success";
35+
inline constexpr char kUnknown[] = "Unknown";
36+
37+
constexpr const char* IsSuccess(bool is_success) {
38+
return is_success ? kSuccess : kError;
39+
}
40+
3241
constexpr uint32_t ConstStrLen(const char* str) {
3342
uint32_t len = 0;
3443
while (str[len] != '\0') {

common/include/common/utils/utils_units.h

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

3131
namespace common::units {
32-
inline constexpr uint32_t kUsPerMs = 1'000U;
33-
inline constexpr uint32_t kMsPerSecond = 1'000U;
34-
inline constexpr uint32_t kUsPerSecond = 1'000'000U;
32+
inline constexpr int32_t kSecondPerMinute = 60;
3533

36-
inline constexpr uint32_t kNsPerUs = 1'000U;
37-
inline constexpr uint32_t kNsPerMs = 1'000'000U;
38-
inline constexpr uint32_t kNsPerSecond = 1'000'000'000U;
34+
inline constexpr uint32_t kMsPerSecond = 1'000;
35+
36+
inline constexpr uint32_t kUsPerMs = 1'000;
37+
inline constexpr uint32_t kUsPerSecond = 1'000'000;
38+
39+
inline constexpr uint32_t kNsPerUs = 1'000;
40+
inline constexpr uint32_t kNsPerMs = 1'000'000;
41+
inline constexpr uint32_t kNsPerSecond = 1'000'000'000;
3942

4043
inline constexpr uint32_t k1KiB = 1024;
4144

lib-hwclock/src/hwclock.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <sys/time.h>
2828

2929
#include "hwclock.h"
30+
#include "common/utils/utils_string.h"
31+
#include "common/utils/utils_units.h"
3032
#include "watchdog.h"
3133
#include "timing.h"
3234

@@ -41,7 +43,7 @@ void HwClock::Print() {
4143
return;
4244
}
4345

44-
const char* type = "Unknown";
46+
const char* type = common::kUnknown;
4547

4648
switch (type_) {
4749
case rtc::Type::kMcP7941X:
@@ -87,7 +89,7 @@ void HwClock::HcToSys() {
8789
RtcGet(&rtc_t1);
8890
gettimeofday(&tv_t1, nullptr);
8991

90-
const auto kSecondsT1 = rtc_t1.tm_sec + rtc_t1.tm_min * 60;
92+
const auto kSecondsT1 = rtc_t1.tm_sec + (rtc_t1.tm_min * common::units::kSecondPerMinute);
9193
const auto kSeconds = mktime(&rtc_t1);
9294

9395
struct tm rtc_t2;
@@ -96,28 +98,28 @@ void HwClock::HcToSys() {
9698
while (true) {
9799
RtcGet(&rtc_t2);
98100

99-
const auto kSeconds2 = rtc_t2.tm_sec + rtc_t2.tm_min * 60;
101+
const auto kSeconds2 = rtc_t2.tm_sec + (rtc_t2.tm_min * common::units::kSecondPerMinute);
100102

101103
if (kSecondsT1 != kSeconds2) {
102104
gettimeofday(&tv_t2, nullptr);
103105
break;
104106
}
105107
}
106108

107-
struct timeval tv;
108-
tv.tv_sec = kSeconds;
109+
struct timeval time_val;
110+
time_val.tv_sec = kSeconds;
109111

110112
if (tv_t2.tv_sec == tv_t1.tv_sec) {
111-
tv.tv_usec = 1000000 - (tv_t2.tv_usec - tv_t1.tv_usec);
113+
time_val.tv_usec = 1000000 - (tv_t2.tv_usec - tv_t1.tv_usec);
112114
} else {
113115
if (tv_t2.tv_usec - tv_t1.tv_usec >= 0) {
114-
tv.tv_usec = tv_t2.tv_usec - tv_t1.tv_usec;
116+
time_val.tv_usec = tv_t2.tv_usec - tv_t1.tv_usec;
115117
} else {
116-
tv.tv_usec = tv_t1.tv_usec - tv_t2.tv_usec;
118+
time_val.tv_usec = tv_t1.tv_usec - tv_t2.tv_usec;
117119
}
118120
}
119121

120-
settimeofday(&tv, nullptr);
122+
settimeofday(&time_val, nullptr);
121123

122124
last_hc_to_sys_millis_ = timing::Millis();
123125

lib-pixel/include/pixelpatterns.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include <cstdint>
3232

33+
#include "common/utils/utils_string.h"
3334
#include "pixel.h"
3435
#include "pixelconfiguration.h"
3536
#include "timing.h"
@@ -85,10 +86,10 @@ class PixelPatterns {
8586
return pixelpatterns::kPatternName[static_cast<uint32_t>(pattern)];
8687
}
8788

88-
return "Unknown";
89+
return common::kUnknown;
8990
}
9091

91-
uint32_t GetActivePorts() const { return s_active_ports; }
92+
[[nodiscard]] uint32_t GetActivePorts() const { return s_active_ports; }
9293

9394
void RainbowCycle(uint32_t port_index, uint32_t interval, pixelpatterns::Direction direction = pixelpatterns::Direction::kForward) {
9495
Clear(port_index);
@@ -288,8 +289,8 @@ class PixelPatterns {
288289
}
289290

290291
uint32_t DimColour(uint32_t colour) {
291-
const pixel::PixelColours kC(colour);
292-
return pixel::GetColour(static_cast<uint8_t>(kC.Red() >> 1), static_cast<uint8_t>(kC.Green() >> 1), static_cast<uint8_t>(kC.Blue() >> 1));
292+
const pixel::PixelColours kColour(colour);
293+
return pixel::GetColour(static_cast<uint8_t>(kColour.Red() >> 1), static_cast<uint8_t>(kColour.Green() >> 1), static_cast<uint8_t>(kColour.Blue() >> 1));
293294
}
294295

295296
void Clear(uint32_t port_index) { pixel::SetPixelColour(port_index, 0); }

lib-pixel/include/pixeltype.h

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,42 +40,42 @@ inline constexpr uint32_t kNoSpeedHz = 0;
4040
inline constexpr uint8_t kNoCode = 0;
4141

4242
enum class LedType : uint8_t {
43-
kWS2801, //
44-
kWS2811, //
45-
kWS2812, //
46-
kWS2812B, //
47-
kWS2813, //
48-
kWS2815, //
49-
kSK6812, //
50-
kSK6812W, //
51-
kUCS1903, //
52-
kUCS2903, //
53-
kCS8812, //
54-
kAPA102, //
55-
kSK9822, //
56-
kP9813, //
57-
kUndefined //
43+
kWS2801, //
44+
kWS2811, //
45+
kWS2812, //
46+
kWS2812B, //
47+
kWS2813, //
48+
kWS2815, //
49+
kSK6812, //
50+
kSK6812W, //
51+
kUCS1903, //
52+
kUCS2903, //
53+
kCS8812, //
54+
kAPA102, //
55+
kSK9822, //
56+
kP9813, //
57+
kUndefined, //
5858
};
5959

6060
enum class LedMap : uint8_t {
61-
kRGB, //
62-
kRBG, //
63-
kGRB, //
64-
kGBR, //
65-
kBRG, //
66-
kBGR, //
67-
kRGBW, //
68-
kUndefined //
61+
kRGB, //
62+
kRBG, //
63+
kGRB, //
64+
kGBR, //
65+
kBRG, //
66+
kBGR, //
67+
kRGBW, //
68+
kUndefined, //
6969
};
7070

7171
inline constexpr char kMaps[static_cast<uint32_t>(pixel::LedMap::kUndefined)][5] = {
72-
"RGB", //
73-
"RBG", //
74-
"GRB", //
75-
"GBR", //
76-
"BRG", //
77-
"BGR", //
78-
"RGBW" //
72+
"RGB", //
73+
"RBG", //
74+
"GRB", //
75+
"GBR", //
76+
"BRG", //
77+
"BGR", //
78+
"RGBW", //
7979
};
8080

8181
constexpr uint32_t kMapsCount = static_cast<uint32_t>(sizeof(kMaps) / sizeof(kMaps[0]));
@@ -186,7 +186,7 @@ constexpr const char* GetTypeName(LedType type) {
186186
return kTypeInfo[kIndex].name;
187187
}
188188

189-
return "Unknown";
189+
return common::kUnknown;
190190
}
191191

192192
inline LedType GetTypeByName(const char* string) {
@@ -208,7 +208,7 @@ inline const char* GetMapName(LedMap map) {
208208
return kMaps[kIndex];
209209
}
210210

211-
return "Unknown";
211+
return common::kUnknown;
212212
}
213213

214214
inline LedMap GetMapByName(const char* string) {

lib-rdmsensor/include/rdm_sensors.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <cstring>
3131
#include <cassert>
3232

33+
#include "common/utils/utils_string.h"
3334
#include "json/rdmsensorsparams.h"
3435

3536
namespace rdm::sensors {
@@ -45,13 +46,13 @@ enum class Types : uint32_t {
4546

4647
static_assert(json::RdmSensorsParams::KeysSize() == static_cast<size_t>(Types::kUndefined));
4748

48-
[[nodiscard]] inline constexpr const char* GetType(Types type) {
49+
[[nodiscard]] constexpr const char* GetType(Types type) {
4950
if (type < rdm::sensors::Types::kUndefined) {
5051
const auto& k = json::RdmSensorsParams::Keys();
5152
return k[static_cast<uint32_t>(type)].GetName();
5253
}
5354

54-
return "Unknown";
55+
return common::kUnknown;
5556
}
5657

5758
inline Types GetType(const char* string) {

lib-superloop/include/superloop/softwaretimers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ using TimerCallbackFunction_t = void (*)(TimerHandle_t);
4040

4141
inline constexpr TimerHandle_t kTimerIdNone = -1;
4242

43-
TimerHandle_t SoftwareTimerAdd(uint32_t interval_millis, TimerCallbackFunction_t k_callback);
43+
TimerHandle_t SoftwareTimerAdd(uint32_t interval_millis, TimerCallbackFunction_t k_callback_function);
4444
bool SoftwareTimerDelete(TimerHandle_t& handle);
4545
bool SoftwareTimerChange(TimerHandle_t handle, uint32_t interval_millis);
4646

0 commit comments

Comments
 (0)