Rework the hook handling

We can't register hooks from constructors, because there is no guaranteed order
in which the objects will be created. So it may well happen that the Keyboardio
object gets created later, and zeroes out everything. Or it gets created first,
and registers the default handler as the first one, making all the others
pointless.

Instead, we create a KeyboardioPlugin class, that has a `begin` method. This is
responsible for setting up the hooks and whatnot. To make things simpler (for
some values of simple), a `Keyboardio.use` method is introduced, which, when
given a NULL-terminated list of plugin object pointers, will call the begin
method of each.

All LED effects and other plugins that used to register a static object now use
an extern, and had their initialization moved to the `begin` method.

The end result is not the nicest thing, but it works. We can try figuring out
something nicer later.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/68/head
Gergely Nagy 8 years ago
parent 15e9be25bb
commit afac5ed6f2

@ -24,7 +24,6 @@ uint8_t temporary_keymap = 0;
const Key keymaps[][ROWS][COLS] PROGMEM = { KEYMAP_LIST };
static LEDOff LEDSOff;
static LEDSolidColor solidRed (100, 0, 0);
static LEDSolidColor solidOrange (100, 30, 0);
static LEDSolidColor solidYellow (90, 70, 0);
@ -33,11 +32,6 @@ static LEDSolidColor solidBlue (0, 30, 200);
static LEDSolidColor solidIndigo (0, 0, 200);
static LEDSolidColor solidViolet (100, 0, 120);
static LEDBreatheEffect breatheEffect;
static LEDRainbowEffect rainbowEffect;
static LEDRainbowWaveEffect rainbowWaveEffect;
static LEDChaseEffect chaseEffect;
static LEDNumlock numLockEffect (NUMPAD_KEYMAP);
const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) {
@ -54,7 +48,16 @@ const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) {
}
void setup() {
Keyboardio.use(&LEDOff,
&solidRed, &solidOrange, &solidYellow, &solidGreen, &solidBlue, &solidIndigo, &solidViolet,
&LEDBreatheEffect, &LEDRainbowEffect, &LEDChaseEffect, &numLockEffect,
&Macros,
&MouseKeys,
NULL);
Keyboardio.setup(KEYMAP_SIZE);
LEDOff.activate();
}

@ -55,5 +55,11 @@ static bool handleMacroEvent(Key mappedKey, byte row, byte col, uint8_t keyState
}
Macros_::Macros_ (void) {
}
void
Macros_::begin (void) {
event_handler_hook_add (handleMacroEvent);
}
Macros_ Macros;

@ -7,11 +7,13 @@
const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState);
class Macros_ {
class Macros_ : public KeyboardioPlugin {
public:
Macros_(void);
virtual void begin(void) final;
void play(const macro_t *macro_p);
};
static Macros_ Macros;
extern Macros_ Macros;

@ -54,5 +54,11 @@ static bool handleMouseKeys(Key mappedKey, byte row, byte col, uint8_t keyState)
}
MouseKeys_::MouseKeys_(void) {
}
void
MouseKeys_::begin (void) {
event_handler_hook_add (handleMouseKeys);
}
MouseKeys_ MouseKeys;

