summaryrefslogtreecommitdiffstats
path: root/utils/nwztools/plattools/test_power.c
blob: c02f689b170e1042c9f4798667b3c9af648ac66a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016 Amaury Pouly
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "nwz_lib.h"

static const char *charge_status_name(int chgstat)
{
    switch(chgstat)
    {
        case NWZ_POWER_STATUS_CHARGE_STATUS_CHARGING: return "charging";
        case NWZ_POWER_STATUS_CHARGE_STATUS_SUSPEND: return "suspend";
        case NWZ_POWER_STATUS_CHARGE_STATUS_TIMEOUT: return "timeout";
        case NWZ_POWER_STATUS_CHARGE_STATUS_NORMAL: return "normal";
        default: return "unknown";
    }
}

static const char *get_batt_gauge_name(int gauge)
{
    switch(gauge)
    {
        case NWZ_POWER_BAT_NOBAT: return "no batt";
        case NWZ_POWER_BAT_VERYLOW: return "very low";
        case NWZ_POWER_BAT_LOW: return "low";
        case NWZ_POWER_BAT_GAUGE0: return "____";
        case NWZ_POWER_BAT_GAUGE1: return "O___";
        case NWZ_POWER_BAT_GAUGE2: return "OO__";
        case NWZ_POWER_BAT_GAUGE3: return "OOO_";
        case NWZ_POWER_BAT_GAUGE4: return "OOOO";
        default: return "unknown";
    }
}

static const char *acc_charge_mode_name(int mode)
{
    switch(mode)
    {
        case NWZ_POWER_ACC_CHARGE_NONE: return "none";
        case NWZ_POWER_ACC_CHARGE_VBAT: return "vbat";
        case NWZ_POWER_ACC_CHARGE_VSYS: return "vsys";
        default: return "unknown";
    }
}

int main(int argc, char **argv)
{
    /* clear screen and display welcome message */
    nwz_lcdmsg(true, 0, 0, "test_power");
    nwz_lcdmsg(false, 0, 2, "PWR OFF: quit");
    /* open input device */
    int input_fd = nwz_key_open();
    if(input_fd < 0)
    {
        nwz_lcdmsg(false, 3, 4, "Cannot open input device");
        sleep(2);
        return 1;
    }
    /* open adc device */
    int power_fd = nwz_power_open();
    if(power_fd < 0)
    {
        nwz_lcdmsg(false, 3, 4, "Cannot open power device");
        sleep(2);
        return 1;
    }
    /* display input state in a loop */
    while(1)
    {
        /* print status */
        int line = 4;
        int status = nwz_power_get_status(power_fd);
        int chgstat = status & NWZ_POWER_STATUS_CHARGE_STATUS;
        int acc_chg_mode = nwz_power_get_acc_charge_mode(power_fd);
        nwz_lcdmsgf(false, 0, line++, "ac detected: %s   ",
            (status & NWZ_POWER_STATUS_AC_DET) ? "yes" : "no");
        nwz_lcdmsgf(false, 0, line++, "vbus detected: %s   ",
            (status & NWZ_POWER_STATUS_VBUS_DET) ? "yes" : "no");
        nwz_lcdmsgf(false, 0, line++, "vbus voltage: %d mV (AD=%d)      ",
            nwz_power_get_vbus_voltage(power_fd), nwz_power_get_vbus_adval(power_fd));
        nwz_lcdmsgf(false, 0, line++, "vbus limit: %d mA      ",
            nwz_power_get_vbus_limit(power_fd));
        nwz_lcdmsgf(false, 0, line++, "vsys voltage: %d mV (AD=%d)      ",
            nwz_power_get_vsys_voltage(power_fd), nwz_power_get_vsys_adval(power_fd));
        nwz_lcdmsgf(false, 0, line++, "charge switch: %s       ",
            nwz_power_get_charge_switch(power_fd) ? "on" : "off");
        nwz_lcdmsgf(false, 0, line++, "full voltage: %s V      ",
            (status & NWZ_POWER_STATUS_CHARGE_LOW) ? "4.1" : "4.2");
        nwz_lcdmsgf(false, 0, line++, "current limit: %d mA     ",
            nwz_power_get_charge_current(power_fd));
        nwz_lcdmsgf(false, 0, line++, "charge status: %s (%x)     ",
            charge_status_name(chgstat), chgstat);
        nwz_lcdmsgf(false, 0, line++, "battery full: %s       ",
            nwz_power_is_fully_charged(power_fd) ? "yes" : "no");
        nwz_lcdmsgf(false, 0, line++, "bat gauge: %s (%d)       ",
            get_batt_gauge_name(nwz_power_get_battery_gauge(power_fd)),
            nwz_power_get_battery_gauge(power_fd));
        nwz_lcdmsgf(false, 0, line++, "avg voltage: %d mV (AD=%d)      ",
            nwz_power_get_battery_voltage(power_fd), nwz_power_get_battery_adval(power_fd));
        nwz_lcdmsgf(false, 0, line++, "sample count: %d       ",
            nwz_power_get_sample_count(power_fd));
        nwz_lcdmsgf(false, 0, line++, "raw voltage: %d mV (AD=%d)      ",
            nwz_power_get_vbat_voltage(power_fd), nwz_power_get_vbat_adval(power_fd));
        nwz_lcdmsgf(false, 0, line++, "acc charge mode: %s (%d)     ",
            acc_charge_mode_name(acc_chg_mode), acc_chg_mode);
        /* wait for event (1s) */
        int ret = nwz_key_wait_event(input_fd, 1000000);
        if(ret != 1)
            continue;
        struct input_event evt;
        if(nwz_key_read_event(input_fd, &evt) != 1)
            continue;
        if(nwz_key_event_get_keycode(&evt) == NWZ_KEY_OPTION && !nwz_key_event_is_press(&evt))
            break;
    }
    /* finish nicely */
    return 0;
}