|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | +/* |
| 7 | + Example: UnitINA226 |
| 8 | +*/ |
| 9 | +#include <M5Unified.h> |
| 10 | +#include <M5UnitUnified.h> |
| 11 | +#include <M5UnitUnifiedMETER.h> |
| 12 | +#include <Wire.h> |
| 13 | + |
| 14 | +// ************************************************************* |
| 15 | +// Choose one define symbol to match the unit you are using |
| 16 | +// ************************************************************* |
| 17 | +#if !defined(USING_UNIT_INA226_1A) && !defined(USING_UNIT_INA226_10A) && !defined(USING_UNIT_INA226_10A_IN_TAB5) |
| 18 | +// #define USING_UNIT_INA226_1A |
| 19 | +// #define USING_UNIT_INA226_10A |
| 20 | +// #define USING_UNIT_INA226_10A_IN_TAB5 |
| 21 | +#endif |
| 22 | + |
| 23 | +namespace { |
| 24 | +auto& lcd = M5.Display; |
| 25 | +m5::unit::UnitUnified Units; |
| 26 | +#if defined(USING_UNIT_INA226_1A) |
| 27 | + |
| 28 | +#pragma message "Using 1A" |
| 29 | +m5::unit::UnitINA226_1A unit; |
| 30 | + |
| 31 | +#elif defined(USING_UNIT_INA226_10A) |
| 32 | + |
| 33 | +#pragma message "Using 10A" |
| 34 | +m5::unit::UnitINA226_10A unit; |
| 35 | + |
| 36 | +#elif defined(USING_UNIT_INA226_10A_IN_TAB5) |
| 37 | + |
| 38 | +#pragma message "Using 10A (Tab5)" |
| 39 | +m5::unit::UnitINA226_10A unit; |
| 40 | +#if !defined(CONFIG_IDF_TARGET_ESP32P4) |
| 41 | +#error "Compile not for Tab5" |
| 42 | +#endif |
| 43 | + |
| 44 | +#else |
| 45 | +#error "Choose unit" |
| 46 | +#endif |
| 47 | +} // namespace |
| 48 | + |
| 49 | +void setup() |
| 50 | +{ |
| 51 | + M5.begin(); |
| 52 | + |
| 53 | + // The screen shall be in landscape mode |
| 54 | + if (lcd.height() > lcd.width()) { |
| 55 | + lcd.setRotation(1); |
| 56 | + } |
| 57 | + |
| 58 | +#if defined(USING_UNIT_INA226_10A_IN_TAB5) |
| 59 | + auto board = M5.getBoard(); |
| 60 | + if (board != m5::board_t::board_M5Tab5) { |
| 61 | + M5_LOGE("Core is NOT Tab5"); |
| 62 | + while (true) { |
| 63 | + m5::utility::delay(10000); |
| 64 | + } |
| 65 | + } |
| 66 | + auto pin_num_sda = 31; |
| 67 | + auto pin_num_scl = 32; |
| 68 | + M5_LOGI("Pin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl); |
| 69 | + Wire1.end(); |
| 70 | + Wire1.begin(pin_num_sda, pin_num_scl, 400000U); |
| 71 | + |
| 72 | + if (!Units.add(unit, Wire1) || !Units.begin()) { |
| 73 | + M5_LOGE("Failed to begin"); |
| 74 | + lcd.clear(TFT_RED); |
| 75 | + while (true) { |
| 76 | + m5::utility::delay(10000); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +#else |
| 81 | + auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda); |
| 82 | + auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl); |
| 83 | + M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl); |
| 84 | + |
| 85 | + Wire.end(); |
| 86 | + Wire.begin(pin_num_sda, pin_num_scl, 400000U); |
| 87 | + if (!Units.add(unit, Wire) || !Units.begin()) { |
| 88 | + M5_LOGE("Failed to begin"); |
| 89 | + lcd.clear(TFT_RED); |
| 90 | + while (true) { |
| 91 | + m5::utility::delay(10000); |
| 92 | + } |
| 93 | + } |
| 94 | +#endif |
| 95 | + |
| 96 | + lcd.setFont(&fonts::AsciiFont8x16); |
| 97 | + |
| 98 | + M5_LOGI("M5UnitUnified has been begun"); |
| 99 | + M5_LOGI("%s", Units.debugInfo().c_str()); |
| 100 | + lcd.clear(TFT_DARKGREEN); |
| 101 | +} |
| 102 | + |
| 103 | +void loop() |
| 104 | +{ |
| 105 | + using namespace m5::unit::ina226; |
| 106 | + |
| 107 | + M5.update(); |
| 108 | + auto touch = M5.Touch.getDetail(); |
| 109 | + Units.update(); |
| 110 | + |
| 111 | + if (unit.updated()) { |
| 112 | + M5.Log.printf(">A:%f\n>SV:%f\n>BV:%f\n>W:%f\n", unit.current(), unit.shuntVoltage(), unit.voltage(), |
| 113 | + unit.power()); |
| 114 | + |
| 115 | + lcd.startWrite(); |
| 116 | + lcd.fillRect(0, 0, lcd.width(), 16 * 4); |
| 117 | + lcd.setCursor(0, 0); |
| 118 | + lcd.printf( |
| 119 | + " C:%5.2f mA\n" |
| 120 | + "SV:%5.2f mV\n" |
| 121 | + "BV:%5.2f mV\n" |
| 122 | + " P:%5.2f mW", |
| 123 | + unit.current(), unit.shuntVoltage(), unit.voltage(), unit.power()); |
| 124 | + lcd.endWrite(); |
| 125 | + } |
| 126 | + |
| 127 | + if (M5.BtnA.wasClicked() || touch.wasClicked()) { |
| 128 | + static bool single{}; |
| 129 | + single = !single; |
| 130 | + if (single) { |
| 131 | + M5.Speaker.tone(1500, 20); |
| 132 | + Data d{}; |
| 133 | + unit.stopPeriodicMeasurement(); |
| 134 | + if (unit.measureSingleshot(d)) { |
| 135 | + M5.Log.printf("Single:A:%f SV:%f BV:%f W:%f\n", unit.current(), unit.shuntVoltage(), unit.voltage(), |
| 136 | + unit.power()); |
| 137 | + } else { |
| 138 | + M5_LOGE("Failed to measureSingleshot"); |
| 139 | + } |
| 140 | + } else { |
| 141 | + M5.Speaker.tone(2500, 20); |
| 142 | + M5.Log.printf("Start periodic\n"); |
| 143 | + unit.startPeriodicMeasurement(); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments