You've already forked M5Unit-DISTANCE
mirror of
https://github.com/m5stack/M5Unit-DISTANCE.git
synced 2026-05-20 11:35:18 -07:00
155 lines
5.2 KiB
C++
155 lines
5.2 KiB
C++
/*
|
|
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
/*
|
|
Example using M5UnitUnified for UnitUltraSonic I2C/IO
|
|
*/
|
|
#include <M5Unified.h>
|
|
#include <M5UnitUnified.h>
|
|
#include <M5UnitUnifiedDISTANCE.h>
|
|
#include <M5Utility.h>
|
|
#include <M5HAL.hpp> // For NessoN1
|
|
|
|
// *********************************************************************
|
|
// Choose connection
|
|
// *********************************************************************
|
|
#if !defined(CONNECT_VIA_I2C) && !defined(CONNECT_VIA_GPIO)
|
|
// UnitUltraSonicI2C (SKU:U098-B1)
|
|
// #define CONNECT_VIA_I2C
|
|
// UnitUltraSonicIO (SKU:U098-B2)
|
|
// #define CONNECT_VIA_GPIO
|
|
#endif
|
|
|
|
namespace {
|
|
auto& lcd = M5.Display;
|
|
m5::unit::UnitUnified Units;
|
|
#if defined(CONNECT_VIA_I2C)
|
|
m5::unit::UnitUltraSonicI2C unit;
|
|
constexpr uint32_t interval{150}; // For I2C, the measurement interval is 150 ms or more
|
|
#elif defined(CONNECT_VIA_GPIO)
|
|
m5::unit::UnitUltraSonicIO unit;
|
|
constexpr uint32_t interval{50}; // For GPIO, the measurement interval is 50 ms or more
|
|
#else
|
|
#error "Choose connection"
|
|
#endif
|
|
|
|
} // namespace
|
|
|
|
using namespace m5::unit::rcwl9620;
|
|
|
|
void setup()
|
|
{
|
|
M5.begin();
|
|
M5.setTouchButtonHeightByRatio(100);
|
|
// The screen shall be in landscape mode
|
|
if (lcd.height() > lcd.width()) {
|
|
lcd.setRotation(1);
|
|
}
|
|
|
|
auto cfg = unit.config();
|
|
cfg.interval_ms = interval;
|
|
unit.config(cfg);
|
|
|
|
#if defined(CONNECT_VIA_I2C)
|
|
// PortA as I2C
|
|
M5.Log.printf("Using I2C\n");
|
|
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);
|
|
|
|
auto board = M5.getBoard();
|
|
|
|
// 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)
|
|
pin_num_sda = M5.getPin(m5::pin_name_t::port_b_out);
|
|
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(unit, 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(unit, M5.Ex_I2C) && Units.begin();
|
|
} else {
|
|
M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl);
|
|
Wire.end();
|
|
Wire.begin(pin_num_sda, pin_num_scl, 400 * 1000U);
|
|
unit_ready = Units.add(unit, Wire) && Units.begin();
|
|
}
|
|
if (!unit_ready) {
|
|
M5_LOGE("Failed to begin");
|
|
lcd.fillScreen(TFT_RED);
|
|
while (true) {
|
|
m5::utility::delay(10000);
|
|
}
|
|
}
|
|
#elif defined(CONNECT_VIA_GPIO)
|
|
// PortB as GPIO if available, PortA if not
|
|
M5.Log.printf("Using GPIO\n");
|
|
auto pin_num_gpio_in = M5.getPin(m5::pin_name_t::port_b_in);
|
|
auto pin_num_gpio_out = M5.getPin(m5::pin_name_t::port_b_out);
|
|
if (pin_num_gpio_in < 0 || pin_num_gpio_out < 0) {
|
|
M5_LOGW("PortB is not available");
|
|
Wire.end();
|
|
pin_num_gpio_in = M5.getPin(m5::pin_name_t::port_a_pin1);
|
|
pin_num_gpio_out = M5.getPin(m5::pin_name_t::port_a_pin2);
|
|
}
|
|
M5_LOGI("getPin :%d,%d", pin_num_gpio_in, pin_num_gpio_out);
|
|
|
|
if (!Units.add(unit, pin_num_gpio_in, pin_num_gpio_out) || !Units.begin()) {
|
|
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.setFont(&fonts::AsciiFont8x16);
|
|
lcd.fillScreen(TFT_DARKGREEN);
|
|
lcd.fillRect(8, 8, 8 * 24, 16 * 1, TFT_BLACK);
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
M5.update();
|
|
|
|
// Periodic
|
|
Units.update();
|
|
if (unit.updated()) {
|
|
M5.Log.printf("Distance:%f Raw:%x\n", unit.distance(), unit.oldest().raw_distance());
|
|
|
|
lcd.startWrite();
|
|
lcd.fillRect(8, 8, 8 * 24, 16 * 1, TFT_BLACK);
|
|
lcd.setCursor(8, 8 + 16 * 0);
|
|
lcd.printf("Distance:%7.2f mm", unit.distance());
|
|
lcd.endWrite();
|
|
}
|
|
|
|
if (M5.BtnA.wasClicked()) {
|
|
unit.stopPeriodicMeasurement();
|
|
Data d{};
|
|
if (unit.measureSingleshot(d)) {
|
|
M5.Log.printf("Single: %.2f mm\n", d.distance());
|
|
}
|
|
unit.startPeriodicMeasurement(interval);
|
|
}
|
|
}
|