Skip to content

Commit

Permalink
Merge pull request #4 from KodinLanewave/devel
Browse files Browse the repository at this point in the history
Version 1.0.0 package descriptors
  • Loading branch information
KodinLanewave authored Jan 12, 2024
2 parents c52b6d7 + f6a6f57 commit 1ba2374
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 56 deletions.
12 changes: 6 additions & 6 deletions examples/get_started/get_started.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ void setup() {
void loop() {
Serial.printf(
"A1%3.0fma %1.1fV A2%3.0fma %1.1fV\r\n",
ina_0.getCurrent(INA3221_CH1) * 1000, ina_0.getVoltage(INA3221_CH1),
ina_0.getCurrent(INA3221_CH2) * 1000, ina_0.getVoltage(INA3221_CH2));
ina_0.getCurrent(INA3221_CH1), ina_0.getVoltage(INA3221_CH1),
ina_0.getCurrent(INA3221_CH2), ina_0.getVoltage(INA3221_CH2));
Serial.printf(
"B1%3.0fma %1.1fV B2%3.0fma %1.1fV\r\n",
ina_0.getCurrent(INA3221_CH3) * 1000, ina_0.getVoltage(INA3221_CH3),
ina_1.getCurrent(INA3221_CH1) * 1000, ina_1.getVoltage(INA3221_CH1));
ina_0.getCurrent(INA3221_CH3), ina_0.getVoltage(INA3221_CH3),
ina_1.getCurrent(INA3221_CH1), ina_1.getVoltage(INA3221_CH1));
Serial.printf(
"C1%3.0fma %1.1fV C2%3.0fma %1.1fV\r\n\n",
ina_1.getCurrent(INA3221_CH2) * 1000, ina_1.getVoltage(INA3221_CH2),
ina_1.getCurrent(INA3221_CH3) * 1000, ina_1.getVoltage(INA3221_CH3));
ina_1.getCurrent(INA3221_CH2), ina_1.getVoltage(INA3221_CH2),
ina_1.getCurrent(INA3221_CH3), ina_1.getVoltage(INA3221_CH3));

delay(1000);
}
10 changes: 5 additions & 5 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"description": "Library for INA3221 Chip",
"keywords": "INA3221 current and voltage sensor",
"authors": {
"name": "Tinyu",
"url": "https://github.com/Tinyu-Zhao"
"name": "KodinLanewave",
"url": "https://github.com/KodinLanewave"
},
"repository": {
"type": "git",
"url": "https://github.com/Tinyu-Zhao/INA3221.git"
"url": "https://github.com/KodinLanewave/INA3221.git"
},
"version": "0.0.1",
"version": "1.0.0",
"frameworks": "arduino",
"platforms": "espressif32"
}
}
10 changes: 5 additions & 5 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=INA3221
version=0.0.1
author=Tinyu
maintainer=Tinyu <Tinyu.Zhao@gmail.com>
version=1.0.0
author=KodinLanewave
maintainer=KodinLanewave <Kodin.Lanewave@gmail.com>
sentence=INA3221 Triple-Channel Sensor Driver.
paragraph=INA3221 Triple-Channel Sensor Driver.
category=Sensors
url=https://github.com/Tinyu-Zhao/INA3221
architectures=*
url=https://github.com/KodinLanewave/INA3221
architectures=*
52 changes: 26 additions & 26 deletions src/INA3221.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ void INA3221::begin(TwoWire *theWire) {
_i2c->begin();
}

