2026-02-09 16:09:20 +09:00
|
|
|
/*
|
|
|
|
|
* 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 <M5Unified.h>
|
|
|
|
|
#include <M5UnitUnified.h>
|
|
|
|
|
#include <M5UnitUnifiedHEART.h>
|
|
|
|
|
#include <Wire.h>
|
|
|
|
|
#include <M5HAL.hpp>
|
|
|
|
|
#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:
|
2026-02-20 17:41:19 +09:00
|
|
|
return {0, 26};
|
2026-02-09 16:09:20 +09:00
|
|
|
case m5::board_t::board_M5StickS3:
|
2026-02-20 17:41:19 +09:00
|
|
|
return {8, 0};
|
2026-02-09 16:09:20 +09:00
|
|
|
case m5::board_t::board_M5StackCoreInk:
|
2026-02-20 17:41:19 +09:00
|
|
|
return {25, 26};
|
2026-02-09 16:09:20 +09:00
|
|
|
case m5::board_t::board_ArduinoNessoN1:
|
2026-02-20 17:41:19 +09:00
|
|
|
return {6, 7};
|
2026-02-09 16:09:20 +09:00
|
|
|
default:
|
2026-02-20 17:41:19 +09:00
|
|
|
return {-1, -1};
|
2026-02-09 16:09:20 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#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);
|
|
|
|
|
|
2026-02-09 20:17:51 +09:00
|
|
|
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()) {
|
2026-02-09 16:09:20 +09:00
|
|
|
lcd.setRotation(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(USING_HAT_HEART)
|
|
|
|
|
const auto pins = get_hat_i2c_pins(board);
|
2026-03-04 19:39:21 +09:00
|
|
|
M5_LOGI("getHatPin: SDA:%u SCL:%u", pins.sda, pins.scl);
|
2026-02-09 16:09:20 +09:00
|
|
|
if (pins.sda < 0 || pins.scl < 0) {
|
|
|
|
|
M5_LOGE("Illegal pin number");
|
2026-03-04 19:39:21 +09:00
|
|
|
lcd.fillScreen(TFT_RED);
|
2026-02-09 16:09:20 +09:00
|
|
|
while (true) {
|
|
|
|
|
m5::utility::delay(10000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Setup required to use HatHEART
|
|
|
|
|
pinMode(pins.scl, OUTPUT);
|
|
|
|
|
|
2026-03-04 19:39:21 +09:00
|
|
|
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()) {
|
2026-02-09 16:09:20 +09:00
|
|
|
M5_LOGE("Failed to begin");
|
2026-03-04 19:39:21 +09:00
|
|
|
lcd.fillScreen(TFT_RED);
|
2026-02-09 16:09:20 +09:00
|
|
|
while (true) {
|
|
|
|
|
m5::utility::delay(10000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else
|
2026-03-04 19:39:21 +09:00
|
|
|
// 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{};
|
2026-02-09 16:09:20 +09:00
|
|
|
if (board == m5::board_t::board_ArduinoNessoN1) {
|
2026-03-04 19:39:21 +09:00
|
|
|
// 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);
|
2026-02-09 16:09:20 +09:00
|
|
|
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());
|
2026-03-04 19:39:21 +09:00
|
|
|
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();
|
2026-02-09 16:09:20 +09:00
|
|
|
} else {
|
2026-03-04 19:39:21 +09:00
|
|
|
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);
|
2026-02-09 16:09:20 +09:00
|
|
|
M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl);
|
|
|
|
|
Wire.end();
|
|
|
|
|
Wire.begin(pin_num_sda, pin_num_scl, 400000U);
|
2026-03-04 19:39:21 +09:00
|
|
|
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);
|
2026-02-09 16:09:20 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
M5_LOGI("M5UnitUnified has been begun");
|
|
|
|
|
M5_LOGI("%s", Units.debugInfo().c_str());
|
|
|
|
|
|
2026-03-04 19:39:21 +09:00
|
|
|
lcd.fillScreen(0);
|
2026-02-09 16:09:20 +09:00
|
|
|
|
|
|
|
|
#if defined(USING_UNIT_HEART)
|
|
|
|
|
view = new View(lcd.width(), lcd.height(), true);
|
|
|
|
|
#else
|
|
|
|
|
view = new View(lcd.width(), lcd.height(), false);
|
|
|
|
|
#endif
|
2026-02-09 17:26:44 +09:00
|
|
|
view->_monitor.setSamplingRate(heart.calculateSamplingRate());
|
2026-02-09 16:09:20 +09:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|