summaryrefslogtreecommitdiff
path: root/utils/nwztools/plattools/test_power.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/nwztools/plattools/test_power.c')
-rw-r--r--utils/nwztools/plattools/test_power.c134
1 files changed, 134 insertions, 0 deletions
diff --git a/utils/nwztools/plattools/test_power.c b/utils/nwztools/plattools/test_power.c
new file mode 100644
index 0000000000..c02f689b17
--- /dev/null
+++ b/utils/nwztools/plattools/test_power.c
@@ -0,0 +1,134 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2016 Amaury Pouly
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "nwz_lib.h"
22
23static const char *charge_status_name(int chgstat)
24{
25 switch(chgstat)
26 {
27 case NWZ_POWER_STATUS_CHARGE_STATUS_CHARGING: return "charging";
28 case NWZ_POWER_STATUS_CHARGE_STATUS_SUSPEND: return "suspend";
29 case NWZ_POWER_STATUS_CHARGE_STATUS_TIMEOUT: return "timeout";
30 case NWZ_POWER_STATUS_CHARGE_STATUS_NORMAL: return "normal";
31 default: return "unknown";
32 }
33}
34
35static const char *get_batt_gauge_name(int gauge)
36{
37 switch(gauge)
38 {
39 case NWZ_POWER_BAT_NOBAT: return "no batt";
40 case NWZ_POWER_BAT_VERYLOW: return "very low";
41 case NWZ_POWER_BAT_LOW: return "low";
42 case NWZ_POWER_BAT_GAUGE0: return "____";
43 case NWZ_POWER_BAT_GAUGE1: return "O___";
44 case NWZ_POWER_BAT_GAUGE2: return "OO__";
45 case NWZ_POWER_BAT_GAUGE3: return "OOO_";
46 case NWZ_POWER_BAT_GAUGE4: return "OOOO";
47 default: return "unknown";
48 }
49}
50
51static const char *acc_charge_mode_name(int mode)
52{
53 switch(mode)
54 {
55 case NWZ_POWER_ACC_CHARGE_NONE: return "none";
56 case NWZ_POWER_ACC_CHARGE_VBAT: return "vbat";
57 case NWZ_POWER_ACC_CHARGE_VSYS: return "vsys";
58 default: return "unknown";
59 }
60}
61
62int main(int argc, char **argv)
63{
64 /* clear screen and display welcome message */
65 nwz_lcdmsg(true, 0, 0, "test_power");
66 nwz_lcdmsg(false, 0, 2, "PWR OFF: quit");
67 /* open input device */
68 int input_fd = nwz_key_open();
69 if(input_fd < 0)
70 {
71 nwz_lcdmsg(false, 3, 4, "Cannot open input device");
72 sleep(2);
73 return 1;
74 }
75 /* open adc device */
76 int power_fd = nwz_power_open();
77 if(power_fd < 0)
78 {
79 nwz_lcdmsg(false, 3, 4, "Cannot open power device");
80 sleep(2);
81 return 1;
82 }
83 /* display input state in a loop */
84 while(1)
85 {
86 /* print status */
87 int line = 4;
88 int status = nwz_power_get_status(power_fd);
89 int chgstat = status & NWZ_POWER_STATUS_CHARGE_STATUS;
90 int acc_chg_mode = nwz_power_get_acc_charge_mode(power_fd);
91 nwz_lcdmsgf(false, 0, line++, "ac detected: %s ",
92 (status & NWZ_POWER_STATUS_AC_DET) ? "yes" : "no");
93 nwz_lcdmsgf(false, 0, line++, "vbus detected: %s ",
94 (status & NWZ_POWER_STATUS_VBUS_DET) ? "yes" : "no");
95 nwz_lcdmsgf(false, 0, line++, "vbus voltage: %d mV (AD=%d) ",
96 nwz_power_get_vbus_voltage(power_fd), nwz_power_get_vbus_adval(power_fd));
97 nwz_lcdmsgf(false, 0, line++, "vbus limit: %d mA ",
98 nwz_power_get_vbus_limit(power_fd));
99 nwz_lcdmsgf(false, 0, line++, "vsys voltage: %d mV (AD=%d) ",
100 nwz_power_get_vsys_voltage(power_fd), nwz_power_get_vsys_adval(power_fd));
101 nwz_lcdmsgf(false, 0, line++, "charge switch: %s ",
102 nwz_power_get_charge_switch(power_fd) ? "on" : "off");
103 nwz_lcdmsgf(false, 0, line++, "full voltage: %s V ",
104 (status & NWZ_POWER_STATUS_CHARGE_LOW) ? "4.1" : "4.2");
105 nwz_lcdmsgf(false, 0, line++, "current limit: %d mA ",
106 nwz_power_get_charge_current(power_fd));
107 nwz_lcdmsgf(false, 0, line++, "charge status: %s (%x) ",
108 charge_status_name(chgstat), chgstat);
109 nwz_lcdmsgf(false, 0, line++, "battery full: %s ",
110 nwz_power_is_fully_charged(power_fd) ? "yes" : "no");
111 nwz_lcdmsgf(false, 0, line++, "bat gauge: %s (%d) ",
112 get_batt_gauge_name(nwz_power_get_battery_gauge(power_fd)),
113 nwz_power_get_battery_gauge(power_fd));
114 nwz_lcdmsgf(false, 0, line++, "avg voltage: %d mV (AD=%d) ",
115 nwz_power_get_battery_voltage(power_fd), nwz_power_get_battery_adval(power_fd));
116 nwz_lcdmsgf(false, 0, line++, "sample count: %d ",
117 nwz_power_get_sample_count(power_fd));
118 nwz_lcdmsgf(false, 0, line++, "raw voltage: %d mV (AD=%d) ",
119 nwz_power_get_vbat_voltage(power_fd), nwz_power_get_vbat_adval(power_fd));
120 nwz_lcdmsgf(false, 0, line++, "acc charge mode: %s (%d) ",
121 acc_charge_mode_name(acc_chg_mode), acc_chg_mode);
122 /* wait for event (1s) */
123 int ret = nwz_key_wait_event(input_fd, 1000000);
124 if(ret != 1)
125 continue;
126 struct input_event evt;
127 if(nwz_key_read_event(input_fd, &evt) != 1)
128 continue;
129 if(nwz_key_event_get_keycode(&evt) == NWZ_KEY_OPTION && !nwz_key_event_is_press(&evt))
130 break;
131 }
132 /* finish nicely */
133 return 0;
134}