1 /*
2  * Copyright (c) 2023 Alvaro Garcia Gomez <maxpowel@gmail.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "zephyr/sys/util.h"
8 #include <zephyr/kernel.h>
9 #include <zephyr/device.h>
10 #include <zephyr/devicetree.h>
11 #include <zephyr/drivers/fuel_gauge.h>
12 
13 
14 
main(void)15 int main(void)
16 {
17 	const struct device *const dev = DEVICE_DT_GET_ANY(maxim_max17048);
18 	int ret = 0;
19 
20 	if (dev == NULL) {
21 		printk("\nError: no device found.\n");
22 		return 0;
23 	}
24 
25 	if (!device_is_ready(dev)) {
26 		printk("\nError: Device \"%s\" is not ready; "
27 		       "check the driver initialization logs for errors.\n",
28 		       dev->name);
29 		return 0;
30 	}
31 
32 
33 
34 	printk("Found device \"%s\", getting fuel gauge data\n", dev->name);
35 
36 	if (dev == NULL) {
37 		return 0;
38 	}
39 
40 	while (1) {
41 
42 		fuel_gauge_prop_t props[] = {
43 			FUEL_GAUGE_RUNTIME_TO_EMPTY,
44 			FUEL_GAUGE_RUNTIME_TO_FULL,
45 			FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE,
46 			FUEL_GAUGE_VOLTAGE,
47 		};
48 
49 		union fuel_gauge_prop_val vals[ARRAY_SIZE(props)];
50 
51 		ret = fuel_gauge_get_props(dev, props, vals, ARRAY_SIZE(props));
52 		if (ret < 0) {
53 			printk("Error: cannot get properties\n");
54 		} else {
55 			printk("Time to empty %d minutes\n", vals[0].runtime_to_empty);
56 
57 			printk("Time to full %d minutes\n", vals[1].runtime_to_full);
58 
59 			printk("Charge %d%%\n", vals[2].relative_state_of_charge);
60 
61 			printk("Voltage %d\n uV", vals[3].voltage);
62 		}
63 
64 		k_sleep(K_MSEC(5000));
65 	}
66 	return 0;
67 }
68