|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Alvaro Garcia Gomez <[email protected]> |
| 3 | + * Copyright (c) 2025 Philipp Steiner <[email protected]> |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include "zephyr/sys/util.h" |
| 9 | +#include <zephyr/kernel.h> |
| 10 | +#include <zephyr/device.h> |
| 11 | +#include <zephyr/devicetree.h> |
| 12 | +#include <zephyr/drivers/fuel_gauge.h> |
| 13 | + |
| 14 | +#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL |
| 15 | +#include <zephyr/logging/log.h> |
| 16 | +LOG_MODULE_REGISTER(app); |
| 17 | + |
| 18 | +int main(void) |
| 19 | +{ |
| 20 | + const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(fuel_gauge0)); |
| 21 | + int ret = 0; |
| 22 | + |
| 23 | + if (dev == NULL) { |
| 24 | + LOG_ERR("no device found."); |
| 25 | + return 0; |
| 26 | + } |
| 27 | + |
| 28 | + if (!device_is_ready(dev)) { |
| 29 | + LOG_ERR("Error: Device \"%s\" is not ready; check the driver initialization logs " |
| 30 | + "for errors.", |
| 31 | + dev->name); |
| 32 | + return 0; |
| 33 | + } |
| 34 | + |
| 35 | + LOG_INF("Found device \"%s\", getting fuel gauge data", dev->name); |
| 36 | + |
| 37 | + while (1) { |
| 38 | + |
| 39 | + fuel_gauge_prop_t props[] = { |
| 40 | + FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE, |
| 41 | + FUEL_GAUGE_VOLTAGE, |
| 42 | + }; |
| 43 | + |
| 44 | + union fuel_gauge_prop_val vals[ARRAY_SIZE(props)]; |
| 45 | + |
| 46 | + ret = fuel_gauge_get_props(dev, props, vals, ARRAY_SIZE(props)); |
| 47 | + |
| 48 | + if (ret < 0) { |
| 49 | + LOG_ERR("Error: cannot get properties"); |
| 50 | + } else { |
| 51 | + LOG_INF("Fuel gauge data: Charge: %d%%, Voltage: %dmV", |
| 52 | + vals[0].relative_state_of_charge, vals[1].voltage / 1000); |
| 53 | + } |
| 54 | + |
| 55 | + k_sleep(K_MSEC(5000)); |
| 56 | + } |
| 57 | + return 0; |
| 58 | +} |
0 commit comments