|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | +/* |
| 7 | + Example of using M5UnitUnified to connect both UnitHeart and HatHeart |
| 8 | +*/ |
| 9 | +#include <M5Unified.h> |
| 10 | +#include <M5UnitUnified.h> |
| 11 | +#include <M5UnitUnifiedHEART.h> |
| 12 | +#include <Wire.h> |
| 13 | +#include "../src/view.hpp" |
| 14 | + |
| 15 | +namespace { |
| 16 | +auto& lcd = M5.Display; |
| 17 | +m5::unit::UnitUnified Units; |
| 18 | +m5::unit::UnitHeart unit; |
| 19 | +m5::unit::HatHeart hat; |
| 20 | + |
| 21 | +View* view[2]{}; |
| 22 | + |
| 23 | +} // namespace |
| 24 | + |
| 25 | +using namespace m5::unit::max30102; |
| 26 | + |
| 27 | +void setup() |
| 28 | +{ |
| 29 | + // Configuration for using Wire1 |
| 30 | + auto m5cfg = M5.config(); |
| 31 | + m5cfg.pmic_button = false; // Disable BtnPWR |
| 32 | + m5cfg.internal_imu = false; // Disable internal IMU |
| 33 | + m5cfg.internal_rtc = false; // Disable internal RTC |
| 34 | + M5.begin(m5cfg); |
| 35 | + |
| 36 | + auto board = M5.getBoard(); |
| 37 | + if (board != m5::board_t::board_M5StickCPlus && board != m5::board_t::board_M5StickCPlus2) { |
| 38 | + M5_LOGE("Example for StickCPlus/CPlus2"); |
| 39 | + lcd.clear(TFT_RED); |
| 40 | + while (true) { |
| 41 | + m5::utility::delay(10000); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // The screen shall be in landscape mode |
| 46 | + if (lcd.height() > lcd.width()) { |
| 47 | + lcd.setRotation(1); |
| 48 | + } |
| 49 | + |
| 50 | + // Setup required to use HatHEART |
| 51 | + pinMode(25, INPUT_PULLUP); |
| 52 | + pinMode(26, OUTPUT); |
| 53 | + |
| 54 | + // Wire settings |
| 55 | + auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda); |
| 56 | + auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl); |
| 57 | + Wire.end(); |
| 58 | + Wire.begin(pin_num_sda, pin_num_scl, 400 * 1000U); |
| 59 | + |
| 60 | + Wire1.end(); |
| 61 | + Wire1.begin(0, 26, 400 * 1000U); |
| 62 | + |
| 63 | + // UnitHeart connected to GROOVE with Wire |
| 64 | + // HatHeart connected to PIN sockect with Wire1 |
| 65 | + if (!Units.add(unit, Wire) || !Units.add(hat, Wire1) || !Units.begin()) { |
| 66 | + M5_LOGE("Failed to begin"); |
| 67 | + lcd.clear(TFT_RED); |
| 68 | + while (true) { |
| 69 | + m5::utility::delay(10000); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + M5_LOGI("M5UnitUnified has been begun"); |
| 74 | + M5_LOGI("%s", Units.debugInfo().c_str()); |
| 75 | + |
| 76 | + lcd.startWrite(); |
| 77 | + |
| 78 | + view[0] = new View(lcd.width() >> 1, lcd.height(), true); |
| 79 | + view[1] = new View(lcd.width() >> 1, lcd.height(), false); |
| 80 | + view[0]->_monitor.setSamplingRate(unit.caluculateSamplingRate()); |
| 81 | + view[1]->_monitor.setSamplingRate(hat.caluculateSamplingRate()); |
| 82 | + view[0]->push(&lcd, lcd.width() >> 1, 0); |
| 83 | + view[1]->push(&lcd, 0, 0); |
| 84 | +} |
| 85 | + |
| 86 | +void loop() |
| 87 | +{ |
| 88 | + M5.update(); |
| 89 | + Units.update(); |
| 90 | + |
| 91 | + if (unit.updated()) { |
| 92 | + if (unit.overflow()) { |
| 93 | + M5_LOGW("OVERFLOW U:%u", unit.overflow()); |
| 94 | + } |
| 95 | + while (unit.available()) { |
| 96 | + view[0]->push_back(unit.ir(), unit.red()); |
| 97 | + view[0]->update(); |
| 98 | + unit.discard(); |
| 99 | + } |
| 100 | + view[0]->render(); |
| 101 | + view[0]->push(&lcd, lcd.width() >> 1, 0); |
| 102 | + } |
| 103 | + |
| 104 | + if (hat.updated()) { |
| 105 | + if (hat.overflow()) { |
| 106 | + M5_LOGW("OVERFLOW H:%u", hat.overflow()); |
| 107 | + } |
| 108 | + while (hat.available()) { |
| 109 | + view[1]->push_back(hat.ir(), hat.red()); |
| 110 | + view[1]->update(); |
| 111 | + hat.discard(); |
| 112 | + } |
| 113 | + view[1]->render(); |
| 114 | + view[1]->push(&lcd, 0, 0); |
| 115 | + } |
| 116 | + |
| 117 | + if (M5.BtnA.wasClicked()) { |
| 118 | + view[0]->clear(); |
| 119 | + view[1]->clear(); |
| 120 | + } |
| 121 | +} |
0 commit comments