@ -3,9 +3,11 @@
#include "KeyboardioFirmware.h"
#include "MouseKeyDefs.h"
class MouseKeys_ {
class MouseKeys_ : public KeyboardioPlugin {
public:
MouseKeys_ (void);
virtual void begin(void) final;
};
static MouseKeys_ MouseKeys;
extern MouseKeys_ MouseKeys;

@ -1,4 +1,5 @@
#include "KeyboardioFirmware.h"
#include <stdarg.h>
Keyboardio_::Keyboardio_(void) {
memset(eventHandlers, 0, HOOK_MAX * sizeof(custom_handler_t));
@ -7,7 +8,6 @@ Keyboardio_::Keyboardio_(void) {
void
Keyboardio_::setup(const byte keymap_count) {
event_handler_hook_add (handle_key_event_default);
wdt_disable();
delay(100);
Keyboard.begin();
@ -15,6 +15,8 @@ Keyboardio_::setup(const byte keymap_count) {
LEDControl.setup();
temporary_keymap = primary_keymap = Storage.load_primary_keymap(keymap_count);
event_handler_hook_add (handle_key_event_default);
}
custom_loop_t loopHooks[HOOK_MAX];
@ -32,3 +34,15 @@ Keyboardio_::loop(void) {
}
}
void
Keyboardio_::use(KeyboardioPlugin *plugin, ...) {
va_list ap;
KeyboardioPlugin *p;
plugin->begin();
va_start(ap, plugin);
while ((p = va_arg(ap, KeyboardioPlugin*)) != NULL) {
p->begin();
};
va_end(ap);
}

@ -23,6 +23,7 @@ void setup();
#include "KeyboardConfig.h"
#include "key_events.h"
#include "plugin.h"
extern HARDWARE_IMPLEMENTATION KeyboardHardware;
@ -41,6 +42,7 @@ class Keyboardio_ {
void setup(const byte keymap_count);
void loop(void);
void use(KeyboardioPlugin *plugin, ...);
};
static Keyboardio_ Keyboardio;

@ -1,14 +1,14 @@
#include "LED-BreatheEffect.h"
LEDBreatheEffect::LEDBreatheEffect (void) {
LEDBreatheEffect_::LEDBreatheEffect_ (void) {
state.brightness = 0;
state.fadeAmount = 1;
LEDControl.mode_add (this);
}
void
LEDBreatheEffect::update (void) {
LEDBreatheEffect_::update (void) {
cRGB color = breath_compute (&state);
LEDControl.set_all_leds_to (color);
}
LEDBreatheEffect_ LEDBreatheEffect;

@ -3,12 +3,14 @@
#include "LEDControl.h"
#include "LEDUtils.h"
class LEDBreatheEffect : LEDMode {
class LEDBreatheEffect_ : LEDMode {
public:
LEDBreatheEffect (void);
LEDBreatheEffect_ (void);
virtual void update (void) final;
private:
BreathState state;
};
extern LEDBreatheEffect_ LEDBreatheEffect;

@ -1,11 +1,10 @@
#include "LED-ChaseEffect.h"
LEDChaseEffect::LEDChaseEffect (void) {
LEDControl.mode_add (this);
LEDChaseEffect_::LEDChaseEffect_ (void) {
}
void
LEDChaseEffect::update (void) {
LEDChaseEffect_::update (void) {
if (current_chase_counter++ < chase_threshold) {
return;
}
@ -20,3 +19,5 @@ LEDChaseEffect::update (void) {
led_set_crgb_at(pos, {0, 0, 255});
led_set_crgb_at(pos - (chase_sign * chase_pixels), {255, 0, 0});
}
LEDChaseEffect_ LEDChaseEffect;

@ -3,9 +3,9 @@
#include "LEDControl.h"
#include "LEDUtils.h"
class LEDChaseEffect : LEDMode {
class LEDChaseEffect_ : LEDMode {
public:
LEDChaseEffect (void);
LEDChaseEffect_ (void);
virtual void update (void) final;
@ -16,3 +16,5 @@ class LEDChaseEffect : LEDMode {
uint8_t current_chase_counter = 0;
static const uint8_t chase_threshold = 20;
};
extern LEDChaseEffect_ LEDChaseEffect;

@ -7,13 +7,17 @@ static uint8_t us;
LEDNumlock::LEDNumlock (uint8_t numpadIdx) {
numpadIndex = numpadIdx;
us = LEDControl.mode_add (this);
loop_hook_add (this->loopHook);
breathState.brightness = 0;
breathState.fadeAmount = 1;
}
void
LEDNumlock::begin (void) {
us = LEDControl.mode_add (this);
loop_hook_add (this->loopHook);
}
void
LEDNumlock::setup (void) {
if (temporary_keymap != numpadIndex) {

@ -7,6 +7,8 @@ class LEDNumlock : LEDMode {
public:
LEDNumlock (uint8_t numpadIndex);
virtual void begin (void) final;
virtual void update (void) final;
virtual void setup (void) final;

@ -0,0 +1,3 @@
#include "LED-Off.h"
LEDOff_ LEDOff;

@ -2,7 +2,9 @@
#include "LEDControl.h"
class LEDOff : LEDMode {
class LEDOff_ : public LEDMode {
public:
LEDOff (void) { LEDControl.mode_add (this); };
LEDOff_ (void) { };
};
extern LEDOff_ LEDOff;

@ -1,11 +1,10 @@
#include "LED-RainbowEffect.h"
LEDRainbowEffect::LEDRainbowEffect (void) {
LEDControl.mode_add (this);
LEDRainbowEffect_::LEDRainbowEffect_ (void) {
}
void
LEDRainbowEffect::update (void) {
LEDRainbowEffect_::update (void) {
if (rainbow_current_ticks++ < rainbow_ticks) {
return;
} else {
@ -21,14 +20,15 @@ LEDRainbowEffect::update (void) {
LEDControl.set_all_leds_to(rainbow);
}
LEDRainbowEffect_ LEDRainbowEffect;
// ---------
LEDRainbowWaveEffect::LEDRainbowWaveEffect (void) {
LEDControl.mode_add (this);
LEDRainbowWaveEffect_::LEDRainbowWaveEffect_ (void) {
}
void
LEDRainbowWaveEffect::update (void) {
LEDRainbowWaveEffect_::update (void) {
if (rainbow_current_ticks++ < rainbow_wave_ticks) {
return;
} else {
@ -48,3 +48,5 @@ LEDRainbowWaveEffect::update (void) {
rainbow_hue -= 255;
}
}
LEDRainbowWaveEffect_ LEDRainbowWaveEffect;

@ -3,9 +3,9 @@
#include "LEDControl.h"
#include "LEDUtils.h"
class LEDRainbowEffect : LEDMode {
class LEDRainbowEffect_ : LEDMode {
public:
LEDRainbowEffect (void);
LEDRainbowEffect_ (void);
virtual void update (void) final;
@ -21,9 +21,11 @@ class LEDRainbowEffect : LEDMode {
};
class LEDRainbowWaveEffect : LEDMode {
extern LEDRainbowEffect_ LEDRainbowEffect;
class LEDRainbowWaveEffect_ : LEDMode {
public:
LEDRainbowWaveEffect (void);
LEDRainbowWaveEffect_ (void);
virtual void update (void) final;
@ -37,3 +39,5 @@ class LEDRainbowWaveEffect : LEDMode {
static const byte rainbow_saturation = 255;
static const byte rainbow_value = 50;
};
extern LEDRainbowWaveEffect_ LEDRainbowWaveEffect;

@ -5,6 +5,11 @@ LEDMode::activate (void) {
LEDControl.activate (this);
}
void
LEDMode::begin(void) {
LEDControl.mode_add(this);
}
LEDControl_::LEDControl_(void) {
memset (modes, 0, LED_MAX_MODES * sizeof (modes[0]));
}

@ -2,11 +2,13 @@
#include <Arduino.h>
#include "KeyboardConfig.h"
#include "plugin.h"
#define LED_MAX_MODES 24
class LEDMode {
class LEDMode : public KeyboardioPlugin {
public:
virtual void begin (void);
virtual void setup (void) {};
virtual void init (void) {};
virtual void update (void) {};

@ -2,8 +2,6 @@
#include "TestMode.h"
#include "LED-RainbowEffect.h"
static LEDRainbowEffect testRainbowEffect;
cRGB red;
cRGB blue;
@ -47,7 +45,7 @@ void TestMode_::TestLEDs(void) {
delay(LED_TEST_DELAY);
// rainbow for 10 seconds
for(auto i=0; i<1000; i++ ) {
testRainbowEffect.update();
LEDRainbowEffect.update();
led_sync();
}
// set all the keys to red

@ -0,0 +1,7 @@
#pragma once
class KeyboardioPlugin {
public:
virtual void begin(void) = 0;
};
Loading…
Cancel
Save