Skip to content

Commit 52239ab

Browse files
committed
Refactor headers: naming, style, add ANSI & board
Apply consistent coding style and naming across headers, plus add ANSI colour support and a new GD32F207C-EVAL board header. Changes include: - Add new ansi_colour.h providing ANSI foreground/background colour helpers. - Add new board_gd32f207c_eval.h to support GD32F207C-EVAL (DMX port and DIR pin definitions). - Rename board macros/constants to k-prefixed identifiers (e.g. USART0_PORT -> kUsart0Port, DIR_PORT_* -> kDirPort*). - Update copyright headers to include 2026 where applicable. - Standardize brace placement, namespace formatting, and small API formatting changes across various JSON and utility headers. - Minor behavioral change: debug::PrintBits now prints the value with printf("%.8x "). - Add include path for lib-hwclock in common/make/gd32/Includes.mk. These changes are primarily for consistency, readability, and to add support for the new board and ANSI colouring utilities.
1 parent 97424cb commit 52239ab

18 files changed

Lines changed: 313 additions & 191 deletions

common/include/ansi_colour.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @file ansi_colour.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 ANSI_COLOUR_H_
27+
#define ANSI_COLOUR_H_
28+
29+
namespace ansi {
30+
// https://github.com/shiena/ansicolor/blob/master/README.md
31+
struct Colours {
32+
enum class Colour {
33+
kBlack,
34+
kRed,
35+
kGreen,
36+
kYellow,
37+
kBlue,
38+
kMagenta,
39+
kCyan,
40+
kWhite,
41+
kDefault
42+
};
43+
44+
struct Fg {
45+
static constexpr char kBlack[] = "\x1b[30m";
46+
static constexpr char kRed[] = "\x1b[31m";
47+
static constexpr char kGreen[] = "\x1b[32m";
48+
static constexpr char kYellow[] = "\x1b[33m";
49+
static constexpr char kWhite[] = "\x1b[37m";
50+
static constexpr char kDefault[] = "\x1b[39m";
51+
};
52+
53+
static constexpr const char* Foreground(Colour colour) {
54+
switch (colour) {
55+
case Colour::kBlack:
56+
return ansi::Colours::Fg::kBlack;
57+
break;
58+
case Colour::kRed:
59+
return ansi::Colours::Fg::kRed;
60+
break;
61+
case Colour::kGreen:
62+
return ansi::Colours::Fg::kGreen;
63+
break;
64+
case Colour::kYellow:
65+
return ansi::Colours::Fg::kYellow;
66+
break;
67+
case Colour::kWhite:
68+
return ansi::Colours::Fg::kWhite;
69+
break;
70+
default:
71+
return ansi::Colours::Fg::kDefault;
72+
break;
73+
}
74+
};
75+
76+
struct Bg {
77+
static constexpr char kBlack[] = "\x1b[40m";
78+
static constexpr char kRed[] = "\x1b[41m";
79+
static constexpr char kGreen[] = "\x1b[42m";
80+
static constexpr char kYellow[] = "\x1b[43m";
81+
static constexpr char kWhite[] = "\x1b[47m";
82+
static constexpr char kDefault[] = "\x1b[49m";
83+
};
84+
85+
static constexpr const char* Background(Colour colour) {
86+
switch (colour) {
87+
case Colour::kBlack:
88+
return ansi::Colours::Bg::kBlack;
89+
break;
90+
case Colour::kRed:
91+
return ansi::Colours::Bg::kRed;
92+
break;
93+
case Colour::kGreen:
94+
return ansi::Colours::Bg::kGreen;
95+
break;
96+
case Colour::kYellow:
97+
return ansi::Colours::Bg::kYellow;
98+
break;
99+
case Colour::kWhite:
100+
return ansi::Colours::Bg::kWhite;
101+
break;
102+
default:
103+
return ansi::Colours::Bg::kDefault;
104+
break;
105+
}
106+
};
107+
};
108+
} // namespace ansi
109+
110+
#endif // ANSI_COLOUR_H_

