1 /*
2  * Copyright (c) 2020 Matija Tudan
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/devicetree.h>
10 #include <zephyr/drivers/sensor.h>
11 
main(void)12 int main(void)
13 {
14 	const struct device *const dev = DEVICE_DT_GET_ONE(maxim_max17262);
15 
16 	if (!device_is_ready(dev)) {
17 		printk("sensor: device not ready.\n");
18 		return 0;
19 	}
20 
21 	while (1) {
22 		struct sensor_value voltage, avg_current, temperature;
23 		float i_avg;
24 
25 		sensor_sample_fetch(dev);
26 		sensor_channel_get(dev, SENSOR_CHAN_GAUGE_VOLTAGE, &voltage);
27 		sensor_channel_get(dev, SENSOR_CHAN_GAUGE_AVG_CURRENT,
28 						  &avg_current);
29 		sensor_channel_get(dev, SENSOR_CHAN_GAUGE_TEMP, &temperature);
30 
31 		i_avg = avg_current.val1 + (avg_current.val2 / 1000000.0);
32 
33 		printk("V: %d.%06d V; I: %f mA; T: %d.%06d °C\n",
34 		      voltage.val1, voltage.val2, (double)i_avg,
35 		      temperature.val1, temperature.val2);
36 
37 		k_sleep(K_MSEC(1000));
38 	}
39 	return 0;
40 }
41