void INA3221::setShuntRes(uint32_t res_ch1, uint32_t res_ch2,
uint32_t res_ch3) {
void INA3221::setShuntRes(float res_ch1, float res_ch2,
float res_ch3) {
_shuntRes[0] = res_ch1;
_shuntRes[1] = res_ch2;
_shuntRes[2] = res_ch3;
}

void INA3221::setFilterRes(uint32_t res_ch1, uint32_t res_ch2,
uint32_t res_ch3) {
void INA3221::setFilterRes(float res_ch1, float res_ch2,
float res_ch3) {
_filterRes[0] = res_ch1;
_filterRes[1] = res_ch2;
_filterRes[2] = res_ch3;
Expand Down Expand Up @@ -179,15 +179,15 @@ void INA3221::setPwrValidLowLimit(int16_t voltagemV) {
_write(INA3221_REG_PWR_VALID_LO_LIM, (uint16_t *)&voltagemV);
}

void INA3221::setShuntSumAlertLimit(int32_t voltageuV) {
void INA3221::setShuntSumAlertLimit(int16_t voltageuV) {
int16_t val = 0;
val = voltageuV / 20;
_write(INA3221_REG_SHUNTV_SUM_LIM, (uint16_t *)&val);
}

void INA3221::setCurrentSumAlertLimit(int32_t currentmA) {
void INA3221::setCurrentSumAlertLimit(int16_t currentmA) {
int16_t shuntuV = 0;
shuntuV = currentmA * (int32_t)_shuntRes[INA3221_CH1];
shuntuV = currentmA * (int16_t)_shuntRes[INA3221_CH1];
setShuntSumAlertLimit(shuntuV);
}

Expand Down Expand Up @@ -299,7 +299,7 @@ void INA3221::setChannelDisable(ina3221_ch_t channel) {
_write(INA3221_REG_CONF, (uint16_t *)&conf_reg);
}

void INA3221::setWarnAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV) {
void INA3221::setWarnAlertShuntLimit(ina3221_ch_t channel, int16_t voltageuV) {
ina3221_reg_t reg;
int16_t val = 0;

Expand All @@ -319,7 +319,7 @@ void INA3221::setWarnAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV) {
_write(reg, (uint16_t *)&val);
}

void INA3221::setCritAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV) {
void INA3221::setCritAlertShuntLimit(ina3221_ch_t channel, int16_t voltageuV) {
ina3221_reg_t reg;
int16_t val = 0;

Expand All @@ -340,16 +340,16 @@ void INA3221::setCritAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV) {
}

void INA3221::setWarnAlertCurrentLimit(ina3221_ch_t channel,
int32_t currentmA) {
int32_t shuntuV = 0;
shuntuV = currentmA * (int32_t)_shuntRes[channel];
int16_t currentmA) {
int16_t shuntuV = 0;
shuntuV = currentmA * (int16_t)_shuntRes[channel];
setWarnAlertShuntLimit(channel, shuntuV);
}

void INA3221::setCritAlertCurrentLimit(ina3221_ch_t channel,
int32_t currentmA) {
int32_t shuntuV = 0;
shuntuV = currentmA * (int32_t)_shuntRes[channel];
int16_t currentmA) {
int16_t shuntuV = 0;
shuntuV = currentmA * (int16_t)_shuntRes[channel];
setCritAlertShuntLimit(channel, shuntuV);
}

Expand Down Expand Up @@ -395,7 +395,7 @@ void INA3221::setCurrentSumDisable(ina3221_ch_t channel) {
_masken_reg = masken_reg;
}

int32_t INA3221::getShuntVoltage(ina3221_ch_t channel) {
int16_t INA3221::getShuntVoltage(ina3221_ch_t channel) {
int16_t res;
ina3221_reg_t reg;
uint16_t val_raw = 0;
Expand Down Expand Up @@ -447,13 +447,13 @@ bool INA3221::getCritAlertFlag(ina3221_ch_t channel) {
}
}

int32_t INA3221::estimateOffsetVoltage(ina3221_ch_t channel, uint32_t busV) {
int16_t INA3221::estimateOffsetVoltage(ina3221_ch_t channel, uint16_t busV) {
float bias_in = 10.0; // Input bias current at IN– in uA
float r_in = 0.670; // Input resistance at IN– in MOhm
uint32_t adc_step = 40; // smallest shunt ADC step in uV
uint16_t adc_step = 40; // smallest shunt ADC step in uV
float shunt_res = _shuntRes[channel] / 1000.0; // convert to Ohm
float filter_res = _filterRes[channel];
int32_t offset = 0.0;
int16_t offset = 0.0;
float reminder;

offset = (shunt_res + filter_res) * (busV / r_in + bias_in) -
Expand All @@ -471,25 +471,25 @@ int32_t INA3221::estimateOffsetVoltage(ina3221_ch_t channel, uint32_t busV) {
}

float INA3221::getCurrent(ina3221_ch_t channel) {
int32_t shunt_uV = 0;
float current_mA = 0;
int16_t shunt_uV = 0;
float current_mA = 0.0;

shunt_uV = getShuntVoltage(channel);
current_mA = shunt_uV / (int32_t)_shuntRes[channel];
current_mA = shunt_uV / _shuntRes[channel];
return current_mA;
}

float INA3221::getCurrentCompensated(ina3221_ch_t channel) {
int32_t shunt_uV = 0;
int32_t bus_V = 0;
int16_t shunt_uV = 0;
int16_t bus_V = 0;
float current_mA = 0.0;
int32_t offset_uV = 0;
int16_t offset_uV = 0;

shunt_uV = getShuntVoltage(channel);
bus_V = getVoltage(channel);
offset_uV = estimateOffsetVoltage(channel, bus_V);

current_mA = (shunt_uV - offset_uV) / (int32_t)_shuntRes[channel] / 1000.0;
current_mA = (shunt_uV - offset_uV) / _shuntRes[channel];

return current_mA;
}
Expand Down
28 changes: 14 additions & 14 deletions src/INA3221.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ class INA3221 {
ina3221_addr_t _i2c_addr;

// Shunt resistance in mOhm
uint32_t _shuntRes[INA3221_CH_NUM];
float _shuntRes[INA3221_CH_NUM];

// Series filter resistance in Ohm
uint32_t _filterRes[INA3221_CH_NUM];
float _filterRes[INA3221_CH_NUM];

// Value of Mask/Enable register.
masken_reg_t _masken_reg;
Expand All @@ -157,10 +157,10 @@ class INA3221 {
void begin(TwoWire *theWire = &Wire);

// Sets shunt resistor value in mOhm
void setShuntRes(uint32_t res_ch1, uint32_t res_ch2, uint32_t res_ch3);
void setShuntRes(float res_ch1, float res_ch2, float res_ch3);

// Sets filter resistors value in Ohm
void setFilterRes(uint32_t res_ch1, uint32_t res_ch2, uint32_t res_ch3);
void setFilterRes(float res_ch1, float res_ch2, float res_ch3);

// Sets I2C address of INA3221
void setAddr(ina3221_addr_t addr) {
Expand Down Expand Up @@ -216,12 +216,12 @@ class INA3221 {
// Sets the value that is compared to the Shunt-Voltage Sum register value
// following each completed cycle of all selected channels to detect
// for system overcurrent events.
void setShuntSumAlertLimit(int32_t voltagemV);
void setShuntSumAlertLimit(int16_t voltagemV);

// Sets the current value that is compared to the sum all currents.
// This function is a helper for setShuntSumAlertLim(). It onverts current
// value to shunt voltage value.
void setCurrentSumAlertLimit(int32_t currentmA);
void setCurrentSumAlertLimit(int16_t currentmA);

// Enables warning alert latch.
void setWarnAlertLatchEnable();
Expand Down Expand Up @@ -269,16 +269,16 @@ class INA3221 {
void setChannelDisable(ina3221_ch_t channel);

// Sets warning alert shunt voltage limit
void setWarnAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV);
void setWarnAlertShuntLimit(ina3221_ch_t channel, int16_t voltageuV);

// Sets critical alert shunt voltage limit
void setCritAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV);
void setCritAlertShuntLimit(ina3221_ch_t channel, int16_t voltageuV);

// Sets warning alert current limit
void setWarnAlertCurrentLimit(ina3221_ch_t channel, int32_t currentmA);
void setWarnAlertCurrentLimit(ina3221_ch_t channel, int16_t currentmA);

// Sets critical alert current limit
void setCritAlertCurrentLimit(ina3221_ch_t channel, int32_t currentmA);
void setCritAlertCurrentLimit(ina3221_ch_t channel, int16_t currentmA);

// Includes channel to fill Shunt-Voltage Sum register.
void setCurrentSumEnable(ina3221_ch_t channel);
Expand All @@ -287,7 +287,7 @@ class INA3221 {
void setCurrentSumDisable(ina3221_ch_t channel);

// Gets shunt voltage in uV.
int32_t getShuntVoltage(ina3221_ch_t channel);
int16_t getShuntVoltage(ina3221_ch_t channel);

// Gets warning alert flag.
bool getWarnAlertFlag(ina3221_ch_t channel);
Expand All @@ -296,9 +296,9 @@ class INA3221 {
bool getCritAlertFlag(ina3221_ch_t channel);

// Estimates offset voltage added by the series filter resitors
int32_t estimateOffsetVoltage(ina3221_ch_t channel, uint32_t busVoltage);
int16_t estimateOffsetVoltage(ina3221_ch_t channel, uint16_t busVoltage);

// Gets current in A.
// Gets current in mA.
float getCurrent(ina3221_ch_t channel);

// Gets current compensated with calculated offset voltage.
Expand All @@ -308,4 +308,4 @@ class INA3221 {
float getVoltage(ina3221_ch_t channel);
};

#endif
#endif

0 comments on commit 1ba2374

Please sign in to comment.