|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | +/* |
| 7 | + Example: Reset and play using TOF units |
| 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 | + |
| 18 | +m5::unit::UnitToF unitToF; |
| 19 | +m5::unit::UnitToF4M unitToF4M; |
| 20 | + |
| 21 | +m5::unit::UnitToF* pUnitToF; |
| 22 | +m5::unit::UnitToF4M* pUnitToF4M; |
| 23 | + |
| 24 | +int16_t lastRange{}; |
| 25 | +} // namespace |
| 26 | + |
| 27 | +void setup() |
| 28 | +{ |
| 29 | + M5.begin(); |
| 30 | + |
| 31 | + if (lcd.height() > lcd.width()) { |
| 32 | + lcd.setRotation(1); |
| 33 | + } |
| 34 | + |
| 35 | + auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda); |
| 36 | + auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl); |
| 37 | + Wire.end(); |
| 38 | + Wire.begin(pin_num_sda, pin_num_scl, 400 * 1000U); |
| 39 | + |
| 40 | + // Add all units |
| 41 | + if (!Units.add(unitToF, Wire) || !Units.add(unitToF4M, Wire) || !Units.begin(true /* ignore error on begin */)) { |
| 42 | + M5_LOGE("Failed to begin"); |
| 43 | + lcd.fillScreen(TFT_RED); |
| 44 | + while (true) m5::utility::delay(1000); |
| 45 | + } |
| 46 | + M5_LOGI("M5UnitUnified has been begun"); |
| 47 | + M5_LOGI("%s", Units.debugInfo().c_str()); |
| 48 | + |
| 49 | + // Reset and Play! |
| 50 | + pUnitToF = Units.get<m5::unit::UnitToF>(); |
| 51 | + pUnitToF4M = Units.get<m5::unit::UnitToF4M>(); |
| 52 | + |
| 53 | + if (pUnitToF) { |
| 54 | + M5.Log.printf("Found UnitTOF\n"); |
| 55 | + lcd.setTextColor(TFT_ORANGE); |
| 56 | + } else if (pUnitToF4M) { |
| 57 | + M5.Log.printf("Found UnitTOF4M\n"); |
| 58 | + lcd.setTextColor(TFT_CYAN); |
| 59 | + } else { |
| 60 | + M5_LOGE("Not exists unit"); |
| 61 | + lcd.fillScreen(TFT_RED); |
| 62 | + while (true) m5::utility::delay(1000); |
| 63 | + } |
| 64 | + |
| 65 | + lcd.setFont(&fonts::Orbitron_Light_32); |
| 66 | + lcd.setTextDatum(middle_center); |
| 67 | + float scale = lcd.width() / (32 * 4.0f); |
| 68 | + lcd.setTextSize(scale, scale); |
| 69 | + |
| 70 | + lcd.startWrite(); |
| 71 | + lcd.clear(); |
| 72 | +} |
| 73 | + |
| 74 | +void loop() |
| 75 | +{ |
| 76 | + M5.update(); |
| 77 | + Units.update(); |
| 78 | + |
| 79 | + // For UnitToF |
| 80 | + if (pUnitToF && pUnitToF->updated()) { |
| 81 | + auto range = pUnitToF->range(); |
| 82 | + if (range >= 0 && range != lastRange) { |
| 83 | + lcd.fillRect(0, lcd.height() / 2 - lcd.fontHeight() / 2, lcd.width(), lcd.fontHeight(), TFT_BLACK); |
| 84 | + lcd.drawString(m5::utility::formatString("%d", range).c_str(), lcd.width() / 2, lcd.height() / 2); |
| 85 | + lastRange = range; |
| 86 | + } |
| 87 | + } |
| 88 | + // For UnitToF4M |
| 89 | + if (pUnitToF4M && pUnitToF4M->updated()) { |
| 90 | + auto range = pUnitToF4M->range(); |
| 91 | + if (range >= 0 && range != lastRange) { |
| 92 | + lcd.fillRect(0, lcd.height() / 2 - lcd.fontHeight() / 2, lcd.width(), lcd.fontHeight(), TFT_BLACK); |
| 93 | + lcd.drawString(m5::utility::formatString("%d", range).c_str(), lcd.width() / 2, lcd.height() / 2); |
| 94 | + lastRange = range; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments