Skip to content

Commit 1c6813f

Browse files
committed
Resolve dependency chain - Split INA260 + 2XX to cpp files, forward declare INA classes to hide enums from each other
1 parent b391585 commit 1c6813f

8 files changed

+329
-108
lines changed

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686

8787
// forward decl.
8888
class Wippersnapper;
89+
class WipperSnapper_I2C_Driver_INA260;
90+
class WipperSnapper_I2C_Driver_INA237;
91+
class WipperSnapper_I2C_Driver_INA238;
92+
8993

9094
/**************************************************************************/
9195
/*!

src/components/i2c/drivers/WipperSnapper_I2C_Driver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include <Adafruit_Sensor.h>
2020
#include <Arduino.h>
21+
#include <Wire.h>
22+
#include "../../../wippersnapper/i2c/v1/i2c.pb.h"
2123

2224
#define PERIOD_24HRS_AGO_MILLIS (millis() - (24 * 60 * 60 * 1000))
2325
///< Used for last sensor read time, initially set 24hrs ago (max period)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_INA237.cpp
3+
*
4+
* Device driver implementation for the INA237 DC Current and Voltage Monitor
5+
* (Avoids import conflict with INA260 typedef enum _mode etc)
6+
*
7+
*/
8+
9+
#include "WipperSnapper_I2C_Driver_INA237.h"
10+
#include <Adafruit_INA237.h>
11+
#include "../../../Wippersnapper.h"
12+
13+
/*******************************************************************************/
14+
/*!
15+
@brief Constructor for a INA237 sensor.
16+
@param i2c
17+
The I2C interface.
18+
@param sensorAddress
19+
The 7-bit I2C address of the sensor.
20+
*/
21+
/*******************************************************************************/
22+
WipperSnapper_I2C_Driver_INA237::WipperSnapper_I2C_Driver_INA237(
23+
TwoWire *i2c, uint16_t sensorAddress)
24+
: WipperSnapper_I2C_Driver(i2c, sensorAddress), _ina237(nullptr) {
25+
_i2c = i2c;
26+
_sensorAddress = sensorAddress;
27+
}
28+
29+
/*******************************************************************************/
30+
/*!
31+
@brief Destructor for an INA237 sensor.
32+
*/
33+
/*******************************************************************************/
34+
WipperSnapper_I2C_Driver_INA237::~WipperSnapper_I2C_Driver_INA237() {
35+
delete _ina237;
36+
}
37+
38+
/*******************************************************************************/
39+
/*!
40+
@brief Initializes the INA237 sensor and begins I2C.
41+
@returns True if initialized successfully, False otherwise.
42+
*/
43+
/*******************************************************************************/
44+
bool WipperSnapper_I2C_Driver_INA237::begin() {
45+
_ina237 = new Adafruit_INA237();
46+
if (!_ina237->begin(_sensorAddress, _i2c)) {
47+
WS_DEBUG_PRINTLN("INA237 failed to initialise!");
48+
return false;
49+
}
50+
51+
// Configuration based on INA237 example sketch
52+
// Set default shunt resistance and maximum current
53+
// Default 0.015 ohm shunt, 10A max current
54+
_ina237->setShunt(0.015, 10.0);
55+
56+
// Set averaging for better accuracy (16 samples)
57+
_ina237->setAveragingCount(INA2XX_COUNT_16);
58+
59+
// Set conversion times as per example
60+
_ina237->setVoltageConversionTime(INA2XX_TIME_150_us);
61+
_ina237->setCurrentConversionTime(INA2XX_TIME_280_us);
62+
63+
return true;
64+
}
65+
66+
/*******************************************************************************/
67+
/*!
68+
@brief Reads a voltage sensor and converts the
69+
reading into the expected SI unit.
70+
@param voltageEvent
71+
voltage sensor reading, in volts.
72+
@returns True if the sensor event was obtained successfully, False
73+
otherwise.
74+
*/
75+
/*******************************************************************************/
76+
bool WipperSnapper_I2C_Driver_INA237::getEventVoltage(
77+
sensors_event_t *voltageEvent) {
78+
voltageEvent->voltage = _ina237->getBusVoltage_V();
79+
return true;
80+
}
81+
82+
/**
83+
* @brief Get the current sensor event.
84+
*
85+
* @param currentEvent Pointer to the current sensor event.
86+
*
87+
* @returns True if the sensor event was obtained successfully, False
88+
* otherwise.
89+
*/
90+
bool WipperSnapper_I2C_Driver_INA237::getEventCurrent(
91+
sensors_event_t *currentEvent) {
92+
currentEvent->current = _ina237->getCurrent_mA();
93+
return true;
94+
}
95+
96+
/**
97+
* @brief Get the raw (power) sensor event.
98+
*
99+
* @param powerEvent Pointer to the power sensor event.
100+
*
101+
* @returns True if the sensor event was obtained successfully, False
102+
* otherwise.
103+
*/
104+
bool WipperSnapper_I2C_Driver_INA237::getEventRaw(sensors_event_t *powerEvent) {
105+
powerEvent->data[0] = _ina237->getPower_mW();
106+
return true;
107+
}

