summaryrefslogtreecommitdiff
path: root/quantum/keyboard.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/keyboard.c')
-rw-r--r--quantum/keyboard.c227
1 files changed, 178 insertions, 49 deletions
diff --git a/quantum/keyboard.c b/quantum/keyboard.c
index 3bca05aab7..ba5609f0aa 100644
--- a/quantum/keyboard.c
+++ b/quantum/keyboard.c
@@ -16,9 +16,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
+#include "quantum.h"
#include "keyboard.h"
#include "matrix.h"
#include "keymap.h"
+#include "magic.h"
#include "host.h"
#include "led.h"
#include "keycode.h"
@@ -100,20 +102,42 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifdef SLEEP_LED_ENABLE
# include "sleep_led.h"
#endif
+#ifdef SPLIT_KEYBOARD
+# include "split_util.h"
+#endif
+#ifdef BLUETOOTH_ENABLE
+# include "outputselect.h"
+#endif
static uint32_t last_input_modification_time = 0;
-uint32_t last_input_activity_time(void) { return last_input_modification_time; }
-uint32_t last_input_activity_elapsed(void) { return timer_elapsed32(last_input_modification_time); }
+uint32_t last_input_activity_time(void) {
+ return last_input_modification_time;
+}
+uint32_t last_input_activity_elapsed(void) {
+ return timer_elapsed32(last_input_modification_time);
+}
static uint32_t last_matrix_modification_time = 0;
-uint32_t last_matrix_activity_time(void) { return last_matrix_modification_time; }
-uint32_t last_matrix_activity_elapsed(void) { return timer_elapsed32(last_matrix_modification_time); }
-void last_matrix_activity_trigger(void) { last_matrix_modification_time = last_input_modification_time = timer_read32(); }
+uint32_t last_matrix_activity_time(void) {
+ return last_matrix_modification_time;
+}
+uint32_t last_matrix_activity_elapsed(void) {
+ return timer_elapsed32(last_matrix_modification_time);
+}
+void last_matrix_activity_trigger(void) {
+ last_matrix_modification_time = last_input_modification_time = timer_read32();
+}
static uint32_t last_encoder_modification_time = 0;
-uint32_t last_encoder_activity_time(void) { return last_encoder_modification_time; }
-uint32_t last_encoder_activity_elapsed(void) { return timer_elapsed32(last_encoder_modification_time); }
-void last_encoder_activity_trigger(void) { last_encoder_modification_time = last_input_modification_time = timer_read32(); }
+uint32_t last_encoder_activity_time(void) {
+ return last_encoder_modification_time;
+}
+uint32_t last_encoder_activity_elapsed(void) {
+ return timer_elapsed32(last_encoder_modification_time);
+}
+void last_encoder_activity_trigger(void) {
+ last_encoder_modification_time = last_input_modification_time = timer_read32();
+}
// Only enable this if console is enabled to print to
#if defined(DEBUG_MATRIX_SCAN_RATE)
@@ -135,7 +159,9 @@ void matrix_scan_perf_task(void) {
}
}
-uint32_t get_matrix_scan_rate(void) { return last_matrix_scan_count; }
+uint32_t get_matrix_scan_rate(void) {
+ return last_matrix_scan_count;
+}
#else
# define matrix_scan_perf_task()
#endif
@@ -155,7 +181,7 @@ static matrix_row_t get_real_keys(uint8_t row, matrix_row_t rowdata) {
}
static inline bool popcount_more_than_one(matrix_row_t rowdata) {
- rowdata &= rowdata - 1; // if there are less than two bits (keys) set, rowdata will become zero
+ rowdata &= rowdata - 1; // if there are less than two bits (keys) set, rowdata will become zero
return rowdata;
}
@@ -212,7 +238,9 @@ __attribute__((weak)) void keyboard_pre_init_user(void) {}
*
* FIXME: needs doc
*/
-__attribute__((weak)) void keyboard_pre_init_kb(void) { keyboard_pre_init_user(); }
+__attribute__((weak)) void keyboard_pre_init_kb(void) {
+ keyboard_pre_init_user();
+}
/** \brief keyboard_post_init_user
*
@@ -226,7 +254,9 @@ __attribute__((weak)) void keyboard_post_init_user() {}
* FIXME: needs doc
*/
-__attribute__((weak)) void keyboard_post_init_kb(void) { keyboard_post_init_user(); }
+__attribute__((weak)) void keyboard_post_init_kb(void) {
+ keyboard_post_init_user();
+}
/** \brief keyboard_setup
*
@@ -250,13 +280,17 @@ void keyboard_setup(void) {
*
* FIXME: needs doc
*/
-__attribute__((weak)) bool is_keyboard_master(void) { return true; }
+__attribute__((weak)) bool is_keyboard_master(void) {
+ return true;
+}
/** \brief is_keyboard_left
*
* FIXME: needs doc
*/
-__attribute__((weak)) bool is_keyboard_left(void) { return true; }
+__attribute__((weak)) bool is_keyboard_left(void) {
+ return true;
+}
#endif
@@ -265,7 +299,9 @@ __attribute__((weak)) bool is_keyboard_left(void) { return true; }
* Override this function if you have a condition where keypresses processing should change:
* - splits where the slave side needs to process for rgb/oled functionality
*/
-__attribute__((weak)) bool should_process_keypress(void) { return is_keyboard_master(); }
+__attribute__((weak)) bool should_process_keypress(void) {
+ return is_keyboard_master();
+}
/** \brief housekeeping_task_kb
*
@@ -290,6 +326,36 @@ void housekeeping_task(void) {
housekeeping_task_user();
}
+/** \brief Init tasks previously located in matrix_init_quantum
+ *
+ * TODO: rationalise against keyboard_init and current split role
+ */
+void quantum_init(void) {
+ magic();
+ led_init_ports();
+#ifdef BACKLIGHT_ENABLE
+ backlight_init_ports();
+#endif
+#ifdef AUDIO_ENABLE
+ audio_init();
+#endif
+#ifdef LED_MATRIX_ENABLE
+ led_matrix_init();
+#endif
+#ifdef RGB_MATRIX_ENABLE
+ rgb_matrix_init();
+#endif
+#if defined(UNICODE_COMMON_ENABLE)
+ unicode_input_mode_init();
+#endif
+#ifdef HAPTIC_ENABLE
+ haptic_init();
+#endif
+#if defined(BLUETOOTH_ENABLE) && defined(OUTPUT_AUTO_ENABLE)
+ set_output(OUTPUT_AUTO);
+#endif
+}
+
/** \brief keyboard_init
*
* FIXME: needs doc
@@ -300,7 +366,11 @@ void keyboard_init(void) {
#ifdef VIA_ENABLE
via_init();
#endif
+#ifdef SPLIT_KEYBOARD
+ split_pre_init();
+#endif
matrix_init();
+ quantum_init();
#if defined(CRC_ENABLE)
crc_init();
#endif
@@ -341,6 +411,9 @@ void keyboard_init(void) {
#ifdef VIRTSER_ENABLE
virtser_init();
#endif
+#ifdef SPLIT_KEYBOARD
+ split_post_init();
+#endif
#if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE)
debug_enable = true;
@@ -363,28 +436,17 @@ void switch_events(uint8_t row, uint8_t col, bool pressed) {
#endif
}
-/** \brief Keyboard task: Do keyboard routine jobs
- *
- * Do routine keyboard jobs:
+/** \brief Perform scan of keyboard matrix
*
- * * scan matrix
- * * handle mouse movements
- * * handle midi commands
- * * light LEDs
- *
- * This is repeatedly called as fast as possible.
+ * Any detected changes in state are sent out as part of the processing
*/
-void keyboard_task(void) {
+bool matrix_scan_task(void) {
static matrix_row_t matrix_prev[MATRIX_ROWS];
- static uint8_t led_status = 0;
matrix_row_t matrix_row = 0;
matrix_row_t matrix_change = 0;
#ifdef QMK_KEYS_PER_SCAN
uint8_t keys_processed = 0;
#endif
-#ifdef ENCODER_ENABLE
- bool encoders_changed = false;
-#endif
uint8_t matrix_changed = matrix_scan();
if (matrix_changed) last_matrix_activity_trigger();
@@ -431,10 +493,94 @@ void keyboard_task(void) {
MATRIX_LOOP_END:
-#ifdef DEBUG_MATRIX_SCAN_RATE
matrix_scan_perf_task();
+ return matrix_changed;
+}
+
+/** \brief Tasks previously located in matrix_scan_quantum
+ *
+ * TODO: rationalise against keyboard_task and current split role
+ */
+void quantum_task(void) {
+#ifdef SPLIT_KEYBOARD
+ // some tasks should only run on master
+ if (!is_keyboard_master()) return;
+#endif
+
+#if defined(AUDIO_ENABLE) && defined(AUDIO_INIT_DELAY)
+ // There are some tasks that need to be run a little bit
+ // after keyboard startup, or else they will not work correctly
+ // because of interaction with the USB device state, which
+ // may still be in flux...
+ //
+ // At the moment the only feature that needs this is the
+ // startup song.
+ static bool delayed_tasks_run = false;
+ static uint16_t delayed_task_timer = 0;
+ if (!delayed_tasks_run) {
+ if (!delayed_task_timer) {
+ delayed_task_timer = timer_read();
+ } else if (timer_elapsed(delayed_task_timer) > 300) {
+ audio_startup();
+ delayed_tasks_run = true;
+ }
+ }
+#endif
+
+#if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
+ music_task();
+#endif
+
+#ifdef KEY_OVERRIDE_ENABLE
+ key_override_task();
+#endif
+
+#ifdef SEQUENCER_ENABLE
+ sequencer_task();
+#endif
+
+#ifdef TAP_DANCE_ENABLE
+ tap_dance_task();
#endif
+#ifdef COMBO_ENABLE
+ combo_task();
+#endif
+
+#ifdef WPM_ENABLE
+ decay_wpm();
+#endif
+
+#ifdef HAPTIC_ENABLE
+ haptic_task();
+#endif
+
+#ifdef DIP_SWITCH_ENABLE
+ dip_switch_read(false);
+#endif
+
+#ifdef AUTO_SHIFT_ENABLE
+ autoshift_matrix_scan();
+#endif
+}
+
+/** \brief Keyboard task: Do keyboard routine jobs
+ *
+ * Do routine keyboard jobs:
+ *
+ * * scan matrix
+ * * handle mouse movements
+ * * handle midi commands
+ * * light LEDs
+ *
+ * This is repeatedly called as fast as possible.
+ */
+void keyboard_task(void) {
+ bool matrix_changed = matrix_scan_task();
+ (void)matrix_changed;
+
+ quantum_task();
+
#if defined(RGBLIGHT_ENABLE)
rgblight_task();
#endif
@@ -453,7 +599,7 @@ MATRIX_LOOP_END:
#endif
#ifdef ENCODER_ENABLE
- encoders_changed = encoder_read();
+ bool encoders_changed = encoder_read();
if (encoders_changed) last_encoder_activity_trigger();
#endif
@@ -516,22 +662,5 @@ MATRIX_LOOP_END:
programmable_button_send();
#endif
- // update LED
- if (led_status != host_keyboard_leds()) {
- led_status = host_keyboard_leds();
- keyboard_set_leds(led_status);
- }
-}
-
-/** \brief keyboard set leds
- *
- * FIXME: needs doc
- */
-void keyboard_set_leds(uint8_t leds) {
- if (debug_keyboard) {
- debug("keyboard_set_led: ");
- debug_hex8(leds);
- debug("\n");
- }
- led_set(leds);
+ led_task();
}