added ina219
This commit is contained in:
@@ -156,18 +156,21 @@ public:
|
|||||||
dst.fillRect(x,y,w,h);
|
dst.fillRect(x,y,w,h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** draw a horizontal line from x1 to x2 (inclusive) */
|
||||||
void drawLineHor(const Scalar x1, const Scalar x2, const Scalar y) {
|
void drawLineHor(const Scalar x1, const Scalar x2, const Scalar y) {
|
||||||
dst.fillRect(x1,y,x2-x1,1);
|
for (Scalar x = x1; x <= x2; ++x) {dst.setPixel(x, y);}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** draw a vertical line from y1 to y2 (inclusive) */
|
||||||
void drawLineVer(const Scalar y1, const Scalar y2, const Scalar x) {
|
void drawLineVer(const Scalar y1, const Scalar y2, const Scalar x) {
|
||||||
dst.fillRect(x,y1,1,y2-y1);
|
for (Scalar y = y1; y <= y2; ++y) {dst.setPixel(x, y);}
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawRect(const Scalar x, const Scalar y, const Scalar w, const Scalar h) {
|
void drawRect(const Scalar x, const Scalar y, const Scalar w, const Scalar h) {
|
||||||
drawLineHor(x,x+w, y); // top
|
drawLineHor(x, x+w-1, y); // top
|
||||||
drawLineHor(x,x+w, y+h); // bottom
|
drawLineHor(x, x+w-1, y+h-1); // bottom
|
||||||
drawLineVer(y,y+h, x); // left
|
drawLineVer(y+1, y+h-2, x); // left 2 pixels already included above
|
||||||
drawLineVer(y,y+h, x+w); // right
|
drawLineVer(y+1, y+h-2, x+w-1); // right 2 pixels already included above
|
||||||
}
|
}
|
||||||
|
|
||||||
// void drawRect(const Scalar x1, const Scalar y1, const Scalar x2, const Scalar y2) {
|
// void drawRect(const Scalar x1, const Scalar y1, const Scalar x2, const Scalar y2) {
|
||||||
|
|||||||
98
ext/sens/INA219.h
Normal file
98
ext/sens/INA219.h
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
#ifndef SENS_INA_219
|
||||||
|
#define SENS_INA_219
|
||||||
|
|
||||||
|
/** 1-Channel i2c volt/ampere sensor */
|
||||||
|
template <typename I2C> class INA219 {
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
I2C& i2c;
|
||||||
|
static constexpr const uint8_t ADDR = 0b1000000;
|
||||||
|
static constexpr const char* NAME = "INA219";
|
||||||
|
|
||||||
|
static constexpr const uint8_t REG_CFG = 0x00;
|
||||||
|
|
||||||
|
static constexpr const uint8_t REG_VS = 0x01; // shunt voltage
|
||||||
|
static constexpr const uint8_t REG_VB = 0x02; // bus voltage
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
struct Voltages {
|
||||||
|
|
||||||
|
int vShunt; // in uV * 10(!!!)
|
||||||
|
int16_t vBus; // in mV
|
||||||
|
|
||||||
|
int getMilliAmps(int shunt_milliOhm) const {
|
||||||
|
return (vShunt * 10) / shunt_milliOhm;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
INA219(I2C& i2c) : i2c(i2c) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/** get the current config */
|
||||||
|
uint16_t getConfig() {
|
||||||
|
uint8_t buf[2];
|
||||||
|
i2c.readReg(ADDR, REG_CFG, 2, (uint8_t*)&buf);
|
||||||
|
return getU16(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** read both voltages (shunt and bus) */
|
||||||
|
Voltages getVoltages() {
|
||||||
|
|
||||||
|
uint8_t buf[2];
|
||||||
|
Voltages res;
|
||||||
|
|
||||||
|
i2c.readReg(ADDR, REG_VS, sizeof(buf), buf);
|
||||||
|
res.vShunt = getShuntVoltage(buf);
|
||||||
|
i2c.readReg(ADDR, REG_VB, sizeof(buf), buf);
|
||||||
|
res.vBus = getBusVoltage(buf);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** is an INA219 present on the bus? */
|
||||||
|
bool isPresent() {
|
||||||
|
return i2c.query(ADDR);
|
||||||
|
}
|
||||||
|
|
||||||
|
void dumpConfig() {
|
||||||
|
const uint16_t cfg = getConfig();
|
||||||
|
const uint8_t mode = (cfg & 0b00000000000111) >> 0;
|
||||||
|
const uint8_t sadc = (cfg & 0b00000001111000) >> 3;
|
||||||
|
const uint8_t badc = (cfg & 0b00011110000000) >> 7;
|
||||||
|
const uint8_t pg = (cfg & 0b01100000000000) >> 11;
|
||||||
|
const uint8_t brng = (cfg & 0b10000000000000) >> 13;
|
||||||
|
printf("INA219\n");
|
||||||
|
printf("- Mode: %d", mode);
|
||||||
|
printf("- SADC: %d", sadc);
|
||||||
|
printf("- BADC: %d", badc);
|
||||||
|
printf("PG (shunt divider) %d", (1<<pg));
|
||||||
|
if (brng) {printf("- bus voltage range [0:16]");}
|
||||||
|
else {printf("- bus voltage range [0:32]");}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
/** convert to bytes into a mV reading */
|
||||||
|
int16_t getBusVoltage(const uint8_t* buf) {
|
||||||
|
return (getU16(buf) >> 3) * 4; // lower 3 bits are status indicators -> remove, the LSB equals 4 mV -> * 4
|
||||||
|
}
|
||||||
|
|
||||||
|
/** convert to bytes into a uV reading */
|
||||||
|
int getShuntVoltage(const uint8_t* buf) {
|
||||||
|
return ((int)((int16_t)getU16(buf))); // NOTE: LSB = 10 uV
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t getU16(const uint8_t* buf) {
|
||||||
|
return ((buf[0] << 8) | (buf[1] << 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif // SENS_INA_219
|
||||||
Reference in New Issue
Block a user