src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.h

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
#define WipperSnapper_I2C_Driver_INA237_H
1919

2020
#include "WipperSnapper_I2C_Driver.h"
21-
#include <Adafruit_INA237.h>
21+
#include "Wippersnapper.h"
22+
23+
// Forward declaration
24+
class Adafruit_INA237;
2225

2326
/**************************************************************************/
2427
/*!
@@ -36,46 +39,22 @@ class WipperSnapper_I2C_Driver_INA237 : public WipperSnapper_I2C_Driver {
3639
The 7-bit I2C address of the sensor.
3740
*/
3841
/*******************************************************************************/
39-
WipperSnapper_I2C_Driver_INA237(TwoWire *i2c, uint16_t sensorAddress)
40-
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
41-
_i2c = i2c;
42-
_sensorAddress = sensorAddress;
43-
}
42+
WipperSnapper_I2C_Driver_INA237(TwoWire *i2c, uint16_t sensorAddress);
4443

4544
/*******************************************************************************/
4645
/*!
4746
@brief Destructor for an INA237 sensor.
4847
*/
4948
/*******************************************************************************/
50-
~WipperSnapper_I2C_Driver_INA237() { delete _ina237; }
49+
~WipperSnapper_I2C_Driver_INA237();
5150

5251
/*******************************************************************************/
5352
/*!
5453
@brief Initializes the INA237 sensor and begins I2C.
5554
@returns True if initialized successfully, False otherwise.
5655
*/
5756
/*******************************************************************************/
58-
bool begin() {
59-
_ina237 = new Adafruit_INA237();
60-
if (!_ina237->begin(_sensorAddress, _i2c)) {
61-
WS_DEBUG_PRINTLN("INA237 failed to initialise!");
62-
return false;
63-
}
64-
65-
// Configuration based on INA237 example sketch
66-
// Set default shunt resistance and maximum current
67-
// Default 0.015 ohm shunt, 10A max current
68-
_ina237->setShunt(0.015, 10.0);
69-
70-
// Set averaging for better accuracy (16 samples)
71-
_ina237->setAveragingCount(INA2XX_COUNT_16);
72-
73-
// Set conversion times as per example
74-
_ina237->setVoltageConversionTime(INA2XX_TIME_150_us);
75-
_ina237->setCurrentConversionTime(INA2XX_TIME_280_us);
76-
77-
return true;
78-
}
57+
bool begin();
7958

8059
/*******************************************************************************/
8160
/*!
@@ -87,10 +66,7 @@ class WipperSnapper_I2C_Driver_INA237 : public WipperSnapper_I2C_Driver {
8766
otherwise.
8867
*/
8968
/*******************************************************************************/
90-
bool getEventVoltage(sensors_event_t *voltageEvent) {
91-
voltageEvent->voltage = _ina237->getBusVoltage_V();
92-
return true;
93-
}
69+
bool getEventVoltage(sensors_event_t *voltageEvent);
9470

9571
/**
9672
* @brief Get the current sensor event.
@@ -100,10 +76,7 @@ class WipperSnapper_I2C_Driver_INA237 : public WipperSnapper_I2C_Driver {
10076
* @returns True if the sensor event was obtained successfully, False
10177
* otherwise.
10278
*/
103-
bool getEventCurrent(sensors_event_t *currentEvent) {
104-
currentEvent->current = _ina237->getCurrent_mA();
105-
return true;
106-
}
79+
bool getEventCurrent(sensors_event_t *currentEvent);
10780

10881
/**
10982
* @brief Get the raw (power) sensor event.
@@ -113,13 +86,10 @@ class WipperSnapper_I2C_Driver_INA237 : public WipperSnapper_I2C_Driver {
11386
* @returns True if the sensor event was obtained successfully, False
11487
* otherwise.
11588
*/
116-
bool getEventRaw(sensors_event_t *powerEvent) {
117-
powerEvent->data[0] = _ina237->getPower_mW();
118-
return true;
119-
}
89+
bool getEventRaw(sensors_event_t *powerEvent);
12090

