summaryrefslogtreecommitdiffstats
path: root/users/vitoni/vitoni.c
blob: f8cb50ea78a4032008ef91fd13d52a444ae7a373 (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
// Copyright 2021 Victor Toni (@vitoni)
// SPDX-License-Identifier: GPL-2.0-or-later

#include "vitoni.h"

#include <rgb_matrix.h>
#include <lib/lib8tion/lib8tion.h>

#include "rgb_matrix_effects.h"
#include "utils.h"

#if defined(RGB_FADE_IN) || defined(RGB_DISABLE_WITH_FADE_OUT) || defined(RGB_IDLE_TIMEOUT)
static uint8_t state;

// flag used to indicate that offset calculation is needed to adjust the timer,
// so that it matches the index used for sine calculation
static bool calc_offset;

void matrix_scan_user_rgb(void) {
#if defined(RGB_DISABLE_WITH_FADE_OUT) || defined(RGB_IDLE_TIMEOUT)
    const uint8_t time = rgb_time_2_scale();
#endif
    static uint8_t time_offset;

    const uint32_t inactivity_millis = last_input_activity_elapsed();

#if defined(RGB_IDLE_TIMEOUT)
    if (IDLE != state && RGB_IDLE_TIMEOUT <= inactivity_millis) {
        update_value(&state, IDLE_FADE_OUT, &calc_offset);
    }
#endif
#if defined(RGB_DISABLE_WITH_FADE_OUT)
    const uint32_t fade_out_duration = scale_2_rgb_time(128);
    const uint32_t start_fade_out_after_millis = (RGB_MATRIX_TIMEOUT) > fade_out_duration
        ? (RGB_MATRIX_TIMEOUT) - fade_out_duration
        : 0;

    if (start_fade_out_after_millis <= inactivity_millis) {
        update_value(&state, FADE_OUT, &calc_offset);
    }
#elif defined(RGB_MATRIX_TIMEOUT)
    // having to set brightness "manually" to black as starting point for fade in
    // for the time when returning from suspended state
    if (RGB_MATRIX_TIMEOUT <= inactivity_millis + 15) {
        rgb_matrix_config.hsv.v = 0;
        state = SUSPENDED;
    }
#endif

    switch(state) {
#if defined(RGB_IDLE_TIMEOUT)
        case IDLE_FADE_OUT:
            if (calc_offset) {
                time_offset = calc_fade_out_offset(time);

                // resetting flag for subsequent calls
                calc_offset = false;
            }
            if (idle_fade_out(time + time_offset)) {
                update_value(&state, IDLE, &calc_offset);
            }
            break;
        case IDLE:
#if defined(RGB_IDLE_BREATHE)
            if (calc_offset) {
                // no need to calculate time_offset since we are aligned already due to IDLE_FADE_OUT
                // resetting flag for subsequent calls
                calc_offset = false;
            }
            idle_breathe(time + time_offset);
#endif
            break;
#endif
#if defined(RGB_DISABLE_WITH_FADE_OUT)
        case FADE_OUT:
            if (calc_offset) {
                time_offset = calc_fade_out_offset(time);

                // resetting flag for subsequent calls
                calc_offset = false;
            }
            if (fade_out(time + time_offset)) {
                update_value(&state, SUSPENDED, &calc_offset);
            }
            break;
#endif
#if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT)
        case FADE_IN:
            {
                // since we want to be active, fade in should be faster than e.g. fading out
                const uint8_t fade_in_time = rgb_time_2_scale_w_factor(4);
                if (calc_offset) {
                    time_offset = calc_fade_in_offset(fade_in_time);

                    // resetting flag for subsequent calls
                    calc_offset = false;
                }
                if (fade_in(fade_in_time + time_offset)) {
                    update_value(&state, REGULAR, &calc_offset);
                }
            }
            break;
#endif
        default:
            break;
    }
}

#if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT)
bool process_record_user_rgb(const uint16_t keycode, const keyrecord_t *record) {
    // if we are in a non regular state we might have faded out (eventually partially)
    // so we restore brightness (to max as we don't keep track of manually changed brightness)
    // if (REGULAR != state && FADE_IN != state) {
    if (FADE_IN != state && REGULAR != state) {
        update_value(&state, FADE_IN, &calc_offset);
    }

    return true; // Process all other keycodes normally
}

void suspend_wakeup_init_user(void) {
    if (FADE_IN != state) {
        // setting brightness to black as starting point for fade in
        rgb_matrix_config.hsv.v = 0;

        update_value(&state, FADE_IN, &calc_offset);
    }
}
#endif // defined(RGB_FADE_IN)

#endif // defined(RGB_FADE_IN) || defined(RGB_DISABLE_WITH_FADE_OUT)