forked from qmk/qmk_firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.c
137 lines (124 loc) · 4.28 KB
/
led.c
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
135
136
137
/* Copyright 2020 zvecr<[email protected]>
*
* 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 program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "quantum.h"
#ifdef BACKLIGHT_ENABLE
# include "backlight.h"
extern backlight_config_t backlight_config;
#else
// Cannot use BACKLIGHT_CAPS_LOCK without backlight being enabled
# undef BACKLIGHT_CAPS_LOCK
#endif
#ifndef LED_PIN_ON_STATE
# define LED_PIN_ON_STATE 1
#endif
#if defined(BACKLIGHT_CAPS_LOCK)
/** \brief Caps Lock indicator using backlight (for keyboards without dedicated LED)
*/
static void handle_backlight_caps_lock(led_t led_state) {
// Use backlight as Caps Lock indicator
uint8_t bl_toggle_lvl = 0;
if (led_state.caps_lock && !backlight_config.enable) {
// Turning Caps Lock ON and backlight is disabled in config
// Toggling backlight to the brightest level
bl_toggle_lvl = BACKLIGHT_LEVELS;
} else if (!led_state.caps_lock && backlight_config.enable) {
// Turning Caps Lock OFF and backlight is enabled in config
// Toggling backlight and restoring config level
bl_toggle_lvl = backlight_config.level;
}
// Set level without modify backlight_config to keep ability to restore state
backlight_set(bl_toggle_lvl);
}
#endif
/** \brief Lock LED set callback - keymap/user level
*
* \deprecated Use led_update_user() instead.
*/
__attribute__((weak)) void led_set_user(uint8_t usb_led) {}
/** \brief Lock LED set callback - keyboard level
*
* \deprecated Use led_update_kb() instead.
*/
__attribute__((weak)) void led_set_kb(uint8_t usb_led) { led_set_user(usb_led); }
/** \brief Lock LED update callback - keymap/user level
*
* \return True if led_update_kb() should run its own code, false otherwise.
*/
__attribute__((weak)) bool led_update_user(led_t led_state) { return true; }
/** \brief Lock LED update callback - keyboard level
*
* \return Ignored for now.
*/
__attribute__((weak)) bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if (res) {
#if defined(LED_NUM_LOCK_PIN) || defined(LED_CAPS_LOCK_PIN) || defined(LED_SCROLL_LOCK_PIN) || defined(LED_COMPOSE_PIN) || defined(LED_KANA_PIN)
# if LED_PIN_ON_STATE == 0
// invert the whole thing to avoid having to conditionally !led_state.x later
led_state.raw = ~led_state.raw;
# endif
# ifdef LED_NUM_LOCK_PIN
writePin(LED_NUM_LOCK_PIN, led_state.num_lock);
# endif
# ifdef LED_CAPS_LOCK_PIN
writePin(LED_CAPS_LOCK_PIN, led_state.caps_lock);
# endif
# ifdef LED_SCROLL_LOCK_PIN
writePin(LED_SCROLL_LOCK_PIN, led_state.scroll_lock);
# endif
# ifdef LED_COMPOSE_PIN
writePin(LED_COMPOSE_PIN, led_state.compose);
# endif
# ifdef LED_KANA_PIN
writePin(LED_KANA_PIN, led_state.kana);
# endif
#endif
}
return res;
}
/** \brief Initialise any LED related hardware and/or state
*/
__attribute__((weak)) void led_init_ports(void) {
#ifdef LED_NUM_LOCK_PIN
setPinOutput(LED_NUM_LOCK_PIN);
writePin(LED_NUM_LOCK_PIN, !LED_PIN_ON_STATE);
#endif
#ifdef LED_CAPS_LOCK_PIN
setPinOutput(LED_CAPS_LOCK_PIN);
writePin(LED_CAPS_LOCK_PIN, !LED_PIN_ON_STATE);
#endif
#ifdef LED_SCROLL_LOCK_PIN
setPinOutput(LED_SCROLL_LOCK_PIN);
writePin(LED_SCROLL_LOCK_PIN, !LED_PIN_ON_STATE);
#endif
#ifdef LED_COMPOSE_PIN
setPinOutput(LED_COMPOSE_PIN);
writePin(LED_COMPOSE_PIN, !LED_PIN_ON_STATE);
#endif
#ifdef LED_KANA_PIN
setPinOutput(LED_KANA_PIN);
writePin(LED_KANA_PIN, !LED_PIN_ON_STATE);
#endif
}
/** \brief Entrypoint for protocol to LED binding
*/
__attribute__((weak)) void led_set(uint8_t usb_led) {
#ifdef BACKLIGHT_CAPS_LOCK
handle_backlight_caps_lock((led_t)usb_led);
#endif
led_set_kb(usb_led);
led_update_kb((led_t)usb_led);
}