Merge pull request #40 from algernon/f/hooks/additional-helpers

hooks: Add a way to replace hooks
pull/45/head
Jesse Vincent 8 years ago committed by GitHub
commit 4cc81b6c69

@ -3,11 +3,13 @@
byte NUMPAD_KEYMAP = 0; byte NUMPAD_KEYMAP = 0;
Keyboardio_::Keyboardio_(void) { Keyboardio_::Keyboardio_(void) {
memset(eventHandlers, 0, HOOK_MAX * sizeof(custom_handler_t));
memset(loopHooks, 0, HOOK_MAX * sizeof(custom_handler_t));
} }
void void
Keyboardio_::setup(const byte keymap_count, const byte numpad_layer) { Keyboardio_::setup(const byte keymap_count, const byte numpad_layer) {
event_handler_hook_add (handle_key_event_default); event_handler_hook_add(handle_key_event_default);
wdt_disable(); wdt_disable();
delay(100); delay(100);
Keyboard.begin(); Keyboard.begin();
@ -20,7 +22,7 @@ Keyboardio_::setup(const byte keymap_count, const byte numpad_layer) {
temporary_keymap = primary_keymap = Storage.load_primary_keymap(keymap_count); temporary_keymap = primary_keymap = Storage.load_primary_keymap(keymap_count);
} }
custom_loop_t loopHooks[HOOK_MAX] = {NULL}; custom_loop_t loopHooks[HOOK_MAX];
void void
Keyboardio_::loop(void) { Keyboardio_::loop(void) {

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

@ -9,8 +9,10 @@ typedef bool (*custom_handler_t)(Key mappedKey, byte row, byte col, uint8_t curr
extern custom_handler_t eventHandlers[HOOK_MAX]; extern custom_handler_t eventHandlers[HOOK_MAX];
void event_handler_hook_add (custom_handler_t hook); void event_handler_hook_add (custom_handler_t hook);
void event_handler_hook_replace (custom_handler_t oldHook, custom_handler_t newHook);
typedef void (*custom_loop_t)(void); typedef void (*custom_loop_t)(void);
extern custom_loop_t loopHooks[HOOK_MAX]; extern custom_loop_t loopHooks[HOOK_MAX];
void loop_hook_add (custom_loop_t hook); void loop_hook_add (custom_loop_t hook);
void loop_hook_replace (custom_loop_t oldHook, custom_loop_t newHook);

@ -39,7 +39,7 @@ void handle_synthetic_key_event(Key mappedKey, uint8_t currentState, uint8_t pre
} }
} }
custom_handler_t eventHandlers[HOOK_MAX] = {NULL}; custom_handler_t eventHandlers[HOOK_MAX];
Key lookup_key(byte keymap, byte row, byte col) { Key lookup_key(byte keymap, byte row, byte col) {
Key mappedKey; Key mappedKey;

Loading…
Cancel
Save