From f3c9d328ba406528a25233995a00d53e71a7c35a Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 10 Nov 2016 09:41:00 +0100 Subject: [PATCH] key_events: A way to hook into the event handling This adds a new `handle_user_key_event` function, that will get called before anything else, and can override what happens. If it returns true, the event will not be processed further by the firmware. By default, this function returns false, and is a no-op. It is defined with a `weak` attribute, to let the linker know that any other function with the same name should override his one. This makes it possible to have another version of this function in a firmware Sketch, and override the behaviour, to extend the event handling. This is the foundation that allows it to use external libraries, and tap into the firmware's event handler, to add new stuff. (We can already hook into the main loop by changing the top `loop` function in the Sketch) This addresses #21 for the most part. Signed-off-by: Gergely Nagy --- KeyboardioFirmware.ino | 5 +++++ key_events.cpp | 10 ++++++++++ key_events.h | 1 + 3 files changed, 16 insertions(+) diff --git a/KeyboardioFirmware.ino b/KeyboardioFirmware.ino index a267f412..81b383cf 100644 --- a/KeyboardioFirmware.ino +++ b/KeyboardioFirmware.ino @@ -10,6 +10,11 @@ uint8_t primary_keymap = 0; uint8_t temporary_keymap = 0; +bool handle_user_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState) { + //Serial.print ("user_key_event"); + return false; +} + void setup() { wdt_disable(); delay(100); diff --git a/key_events.cpp b/key_events.cpp index 7ebeb7d3..fb52a2df 100644 --- a/key_events.cpp +++ b/key_events.cpp @@ -46,10 +46,20 @@ void handle_synthetic_key_event(Key mappedKey, uint8_t currentState, uint8_t pre } } + +__attribute__((weak)) +bool handle_user_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState) { + return false; +} + void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState) { //for every newly pressed button, figure out what logical key it is and send a key down event // for every newly released button, figure out what logical key it is and send a key up event + if (handle_user_key_event(row, col, currentState, previousState)) { + return; + } + Key mappedKey; mappedKey.raw= pgm_read_word(&(keymaps[temporary_keymap][row][col])); Key baseKey; diff --git a/key_events.h b/key_events.h index 20661ab9..b459e154 100644 --- a/key_events.h +++ b/key_events.h @@ -20,3 +20,4 @@ void press_key(Key mappedKey); void handle_keymap_key_event(Key keymapEntry, uint8_t currentState, uint8_t previousState); void handle_mouse_key_event(Key mappedKey, uint8_t currentState, uint8_t previousState); +bool handle_user_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState);