@@ -22,35 +22,72 @@ using namespace m5::unit::max30102;
2222using namespace m5 ::unit::max30102::command;
2323using m5::unit::types::elapsed_time_t ;
2424
25- #if !defined(M5STACK_M5STICK_CPLUS2) && !defined(ARDUINO_M5Stick_C)
26- #error This test for M5StckCPlus or M5StckCPlus2
27- #else
2825namespace hat {
26+ struct I2cPins {
27+ int sda;
28+ int scl;
29+ };
30+
31+ I2cPins get_hat_i2c_pins (const m5::board_t board)
32+ {
33+ switch (board) {
34+ case m5::board_t ::board_M5StickC:
35+ case m5::board_t ::board_M5StickCPlus:
36+ case m5::board_t ::board_M5StickCPlus2:
37+ return {0 , 26 };
38+ case m5::board_t ::board_M5StickS3:
39+ return {8 , 0 };
40+ case m5::board_t ::board_M5StackCoreInk:
41+ return {25 , 26 };
42+ case m5::board_t ::board_ArduinoNessoN1:
43+ return {6 , 7 };
44+ default :
45+ return {-1 , -1 };
46+ }
47+ }
48+
2949template <uint32_t FREQ, uint32_t WNUM = 0 >
3050class GlobalFixture : public ::testing::Environment {
3151 static_assert (WNUM < 2 , " Wire number must be lesser than 2" );
3252
3353public:
3454 void SetUp () override
3555 {
36- // Setup required to use HatHEART
37- pinMode (25 , INPUT_PULLUP);
38- pinMode (26 , OUTPUT);
56+ const auto pins = get_hat_i2c_pins (M5.getBoard ());
57+ // M5_LOGI("pin:%d %d", pins.sda, pins.scl);
58+
59+ pinMode (pins.scl , OUTPUT);
3960
4061 TwoWire* w[2 ] = {&Wire, &Wire1};
4162 if (WNUM < m5::stl::size (w) && i2cIsInit (WNUM)) {
4263 M5_LOGW (" Already inititlized Wire %d. Terminate and restart FREQ %u" , WNUM, FREQ);
4364 w[WNUM]->end ();
4465 }
45- w[WNUM]->begin (0 , 26 , FREQ);
66+ w[WNUM]->begin (pins. sda , pins. scl , FREQ);
4667 }
4768};
69+
4870} // namespace hat
49- const ::testing::Environment* global_fixture = ::testing::AddGlobalTestEnvironment( new hat::GlobalFixture< 400000U >());
50- # endif
71+ const ::testing::Environment* global_fixture =
72+ ::testing::AddGlobalTestEnvironment ( new hat::GlobalFixture< 400000U , 1 /* Wire1 */ >());
5173
5274class TestMAX30102 : public ComponentTestBase <UnitMAX30102, bool > {
5375protected:
76+ virtual bool begin () override
77+ {
78+ if (is_using_hal ()) {
79+ const auto pins = hat::get_hat_i2c_pins (M5.getBoard ());
80+ m5::hal::bus::I2CBusConfig i2c_cfg;
81+ i2c_cfg.pin_sda = m5::hal::gpio::getPin (pins.sda );
82+ i2c_cfg.pin_scl = m5::hal::gpio::getPin (pins.scl );
83+ auto i2c_bus = m5::hal::bus::i2c::getBus (i2c_cfg);
84+ return Units.add (*unit, i2c_bus ? i2c_bus.value () : nullptr ) && Units.begin ();
85+ }
86+
87+ // Using TwoWire
88+ return Units.add (*unit, Wire1) && Units.begin ();
89+ }
90+
5491 virtual UnitMAX30102* get_instance () override
5592 {
5693 auto ptr = new m5::unit::UnitMAX30102 ();
@@ -362,7 +399,6 @@ void test_periodic_spo2(UnitMAX30102* unit)
362399 EXPECT_TRUE (unit->stopPeriodicMeasurement ());
363400 EXPECT_FALSE (unit->inPeriodic ());
364401
365- EXPECT_NE (elapsed, 0 );
366402 EXPECT_GE (elapsed, STORED_SIZE * unit->interval ());
367403 // M5_LOGI(">>> %s>elapsed: %ld/%u retrieved:%u overflow:%u", s.c_str(), elapsed, STORED_SIZE *
368404 // unit->interval(),
@@ -481,7 +517,6 @@ void test_periodic_hr(UnitMAX30102* unit)
481517 EXPECT_TRUE (unit->stopPeriodicMeasurement ());
482518 EXPECT_FALSE (unit->inPeriodic ());
483519
484- EXPECT_NE (elapsed, 0 );
485520 EXPECT_GE (elapsed, STORED_SIZE * unit->interval ());
486521 // M5_LOGI(">>> %s>elapsed: %ld/%u retrieved:%u overflow:%u", s.c_str(), elapsed, STORED_SIZE *
487522 // unit->interval(),
@@ -554,7 +589,6 @@ void test_periodic_multi(UnitMAX30102* unit)
554589 EXPECT_TRUE (unit->stopPeriodicMeasurement ());
555590 EXPECT_FALSE (unit->inPeriodic ());
556591
557- EXPECT_NE (elapsed, 0 );
558592 EXPECT_GE (elapsed, STORED_SIZE * unit->interval ());
559593 // M5_LOGI(">>> %s>elapsed: %ld/%u retrieved:%u overflow:%u", s.c_str(), elapsed, STORED_SIZE *
560594 // unit->interval(),
0 commit comments