|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "LoadConfig.hpp" |
| 4 | +#include <Adafruit_INA260.h> |
| 5 | +#include <atomic> |
| 6 | +#include <etl/format_spec.h> |
| 7 | +#include <etl/string.h> |
| 8 | +#include <etl/to_string.h> |
| 9 | + |
| 10 | +namespace INA260 { |
| 11 | + constexpr uint_fast16_t m_TO_BASE = 1000; |
| 12 | + static constexpr int_fast8_t MIN_VOLTAGE_V = -32; |
| 13 | + static constexpr int_fast16_t MIN_VOLTAGE_mV = MIN_VOLTAGE_V * m_TO_BASE; |
| 14 | + static constexpr int_fast8_t MAX_VOLTAGE_V = 32; |
| 15 | + static constexpr int_fast16_t MAX_VOLTAGE_mV = MAX_VOLTAGE_V * m_TO_BASE; |
| 16 | + static constexpr int_fast8_t MIN_CURRENT_A = -15; |
| 17 | + static constexpr int_fast16_t MIN_CURRENT_mA = MIN_CURRENT_A * m_TO_BASE; |
| 18 | + static constexpr int_fast8_t MAX_CURRENT_A = 15; |
| 19 | + static constexpr int_fast16_t MAX_CURRENT_mA = MAX_CURRENT_A * m_TO_BASE; |
| 20 | + static constexpr int_fast16_t MIN_POWER_W = |
| 21 | + MIN_VOLTAGE_V * MIN_CURRENT_A * -1; |
| 22 | + static constexpr int_fast16_t MIN_POWER_mW = MIN_POWER_W * m_TO_BASE; |
| 23 | + static constexpr int_fast16_t MAX_POWER_W = MAX_VOLTAGE_V * MAX_CURRENT_A; |
| 24 | + static constexpr int_fast16_t MAX_POWER_mW = MAX_POWER_W * m_TO_BASE; |
| 25 | + |
| 26 | + static constexpr const char *TAG = "PS"; |
| 27 | + Adafruit_INA260 powerSensor; |
| 28 | + |
| 29 | + std::atomic<int_fast16_t> voltage_mV = 0; |
| 30 | + std::atomic<int_fast16_t> current_mA = 0; |
| 31 | + std::atomic<int_fast32_t> power_mW = 0; |
| 32 | + |
| 33 | + bool begin(uint8_t i2c_addr, INA260_AveragingCount avgCount, |
| 34 | + INA260_ConversionTime convTime) { |
| 35 | + if (!powerSensor.begin(i2c_addr)) { |
| 36 | + ESP_LOGE("TAG", "Failed to initialize INA260 at 0x%02X", i2c_addr); |
| 37 | + return false; |
| 38 | + } |
| 39 | + powerSensor.setAveragingCount(avgCount); |
| 40 | + powerSensor.setVoltageConversionTime(convTime); |
| 41 | + |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @brief Update the sensor readings and check if they are within the |
| 47 | + * defined bounds. |
| 48 | + * @return Whether the readings are valid (within bounds) |
| 49 | + */ |
| 50 | + bool updateReadings() { |
| 51 | + bool result = true; |
| 52 | + |
| 53 | + auto vTemp_mV = (uint_fast32_t)powerSensor.readBusVoltage(); |
| 54 | + if ((vTemp_mV > MAX_VOLTAGE_mV) || |
| 55 | + (vTemp_mV < PSENSOR::MIN_VOLTAGE_mV)) { |
| 56 | + ESP_LOGE("TAG", "V reading out of bounds: %u mV", vTemp_mV); |
| 57 | + result &= false; |
| 58 | + } // Else: Valid reading |
| 59 | + voltage_mV = vTemp_mV; |
| 60 | + |
| 61 | + auto iTemp = (uint_fast32_t)powerSensor.readCurrent(); |
| 62 | + if ((iTemp > MAX_CURRENT_mA) || (iTemp < MIN_CURRENT_mA)) { |
| 63 | + ESP_LOGE("TAG", "C reading out of bounds: %d mA", iTemp); |
| 64 | + result &= false; |
| 65 | + } // Else: Valid reading |
| 66 | + current_mA = iTemp; |
| 67 | + |
| 68 | + auto pTemp = (uint_fast32_t)powerSensor.readPower(); |
| 69 | + if ((pTemp > (MAX_VOLTAGE_mV * MAX_CURRENT_A)) || |
| 70 | + (pTemp < (PSENSOR::MIN_VOLTAGE_mV * MIN_CURRENT_A))) { |
| 71 | + ESP_LOGE("TAG", "P out of bounds: %d mW", pTemp); |
| 72 | + result &= false; |
| 73 | + } // Else: Valid reading |
| 74 | + power_mW = pTemp; |
| 75 | + |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + // TODO - improve this and null terminator may not be needed |
| 80 | + static constexpr uint_fast8_t LOG_STRING_SIZE = |
| 81 | + 3 + 6 + 5 + 6 + 5 + 5 + 5 + 1; |
| 82 | + /** |
| 83 | + * @brief Get at string that describes the current state of the PID instance |
| 84 | + * @returns the current state of the PID instance as a string |
| 85 | + */ |
| 86 | + etl::string<LOG_STRING_SIZE> getLogString() { |
| 87 | + etl::string<LOG_STRING_SIZE> logString(TAG); // 3 chars |
| 88 | + logString.append(": mV: "); // 6 chars |
| 89 | + |
| 90 | + etl::format_spec decFormatA; |
| 91 | + decFormatA.width(5).fill('0'); // [5 chars] |
| 92 | + /** |
| 93 | + * @details I don't think we need strong guarantees on logging data |
| 94 | + * @see |
| 95 | + * https://stackoverflow.com/questions/12346487/what-do-each-memory-order-mean |
| 96 | + * @see https://en.cppreference.com/cpp/atomic/memory_order |
| 97 | + */ |
| 98 | + etl::to_string(voltage_mV.load(std::memory_order::relaxed), logString, |
| 99 | + decFormatA, true); // 5 chars |
| 100 | + logString.append(", mA: "); // 6 chars |
| 101 | + etl::to_string(current_mA.load(std::memory_order::relaxed), logString, |
| 102 | + decFormatA, true); // 5 chars |
| 103 | + logString.append(", W: "); // 5 chars |
| 104 | + etl::to_string(power_mW.load(std::memory_order::relaxed) / m_TO_BASE, |
| 105 | + logString, decFormatA, true); // 5 chars |
| 106 | + |
| 107 | + return logString; |
| 108 | + } |
| 109 | +} // namespace INA260 |
0 commit comments