SELKIELogger  1.0.0
PowerHatRead.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Swansea University
3  *
4  * This file is part of the SELKIELogger suite of tools.
5  *
6  * SELKIELogger is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * SELKIELogger is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this SELKIELogger product.
18  * If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #include <math.h>
28 
29 #include "SELKIELoggerI2C.h"
30 
44 int main(void) {
45  int handle = i2c_openConnection("/dev/i2c-1");
46 
47  if (handle < 0) {
48  fprintf(stderr, "Unable to open I2C bus: %s\n", strerror(errno));
49  return -1;
50  }
51 
52  for (int i2cc = 0x40; i2cc < 0x44; i2cc++) {
53  i2c_ina219_configure(handle, i2cc);
54  fprintf(stdout, "Input %02x\n", i2cc - 0x40 + 1);
55  float shuntVolts = i2c_ina219_read_shuntVoltage(handle, i2cc, NULL);
56  float busVolts = i2c_ina219_read_busVoltage(handle, i2cc, NULL);
57  float current = i2c_ina219_read_current(handle, i2cc, NULL);
58  float power = i2c_ina219_read_power(handle, i2cc, NULL);
59  if (isfinite(shuntVolts) && isfinite(busVolts)) {
60  fprintf(stdout, "\tShunt Voltage: %+.3f mV\n", shuntVolts);
61  fprintf(stdout, "\tBus Voltage: %+.6f V\n", busVolts);
62  fprintf(stdout, "\tCurrent: %+.6f A\n", current);
63  fprintf(stdout, "\tPower: %+.6f W\n", power);
64  } else {
65  fprintf(stdout, "\tBad voltage readings - input disconnected?\n");
66  }
67  }
68  i2c_closeConnection(handle);
69  return 0;
70 }
int main(void)
Definition: PowerHatRead.c:44
int i2c_openConnection(const char *bus)
Set up a connection to the specified bus.
Definition: I2CConnection.c:34
void i2c_closeConnection(int handle)
Close existing connection.
Definition: I2CConnection.c:46
float i2c_ina219_read_shuntVoltage(const int busHandle, const int devAddr, const void *opts)
Get voltage across the shunt resistor in millivolts.
Definition: I2C-INA219.c:77
bool i2c_ina219_configure(const int busHandle, const int devAddr)
Send configuration command to the specified device.
Definition: I2C-INA219.c:40
float i2c_ina219_read_current(const int busHandle, const int devAddr, const void *opts)
Get current flow through shunt resistor in amps.
Definition: I2C-INA219.c:173
float i2c_ina219_read_busVoltage(const int busHandle, const int devAddr, const void *opts)
Get bus voltage (at V- terminal) in volts.
Definition: I2C-INA219.c:115
float i2c_ina219_read_power(const int busHandle, const int devAddr, const void *opts)
Get power consumption in watts.
Definition: I2C-INA219.c:148