Skip to content

Commit c81ad52

Browse files
committed
Refactor utils
This change introduces new shared utility headers (`utils_math.h`, `utils_float.h`, and renamed `utils_bitfield.h`) and updates code to use `common::Min/Max` instead of `<algorithm>` helpers for better freestanding compatibility. It also relocates core headers like `ansi_colour.h`, `global.h`, and `utc.h` under `common/include/firmware/` and updates include guards and includes accordingly. Additional cleanup includes minor API annotations/formatting improvements and Makefile logic updates to derive `CONFIG_FATFS_MKFS` from FATFS RAM/SPI options.
1 parent d254583 commit c81ad52

39 files changed

Lines changed: 327 additions & 222 deletions

File tree

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* @file utils_port.h
2+
* @file utils_bitfield.h
33
*
44
*/
5-
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 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
@@ -23,24 +23,22 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
#ifndef COMMON_UTILS_UTILS_PORT_H_
27-
#define COMMON_UTILS_UTILS_PORT_H_
26+
#ifndef COMMON_UTILS_UTILS_BITFIELD_H_
27+
#define COMMON_UTILS_UTILS_BITFIELD_H_
2828

2929
#include <cstdint>
3030

3131
namespace common {
32-
template <class S>
33-
void PortSet(uint32_t port_index, S s, uint16_t& n) {
34-
uint16_t value = n; // Create a local copy
35-
value &= static_cast<uint16_t>(~(0x3 << (port_index * 2)));
36-
value |= static_cast<uint16_t>((static_cast<uint32_t>(s) & 0x3) << (port_index * 2));
37-
n = value; // Write back to the original field
32+
template <class T>
33+
void Set2BitField(uint32_t index, T value, uint16_t& packed) {
34+
packed &= static_cast<uint16_t>(~(0x3U << (index * 2U)));
35+
packed |= static_cast<uint16_t>((static_cast<uint32_t>(value) & 0x3U) << (index * 2U));
3836
}
3937

40-
template <class S>
41-
S PortGet(uint32_t port_index, uint16_t n) {
42-
return static_cast<S>((n >> (port_index * 2)) & 0x3);
38+
template <class T>
39+
T Get2BitField(uint32_t index, uint16_t packed) {
40+
return static_cast<T>((packed >> (index * 2U)) & 0x3U);
4341
}
4442
} // namespace common
4543

46-
#endif // COMMON_UTILS_UTILS_PORT_H_
44+
#endif // COMMON_UTILS_UTILS_BITFIELD_H_
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @file utils_float.h
3+
*
4+
*/
5+
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org */
6+
7+
#ifndef COMMON_UTILS_UTILS_FLOAT_H_
8+
#define COMMON_UTILS_UTILS_FLOAT_H_
9+
10+
#include <cstdint>
11+
12+
namespace common {
13+
inline void FloatCopyTo(uint8_t (&out)[4], float f) noexcept {
14+
static_assert(sizeof(float) == 4, "Requires 32-bit float");
15+
__builtin_memcpy(out, &f, sizeof(f));
16+
}
17+
18+
inline float FloatCopyFrom(const uint8_t (&in)[4]) noexcept {
19+
static_assert(sizeof(float) == 4, "Requires 32-bit float");
20+
float f;
21+
__builtin_memcpy(&f, in, sizeof(f));
22+
return f;
23+
}
24+
} // namespace common
25+
26+
#endif // COMMON_UTILS_UTILS_FLOAT_H_
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @file utils_math.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_MATH_H_
27+
#define COMMON_UTILS_UTILS_MATH_H_
28+
29+
// <algorithm> is not part of freestanding C++23
30+
31+
namespace common {
32+
template <typename T>
33+
constexpr T Min(T a, T b) {
34+
return b < a ? b : a;
35+
}
36+
37+
template <typename T>
38+
constexpr T Max(T a, T b) {
39+
return a < b ? b : a;
40+
}
41+
42+
template <class T, class Compare>
43+
constexpr const T& Clamp(const T& value, const T& low, const T& high, Compare comp) {
44+
return comp(value, low) ? low : comp(high, value) ? high : value;
45+
}
46+
47+
template <class T>
48+
constexpr const T& Clamp(const T& value, const T& low, const T& high) {
49+
return clamp(value, low, high, [](const T& a, const T& b) { return a < b; });
50+
}
51+
} // namespace common
52+
53+
#endif // UTILS_UTILS_MATH_H_
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
#ifndef ANSI_COLOUR_H_
27-
#define ANSI_COLOUR_H_
26+
#ifndef FIRMWARE_ANSI_COLOUR_H_
27+
#define FIRMWARE_ANSI_COLOUR_H_
2828

2929
namespace ansi {
3030
// https://github.com/shiena/ansicolor/blob/master/README.md
@@ -107,4 +107,4 @@ struct Colours {
107107
};
108108
} // namespace ansi
109109

110-
#endif // ANSI_COLOUR_H_
110+
#endif // FIRMWARE_ANSI_COLOUR_H_

common/include/firmware/debug/debug_debug.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
#ifndef FIRMWARE_DEBUG_DEBUG_H_
27-
#define FIRMWARE_DEBUG_DEBUG_H_
26+
#ifndef FIRMWARE_DEBUG_DEBUG_DEBUG_H_
27+
#define FIRMWARE_DEBUG_DEBUG_DEBUG_H_
2828

2929
#include <cstdio>
3030
#include <source_location>
@@ -69,4 +69,4 @@
6969
DEBUG_PRINTF("%s", (message)); \
7070
} while (false)
7171

72-
#endif // FIRMWARE_DEBUG_DEBUG_H_
72+
#endif // FIRMWARE_DEBUG_DEBUG_DEBUG_H_

common/include/firmware/debug/debug_dump.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
#ifndef COMMON_DEBUG_DEBUG_DUMP_H_
27-
#define COMMON_DEBUG_DEBUG_DUMP_H_
26+
#ifndef FIRMWARE_DEBUG_DEBUG_DUMP_H_
27+
#define FIRMWARE_DEBUG_DEBUG_DUMP_H_
2828

2929
#include <cstdint>
3030
#include <cstdio>
3131
#include <cctype>
32+
#include <span>
3233

3334
#include "firmware/debug/debug_config.h"
3435

@@ -104,6 +105,11 @@ inline void Dump([[maybe_unused]] const void* data, [[maybe_unused]] uint32_t si
104105

105106
} while (chars < size);
106107
}
108+
109+
template <typename T>
110+
inline void Dump(std::span<T> data) {
111+
Dump(data.data(), static_cast<uint32_t>(data.size_bytes()));
112+
}
107113
} // namespace debug
108114

