A compilation of some hardware sensors and their shared programming interfaces.
INA219 Class Reference
This class allows for easy control over a INA219 current/power sensing IC. More...
#include <INA219.h>
Inherits I2CSensor, and PeriodicSensor.
Public Member Functions | |
INA219 (PinName sda, PinName scl, int i2c_addr) | |
~INA219 () | |
Destroys this object. | |
void | selfInit () |
Initializes the device to some nice state. | |
void | reset () |
Performs a software reset of the device. | |
void | setBusVoltageRange32V (bool enable) |
Sets the bus voltage range to either 16V or 32V. | |
bool | isBusVoltageRange32V () |
void | setShuntAmpGain (unsigned int gain) |
Sets the PGA amplifier gain for shunt voltage measurement. | |
unsigned int | getShuntAmpGain () |
Gets the PGA amplifier gain for shunt voltage measurement. | |
void | setADCResolutionAndAveraging (bool shunt, bool resolution, unsigned int value) |
Sets the resolution and sample averaging of the bus or shunt ADC. | |
unsigned int | getADCResolutionAndAveraging (bool shunt, bool &resolution) |
Gets the resolution or number of samples in an average of the bus or shunt ADC. | |
void | setMode (bool shuntVoltage, bool busVoltage, bool continuous) |
Sets the operating mode of the device. | |
void | getMode (bool &shuntVoltage, bool &busVoltage, bool &continuous) |
Gets the operating mode of the device. | |
int16_t | getShuntVoltage (bool sampleNow) |
Gets the shunt voltage across the current sensing resistor. | |
float | getShuntVoltageFloat (bool sampleNow) |
Gets the shunt voltage across the current sensing resistor. | |
int16_t | getBusVoltage (bool sampleNow) |
Gets the bus voltage at the negative terminal of the current sensing resistor (V-). | |
float | getBusVoltageFloat (bool sampleNow) |
Gets the bus voltage across the current sensing resistor. | |
int16_t | getPower (bool sampleNow) |
Gets the power consumed by the load. | |
float | getPowerFloat (bool sampleNow) |
Gets the power consumed by the load. | |
int16_t | getCurrent (bool sampleNow) |
Gets the current sunk by the load. | |
float | getCurrentFloat (bool sampleNow) |
Gets the current delivered to the load. | |
uint16_t | getCalibration () |
Gets the calibration configuration setting for current range. | |
PinName | getSDAPin () |
PinName | getSCLPin () |
uint8_t | getDeviceI2CAddress () |
uint8_t | getRegister (const uint8_t reg_addr) |
Read an 8-bit register. | |
uint16_t | getRegister16b (const uint8_t reg_addr) |
Read a 16-bit register. | |
void | setRegister (const uint8_t reg_addr, const uint8_t data) |
Set an 8-bit register. | |
void | setRegister16b (const uint8_t reg_addr, const uint16_t data) |
Set a 16-bit register. | |
bool | isDataReady () |
void | enableBackgroundSampling (bool enable, float sample_period) |
Control the background sampling of the device via interrupts. | |
bool | isBackgroundSamplingEnabled () |
float | getSamplePeriod () |
float | getMinSamplePeriod () |
Protected Member Functions | |
int | __readReg (const uint8_t reg_addr, uint8_t *data, int len) |
int | __writeReg (const uint8_t *data, int total_len) |
Detailed Description
This class allows for easy control over a INA219 current/power sensing IC.
Definition at line 17 of file INA219.h.
Constructor & Destructor Documentation
INA219 | ( | PinName | sda, |
PinName | scl, | ||
int | i2c_addr | ||
) |
- Parameters:
-
sda the pin identifier for SDA I2C signal scl the pin identifier for SCL I2C signal i2c_addr the 8-bit I2C address for this device. Note that LSB is a don't care.
Definition at line 11 of file INA219.cpp.
~INA219 | ( | ) |
Destroys this object.
Definition at line 35 of file INA219.cpp.
Member Function Documentation
int __readReg | ( | const uint8_t | reg_addr, |
uint8_t * | data, | ||
int | len | ||
) | [protected, inherited] |
- Parameters:
-
reg_addr 8-bit register address inside the device data 8-bit data that will be read from the register. This pointer MUST be valid. This array MUST be at least as long as len. len total number of bytes to read. len must be >= 1.
- Returns:
- 0 on success, otherwise error code from I2C
Definition at line 57 of file I2CSensor.cpp.
int __writeReg | ( | const uint8_t * | data, |
int | total_len | ||
) | [protected, inherited] |
- Parameters:
-
data 8-bit data that will be written to the register. This pointer MUST be valid. This array MUST be at least as long as total_len. data[0] should be set to the 8-bit register address. The data payload should start at index 1. total_len total length of the data array, which is the length of the payload + 1 for the register address. total_len must be >= 2.
- Returns:
- 0 on success, otherwise error code from I2C
Definition at line 72 of file I2CSensor.cpp.
void enableBackgroundSampling | ( | bool | enable, |
float | sample_period | ||
) | [inherited] |
Control the background sampling of the device via interrupts.
If disabled, the device can still be sampled using the getter methods directly.
- Parameters:
-
enable if true, enables background sampling with the given sample period. sample_period sampling period in seconds. Must be at least 0.005 sec unless enable is false, in which case it is don't care.
Definition at line 36 of file PeriodicSensor.cpp.
unsigned int getADCResolutionAndAveraging | ( | bool | shunt, |
bool & | resolution | ||
) |
Gets the resolution or number of samples in an average of the bus or shunt ADC.
- Parameters:
-
shunt if true, gets for shunt ADC. if false, gets for bus ADC. resolution sets this to true if the return value represents resolution in bits. else, sets it to false if return value represents sample averaging in # samples.
Definition at line 195 of file INA219.cpp.
int16_t getBusVoltage | ( | bool | sampleNow ) |
Gets the bus voltage at the negative terminal of the current sensing resistor (V-).
- Parameters:
-
sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
- Returns:
- 16-bit integer representing the bus voltage.
Definition at line 267 of file INA219.cpp.
float getBusVoltageFloat | ( | bool | sampleNow ) |
Gets the bus voltage across the current sensing resistor.
- Parameters:
-
sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
- Returns:
- the bus voltage in Volts
Definition at line 281 of file INA219.cpp.
uint16_t getCalibration | ( | ) |
Gets the calibration configuration setting for current range.
- Returns:
- 16-bit unsigned integer representing calibration register value
Definition at line 319 of file INA219.cpp.
int16_t getCurrent | ( | bool | sampleNow ) |
Gets the current sunk by the load.
- Parameters:
-
sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
- Returns:
- 16-bit integer representing current sunk by the load.
Definition at line 305 of file INA219.cpp.
float getCurrentFloat | ( | bool | sampleNow ) |
Gets the current delivered to the load.
- Parameters:
-
sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
- Returns:
- the load current in Amperes
Definition at line 315 of file INA219.cpp.
uint8_t getDeviceI2CAddress | ( | ) | [inherited] |
- Returns:
- Device I2C address (LSB always 0 in this case, it is don't care)
Definition at line 28 of file I2CSensor.cpp.
float getMinSamplePeriod | ( | ) | [inherited] |
- Returns:
- the minimum sample period in seconds
Definition at line 57 of file PeriodicSensor.cpp.
void getMode | ( | bool & | shuntVoltage, |
bool & | busVoltage, | ||
bool & | continuous | ||
) |
Gets the operating mode of the device.
If all parameters are false, the device is powered down. If shuntVoltage and busVoltage are false but continuous is true, the device is on but the ADC is powered down.
- Parameters:
-
shuntVoltage sets true if shunt voltage is being measured. busVoltage sets true if bus voltage is being measured. continuous sets true if continuous readings are being done.
Definition at line 246 of file INA219.cpp.
int16_t getPower | ( | bool | sampleNow ) |
Gets the power consumed by the load.
Note that calling this method with sampleNow == true will also implicitly sample current as well, which can be retrieved with a call to getCurrent(false) after calling getPower(true).
- Parameters:
-
sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
- Returns:
- 16-bit integer representing power consumption of the load.
Definition at line 285 of file INA219.cpp.
float getPowerFloat | ( | bool | sampleNow ) |
Gets the power consumed by the load.
Note that calling this method with sampleNow == true will also implicitly sample current as well, which can be retrieved with a call to getCurrentFloat(false) after calling getPowerFloat(true).
- Parameters:
-
sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
- Returns:
- the load power in Watts
Definition at line 301 of file INA219.cpp.
uint8_t getRegister | ( | const uint8_t | reg_addr ) | [inherited] |
Read an 8-bit register.
- Parameters:
-
reg_addr the register in the device
- Returns:
- The raw value from the register specified by reg_addr.
Definition at line 30 of file I2CSensor.cpp.
uint16_t getRegister16b | ( | const uint8_t | reg_addr ) | [inherited] |
Read a 16-bit register.
- Parameters:
-
reg_addr the register in the device
- Returns:
- The raw value from the register specified by reg_addr.
Definition at line 36 of file I2CSensor.cpp.
float getSamplePeriod | ( | ) | [inherited] |
- Returns:
- the sample period in seconds, if background sampling is enabled. Else, non-positive return.
Definition at line 53 of file PeriodicSensor.cpp.
PinName getSCLPin | ( | ) | [inherited] |
- Returns:
- I2C SCL pin ID
Definition at line 26 of file I2CSensor.cpp.
PinName getSDAPin | ( | ) | [inherited] |
- Returns:
- I2C SDA pin ID
Definition at line 24 of file I2CSensor.cpp.
unsigned int getShuntAmpGain | ( | ) |
Gets the PGA amplifier gain for shunt voltage measurement.
- Returns:
- the gain level
Definition at line 111 of file INA219.cpp.
int16_t getShuntVoltage | ( | bool | sampleNow ) |
Gets the shunt voltage across the current sensing resistor.
- Parameters:
-
sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
- Returns:
- 16-bit integer representing the shunt voltage.
Definition at line 253 of file INA219.cpp.
float getShuntVoltageFloat | ( | bool | sampleNow ) |
Gets the shunt voltage across the current sensing resistor.
- Parameters:
-
sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
- Returns:
- the shunt voltage in Volts
Definition at line 263 of file INA219.cpp.
bool isBackgroundSamplingEnabled | ( | ) | [inherited] |
- Returns:
- true if background sampling is enabled.
Definition at line 49 of file PeriodicSensor.cpp.
bool isBusVoltageRange32V | ( | ) |
- Returns:
- true if the bus voltage range is set to 32V. if false, bus voltage range is 16V.
Definition at line 81 of file INA219.cpp.
bool isDataReady | ( | ) | [inherited] |
- Returns:
- true if there is new data ready to be retrieved.
Definition at line 32 of file PeriodicSensor.cpp.
void reset | ( | ) |
Performs a software reset of the device.
Definition at line 43 of file INA219.cpp.
void selfInit | ( | ) |
Initializes the device to some nice state.
Definition at line 37 of file INA219.cpp.
void setADCResolutionAndAveraging | ( | bool | shunt, |
bool | resolution, | ||
unsigned int | value | ||
) |
Sets the resolution and sample averaging of the bus or shunt ADC.
If resolution is set, sample averaging is disabled. If sample averaging is set, resolution setting is disabled.
- Parameters:
-
shunt if true, sets for shunt ADC. if false, sets for bus ADC. resolution if true, sets ADC resolution using the third parameter. if false, sets the ADC sample averaging using the third parameter. value if resolution is true, then this is the resolution in bits to use. Allowed values: 9, 10, 11, or 12 bits. If resolution is false, then this is the number of samples to average in each data sample. Allowed values: 2, 4, 8, 16, 32, 64, or 128 samples. Any other values will cause this method to have no effect. Default is 12-bit resolution mode.
Definition at line 115 of file INA219.cpp.
void setBusVoltageRange32V | ( | bool | enable ) |
Sets the bus voltage range to either 16V or 32V.
- Parameters:
-
enable if true, sets the bus voltage range to 32V. Else, sets it to 16V.
Definition at line 68 of file INA219.cpp.
void setMode | ( | bool | shuntVoltage, |
bool | busVoltage, | ||
bool | continuous | ||
) |
Sets the operating mode of the device.
If all parameters are false, the device powers down. If shuntVoltage and busVoltage are false but continuous is true, the device remains on but the ADC is powered down.
- Parameters:
-
shuntVoltage if true, samples the shunt voltage. busVoltage if true, samples the bus voltage. continuous if true, samples continuously, otherwise only on triggered reads.
Definition at line 220 of file INA219.cpp.
void setRegister | ( | const uint8_t | reg_addr, |
const uint8_t | data | ||
) | [inherited] |
Set an 8-bit register.
- Parameters:
-
reg_addr the register in the device data the byte to write to the register
Definition at line 43 of file I2CSensor.cpp.
void setRegister16b | ( | const uint8_t | reg_addr, |
const uint16_t | data | ||
) | [inherited] |
Set a 16-bit register.
- Parameters:
-
reg_addr the register in the device data the byte to write to the register
Definition at line 48 of file I2CSensor.cpp.
void setShuntAmpGain | ( | unsigned int | gain ) |
Sets the PGA amplifier gain for shunt voltage measurement.
- Parameters:
-
gain the gain level to set. Allowed values: 1, 2, 4, 8. Default is 8. Any other values will have no effect.
Definition at line 85 of file INA219.cpp.
Generated on Wed Jul 13 2022 19:24:41 by 1.7.2