Skip to content

Commit d2a6338

Browse files
committed
Modernize device headers and SPI/I2C APIs
Refactor many lib-device headers to modern C++ conventions and adapt to the new SPI/I2C abstractions. Key changes: - Replace HAL_SPI/HAL_I2C usage with Spi (and remove direct hal_i2c includes where relevant). - Convert many compile-time constants to inline constexpr and adopt k-prefix naming for clarity. - Normalize identifier names (camel/snake adjustments), rename members (e.g. m_bIsInitialized -> initialized_, address_ usage), and fix class name (BH170 -> BH1750). - Update BW driver classes to use Spi::Write / spi::Transfern and new bw::port::write constants (k-prefixed), simplify constructors and connection checks. - Update MAX7219, MCP and other device drivers to use new SPI API, constant names and tighten initialization logic. - Clean up EMA template naming/behaviour and remove duplicate header guards; use clearer member names and constexpr kHalf. - Update copyright years to 2026 and apply minor formatting/guard comment style changes. Overall this commit prepares device drivers for the new hardware abstraction layer and improves naming consistency and safety across the codebase.
1 parent 8491027 commit d2a6338

81 files changed

Lines changed: 1857 additions & 2139 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib-device/include/bh1750.h

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,25 @@
2828

2929
#include <cstdint>
3030

