M5UnitUnified 0.5.5 git rev:bf711f3
Loading...
Searching...
No Matches
test_template.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
11#ifndef M5_UNIT_COMPONENT_GOOGLETEST_TEMPLATE_HPP
12#define M5_UNIT_COMPONENT_GOOGLETEST_TEMPLATE_HPP
13
15#include <type_traits>
16#include <Wire.h>
17#include <SPI.h>
18#include <esp32-hal-i2c.h>
19#include <M5Unified.h>
20#include "../wiring/m5_unit_unified_wiring.hpp" // board-aware connection helpers (after M5Unified.h)
21
22namespace m5 {
23namespace unit {
28namespace googletest {
29
39template <typename U>
40class I2CComponentTestBase : public ::testing::Test {
41 static_assert(std::is_base_of<m5::unit::Component, U>::value, "U must be derived from Component");
42
43protected:
44 virtual void SetUp() override
45 {
46 unit.reset(get_instance());
47 if (!unit) {
48 FAIL() << "Failed to get_instance";
49 return;
50 }
51 ustr = m5::utility::formatString("%s", unit->deviceName());
52 if (!begin()) {
53 FAIL() << "Failed to begin " << ustr;
54 }
55 }
56
57 virtual void TearDown() override
58 {
59 }
60
61 virtual bool begin()
62 {
63 // Board-aware: NessoN1 -> SoftwareI2C (M5HAL), NanoC6 / NanoH2 -> Ex_I2C, others -> Wire.
64 // Delegates to the shared wiring helper (adds NanoH2 over the previous hand-written 3-branch).
65 // The begin_with_* helpers below remain for tests that override begin() (e.g. DualSensor on Wire1).
66 return m5::unit::wiring::addI2C(Units, *unit, unit->component_config().clock) && Units.begin();
67 }
68
69 bool begin_with_wire(TwoWire& wire, uint32_t wnum = 0)
70 {
71 auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
72 auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
73 auto freq = unit->component_config().clock;
74 if (i2cIsInit(wnum)) {
75 M5_LOGD("Already initialized Wire%u. Terminate and restart FREQ %u", wnum, freq);
76 wire.end();
77 }
78 M5_LOGI("Wire begin SDA:%u SCL:%u FREQ:%u", pin_num_sda, pin_num_scl, freq);
79 wire.begin(pin_num_sda, pin_num_scl, freq);
80 return Units.add(*unit, wire) && Units.begin();
81 }
82
83 bool begin_with_ex_i2c()
84 {
85 M5_LOGI("Using Ex_I2C");
86 return Units.add(*unit, M5.Ex_I2C) && Units.begin();
87 }
88
89 bool begin_with_software_i2c(int8_t sda, int8_t scl)
90 {
91 m5::hal::bus::I2CBusConfig i2c_cfg;
92 i2c_cfg.pin_sda = m5::hal::gpio::getPin(sda);
93 i2c_cfg.pin_scl = m5::hal::gpio::getPin(scl);
94 auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg);
95 M5_LOGI("Using M5HAL SDA:%u SCL:%u", sda, scl);
96 return Units.add(*unit, i2c_bus ? i2c_bus.value() : nullptr) && Units.begin();
97 }
98
100 virtual U* get_instance() = 0;
101
102 std::string ustr{};
103 std::unique_ptr<U> unit{};
105};
106
112template <typename U>
113class GPIOComponentTestBase : public ::testing::Test {
114 static_assert(std::is_base_of<m5::unit::Component, U>::value, "U must be derived from Component");
115
116protected:
117 virtual void SetUp() override
118 {
119 unit.reset(get_instance());
120 if (!unit) {
121 FAIL() << "Failed to get_instance";
122 return;
123 }
124 ustr = m5::utility::formatString("%s:GPIO", unit->deviceName());
125 if (!begin()) {
126 FAIL() << "Failed to begin " << ustr;
127 }
128 }
129
130 virtual void TearDown() override
131 {
132 }
133
134 virtual bool begin()
135 {
136 // PortB preferred, fallback to PortA. Both pins added (matches the previous default).
137 return m5::unit::wiring::addGPIO(Units, *unit) && Units.begin();
138 }
139
141 virtual U* get_instance() = 0;
142
143 std::string ustr{};
144 std::unique_ptr<U> unit{};
146};
147
153template <typename U>
154class UARTComponentTestBase : public ::testing::Test {
155 static_assert(std::is_base_of<m5::unit::Component, U>::value, "U must be derived from Component");
156
157protected:
158 virtual void SetUp() override
159 {
160 unit.reset(get_instance());
161 if (!unit) {
162 FAIL() << "Failed to get_instance";
163 return;
164 }
165 ustr = m5::utility::formatString("%s:UART", unit->deviceName());
166 if (!begin()) {
167 FAIL() << "Failed to begin " << ustr;
168 }
169 }
170
171 virtual void TearDown() override
172 {
173 }
174
175 virtual bool begin()
176 {
177 serial = init_serial();
178 return serial && Units.add(*unit, *serial) && Units.begin();
179 }
180
182 virtual U* get_instance() = 0;
184 virtual HardwareSerial* init_serial() = 0;
185
186 std::string ustr{};
187 std::unique_ptr<U> unit{};
189 HardwareSerial* serial{};
190};
191
197template <typename U>
198class SPIComponentTestBase : public ::testing::Test {
199 static_assert(std::is_base_of<m5::unit::Component, U>::value, "U must be derived from Component");
200
201protected:
202 virtual void SetUp() override
203 {
204 unit.reset(get_instance());
205 if (!unit) {
206 FAIL() << "Failed to get_instance";
207 return;
208 }
209 ustr = m5::utility::formatString("%s:SPI", unit->deviceName());
210 if (!begin()) {
211 FAIL() << "Failed to begin " << ustr;
212 }
213 }
214
215 virtual void TearDown() override
216 {
217 }
218
219 virtual bool begin()
220 {
221 return Units.add(*unit, get_spi(), get_spi_settings()) && Units.begin();
222 }
223
225 virtual U* get_instance() = 0;
227 virtual SPISettings get_spi_settings() = 0;
229 virtual SPIClass& get_spi()
230 {
231 return SPI;
232 }
233
234 std::string ustr{};
235 std::unique_ptr<U> unit{};
237};
238
239} // namespace googletest
240} // namespace unit
241} // namespace m5
242#endif
Main header of M5UnitComponent.
For managing and leading units.
bool begin()
Begin all units under management.
Definition M5UnitUnified.cpp:263
bool add(Component &u, TwoWire &wire)
Add unit to be managed (I2C via TwoWire)
UnitComponent derived class for testing (GPIO)
Definition test_template.hpp:113
virtual U * get_instance()=0
return m5::unit::Component-derived class instance
UnitComponent derived class for testing (I2C)
Definition test_template.hpp:40
virtual U * get_instance()=0
return m5::unit::Component-derived class instance
UnitComponent derived class for testing (SPI)
Definition test_template.hpp:198
virtual U * get_instance()=0
return m5::unit::Component-derived class instance
virtual SPIClass & get_spi()
return SPIClass to be used (default: SPI)
Definition test_template.hpp:229
virtual SPISettings get_spi_settings()=0
return SPISettings for the unit under test
UnitComponent derived class for testing (UART)
Definition test_template.hpp:154
virtual HardwareSerial * init_serial()=0
Initialize the serial to be used.
virtual U * get_instance()=0
return m5::unit::Component-derived class instance
Opt-in, header-only board-aware connection helpers for M5UnitUnified examples.
For GoogleTest.
Top level namespace of M5Stack.
Definition test_helper.hpp:20
Unit-related namespace.