|
| 1 | +/*! |
| 2 | + * @file WipperSnapper_I2C_Driver_INA228.cpp |
| 3 | + * |
| 4 | + * Device driver implementation for the INA228 High Precision DC Current and |
| 5 | + * Voltage Monitor (Avoids import conflict with INA260 typedef enum _mode etc) |
| 6 | + * |
| 7 | + * Adafruit invests time and resources providing this open source code, |
| 8 | + * please support Adafruit and open-source hardware by purchasing |
| 9 | + * products from Adafruit! |
| 10 | + * |
| 11 | + * Copyright (c) Tyeth Gundry 2025 for Adafruit Industries. |
| 12 | + * |
| 13 | + * MIT license, all text here must be included in any redistribution. |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +#include "WipperSnapper_I2C_Driver_INA228.h" |
| 18 | +#include "Wippersnapper.h" |
| 19 | +#include <Adafruit_INA228.h> |
| 20 | + |
| 21 | +/*******************************************************************************/ |
| 22 | +/*! |
| 23 | + @brief Constructor for a INA228 sensor. |
| 24 | + @param i2c |
| 25 | + The I2C interface. |
| 26 | + @param sensorAddress |
| 27 | + The 7-bit I2C address of the sensor. |
| 28 | +*/ |
| 29 | +/*******************************************************************************/ |
| 30 | +WipperSnapper_I2C_Driver_INA228::WipperSnapper_I2C_Driver_INA228( |
| 31 | + TwoWire *i2c, uint16_t sensorAddress) |
| 32 | + : WipperSnapper_I2C_Driver(i2c, sensorAddress), _ina228(nullptr) { |
| 33 | + _i2c = i2c; |
| 34 | + _sensorAddress = sensorAddress; |
| 35 | +} |
| 36 | + |
| 37 | +/*******************************************************************************/ |
| 38 | +/*! |
| 39 | + @brief Destructor for an INA228 sensor. |
| 40 | +*/ |
| 41 | +/*******************************************************************************/ |
| 42 | +WipperSnapper_I2C_Driver_INA228::~WipperSnapper_I2C_Driver_INA228() { |
| 43 | + delete _ina228; |
| 44 | +} |
| 45 | + |
| 46 | +/*******************************************************************************/ |
| 47 | +/*! |
| 48 | + @brief Initializes the INA228 sensor and begins I2C. |
| 49 | + @returns True if initialized successfully, False otherwise. |
| 50 | +*/ |
| 51 | +/*******************************************************************************/ |
| 52 | +bool WipperSnapper_I2C_Driver_INA228::begin() { |
| 53 | + _ina228 = new Adafruit_INA228(); |
| 54 | + if (!_ina228->begin(_sensorAddress, _i2c)) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + // Default shunt: 0.015 ohm, 10A max current |
| 59 | + _ina228->setShunt(0.015, 10.0); |
| 60 | + |
| 61 | + _ina228->setAveragingCount(INA228_COUNT_16); |
| 62 | + _ina228->setVoltageConversionTime(INA228_TIME_150_us); |
| 63 | + _ina228->setCurrentConversionTime(INA228_TIME_280_us); |
| 64 | + |
| 65 | + return true; |
| 66 | +} |
| 67 | + |
| 68 | +/*******************************************************************************/ |
| 69 | +/*! |
| 70 | + @brief Reads a voltage sensor and converts the |
| 71 | + reading into the expected SI unit. |
| 72 | + @param voltageEvent |
| 73 | + voltage sensor reading, in volts. |
| 74 | + @returns True if the sensor event was obtained successfully, False |
| 75 | + otherwise. |
| 76 | +*/ |
| 77 | +/*******************************************************************************/ |
| 78 | +bool WipperSnapper_I2C_Driver_INA228::getEventVoltage( |
| 79 | + sensors_event_t *voltageEvent) { |
| 80 | + voltageEvent->voltage = _ina228->getBusVoltage_V(); |
| 81 | + return true; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * @brief Get the current sensor event. |
| 86 | + * |
| 87 | + * @param currentEvent Pointer to the current sensor event. |
| 88 | + * |
| 89 | + * @returns True if the sensor event was obtained successfully, False |
| 90 | + * otherwise. |
| 91 | + */ |
| 92 | +bool WipperSnapper_I2C_Driver_INA228::getEventCurrent( |
| 93 | + sensors_event_t *currentEvent) { |
| 94 | + currentEvent->current = _ina228->getCurrent_mA(); |
| 95 | + return true; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * @brief Get the raw (power) sensor event. |
| 100 | + * |
| 101 | + * @param powerEvent Pointer to the power sensor event. |
| 102 | + * |
| 103 | + * @returns True if the sensor event was obtained successfully, False |
| 104 | + * otherwise. |
| 105 | + */ |
| 106 | +bool WipperSnapper_I2C_Driver_INA228::getEventRaw(sensors_event_t *powerEvent) { |
| 107 | + powerEvent->data[0] = _ina228->getPower_mW(); |
| 108 | + return true; |
| 109 | +} |
0 commit comments