12191
protected:
122-
Adafruit_INA237 *_ina237 = nullptr; ///< Pointer to INA237 sensor object
92+
Adafruit_INA237 *_ina237; ///< Pointer to INA237 sensor object
12393
};
12494

12595
#endif // WipperSnapper_I2C_Driver_INA237
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_INA238.cpp
3+
*
4+
* Device driver implementation for the INA238 High-precision DC Current and
5+
* Voltage Monitor (Avoids import conflict with INA260 typedef enum _mode etc)
6+
*
7+
*/
8+
9+
#include "WipperSnapper_I2C_Driver_INA238.h"
10+
#include "../../../Wippersnapper.h"
11+
#include <Adafruit_INA238.h>
12+
13+
/*******************************************************************************/
14+
/*!
15+
@brief Constructor for a INA238 sensor.
16+
@param i2c
17+
The I2C interface.
18+
@param sensorAddress
19+
The 7-bit I2C address of the sensor.
20+
*/
21+
/*******************************************************************************/
22+
WipperSnapper_I2C_Driver_INA238::WipperSnapper_I2C_Driver_INA238(
23+
TwoWire *i2c, uint16_t sensorAddress)
24+
: WipperSnapper_I2C_Driver(i2c, sensorAddress), _ina238(nullptr) {
25+
_i2c = i2c;
26+
_sensorAddress = sensorAddress;
27+
}
28+
29+
/*******************************************************************************/
30+
/*!
31+
@brief Destructor for an INA238 sensor.
32+
*/
33+
/*******************************************************************************/
34+
WipperSnapper_I2C_Driver_INA238::~WipperSnapper_I2C_Driver_INA238() {
35+
delete _ina238;
36+
}
37+
38+
/*******************************************************************************/
39+
/*!
40+
@brief Initializes the INA238 sensor and begins I2C.
41+
@returns True if initialized successfully, False otherwise.
42+
*/
43+
/*******************************************************************************/
44+
bool WipperSnapper_I2C_Driver_INA238::begin() {
45+
_ina238 = new Adafruit_INA238();
46+
if (!_ina238->begin(_sensorAddress, _i2c)) {
47+
WS_DEBUG_PRINTLN("INA238 failed to initialise!");
48+
return false;
49+
}
50+
51+
// Configuration based on INA238 example sketch
52+
// Set default shunt resistance and maximum current
53+
// Default 0.015 ohm shunt, 10A max current
54+
_ina238->setShunt(0.015, 10.0);
55+
56+
// Set averaging for better accuracy (16 samples)
57+
_ina238->setAveragingCount(INA2XX_COUNT_16);
58+
59+
// Set conversion times as per example
60+
_ina238->setVoltageConversionTime(INA2XX_TIME_150_us);
61+
_ina238->setCurrentConversionTime(INA2XX_TIME_280_us);
62+
63+
return true;
64+
}
65+
66+
/*******************************************************************************/
67+
/*!
68+
@brief Reads a voltage sensor and converts the
69+
reading into the expected SI unit.
70+
@param voltageEvent
71+
voltage sensor reading, in volts.
72+
@returns True if the sensor event was obtained successfully, False
73+
otherwise.
74+
*/
75+
/*******************************************************************************/
76+
bool WipperSnapper_I2C_Driver_INA238::getEventVoltage(
77+
sensors_event_t *voltageEvent) {
78+
voltageEvent->voltage = _ina238->getBusVoltage_V();
79+
return true;
80+
}
81+
82+
/**
83+
* @brief Get the current sensor event.
84+
*
85+
* @param currentEvent Pointer to the current sensor event.
86+
*
87+
* @returns True if the sensor event was obtained successfully, False
88+
* otherwise.
89+
*/
90+
bool WipperSnapper_I2C_Driver_INA238::getEventCurrent(
91+
sensors_event_t *currentEvent) {
92+
currentEvent->current = _ina238->getCurrent_mA();
93+
return true;
94+
}
95+
96+
/**
97+
* @brief Get the Raw (power) sensor event.
98+
*
99+
* @param powerEvent Pointer to the power sensor event.
100+
*
101+
* @returns True if the sensor event was obtained successfully, False
102+
* otherwise.
103+
*/
104+
bool WipperSnapper_I2C_Driver_INA238::getEventRaw(sensors_event_t *powerEvent) {
105+
powerEvent->data[0] = _ina238->getPower_mW();
106+
return true;
107+
}

0 commit comments

Comments
 (0)