|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | +/* |
| 7 | + Example using M5UnitUnified for UnitToF4M |
| 8 | +*/ |
| 9 | +#include <M5Unified.h> |
| 10 | +#include <M5UnitUnified.h> |
| 11 | +#include <M5UnitUnifiedTOF.h> |
| 12 | +#include <M5Utility.h> |
| 13 | + |
| 14 | +namespace { |
| 15 | +auto& lcd = M5.Display; |
| 16 | +m5::unit::UnitUnified Units; |
| 17 | +m5::unit::UnitToF4M unit; |
| 18 | +int16_t lastRange{}; |
| 19 | +} // namespace |
| 20 | + |
| 21 | +void setup() |
| 22 | +{ |
| 23 | + M5.begin(); |
| 24 | + // The screen shall be in landscape mode if exists |
| 25 | + if (lcd.height() > lcd.width()) { |
| 26 | + lcd.setRotation(1); |
| 27 | + } |
| 28 | + |
| 29 | + auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda); |
| 30 | + auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl); |
| 31 | + Wire.begin(pin_num_sda, pin_num_scl, 400 * 1000U); |
| 32 | + |
| 33 | + if (!Units.add(unit, Wire) || !Units.begin()) { |
| 34 | + M5_LOGE("Failed to begin"); |
| 35 | + lcd.clear(TFT_RED); |
| 36 | + while (true) { |
| 37 | + m5::utility::delay(10000); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + M5_LOGI("M5UnitUnified has been begun"); |
| 42 | + M5_LOGI("%s", Units.debugInfo().c_str()); |
| 43 | + |
| 44 | + lcd.setTextColor(TFT_ORANGE); |
| 45 | + lcd.setFont(&fonts::Orbitron_Light_32); |
| 46 | + lcd.setTextDatum(middle_center); |
| 47 | + float scale = lcd.width() / (32 * 4.0f); |
| 48 | + lcd.setTextSize(scale, scale); |
| 49 | + |
| 50 | + lcd.startWrite(); |
| 51 | + lcd.clear(); |
| 52 | +} |
| 53 | + |
| 54 | +void loop() |
| 55 | +{ |
| 56 | + M5.update(); |
| 57 | + Units.update(); |
| 58 | + |
| 59 | + if (unit.updated()) { |
| 60 | + auto range = unit.range(); |
| 61 | + if (range > 0 && range != lastRange) { |
| 62 | + lcd.fillRect(0, lcd.height() / 2 - lcd.fontHeight() / 2, lcd.width(), lcd.fontHeight(), TFT_BLACK); |
| 63 | + lcd.drawString(m5::utility::formatString("%d", range).c_str(), lcd.width() / 2, lcd.height() / 2); |
| 64 | + lastRange = range; |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments