keyboard stuff
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#include "bootmagic.h"
17#include "matrix.h"
18#include "keyboard.h"
19#include "wait.h"
20#include "eeconfig.h"
21#include "bootloader.h"
22
23#ifndef BOOTMAGIC_DEBOUNCE
24# if defined(DEBOUNCE) && DEBOUNCE > 0
25# define BOOTMAGIC_DEBOUNCE (DEBOUNCE * 2)
26# else
27# define BOOTMAGIC_DEBOUNCE 30
28# endif
29#endif
30
31/** \brief Reset eeprom
32 *
33 * ...just incase someone wants to only change the eeprom behaviour
34 */
35__attribute__((weak)) void bootmagic_reset_eeprom(void) {
36 eeconfig_disable();
37}
38
39/** \brief Decide reboot based on current matrix state
40 */
41__attribute__((weak)) bool bootmagic_should_reset(void) {
42 // If the configured key (commonly Esc) is held down on power up,
43 // reset the EEPROM valid state and jump to bootloader.
44 // This isn't very generalized, but we need something that doesn't
45 // rely on user's keymaps in firmware or EEPROM.
46 uint8_t row = BOOTMAGIC_ROW;
47 uint8_t col = BOOTMAGIC_COLUMN;
48
49#if defined(SPLIT_KEYBOARD) && defined(BOOTMAGIC_ROW_RIGHT) && defined(BOOTMAGIC_COLUMN_RIGHT)
50 if (!is_keyboard_left()) {
51 row = BOOTMAGIC_ROW_RIGHT;
52 col = BOOTMAGIC_COLUMN_RIGHT;
53 }
54#endif
55
56 return matrix_get_row(row) & (1 << col);
57}
58
59/** \brief The abridged version of TMK's bootmagic based on Wilba.
60 *
61 * 100% less potential for accidentally making the keyboard do stupid things.
62 */
63__attribute__((weak)) void bootmagic_scan(void) {
64 // We need multiple scans because debouncing can't be turned off.
65 matrix_scan();
66 wait_ms(BOOTMAGIC_DEBOUNCE);
67 matrix_scan();
68
69 if (bootmagic_should_reset()) {
70 bootmagic_reset_eeprom();
71
72 // Jump to bootloader.
73 bootloader_jump();
74 }
75}
76
77void bootmagic(void) {
78 bootmagic_scan();
79}