Skip to content

Commit

Permalink
Merge pull request #14 from keyboardio/f/onFocusEvent
Browse files Browse the repository at this point in the history
Migrate to the onFocusEvent API
  • Loading branch information
obra committed Oct 5, 2018
2 parents 906f567 + 184981f commit 41fa914
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 101 deletions.
40 changes: 13 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,32 @@ overlay there. In the latter case, whenever there is a non-transparent key on
the overlay, we will use that instead of the keyboard default.

In short, this plugin allows us to change our keymaps, without having to compile
and flash new firmware. It does so through the use of the [Focus][plugin:focus]
plugin.
and flash new firmware. It does so through the use of the
[FocusSerial][plugin:focusSerial] plugin.

[plugin:focus]: https://github.com/keyboardio/Kaleidoscope-Focus
[plugin:focusSerial]: https://github.com/keyboardio/Kaleidoscope-FocusSerial

## Using the plugin

Using the plugin is reasonably simple: after including the header, enable the
plugin, and let the `Layer` object know that we'll be using `EEPROMKeymap` to
figure out which keys to use. We can either use the `getKeyOverride` or the
`getKey` method, depending on whether we want to override, or fully replace the
built-in keymap. Then we need to set at most how many layers we want to store in
`EEPROM`, and that is about it.
plugin, and configure how many layers at most we want to store in `EEPROM`.
There are other settings one can tweak, but these two steps are enough to get
started with.

We can then update the keymap via [Focus][plugin:focus].
Once these are set up, we can update the keymap via [Focus][plugin:focusSerial].

