Skip to content

Commit 79a851b

Browse files
committed
Add ansi_colour and migrate console/display to ANSI
Introduce new ansi_colour.h and migrate console and display code to use ansi::Colours for terminal coloring. Replace legacy console color utilities, update Display/TextStatus usages, and propagate changes across flashcodeinstall, remoteconfig, tftp fileserver and uart0 console. Also apply small API/formatting cleanups (brace/style adjustments), rename m_nSize->size_, add hal Init/Reboot declarations, update copyright years, and remove obsolete lib-hal/console/font.S.
1 parent 346b78d commit 79a851b

16 files changed

Lines changed: 385 additions & 579 deletions

File tree

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/firmware/pixeldmx/show.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,20 @@
3232
#include "display.h"
3333
#include "pixelpatterns.h"
3434

35-
namespace common::firmware::pixeldmx
36-
{
37-
inline void Show(uint32_t line, pixelpatterns::Pattern pattern = pixelpatterns::Pattern::kNone)
38-
{
35+
namespace common::firmware::pixeldmx {
36+
inline void Show(uint32_t line, pixelpatterns::Pattern pattern = pixelpatterns::Pattern::kNone) {
3937
auto& configuration = PixelDmxConfiguration::Get();
4038
auto* display = Display::Get();
4139
assert(display != nullptr);
4240

4341
display->ClearEndOfLine();
44-
display->Printf(line, "%s:%d G%d %s", pixel::GetTypeName(configuration.GetType()), configuration.GetCount(), configuration.GetGroupingCount(),
45-
pixel::GetMapName(configuration.GetMap()));
42+
display->Printf(line, "%s:%d G%d %s",
43+
pixel::GetTypeName(configuration.GetType()),
44+
configuration.GetCount(), configuration.GetGroupingCount(),
45+
pixel::GetMapName(configuration.GetMap()));
4646
display->ClearLine(8); // Status line
4747

48-
if (pattern != pixelpatterns::Pattern::kNone)
49-
{
48+
if (pattern != pixelpatterns::Pattern::kNone) {
5049
display->ClearLine(6);
5150
display->Printf(6, "%s:%u", PixelPatterns::GetName(pattern), static_cast<uint32_t>(pattern));
5251
}

0 commit comments

Comments
 (0)