Small hook rework

Two things are done here: loop() hooks are introduced, and both loop and
event handler hooks are pre-allocated for HOOK_MAX (64 by default)
elements.

Two helpers are introduced too, that make it easier to append a new hook
to the end of these arrays.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/27/head
Gergely Nagy 8 years ago
parent 79ddb15c3b
commit 5a677ce7f0

@ -16,8 +16,15 @@ Keyboardio_::setup(void) {
temporary_keymap = primary_keymap = Storage.load_primary_keymap(KEYMAPS); temporary_keymap = primary_keymap = Storage.load_primary_keymap(KEYMAPS);
} }
custom_loop_t loopHooks[HOOK_MAX] = {NULL};
void void
Keyboardio_::loop(void) { Keyboardio_::loop(void) {
for (byte i = 0; loopHooks[i] != NULL && i < HOOK_MAX; i++) {
custom_loop_t hook = loopHooks[i];
(*hook)();
}
KeyboardHardware.scan_matrix(); KeyboardHardware.scan_matrix();
LEDControl.update(temporary_keymap); LEDControl.update(temporary_keymap);
Keyboard.sendReport(); Keyboard.sendReport();

@ -0,0 +1,27 @@
#include "hooks.h"
void
event_handler_hook_add (custom_handler_t hook) {
byte i;
for (i = 0; i < HOOK_MAX && eventHandlers[i] != NULL; i++) {
}
if (i == HOOK_MAX)
return;
eventHandlers[i] = hook;
}
void
loop_hook_add (custom_loop_t hook) {
byte i;
for (i = 0; i < HOOK_MAX && loopHooks[i] != NULL; i++) {
}
if (i == HOOK_MAX)
return;
loopHooks[i] = hook;
}

@ -0,0 +1,15 @@
#pragma once
#include <Arduino.h>
#define HOOK_MAX 64
typedef bool (*custom_handler_t)(byte row, byte col, uint8_t currentState, uint8_t previousState);
extern custom_handler_t eventHandlers[HOOK_MAX];
void event_handler_hook_add (custom_handler_t hook);
typedef void (*custom_loop_t)(void);
extern custom_loop_t loopHooks[HOOK_MAX];
void loop_hook_add (custom_loop_t hook);

@ -41,8 +41,7 @@ void handle_synthetic_key_event(Key mappedKey, uint8_t currentState, uint8_t pre
} }
} }
__attribute__((weak)) custom_handler_t eventHandlers[HOOK_MAX] = {
custom_handler_t eventHandlers[] = {
handle_key_event_default, handle_key_event_default,
(custom_handler_t) NULL (custom_handler_t) NULL
}; };
@ -56,7 +55,7 @@ Key lookup_key(byte keymap, byte row, byte col) {
} }
void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState) { void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState) {
for (byte i = 0; eventHandlers[i] != NULL; i++) { for (byte i = 0; eventHandlers[i] != NULL && i < HOOK_MAX; i++) {
custom_handler_t handler = eventHandlers[i]; custom_handler_t handler = eventHandlers[i];
if ((*handler)(row, col, currentState, previousState)) if ((*handler)(row, col, currentState, previousState))
return; return;

@ -8,14 +8,12 @@
#include "Storage.h" #include "Storage.h"
#include "keymap_metadata.h" #include "keymap_metadata.h"
#include "generated/keymaps.h" #include "generated/keymaps.h"
#include "hooks.h"
//static const Key keymaps[KEYMAPS][ROWS][COLS]; //static const Key keymaps[KEYMAPS][ROWS][COLS];
extern uint8_t primary_keymap; extern uint8_t primary_keymap;
extern uint8_t temporary_keymap; extern uint8_t temporary_keymap;
typedef bool (*custom_handler_t)(byte row, byte col, uint8_t currentState, uint8_t previousState);
extern custom_handler_t eventHandlers[];
// sending events to the computer // sending events to the computer
void handle_synthetic_key_event( Key mappedKey, uint8_t currentState, uint8_t previousState); void handle_synthetic_key_event( Key mappedKey, uint8_t currentState, uint8_t previousState);
void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState); void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState);

Loading…
Cancel
Save