summaryrefslogtreecommitdiff
path: root/app/src/power.c
blob: bad54d28db63b79ee210683769655f0fdddc7b75 (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
/*
 * Copyright (c) 2020 The ZMK Contributors
 *
 * SPDX-License-Identifier: MIT
 */

#include <zephyr.h>
#include <kernel.h>
#include <power/power.h>

#include <logging/log.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#include <zmk/usb.h>
#include <zmk/event-manager.h>
#include <zmk/events/position-state-changed.h>
#include <zmk/events/sensor-event.h>

static u32_t power_last_uptime;

#define MAX_IDLE_MS CONFIG_ZMK_IDLE_SLEEP_TIMEOUT

bool is_usb_power_present() {
#ifdef CONFIG_USB
    return zmk_usb_is_powered();
#else
    return false;
#endif /* CONFIG_USB */
}

enum power_states sys_pm_policy_next_state(s32_t ticks) {
#ifdef CONFIG_SYS_POWER_DEEP_SLEEP_STATES
#ifdef CONFIG_HAS_SYS_POWER_STATE_DEEP_SLEEP_1
    s32_t current = k_uptime_get();
    if (power_last_uptime > 0 && !is_usb_power_present() &&
        current - power_last_uptime > MAX_IDLE_MS) {
        return SYS_POWER_STATE_DEEP_SLEEP_1;
    }
#endif /* CONFIG_HAS_SYS_POWER_STATE_DEEP_SLEEP_1 */
#endif /* CONFIG_SYS_POWER_DEEP_SLEEP_STATES */

    return SYS_POWER_STATE_ACTIVE;
}

int power_event_listener(const struct zmk_event_header *eh) {
    power_last_uptime = k_uptime_get();

    return 0;
}

int power_init() {
    power_last_uptime = k_uptime_get();

    return 0;
}

ZMK_LISTENER(power, power_event_listener);
ZMK_SUBSCRIPTION(power, position_state_changed);
ZMK_SUBSCRIPTION(power, sensor_event);

SYS_INIT(power_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);