109-
#endif // COMMON_DEBUG_DEBUG_DUMP_H_
115+
#endif // FIRMWARE_DEBUG_DEBUG_DUMP_H_

common/include/firmware/debug/debug_printbits.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
#ifndef COMMON_DEBUG_DEBUG_PRINTBITS_H_
27-
#define COMMON_DEBUG_DEBUG_PRINTBITS_H_
26+
#ifndef FIRMWARE_DEBUG_DEBUG_PRINTBITS_H_
27+
#define FIRMWARE_DEBUG_DEBUG_PRINTBITS_H_
2828

2929
#include <concepts>
3030
#include <cstdio>
@@ -60,4 +60,4 @@ inline void PrintBits(T value) {
6060
}
6161
} // namespace debug
6262

63-
#endif // COMMON_DEBUG_DEBUG_PRINTBITS_H_
63+
#endif // FIRMWARE_DEBUG_DEBUG_PRINTBITS_H_

common/include/firmware/firmwareversion.h

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

25-
#ifndef FIRMWAREVERSION_H_
26-
#define FIRMWAREVERSION_H_
25+
#ifndef FIRMWARE_FIRMWAREVERSION_H_
26+
#define FIRMWARE_FIRMWAREVERSION_H_
2727

2828
#include <cstdint>
2929
#include <cstring>
@@ -97,4 +97,4 @@ class FirmwareVersion {
9797
static inline FirmwareVersion* s_this;
9898
};
9999

100-
#endif // FIRMWAREVERSION_H_
100+
#endif // FIRMWARE_FIRMWAREVERSION_H_
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org
1010
*/
1111

12-
#ifndef GLOBAL_H_
13-
#define GLOBAL_H_
12+
#ifndef FIRMWARE_GLOBAL_H_
13+
#define FIRMWARE_GLOBAL_H_
1414

1515
#include <cstdint>
1616

17-
#include "utc.h"
17+
#include "firmware/utc.h"
1818

1919
namespace global {
2020

@@ -63,4 +63,4 @@ inline bool SetUtcOffsetIfValid(int32_t hours, uint32_t minutes) {
6363
}
6464
} // namespace global
6565

66-
#endif // GLOBAL_H_
66+
#endif // FIRMWARE_GLOBAL_H_

common/include/firmware/pixeldmx/show.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
#ifndef COMMON_FIRMWARE_PIXELDMX_SHOW_H_
27-
#define COMMON_FIRMWARE_PIXELDMX_SHOW_H_
26+
#ifndef FIRMWARE_PIXELDMX_SHOW_H_
27+
#define FIRMWARE_PIXELDMX_SHOW_H_
2828

2929
#include <cstdint>
3030

@@ -61,4 +61,4 @@ inline void Show(uint32_t line, pixelpatterns::Pattern pattern) {
6161
}
6262
} // namespace common::firmware::pixeldmx
6363

64-
#endif // COMMON_FIRMWARE_PIXELDMX_SHOW_H_
64+
#endif // FIRMWARE_PIXELDMX_SHOW_H_

0 commit comments

Comments
 (0)