common/include/common/utils/utils_array.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file utils_array.h
33
*
44
*/
5-
/* Copyright (C) 2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2025-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
@@ -28,12 +28,11 @@
2828

2929
#include <cstddef>
3030

31-
namespace common
32-
{
33-
template <typename T, size_t N> constexpr size_t ArraySize(const T (&)[N]) noexcept
34-
{
31+
namespace common {
32+
template <typename T, size_t N>
33+
constexpr size_t ArraySize(const T (&)[N]) noexcept {
3534
return N;
3635
}
3736
} // namespace common
3837

39-
#endif // COMMON_UTILS_UTILS_ARRAY_H_
38+
#endif // COMMON_UTILS_UTILS_ARRAY_H_

common/include/common/utils/utils_enum.h

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

3131
namespace common
3232
{
33-
/// Converts an enum class value to its underlying integer type.
34-
template <typename Enum> constexpr auto ToValue(Enum e) noexcept -> std::underlying_type_t<Enum>
35-
{
33+
// Converts an enum class value to its underlying integer type.
34+
template <typename Enum>
35+
constexpr auto ToValue(Enum e) noexcept -> std::underlying_type_t<Enum> {
3636
static_assert(std::is_enum_v<Enum>);
3737
return static_cast<std::underlying_type_t<Enum>>(e);
3838
}
3939

40-
/// Converts an integer value to the corresponding enum class value.
41-
template <typename Enum> constexpr Enum FromValue(std::underlying_type_t<Enum> value) noexcept
42-
{
40+
// Converts an integer value to the corresponding enum class value.
41+
template <typename Enum>
42+
constexpr Enum FromValue(std::underlying_type_t<Enum> value) noexcept {
4343
static_assert(std::is_enum_v<Enum>);
4444
return static_cast<Enum>(value);
4545
}
46-
4746
} // namespace common
4847

4948
#endif // COMMON_UTILS_UTILS_ENUM_H_

common/include/dmx/board_bw_opidmx4.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file board_bw_opidmx4.h
33
*
44
*/
5-
/* Copyright (C) 2022 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2022-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
@@ -42,21 +42,21 @@ inline constexpr uint32_t kPorts = DMX_MAX_PORTS;
4242
#define DMX_USE_UART4
4343
#define DMX_USE_USART5
4444

45-
inline constexpr auto USART0_PORT = 3; // OPi One UART0
46-
inline constexpr auto USART2_PORT = 2; // OPi One UART3
47-
inline constexpr auto UART4_PORT = 0; // OPi One UART1 Pin 38 TX, Pin 40 RX
48-
inline constexpr auto USART5_PORT = 1; // OPi One UART2
45+
inline constexpr auto kUsart0Port = 3; // OPi One UART0
46+
inline constexpr auto kUsart2Port = 2; // OPi One UART3
47+
inline constexpr auto kUart4Port = 0; // OPi One UART1 Pin 38 TX, Pin 40 RX
48+
inline constexpr auto kUsart5Port = 1; // OPi One UART2
4949

50-
inline constexpr auto DIR_PORT_0_GPIO_PORT = GPIOA; // OPi One UART1
51-
inline constexpr auto DIR_PORT_0_GPIO_PIN = GPIO_PIN_4; // GPIO_EXT_32
50+
inline constexpr auto kDirPort0GpioPort = GPIOA; // OPi One UART1
51+
inline constexpr auto kDirPort0GpioPin = GPIO_PIN_4; // GPIO_EXT_32
5252

53-
inline constexpr auto DIR_PORT_1_GPIO_PORT = GPIOA; // OPi One UART2
54-
inline constexpr auto DIR_PORT_1_GPIO_PIN = GPIO_PIN_11; // GPIO_EXT_22
53+
inline constexpr auto kDirPort1GpioPort = GPIOA; // OPi One UART2
54+
inline constexpr auto kDirPort1GpioPin = GPIO_PIN_11; // GPIO_EXT_22
5555

56-
inline constexpr auto DIR_PORT_2_GPIO_PORT = GPIOB; // OPi One UART3
57-
inline constexpr auto DIR_PORT_2_GPIO_PIN = GPIO_PIN_10; // GPIO_EXT_12
56+
inline constexpr auto kDirPort2GpioPort = GPIOB; // OPi One UART3
57+
inline constexpr auto kDirPort2GpioPin = GPIO_PIN_10; // GPIO_EXT_12
5858

59-
inline constexpr auto DIR_PORT_3_GPIO_PORT = GPIOA; // OPi One UART0
60-
inline constexpr auto DIR_PORT_3_GPIO_PIN = GPIO_PIN_5; // GPIO_EXT_31
59+
inline constexpr auto kDirPort3GpioPort = GPIOA; // OPi One UART0
60+
inline constexpr auto kDirPort3GpioPin = GPIO_PIN_5; // GPIO_EXT_31
6161
} // namespace dmx::config
6262
#endif // DMX_BOARD_BW_OPIDMX4_H_

common/include/dmx/board_dmx3.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file board_dmx3.h
33
*
44
*/
5-
/* Copyright (C) 2023 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2023-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
@@ -41,17 +41,17 @@ inline constexpr uint32_t kPorts = DMX_MAX_PORTS;
4141
#define DMX_USE_UART4
4242
#define DMX_USE_USART5
4343

44-
inline constexpr auto USART2_PORT = 0;
45-
inline constexpr auto UART4_PORT = 1;
46-
inline constexpr auto USART5_PORT = 2;
44+
inline constexpr auto kUsart2Port = 0;
45+
inline constexpr auto kUart4Port = 1;
46+
inline constexpr auto kUsart5Port = 2;
4747

48-
inline constexpr auto DIR_PORT_0_GPIO_PORT = GPIOB;
49-
inline constexpr auto DIR_PORT_0_GPIO_PIN = GPIO_PIN_10;
48+
inline constexpr auto kDirPort0GpioPort = GPIOB;
49+
inline constexpr auto kDirPort0GpioPin = GPIO_PIN_10;
5050

51-
inline constexpr auto DIR_PORT_1_GPIO_PORT = GPIOA;
52-
inline constexpr auto DIR_PORT_1_GPIO_PIN = GPIO_PIN_5;
51+
inline constexpr auto kDirPort1GpioPort = GPIOA;
52+
inline constexpr auto kDirPort1GpioPin = GPIO_PIN_5;
5353

54-
inline constexpr auto DIR_PORT_2_GPIO_PORT = GPIOB;
55-
inline constexpr auto DIR_PORT_2_GPIO_PIN = GPIO_PIN_14;
54+
inline constexpr auto kDirPort2GpioPort = GPIOB;
55+
inline constexpr auto kDirPort2GpioPin = GPIO_PIN_14;
5656
} // namespace dmx::config
5757
#endif // DMX_BOARD_DMX3_H_

common/include/dmx/board_dmx4.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file board_dmx4.h
33
*
44
*/
5-
/* Copyright (C) 2022 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2022-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
@@ -42,21 +42,21 @@ inline constexpr uint32_t kPorts = DMX_MAX_PORTS;
4242
#define DMX_USE_UART4
4343
#define DMX_USE_USART5
4444

45-
inline constexpr auto USART0_PORT = 0;
46-
inline constexpr auto USART2_PORT = 1;
47-
inline constexpr auto UART4_PORT = 2;
48-
inline constexpr auto USART5_PORT = 3;
45+
inline constexpr auto kUsart0Port = 0;
46+
inline constexpr auto kUsart2Port = 1;
47+
inline constexpr auto kUart4Port = 2;
48+
inline constexpr auto kUsart5Port = 3;
4949

50-
inline constexpr auto DIR_PORT_0_GPIO_PORT = GPIOA;
51-
inline constexpr auto DIR_PORT_0_GPIO_PIN = GPIO_PIN_4;
50+
inline constexpr auto kDirPort0GpioPort = GPIOA;
51+
inline constexpr auto kDirPort0GpioPin = GPIO_PIN_4;
5252

53-
inline constexpr auto DIR_PORT_1_GPIO_PORT = GPIOB;
54-
inline constexpr auto DIR_PORT_1_GPIO_PIN = GPIO_PIN_10;
53+
inline constexpr auto kDirPort1GpioPort = GPIOB;
54+
inline constexpr auto kDirPort1GpioPin = GPIO_PIN_10;
5555

56-
inline constexpr auto DIR_PORT_2_GPIO_PORT = GPIOA;
57-
inline constexpr auto DIR_PORT_2_GPIO_PIN = GPIO_PIN_5;
56+
inline constexpr auto kDirPort2GpioPort = GPIOA;
57+
inline constexpr auto kDirPort2GpioPin = GPIO_PIN_5;
5858

59-
inline constexpr auto DIR_PORT_3_GPIO_PORT = GPIOB;
60-
inline constexpr auto DIR_PORT_3_GPIO_PIN = GPIO_PIN_14;
59+
inline constexpr auto kDirPort3GpioPort = GPIOB;
60+
inline constexpr auto kDirPort3GpioPin = GPIO_PIN_14;
6161
} // namespace dmx::config
6262
#endif // DMX_BOARD_DMX4_H_
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @file board_gd32f207c_eval.h
3+
*
4+
*/
5+
/* Copyright (C) 2021-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 DMX_BOARD_GD32F207C_EVAL_H_
27+
#define DMX_BOARD_GD32F207C_EVAL_H_
28+
29+
#include <cstdint>
30+
31+
#include "gd32.h" // IWYU pragma: keep
32+
33+
#define DMX_MAX_PORTS 2
34+
35+
namespace dmx::config {
36+
namespace max {
37+
inline constexpr uint32_t kPorts = DMX_MAX_PORTS;
38+
} // namespace max
39+
40+
// #define DMX_USE_USART0
41+
#define DMX_USE_USART1
42+
#define DMX_USE_USART2
43+
// #define DMX_USE_UART3
44+
// #define DMX_USE_UART4
45+
// #define DMX_USE_USART5
46+
// #define DMX_USE_UART6
47+
// #define DMX_USE_UART7
48+
49+
// inline constexpr auto kUsart0Port = 0;
50+
inline constexpr auto kUsart1Port = 0;
51+
inline constexpr auto kUsart2Port = 1;
52+
// inline constexpr auto kUart3Port = 2;
53+
// inline constexpr auto kUart4Port = 3;
54+
// inline constexpr auto kUsart5Port = 5;
55+
// inline constexpr auto kUart6Port = 6;
56+
// inline constexpr auto kUart7Port = 7;
57+
58+
inline constexpr auto kDirPort0GpioPort = GPIOE;
59+
inline constexpr auto kDirPort0GpioPin = GPIO_PIN_9;
60+
61+
inline constexpr auto kDirPort1GpioPort = GPIOE;
62+
inline constexpr auto kDirPort1GpioPin = GPIO_PIN_10;
63+
64+
inline constexpr auto kDirPort2GpioPort = GPIOE;
65+
inline constexpr auto kDirPort2GpioPin = GPIO_PIN_11;
66+
67+
inline constexpr auto kDirPort3GpioPort = GPIOE;
68+
inline constexpr auto kDirPort3GpioPin = GPIO_PIN_12;
69+
70+
inline constexpr auto kDirPort4GpioPort = GPIOE;
71+
inline constexpr auto kDirPort4GpioPin = GPIO_PIN_13;
72+
73+
inline constexpr auto kDirPort5GpioPort = GPIOE;
74+
inline constexpr auto kDirPort5GpioPin = GPIO_PIN_14;
75+
76+
inline constexpr auto kDirPort6GpioPort = GPIOE;
77+
inline constexpr auto kDirPort6GpioPin = GPIO_PIN_15;
78+
79+
inline constexpr auto kDirPort7GpioPort = GPIOB;
80+
inline constexpr auto kDirPort7GpioPin = GPIO_PIN_15;
81+
} // namespace dmx::config
82+
#endif // DMX_BOARD_GD32F207C_EVAL_H_

common/include/dmx/board_gd32f207rg.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file board_gd32f207rg.h
33
*
44
*/
5-
/* Copyright (C) 2021-2023 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2021-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
@@ -40,13 +40,13 @@ inline constexpr uint32_t kPorts = DMX_MAX_PORTS;
4040
#define DMX_USE_USART2
4141
#define DMX_USE_USART5
4242

43-
inline constexpr auto USART2_PORT = 0;
44-
inline constexpr auto USART5_PORT = 1;
43+
inline constexpr auto kUsart2Port = 0;
44+
inline constexpr auto kUsart5Port = 1;
4545

46-
inline constexpr auto DIR_PORT_0_GPIO_PORT = GPIOB;
47-
inline constexpr auto DIR_PORT_0_GPIO_PIN = GPIO_PIN_10; // GPIO_EXT_12
46+
inline constexpr auto kDirPort0GpioPort = GPIOB;
47+
inline constexpr auto kDirPort0GpioPin = GPIO_PIN_10; // GPIO_EXT_12
4848

49-
inline constexpr auto DIR_PORT_1_GPIO_PORT = GPIOA;
50-
inline constexpr auto DIR_PORT_1_GPIO_PIN = GPIO_PIN_11; // GPIO_EXT_22
49+
inline constexpr auto kDirPort1GpioPort = GPIOA;
50+
inline constexpr auto kDirPort1GpioPin = GPIO_PIN_11; // GPIO_EXT_22
5151
} // namespace dmx::config
5252
#endif // DMX_BOARD_GD32F207RG_H_

0 commit comments

Comments
 (0)