11#ifndef M5_UNIT_COMPONENT_GOOGLETEST_TEMPLATE_HPP
12#define M5_UNIT_COMPONENT_GOOGLETEST_TEMPLATE_HPP
18#include <esp32-hal-i2c.h>
40 static_assert(std::is_base_of<m5::unit::Component, U>::value,
"U must be derived from Component");
43 virtual void SetUp()
override
47 FAIL() <<
"Failed to get_instance";
50 ustr = m5::utility::formatString(
"%s",
unit->deviceName());
52 FAIL() <<
"Failed to begin " << ustr;
56 virtual void TearDown()
override
62 auto board = M5.getBoard();
63 if (board == m5::board_t::board_ArduinoNessoN1) {
65 auto sda = M5.getPin(m5::pin_name_t::port_b_out);
66 auto scl = M5.getPin(m5::pin_name_t::port_b_in);
67 return begin_with_software_i2c(sda, scl);
69 if (board == m5::board_t::board_M5NanoC6) {
71 return begin_with_ex_i2c();
74 return begin_with_wire(Wire);
77 bool begin_with_wire(TwoWire& wire, uint32_t wnum = 0)
79 auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
80 auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
81 auto freq =
unit->component_config().clock;
82 if (i2cIsInit(wnum)) {
83 M5_LOGD(
"Already initialized Wire%u. Terminate and restart FREQ %u", wnum, freq);
86 M5_LOGI(
"Wire begin SDA:%u SCL:%u FREQ:%u", pin_num_sda, pin_num_scl, freq);
87 wire.begin(pin_num_sda, pin_num_scl, freq);
91 bool begin_with_ex_i2c()
93 M5_LOGI(
"Using Ex_I2C");
97 bool begin_with_software_i2c(int8_t sda, int8_t scl)
99 m5::hal::bus::I2CBusConfig i2c_cfg;
100 i2c_cfg.pin_sda = m5::hal::gpio::getPin(sda);
101 i2c_cfg.pin_scl = m5::hal::gpio::getPin(scl);
102 auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg);
103 M5_LOGI(
"Using M5HAL SDA:%u SCL:%u", sda, scl);
104 return Units.
add(*
unit, i2c_bus ? i2c_bus.value() :
nullptr) && Units.
begin();
111 std::unique_ptr<U>
unit{};
122 static_assert(std::is_base_of<m5::unit::Component, U>::value,
"U must be derived from Component");
125 virtual void SetUp()
override
129 FAIL() <<
"Failed to get_instance";
132 ustr = m5::utility::formatString(
"%s:GPIO",
unit->deviceName());
134 FAIL() <<
"Failed to begin " << ustr;
138 virtual void TearDown()
override
144 auto pin_num_gpio_in = M5.getPin(m5::pin_name_t::port_b_in);
145 auto pin_num_gpio_out = M5.getPin(m5::pin_name_t::port_b_out);
146 if (pin_num_gpio_in < 0 || pin_num_gpio_out < 0) {
147 M5_LOGW(
"PortB is not available");
149 pin_num_gpio_in = M5.getPin(m5::pin_name_t::port_a_pin1);
150 pin_num_gpio_out = M5.getPin(m5::pin_name_t::port_a_pin2);
152 M5_LOGI(
"getPin: %d,%d", pin_num_gpio_in, pin_num_gpio_out);
153 return Units.
add(*
unit, pin_num_gpio_in, pin_num_gpio_out) && Units.
begin();
160 std::unique_ptr<U>
unit{};
171 static_assert(std::is_base_of<m5::unit::Component, U>::value,
"U must be derived from Component");
174 virtual void SetUp()
override
178 FAIL() <<
"Failed to get_instance";
181 ustr = m5::utility::formatString(
"%s:UART",
unit->deviceName());
183 FAIL() <<
"Failed to begin " << ustr;
187 virtual void TearDown()
override
194 return serial && Units.
add(*
unit, *serial) && Units.
begin();
203 std::unique_ptr<U>
unit{};
205 HardwareSerial* serial{};
215 static_assert(std::is_base_of<m5::unit::Component, U>::value,
"U must be derived from Component");
218 virtual void SetUp()
override
222 FAIL() <<
"Failed to get_instance";
225 ustr = m5::utility::formatString(
"%s:SPI",
unit->deviceName());
227 FAIL() <<
"Failed to begin " << ustr;
231 virtual void TearDown()
override
251 std::unique_ptr<U>
unit{};
Main header of M5UnitComponent.
For managing and leading units.
bool begin()
Begin of all units under management.
Definition M5UnitUnified.cpp:169
bool add(Component &u, TwoWire &wire)
Adding unit to be managed (I2C)
Definition M5UnitUnified.cpp:41
UnitComponent derived class for testing (GPIO)
Definition test_template.hpp:121
virtual U * get_instance()=0
return m5::unit::Component-derived class instance
UnitComponent derived class for testing (I2C)
Definition test_template.hpp:39
virtual U * get_instance()=0
return m5::unit::Component-derived class instance
UnitComponent derived class for testing (SPI)
Definition test_template.hpp:214
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:245
virtual SPISettings get_spi_settings()=0
return SPISettings for the unit under test
UnitComponent derived class for testing (UART)
Definition test_template.hpp:170
virtual HardwareSerial * init_serial()=0
Initialize the serial to be used.
virtual U * get_instance()=0
return m5::unit::Component-derived class instance
Top level namespace of M5stack.
Definition test_helper.hpp:20