Add testcase for the active keys cache

This testcase checks to make sure that the active keys cache gets cleared by
`handleKeyswitchEvent()` when a plugin returns `EVENT_CONSUMED`.

Signed-off-by: Michael Richters <gedankenexperimenter@gmail.com>
f/keymap-cache-redesign
Michael Richters 4 years ago committed by Jesse Vincent
parent 4fdff6c9ec
commit b0c9c18004
No known key found for this signature in database
GPG Key ID: 122F5DF7108E4046

@ -0,0 +1,27 @@
// -*- mode: c++ -*-
/* Kaleidoscope - Firmware for computer input devices
* Copyright (C) 2020 Keyboard.io, Inc.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdint>
namespace kaleidoscope {
namespace testing {
} // namespace testing
} // namespace kaleidoscope

@ -0,0 +1,104 @@
/* -*- mode: c++ -*-
* Copyright (C) 2020 Keyboard.io, Inc.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Kaleidoscope.h>
#include "./common.h"
#undef min
#undef max
#include <iostream>
// *INDENT-OFF*
KEYMAPS(
[0] = KEYMAP_STACKED
(
Key_A, ___, ___, ___, ___, ___, ___,
ShiftToLayer(1), ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___,
___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___,
___
),
[1] = KEYMAP_STACKED
(
Key_X, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___,
___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___,
___
),
)
// *INDENT-ON*
namespace kaleidoscope {
namespace plugin {
class ConvertXtoY : public kaleidoscope::Plugin {
public:
EventHandlerResult onKeyswitchEvent(Key &key, KeyAddr key_addr, uint8_t key_state) {
if (keyToggledOn(key_state)) {
if (key == Key_X)
key = Key_Y;
}
// It should be impossible to get a `Key_X` at this point, because the
// previous block changes any `Key_X` to `Key_Y`, which results in the
// active keys cache storing that value. On subsequent cycles (while the key
// is still pressed), the `key` value should be `Key_Y`.
if (keyIsPressed(key_state) && key == Key_X) {
std::cerr << "t=" << Runtime.millisAtCycleStart() << ": "
<< "Error: we shouldn't see a key with value `X`" << std::endl;
}
// When `Key_Y` toggles off, return `EVENT_CONSUMED`. Despite this, the
// active keys cache entry should be cleared. If it's not, then a subsequent
// press of the same key without the layer shift in effect will still result
// in `Key_Y` instead of `Key_A`.
if (keyToggledOff(key_state) && (key == Key_Y)) {
return EventHandlerResult::EVENT_CONSUMED;
}
return EventHandlerResult::OK;
}
};
} // namespace plugin
} // namespace kaleidoscope
kaleidoscope::plugin::ConvertXtoY ConvertXtoY;
KALEIDOSCOPE_INIT_PLUGINS(ConvertXtoY);
void setup() {
Kaleidoscope.setup();
}
void loop() {
Kaleidoscope.loop();
}

@ -0,0 +1,6 @@
{
"cpu": {
"fqbn": "keyboardio:virtual:model01",
"port": ""
}
}

@ -0,0 +1,59 @@
VERSION 1
KEYSWITCH A 0 0
KEYSWITCH LAYER_SHIFT 1 0
# ==============================================================================
# Active Keys Cache
NAME Active keys cache cleared
RUN 10 ms
PRESS A
RUN 1 cycle
EXPECT keyboard-report Key_A # Report should contain only `A`
RUN 5 ms
RELEASE A
RUN 1 cycle
EXPECT keyboard-report empty # Report should be empty
RUN 5 ms
# Press and hold `ShiftToLayer(1)`, changing the `A` key to `X`
PRESS LAYER_SHIFT
RUN 5 ms
# Press `X`, which gets converted to `Y` by the `ConvertXtoY` plugin defined in
# the sketch. The plugin simply changes the value of the key, which causes it to
# get set to `Key_Y` in the active keys cache.
PRESS A
RUN 1 cycle
EXPECT keyboard-report Key_Y # Report should contain only `Y`
RUN 5 ms
# Release the `X` key (on Layer 1), which has become a `Y` key in the active
# keys cache. This should clear its entry in that cache.
RELEASE A
RUN 1 cycle
EXPECT keyboard-report empty # Report should be empty
RUN 5 ms
# Release `ShiftToLayer(1)`. This should restore the `A` key to its base layer
# value on subsequent presses, unless the Macros key gets "stuck" in the active
# keys array because it returns `EVENT_CONSUMED`.
RELEASE LAYER_SHIFT
RUN 5 ms
PRESS A
RUN 1 cycle
EXPECT keyboard-report Key_A # Report should contain only `A`
RUN 5 ms
RELEASE A
RUN 1 cycle
EXPECT keyboard-report empty # Report should be empty
Loading…
Cancel
Save