summaryrefslogtreecommitdiffstats
path: root/keyboards/lily58/keymaps/druotoni/gui_state.c
blob: d86e67ec7bf085936ec323270421fc5c195a29d3 (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
// Copyright 2021 Nicolas Druoton (druotoni)
// SPDX-License-Identifier: GPL-2.0-or-later

#include QMK_KEYBOARD_H

#include "gui_state.h"
#include "draw_helper.h"

// timer for the gui state
uint32_t global_sleep_timer     = 0;
uint32_t global_waking_up_timer = 0;
uint32_t global_booting_timer   = 0;

// timers test for states
#ifdef WITH_BOOT
static bool IsBooting(void) { return (timer_elapsed32(global_booting_timer) < BOOTING_TIME_TRESHOLD); }
#else
static bool IsBooting(void) { return false; }
#endif

// state test
static bool IsWakingUp(void) { return (timer_elapsed32(global_waking_up_timer) < WAKING_UP_TIME_TRESHOLD); }
static bool IsIdle(void) { return (timer_elapsed32(global_sleep_timer) > IDLE_TIME_TRESHOLD && timer_elapsed32(global_sleep_timer) < HALTING_TIME_TRESHOLD); }
static bool IsSleep(void) { return (timer_elapsed32(global_sleep_timer) >= SLEEP_TIME_TRESHOLD); }
static bool IsHalting(void) { return (timer_elapsed32(global_sleep_timer) >= HALTING_TIME_TRESHOLD && timer_elapsed32(global_sleep_timer) < SLEEP_TIME_TRESHOLD); }

gui_state_t get_gui_state(void) {
    // get gui states by testing timers
    if (IsBooting()) return _BOOTING;
    if (IsWakingUp()) return _WAKINGUP;
    if (IsIdle()) return _IDLE;
    if (IsHalting()) return _HALTING;
    if (IsSleep()) return _SLEEP;

    return _UP;
}

void update_gui_state(void) {
    // what to do when a key is pressed
    gui_state_t t = get_gui_state();

#ifdef WITH_BOOT
    if (t == _SLEEP) {
        // booting
        global_booting_timer = timer_read32();
    }

    if (t == _BOOTING) {
        // cancel booting
        global_booting_timer = 1000000;
    }
#else
    if (t == _SLEEP) {
        // waking up
        global_waking_up_timer = timer_read32();
    }
#endif

    if (t == _IDLE || t == _HALTING || t == _BOOTING) {
        // waking up
        global_waking_up_timer = timer_read32();
    }

    // no sleep
    global_sleep_timer = timer_read32();
}

uint8_t get_glitch_probability(void) {
    // more gliches could occur when halting time is near
    return interpo_pourcent(IDLE_TIME_TRESHOLD, HALTING_TIME_TRESHOLD, timer_elapsed32(global_sleep_timer));
}