/* * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD * * SPDX-License-Identifier: MIT */ /* Graphical meter example for UnitHeart / HatHeart The core must be equipped with LCD */ #include #include #include #include #include #include "../src/view.hpp" // ************************************************************* // Choose one define symbol to match the unit you are using // ************************************************************* #if !defined(USING_UNIT_HEART) && !defined(USING_HAT_HEART) // #define USING_UNIT_HEART // #define USING_HAT_HEART #endif #if defined(USING_UNIT_HEART) #pragma message "Using UnitHeart" using namespace m5::unit::max30100; #elif defined(USING_HAT_HEART) #pragma message "Using HatHeart" using namespace m5::unit::max30102; #else #error Please choose unit! #endif namespace { auto& lcd = M5.Display; m5::unit::UnitUnified Units; #if defined(USING_UNIT_HEART) m5::unit::UnitHeart heart; #elif defined(USING_HAT_HEART) m5::unit::HatHeart heart; #endif View* view{}; #if defined(USING_HAT_HEART) struct I2cPins { int sda; int scl; }; I2cPins get_hat_i2c_pins(const m5::board_t board) { switch (board) { case m5::board_t::board_M5StickC: case m5::board_t::board_M5StickCPlus: case m5::board_t::board_M5StickCPlus2: return {0, 26}; case m5::board_t::board_M5StickS3: return {8, 0}; case m5::board_t::board_M5StackCoreInk: return {25, 26}; case m5::board_t::board_ArduinoNessoN1: return {6, 7}; default: return {-1, -1}; } } #endif } // namespace void setup() { auto m5cfg = M5.config(); #if defined(USING_HAT_HEART) m5cfg.pmic_button = false; // Disable BtnPWR m5cfg.internal_imu = false; // Disable internal IMU m5cfg.internal_rtc = false; // Disable internal RTC #endif M5.begin(m5cfg); M5.setTouchButtonHeightByRatio(100); auto board = M5.getBoard(); // The screen shall be in landscape mode (skip for M5Tab5) if (board != m5::board_t::board_M5Tab5 && lcd.height() > lcd.width()) { lcd.setRotation(1); } #if defined(USING_HAT_HEART) const auto pins = get_hat_i2c_pins(board); M5_LOGI("getHatPin: SDA:%u SCL:%u", pins.sda, pins.scl); if (pins.sda < 0 || pins.scl < 0) { M5_LOGE("Illegal pin number"); lcd.fillScreen(TFT_RED); while (true) { m5::utility::delay(10000); } } // Setup required to use HatHEART pinMode(pins.scl, OUTPUT); auto& wire = (board == m5::board_t::board_ArduinoNessoN1) ? Wire1 : Wire; wire.end(); wire.begin(pins.sda, pins.scl, 400 * 1000U); if (!Units.add(heart, wire) || !Units.begin()) { M5_LOGE("Failed to begin"); lcd.fillScreen(TFT_RED); while (true) { m5::utility::delay(10000); } } #else // NessoN1: Arduino Wire (I2C_NUM_0) cannot be used for GROVE port. // Wire is used by M5Unified In_I2C for internal devices (IOExpander etc.). // Wire1 exists but is reserved for HatPort — cannot be used for GROVE. // Reconfiguring Wire to GROVE pins breaks In_I2C, causing ESP_ERR_INVALID_STATE in M5.update(). // Solution: Use SoftwareI2C via M5HAL (bit-banging) for the GROVE port. // NanoC6: Wire.begin() on GROVE pins conflicts with m5::I2C_Class registered by Ex_I2C.setPort() // on the same I2C_NUM_0, causing sporadic NACK errors. // Solution: Use M5.Ex_I2C (m5::I2C_Class) directly instead of Arduino Wire. bool unit_ready{}; if (board == m5::board_t::board_ArduinoNessoN1) { // NessoN1: GROVE is on port_b (GPIO 5/4), not port_a (which maps to Wire pins 8/10) auto pin_num_sda = M5.getPin(m5::pin_name_t::port_b_out); auto pin_num_scl = M5.getPin(m5::pin_name_t::port_b_in); M5_LOGI("getPin(M5HAL): SDA:%u SCL:%u", pin_num_sda, pin_num_scl); m5::hal::bus::I2CBusConfig i2c_cfg; i2c_cfg.pin_sda = m5::hal::gpio::getPin(pin_num_sda); i2c_cfg.pin_scl = m5::hal::gpio::getPin(pin_num_scl); auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg); M5_LOGI("Bus:%d", i2c_bus.has_value()); unit_ready = Units.add(heart, i2c_bus ? i2c_bus.value() : nullptr) && Units.begin(); } else if (board == m5::board_t::board_M5NanoC6) { // NanoC6: Use M5.Ex_I2C (m5::I2C_Class, not Arduino Wire) M5_LOGI("Using M5.Ex_I2C"); unit_ready = Units.add(heart, M5.Ex_I2C) && Units.begin(); } else { auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda); auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl); M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl); Wire.end(); Wire.begin(pin_num_sda, pin_num_scl, 400000U); unit_ready = Units.add(heart, Wire) && Units.begin(); } if (!unit_ready) { M5_LOGE("Failed to begin"); lcd.fillScreen(TFT_RED); while (true) { m5::utility::delay(10000); } } #endif M5_LOGI("M5UnitUnified has been begun"); M5_LOGI("%s", Units.debugInfo().c_str()); lcd.fillScreen(0); #if defined(USING_UNIT_HEART) view = new View(lcd.width(), lcd.height(), true); #else view = new View(lcd.width(), lcd.height(), false); #endif view->_monitor.setSamplingRate(heart.calculateSamplingRate()); view->push(&lcd, 0, 0); M5_LOGI("periodic:%d", heart.inPeriodic()); } void loop() { M5.update(); Units.update(); if (heart.updated()) { if (heart.overflow()) { M5_LOGW("OVERFLOW:%u", heart.overflow()); } while (heart.available()) { view->push_back(heart.ir(), heart.red()); view->update(); heart.discard(); } view->render(); view->push(&lcd, 0, 0); } if (M5.BtnA.wasClicked()) { view->clear(); } }