31-
#include "hal_i2c.h"
32-
33-
namespace sensor
34-
{
35-
namespace bh1750
36-
{
37-
static constexpr char DESCRIPTION[] = "Ambient Light";
38-
static constexpr auto RANGE_MIN = 0;
39-
static constexpr auto RANGE_MAX = 65535;
31+
namespace sensor {
32+
namespace bh1750 {
33+
inline constexpr char kDescription[] = "Ambient Light";
34+
inline constexpr auto kRangeMin = 0;
35+
inline constexpr auto kRangeMax = 65535;
4036
} // namespace bh1750
4137

42-
class BH170 : HAL_I2C
43-
{
38+
class BH1750 {
4439
public:
45-
explicit BH170(uint8_t address = 0);
40+
explicit BH1750(uint8_t address = 0);
4641

47-
bool Initialize() { return m_bIsInitialized; }
42+
bool Initialize() { return initialized_; }
4843

4944
uint16_t Get();
5045

5146
private:
52-
bool m_bIsInitialized = false;
47+
uint8_t address_{0};
48+
bool initialized_{false};
5349
};
54-
5550
} // namespace sensor
5651

57-
#endif // BH1750_H_
52+
#endif // BH1750_H_

lib-device/include/bw.h

Lines changed: 48 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file bw.h
33
*
44
*/
5-
/* Copyright (C) 2020 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2020-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
@@ -30,104 +30,89 @@
3030
#include <string.h>
3131
#include <algorithm>
3232

33-
#include "hal_spi.h"
33+
#include "spi.h"
3434

3535
/*
3636
* http://www.bitwizard.nl/wiki/index.php/Default_addresses
3737
*/
3838

39-
namespace bw
40-
{
41-
namespace id_string
42-
{
43-
static constexpr size_t length = 0;
39+
namespace bw {
40+
namespace id_string {
41+
inline constexpr size_t kLength = 0;
4442
} // namespace id_string
4543

46-
namespace spi::speed
47-
{
48-
static constexpr uint32_t max_hz = 50000; ///< 50 kHz
49-
static constexpr uint32_t default_hz = 50000; ///< 50 kHz
44+
namespace spi::speed {
45+
inline constexpr uint32_t max_hz = 50000; ///< 50 kHz
46+
inline constexpr uint32_t default_hz = 50000; ///< 50 kHz
5047
} // namespace spi::speed
5148

52-
namespace lcd
53-
{
54-
static constexpr uint8_t address = 0x82;
55-
static constexpr char id_string[] = "spi_lcd";
56-
static constexpr uint8_t max_characters = 16;
57-
static constexpr uint8_t max_lines = 2;
58-
namespace spi
59-
{
60-
static constexpr uint32_t write_delay_us = 12;
49+
namespace lcd {
50+
inline constexpr uint8_t address = 0x82;
51+
inline constexpr char id_string[] = "spi_lcd";
52+
inline constexpr uint8_t max_characters = 16;
53+
inline constexpr uint8_t max_lines = 2;
54+
namespace spi {
55+
inline constexpr uint32_t write_delay_us = 12;
6156
} // namespace spi
6257
} // namespace lcd
6358

64-
namespace dio
65-
{
66-
static constexpr uint8_t address = 0x84;
67-
static constexpr char id_string[] = "spi_dio";
59+
namespace dio {
60+
inline constexpr uint8_t address = 0x84;
61+
inline constexpr char id_string[] = "spi_dio";
6862
} // namespace dio
6963

70-
namespace fets
71-
{
72-
static constexpr uint8_t address = 0x88;
73-
static constexpr char id_string[] = "spi_7fets";
64+
namespace fets {
65+
inline constexpr uint8_t address = 0x88;
66+
inline constexpr char id_string[] = "spi_7fets";
7467
} // namespace fets
7568

76-
namespace relay
77-
{
78-
static constexpr uint8_t address = 0x8E;
79-
static constexpr char id_string[] = "spi_relay";
69+
namespace relay {
70+
inline constexpr uint8_t address = 0x8E;
71+
inline constexpr char id_string[] = "spi_relay";
8072
} // namespace relay
8173

82-
namespace dimmer
83-
{
84-
static constexpr uint8_t address = 0x9E;
85-
static constexpr char id_string[] = "spi_dimmer";
74+
namespace dimmer {
75+
inline constexpr uint8_t address = 0x9E;
76+
inline constexpr char id_string[] = "spi_dimmer";
8677
} // namespace dimmer
8778

88-
namespace port
89-
{
90-
namespace read
91-
{
92-
static constexpr uint8_t id_string = 0x01;
79+
namespace port {
80+
namespace read {
81+
inline constexpr uint8_t kIdString = 0x01;
9382
} // namespace read
94-
namespace write
95-
{
96-
static constexpr uint8_t set_all_outputs = 0x10;
97-
static constexpr uint8_t io_direction = 0x30;
83+
namespace write {
84+
inline constexpr uint8_t kSetAllOutputs = 0x10;
85+
inline constexpr uint8_t kIoDirection = 0x30;
9886

99-
static constexpr uint8_t display_data = 0x00;
100-
static constexpr uint8_t clear_screen = 0x10; ///< any data clears the screen
101-
static constexpr uint8_t move_cursor = 0x11;
102-
static constexpr uint8_t reinit_lcd = 0x14;
87+
inline constexpr uint8_t kDisplayData = 0x00;
88+
inline constexpr uint8_t kClearScreen = 0x10; ///< any data clears the screen
89+
inline constexpr uint8_t kMoveCursor = 0x11;
90+
inline constexpr uint8_t kReinitLcd = 0x14;
10391

10492
} // namespace write
10593
} // namespace port
10694

10795
} // namespace bw
10896

109-
class BwSpi : public HAL_SPI
110-
{
97+
class BwSpi : public Spi {
11198
public:
112-
BwSpi(uint8_t nChipSelect, uint8_t address, const char* pIdString) : HAL_SPI(nChipSelect, bw::spi::speed::default_hz), address_(address)
113-
{
114-
char spiBuffer[bw::id_string::length + 2];
99+
BwSpi(uint8_t chip_select, uint8_t address, const char* string) : Spi(chip_select, bw::spi::speed::default_hz), address_(address) {
100+
char buffer[bw::id_string::kLength + 2];
115101

116-
spiBuffer[0] = static_cast<char>(address_ | 1);
117-
spiBuffer[1] = bw::port::read::id_string;
102+
buffer[0] = static_cast<char>(address_ | 1);
103+
buffer[1] = bw::port::read::kIdString;
118104

119-
HAL_SPI::WriteRead(spiBuffer, sizeof(spiBuffer));
105+
spi::Transfern(buffer, sizeof(buffer));
120106

121-
if (pIdString != nullptr)
122-
{
123-
const auto length = std::min(bw::id_string::length, strlen(pIdString));
124-
m_IsConnected = (strncmp(&spiBuffer[2], pIdString, length) == 0);
107+
if (string != nullptr) {
108+
const auto kLength = std::min(bw::id_string::kLength, strlen(string));
109+
connected_ = (strncmp(&buffer[2], string, kLength) == 0);
125110
}
126111
}
127112

128113
protected:
129114
uint8_t address_;
130-
bool m_IsConnected = false;
115+
bool connected_{false};
131116
};
132117

133-
#endif // BW_H_
118+
#endif // BW_H_

lib-device/include/bwspi7fets.h

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file bwspi7fets.h
33
*
44
*/
5-
/* Copyright (C) 2020 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2020-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
@@ -30,34 +30,32 @@
3030

3131
#include "bw.h"
3232

33-
class BwSpi7fets : BwSpi
34-
{
35-
void SetDirection(uint8_t nMask)
36-
{
33+
class BwSpi7fets : BwSpi {
34+
public:
35+
explicit BwSpi7fets(uint8_t chip_select = 0, uint8_t address = bw::fets::address) : BwSpi(chip_select, address, bw::fets::id_string) { SetDirection(0x7F); }
36+
37+
void Output(uint8_t pins) {
3738
char cmd[3];
3839

3940
cmd[0] = static_cast<char>(address_);
40-
cmd[1] = bw::port::write::io_direction;
41-
cmd[2] = static_cast<char>(nMask);
41+
cmd[1] = bw::port::write::kSetAllOutputs;
42+
cmd[2] = static_cast<char>(pins);
4243

43-
HAL_SPI::Write(cmd, sizeof(cmd));
44+
Spi::Write(cmd, sizeof(cmd), true);
4445
}
4546

46-
public:
47-
explicit BwSpi7fets(uint8_t nChipSelect = 0, uint8_t address = bw::fets::address) : BwSpi(nChipSelect, address, bw::fets::id_string) { SetDirection(0x7F); }
47+
bool IsConnected() { return connected_; }
4848

49-
void Output(uint8_t nPins)
50-
{
49+
private:
50+
void SetDirection(uint8_t mask) {
5151
char cmd[3];
5252

5353
cmd[0] = static_cast<char>(address_);
54-
cmd[1] = bw::port::write::set_all_outputs;
55-
cmd[2] = static_cast<char>(nPins);
54+
cmd[1] = bw::port::write::kIoDirection;
55+
cmd[2] = static_cast<char>(mask);
5656

57-
HAL_SPI::Write(cmd, sizeof(cmd));
57+
Spi::Write(cmd, sizeof(cmd), true);
5858
}
59-
60-
bool IsConnected() { return m_IsConnected; }
6159
};
6260

63-
#endif // BWSPI7FETS_H_
61+
#endif // BWSPI7FETS_H_

lib-device/include/bwspidimmer.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file bwspidimmer.h
33
*
44
*/
5-
/* Copyright (C) 2020 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2020-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
@@ -30,23 +30,21 @@
3030

3131
#include "bw.h"
3232

33-
class BwSpiDimmer: BwSpi {
34-
public:
35-
explicit BwSpiDimmer(uint8_t nChipSelect = 0, uint8_t address = bw::dimmer::address): BwSpi(nChipSelect, address, bw::dimmer::id_string) {}
33+
class BwSpiDimmer : BwSpi {
34+
public:
35+
explicit BwSpiDimmer(uint8_t chip_select = 0, uint8_t address = bw::dimmer::address) : BwSpi(chip_select, address, bw::dimmer::id_string) {}
3636

37-
void Output(uint8_t nValue) {
38-
char cmd[3];
37+
void Output(uint8_t value) {
38+
char cmd[3];
3939

40-
cmd[0] = static_cast<char>(address_);
41-
cmd[1] = bw::port::write::set_all_outputs;
42-
cmd[2] = static_cast<char>(nValue);
40+
cmd[0] = static_cast<char>(address_);
41+
cmd[1] = bw::port::write::kSetAllOutputs;
42+
cmd[2] = static_cast<char>(value);
4343

44-
HAL_SPI::Write(cmd, sizeof(cmd));
45-
}
44+
Spi::Write(cmd, sizeof(cmd), true);
45+
}
4646

47-
bool IsConnected() {
48-
return m_IsConnected;
49-
}
47+
bool IsConnected() { return connected_; }
5048
};
5149

52-
#endif // BWSPIDIMMER_H_
50+
#endif // BWSPIDIMMER_H_

lib-device/include/bwspidio.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file bwspidio.h
33
*
44
*/
5-
/* Copyright (C) 2020 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2020-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
@@ -30,34 +30,31 @@
3030

3131
#include "bw.h"
3232

33-
class BwSpiDio : BwSpi
34-
{
33+
class BwSpiDio : BwSpi {
3534
public:
36-
explicit BwSpiDio(uint8_t nChipSelect = 0, uint8_t address = bw::dio::address) : BwSpi(nChipSelect, address, bw::dio::id_string) {}
35+
explicit BwSpiDio(uint8_t chip_select = 0, uint8_t address = bw::dio::address) : BwSpi(chip_select, address, bw::dio::id_string) {}
3736

38-
void SetDirection(uint8_t nMask)
39-
{
37+
void SetDirection(uint8_t mask) {
4038
char cmd[3];
4139

4240
cmd[0] = static_cast<char>(address_);
43-
cmd[1] = bw::port::write::io_direction;
44-
cmd[2] = static_cast<char>(nMask);
41+
cmd[1] = bw::port::write::kIoDirection;
42+
cmd[2] = static_cast<char>(mask);
4543

46-
HAL_SPI::Write(cmd, sizeof(cmd));
44+
Spi::Write(cmd, sizeof(cmd), true);
4745
}
4846

49-
void Output(uint8_t nPins)
50-
{
47+
void Output(uint8_t pins) {
5148
char cmd[3];
5249

5350
cmd[0] = static_cast<char>(address_);
54-
cmd[1] = bw::port::write::set_all_outputs;
55-
cmd[2] = static_cast<char>(nPins);
51+
cmd[1] = bw::port::write::kSetAllOutputs;
52+
cmd[2] = static_cast<char>(pins);
5653

57-
HAL_SPI::Write(cmd, sizeof(cmd));
54+
Spi::Write(cmd, sizeof(cmd), true);
5855
}
5956

60-
bool IsConnected() { return m_IsConnected; }
57+
bool IsConnected() { return connected_; }
6158
};
6259

63-
#endif // BWSPIDIO_H_
60+
#endif // BWSPIDIO_H_

0 commit comments

Comments
 (0)