1 /*
2  * Copyright (c) 2021 Leonard Pollak
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <stdio.h>
9 #include <zephyr/drivers/sensor.h>
10 
11 
main(void)12 int main(void)
13 {
14 	const struct device *const ina = DEVICE_DT_GET_ONE(ti_ina219);
15 	struct sensor_value v_bus, power, current;
16 	int rc;
17 
18 	if (!device_is_ready(ina)) {
19 		printf("Device %s is not ready.\n", ina->name);
20 		return 0;
21 	}
22 
23 	while (true) {
24 		rc = sensor_sample_fetch(ina);
25 		if (rc) {
26 			printf("Could not fetch sensor data.\n");
27 			return 0;
28 		}
29 
30 		sensor_channel_get(ina, SENSOR_CHAN_VOLTAGE, &v_bus);
31 		sensor_channel_get(ina, SENSOR_CHAN_POWER, &power);
32 		sensor_channel_get(ina, SENSOR_CHAN_CURRENT, &current);
33 
34 		printf("Bus: %f [V] -- "
35 			"Power: %f [W] -- "
36 			"Current: %f [A]\n",
37 		       sensor_value_to_double(&v_bus),
38 		       sensor_value_to_double(&power),
39 		       sensor_value_to_double(&current));
40 		k_sleep(K_MSEC(2000));
41 	}
42 	return 0;
43 }
44