```c++
#include <Kaleidoscope.h>
#include <Kaleidoscope-EEPROM-Keymap.h>
#include <Kaleidoscope-Focus.h>
#include <Kaleidoscope-FocusSerial.h>

KALEIDOSCOPE_INIT_PLUGINS(EEPROMKeymap,
Focus);
Focus,
FocusKeymapTransferCommand);

void setup() {
Serial.begin(9600);

Kaleidoscope.setup();

Focus.addHook(FOCUS_HOOK_KEYMAP);
Focus.addHook(FOCUS_HOOK_KEYMAP_TRANSFER);

EEPROMKeymap.max_layers(1);
}
```
Expand All @@ -68,9 +62,8 @@ The plugin provides the `EEPROMKeymap` object, which has the following methods:
## Focus commands
The plugin provides three `Focus` hooks: `FOCUS_HOOK_KEYMAP`, `FOCUS_HOOK_KEYMAP_LAYER`,
and `FOCUS_HOOK_KEYMAP_TRANSFER`. Together, they make the following commands
available, respectively:
The plugin provides a `keymap.map` Focus command unconditionally, and a
`keymap.transfer` via the `FocusKeymapTransferCommand` object.
### `keymap.map [codes...]`
Expand All @@ -82,13 +75,6 @@ available, respectively:
> layer, and go on as long as it has input. It will not go past the layer set
> via the `.max_layers()` method.
### `keymap.layer LAYER [codes...]`
> Without codes, prints the keymap for the given layer (zero-indexed).
> Prints each key as its raw 16-bit keycode.
> With codes, stores them as the keymap for the given layer.
### `keymap.transfer LAYER`
> Transfers the `LAYER` from the built-in memory of the keyboard into `EEPROM`
Expand All @@ -103,7 +89,7 @@ available, respectively:
## Dependencies
* [Kaleidoscope-EEPROM-Settings](https://github.com/keyboardio/Kaleidoscope-EEPROM-Settings)
* [Kaleidoscope-Focus](https://github.com/keyboardio/Kaleidoscope-Focus)
* [Kaleidoscope-FocusSerial](https://github.com/keyboardio/Kaleidoscope-FocusSerial)
## Further reading
Expand Down
12 changes: 2 additions & 10 deletions examples/EEPROM-Keymap/EEPROM-Keymap.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <Kaleidoscope.h>
#include <Kaleidoscope-EEPROM-Keymap.h>
#include <Kaleidoscope-Focus.h>
#include <Kaleidoscope-FocusSerial.h>

// *INDENT-OFF*
KEYMAPS(
Expand All @@ -40,19 +40,11 @@ KEYMAPS(
)
// *INDENT-ON*

KALEIDOSCOPE_INIT_PLUGINS(EEPROMKeymap, Focus);
KALEIDOSCOPE_INIT_PLUGINS(EEPROMKeymap, Focus, FocusKeymapTransfer);

void setup() {
Serial.begin(9600);

Kaleidoscope.setup();

Focus.addHook(FOCUS_HOOK_SETTINGS);
Focus.addHook(FOCUS_HOOK_KEYMAP);
Focus.addHook(FOCUS_HOOK_KEYMAP_TRANSFER);
Focus.addHook(FOCUS_HOOK_HELP);
Focus.addHook(FOCUS_HOOK_VERSION);

EEPROMKeymap.max_layers(1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Kaleidoscope-EEPROM-Keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
#pragma once

#include <Kaleidoscope/EEPROM-Keymap.h>
#include <Kaleidoscope/EEPROM-Keymap-Focus.h>
#include <Kaleidoscope/EEPROM-Keymap-Transfer.h>
50 changes: 50 additions & 0 deletions src/Kaleidoscope/EEPROM-Keymap-Transfer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* -*- mode: c++ -*-
* Kaleidoscope-EEPROM-Keymap -- EEPROM-based keymap support.
* Copyright (C) 2017, 2018 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-EEPROM-Settings.h>
#include <Kaleidoscope-EEPROM-Keymap.h>
#include <Kaleidoscope-FocusSerial.h>

namespace kaleidoscope {
namespace eeprom {

EventHandlerResult FocusKeymapTransferCommand::onFocusEvent(const char *command) {
const char *cmd = PSTR("keymap.transfer");

if (::Focus.handleHelp(command, cmd))
return EventHandlerResult::OK;
if (strcmp_P(command, cmd) != 0)
return EventHandlerResult::OK;

uint8_t layer = Serial.parseInt();

for (uint8_t row = 0; row < ROWS; row++) {
for (uint8_t col = 0; col < COLS; col++) {
Key k = Layer.getKeyFromPROGMEM(layer, row, col);
uint16_t pos = ((layer * ROWS * COLS) + (row * COLS) + col);

::EEPROMKeymap.updateKey(pos, k);
}
}

return EventHandlerResult::EVENT_CONSUMED;
}

}
}

kaleidoscope::eeprom::FocusKeymapTransferCommand FocusKeymapTransferCommand;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- mode: c++ -*-
* Kaleidoscope-EEPROM-Keymap -- EEPROM-based keymap support.
* Copyright (C) 2017 Keyboard.io, Inc
* Copyright (C) 2017-2018 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
Expand All @@ -20,10 +20,15 @@
#include <Kaleidoscope.h>
#include <Kaleidoscope/EEPROM-Keymap.h>

#define FOCUS_HOOK_KEYMAP FOCUS_HOOK(EEPROMKeymap.focusKeymap, "keymap.map")
namespace kaleidoscope {
namespace eeprom {
class FocusKeymapTransferCommand : public Plugin {
public:
FocusKeymapTransferCommand() {}

#define FOCUS_HOOK_KEYMAP_LAYER FOCUS_HOOK(EEPROMKeymap.focusKeymapLayer, \
"keymap.layer")
EventHandlerResult onFocusEvent(const char *command);
};
}
}

#define FOCUS_HOOK_KEYMAP_TRANSFER FOCUS_HOOK(EEPROMKeymap.focusKeymapTransfer, \
"keymap.transfer")
extern kaleidoscope::eeprom::FocusKeymapTransferCommand FocusKeymapTransferCommand;
62 changes: 9 additions & 53 deletions src/Kaleidoscope/EEPROM-Keymap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <Kaleidoscope-EEPROM-Settings.h>
#include <Kaleidoscope-EEPROM-Keymap.h>
#include <Kaleidoscope-Focus.h>
#include <Kaleidoscope-FocusSerial.h>

namespace kaleidoscope {
uint16_t EEPROMKeymap::keymap_base_;
Expand Down Expand Up @@ -88,9 +88,13 @@ void EEPROMKeymap::printKey(Key k) {
::Focus.printNumber(k.raw);
}

bool EEPROMKeymap::focusKeymap(const char *command) {
if (strcmp_P(command, PSTR("keymap.map")) != 0)
return false;
EventHandlerResult EEPROMKeymap::onFocusEvent(const char *command) {
const char *cmd = PSTR("keymap.map");
if (::Focus.handleHelp(command, cmd))
return EventHandlerResult::OK;

if (strcmp_P(command, cmd) != 0)
return EventHandlerResult::OK;

if (Serial.peek() == '\n') {
for (uint8_t layer = 0; layer < max_layers_; layer++) {
Expand All @@ -112,55 +116,7 @@ bool EEPROMKeymap::focusKeymap(const char *command) {
}
}

return true;
}

bool EEPROMKeymap::focusKeymapLayer(const char *command) {
if (strcmp_P(command, PSTR("keymap.layer")) != 0) {
return false;
}

uint8_t layer = Serial.parseInt();
if (layer >= max_layers_) {
return false;
}
if (Serial.peek() == '\n') {
for (uint8_t row = 0; row < ROWS; row++) {
for (uint8_t col = 0; col < COLS; col++) {
Key k = Layer.getKey(layer, row, col);
printKey(k);
::Focus.printSpace();
}
}
Serial.println();
} else {
uint16_t keysPerLayer = ROWS * COLS;
uint16_t offset = layer * keysPerLayer;
for (uint16_t k = 0; (k < keysPerLayer) && (Serial.peek() != '\n'); k++) {
updateKey(offset + k, parseKey());
}
}

return true;

}

bool EEPROMKeymap::focusKeymapTransfer(const char *command) {
if (strcmp_P(command, PSTR("keymap.transfer")) != 0)
return false;

uint8_t layer = Serial.parseInt();

for (uint8_t row = 0; row < ROWS; row++) {
for (uint8_t col = 0; col < COLS; col++) {
Key k = Layer.getKeyFromPROGMEM(layer, row, col);
uint16_t pos = ((layer * ROWS * COLS) + (row * COLS) + col);

updateKey(pos, k);
}
}

return true;
return EventHandlerResult::EVENT_CONSUMED;
}

}
Expand Down
5 changes: 1 addition & 4 deletions src/Kaleidoscope/EEPROM-Keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class EEPROMKeymap : public kaleidoscope::Plugin {
EEPROMKeymap(void) {}

EventHandlerResult onSetup();
EventHandlerResult onFocusEvent(const char *command);

static void max_layers(uint8_t max);

Expand All @@ -34,10 +35,6 @@ class EEPROMKeymap : public kaleidoscope::Plugin {
static Key getKey(uint8_t layer, byte row, byte col);
static Key getKeyOverride(uint8_t layer, byte row, byte col);

static bool focusKeymap(const char *command);
static bool focusKeymapLayer(const char *command);
static bool focusKeymapTransfer(const char *command);

static void updateKey(uint16_t base_pos, Key key);

private:
Expand Down

0 comments on commit 41fa914

Please sign in to comment.