1 /*
2 * Copyright (c) 2023 The ChromiumOS Authors
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/device.h>
8 #include <zephyr/drivers/led.h>
9 #include <zephyr/kernel.h>
10
11 #include "meas.h"
12
13 enum led_color_t {
14 LED_RED,
15 LED_GREEN,
16 LED_BLUE,
17 };
18
set_led(const struct device * const led,enum led_color_t led_color)19 static void set_led(const struct device *const led, enum led_color_t led_color)
20 {
21 if (led_color != LED_RED) {
22 led_off(led, LED_RED);
23 }
24 if (led_color != LED_GREEN) {
25 led_off(led, LED_GREEN);
26 }
27 if (led_color != LED_BLUE) {
28 led_off(led, LED_BLUE);
29 }
30 led_on(led, led_color);
31 }
32
33 #define CHARGING_VOLTAGE 5000
34 #define CHARGING_CURRENT 1000
35
main(void)36 int main(void)
37 {
38 meas_init();
39
40 const struct device *const led = DEVICE_DT_GET_ONE(gpio_leds);
41 int32_t vbus_v = 0;
42 int32_t vbus_c = 0;
43
44 if (!device_is_ready(led)) {
45 return 0;
46 }
47
48 while (1) {
49 meas_vbus_v(&vbus_v);
50 meas_vbus_c(&vbus_c);
51
52 if (vbus_v > CHARGING_VOLTAGE) {
53 if (vbus_c > CHARGING_CURRENT) {
54 set_led(led, LED_GREEN);
55 } else {
56 set_led(led, LED_BLUE);
57 }
58 } else {
59 set_led(led, LED_RED);
60 }
61
62 k_usleep(500);
63 }
64 }
65