gui and ina sensors

This commit is contained in:
2020-07-07 23:39:13 +02:00
parent cd75b7bd0c
commit a2c3b40435
3 changed files with 31 additions and 15 deletions

View File

@@ -25,9 +25,16 @@ private:
public:
struct Channel {
int16_t vShunt;
int16_t vBus;
int16_t vShunt; // in uV * 10(!!!)
int16_t vBus; // in mV
int getMilliAmps(int shunt_milliOhm) const {
return (vShunt * 10) / shunt_milliOhm;
}
};
struct Voltages {
Channel ch1;
Channel ch2;
@@ -54,19 +61,19 @@ public:
Voltages res;
i2c.readReg(ADDR, REG_CH1_VS, sizeof(buf), buf);
res.ch1.vShunt = getVoltage(buf);
res.ch1.vShunt = getShuntVoltage(buf);
i2c.readReg(ADDR, REG_CH1_VB, sizeof(buf), buf);
res.ch1.vBus = getVoltage(buf);
res.ch1.vBus = getBusVoltage(buf);
i2c.readReg(ADDR, REG_CH2_VS, sizeof(buf), buf);
res.ch2.vShunt = getVoltage(buf);
res.ch2.vShunt = getShuntVoltage(buf);
i2c.readReg(ADDR, REG_CH2_VB, sizeof(buf), buf);
res.ch2.vBus = getVoltage(buf);
res.ch2.vBus = getBusVoltage(buf);
i2c.readReg(ADDR, REG_CH3_VS, sizeof(buf), buf);
res.ch3.vShunt = getVoltage(buf);
res.ch3.vShunt = getShuntVoltage(buf);
i2c.readReg(ADDR, REG_CH3_VB, sizeof(buf), buf);
res.ch3.vBus = getVoltage(buf);
res.ch3.vBus = getBusVoltage(buf);
return res;
}
@@ -77,10 +84,19 @@ public:
}
private:
/** convert to bytes into a mV reading */
int16_t getVoltage(const uint8_t* buf) {
return ((buf[0] << 8) | (buf[1] << 0)); // the lowest 3 bits are always zero. no shift required!
int16_t getBusVoltage(const uint8_t* buf) {
return ((int16_t)(getU16(buf)&0b1111111111111000)); // Bit 0,1,2 are status indicators -> remove, Bit3 = 8mV -> (LSB = 1mV)
}
/** convert to bytes into a uV reading */
int16_t getShuntVoltage(const uint8_t* buf) {
return ((int16_t)(getU16(buf)&0b1111111111111000))/2; // Bit 0,1,2 are status indicators -> remove, Bit3 = 40 uV -> / 2 and result is in 10 uV
}
uint16_t getU16(const uint8_t* buf) {
return ((buf[0] << 8) | (buf[1] << 0));
}
};