SELKIELogger  1.0.0
AutomationHatRead.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 "SELKIELoggerI2C.h"
28 
41 int main(void) {
42  int handle = i2c_openConnection("/dev/i2c-1");
43 
44  if (handle < 0) {
45  fprintf(stderr, "Unable to open I2C bus: %s\n", strerror(errno));
46  return EXIT_FAILURE;
47  }
48 
49  const float cfact = 7.88842;
50  i2c_ads1015_options opts = I2C_ADS1015_DEFAULTS;
51  opts.pga = ADS1015_CONFIG_PGA_4096MV;
52 
53  // clang-format off
54  float ch0 = i2c_ads1015_read_mux(handle, ADS1015_ADDR_DEFAULT, ADS1015_CONFIG_MUX_SINGLE_0, &opts);
55  float ch1 = i2c_ads1015_read_mux(handle, ADS1015_ADDR_DEFAULT, ADS1015_CONFIG_MUX_SINGLE_1, &opts) * cfact;
56  float ch2 = i2c_ads1015_read_mux(handle, ADS1015_ADDR_DEFAULT, ADS1015_CONFIG_MUX_SINGLE_2, &opts) * cfact;
57  float ch3 = i2c_ads1015_read_mux(handle, ADS1015_ADDR_DEFAULT, ADS1015_CONFIG_MUX_SINGLE_3, &opts) * cfact;
58  // clang-format on
59 
60  float delta_1_3 = i2c_ads1015_read_diff_ch1_ch3(handle, ADS1015_ADDR_DEFAULT, &opts);
61  float delta_2_3 = i2c_ads1015_read_diff_ch2_ch3(handle, ADS1015_ADDR_DEFAULT, &opts);
62 
63  fprintf(stdout, "Single shot conversion on all mux options:\n");
64 
65  fprintf(stdout, "Channel 0:\t%+.3f mV\n", ch0);
66  fprintf(stdout, "Channel 1:\t%+.3f mV\n", ch1);
67  fprintf(stdout, "Channel 2:\t%+.3f mV\n", ch2);
68  fprintf(stdout, "Channel 3:\t%+.3f mV\n", ch3);
69 
70  fprintf(stdout, "Ch1 - Ch3:\t%+.3f mV\n", delta_1_3);
71  fprintf(stdout, "Ch2 - Ch3:\t%+.3f mV\n", delta_2_3);
72 
73  i2c_closeConnection(handle);
74  return 0;
75 }
int main(void)
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_ads1015_read_mux(const int busHandle, const int devAddr, const uint16_t mux, const i2c_ads1015_options *opts)
Generic ADS1015 read function.
Definition: I2C-ADS1015.c:91
float i2c_ads1015_read_diff_ch2_ch3(const int busHandle, const int devAddr, const void *opts)
Get differential voltage measurement between channels 2 and 3.
Definition: I2C-ADS1015.c:262
float i2c_ads1015_read_diff_ch1_ch3(const int busHandle, const int devAddr, const void *opts)
Get differential voltage measurement between channels 1 and 3.
Definition: I2C-ADS1015.c:248
uint16_t pga
PGA setting.