This introduces a new plugin - `FlashHelper` - to aid with firmware-assisted flashing. During the flashing process, this plugin can temporarily disable the `Prog` key. Addresses the firmware part of keyboardio/Chrysalis#509. Signed-off-by: Gergely Nagy <algernon@keyboard.io>pull/837/head
parent
c097a21bfd
commit
c917acb8a1
@ -0,0 +1,71 @@
|
|||||||
|
# FlashHelper
|
||||||
|
|
||||||
|
A number of keyboards supported by Kaleidoscope need a key held during reset to
|
||||||
|
enter bootloader mode. While held, any action mapped to the key will take
|
||||||
|
effect, which is something we may want to avoid in this special case. This
|
||||||
|
plugin is here to help with that: when asked via [Focus](FocusSerial.md), it
|
||||||
|
will ignore any events on the `Prog` key for a short while (or until asked to
|
||||||
|
resume).
|
||||||
|
|
||||||
|
## Using the plugin
|
||||||
|
|
||||||
|
Using the plugin is simple: after including the header, enable the plugin
|
||||||
|
(preferably before enabling any other plugin that handles key events).
|
||||||
|
|
||||||
|
We also need `Focus` enabled, to take full advantage of the plugin.
|
||||||
|
|
||||||
|
```c++
|
||||||
|
#include <Kaleidoscope.h>
|
||||||
|
#include <Kaleidoscope-FocusSerial.h>
|
||||||
|
#include <Kaleidoscope-FlashHelper.h>
|
||||||
|
|
||||||
|
KALEIDOSCOPE_INIT_PLUGINS(Focus, FlashHelper);
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Kaleidoscope.setup();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The plugin relies on the device descriptor having set a `Prog` key.
|
||||||
|
|
||||||
|
## Plugin methods
|
||||||
|
|
||||||
|
The plugin provides the `FlashHelper` object, which has the following methods:
|
||||||
|
|
||||||
|
### `.setTimeout(timeout)`
|
||||||
|
### `.getTimeout()`
|
||||||
|
|
||||||
|
Set or get the time (in seconds) to wait before re-enabling the Prog key, after
|
||||||
|
the helper has been activated.
|
||||||
|
|
||||||
|
The timer defaults to **10 seconds**.
|
||||||
|
|
||||||
|
### `.activate()`
|
||||||
|
### `.deactivate()`
|
||||||
|
### `.isActive()`
|
||||||
|
|
||||||
|
Activate, deactivate, or check the status of the flashing helper, respectively.
|
||||||
|
|
||||||
|
## Focus commands
|
||||||
|
|
||||||
|
The plugin provides two Focus commands: `flash.prepare` and `flash.resume`.
|
||||||
|
|
||||||
|
### `flash.prepare`
|
||||||
|
|
||||||
|
Prepare for flashing, by disabling the previously configured `Prog` key for a
|
||||||
|
few seconds, or until explicitly asked to resume. If the keyboard resets
|
||||||
|
meanwhile, then preparation is automatically concluded.
|
||||||
|
|
||||||
|
### `flash.resume`
|
||||||
|
|
||||||
|
Resume normal key event handling, and deactivate the helper plugin.
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
* [Kaleidoscope-FocusSerial](FocusSerial.md)
|
||||||
|
|
||||||
|
## Further reading
|
||||||
|
|
||||||
|
Starting from the [example][plugin:example] is the recommended way of getting started with the plugin.
|
||||||
|
|
||||||
|
[plugin:example]: ../../examples/Features/FlashHelper/FlashHelper.ino
|
@ -0,0 +1,52 @@
|
|||||||
|
/* -*- mode: c++ -*-
|
||||||
|
* Kaleidoscope-FlashHelper -- Firmware-assisted flashing
|
||||||
|
* 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 <Kaleidoscope-FocusSerial.h>
|
||||||
|
#include <Kaleidoscope-FlashHelper.h>
|
||||||
|
|
||||||
|
// *INDENT-OFF*
|
||||||
|
KEYMAPS(
|
||||||
|
[0] = KEYMAP_STACKED
|
||||||
|
(
|
||||||
|
Key_0, Key_1, Key_2, Key_3, Key_4, Key_5, Key_NoKey,
|
||||||
|
Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab,
|
||||||
|
Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G,
|
||||||
|
Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape,
|
||||||
|
|
||||||
|
Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift,
|
||||||
|
Key_skip,
|
||||||
|
|
||||||
|
Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip,
|
||||||
|
Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals,
|
||||||
|
Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote,
|
||||||
|
Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus,
|
||||||
|
|
||||||
|
Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl,
|
||||||
|
Key_skip),
|
||||||
|
)
|
||||||
|
// *INDENT-ON*
|
||||||
|
|
||||||
|
KALEIDOSCOPE_INIT_PLUGINS(Focus, FlashHelper);
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Kaleidoscope.setup();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
Kaleidoscope.loop();
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
# This stub makefile for a Kaleidoscope example pulls in all the targets
|
||||||
|
# required to build the example
|
||||||
|
|
||||||
|
UNAME_S := $(shell uname -s)
|
||||||
|
|
||||||
|
ifeq ($(UNAME_S),Darwin)
|
||||||
|
SKETCHBOOK_DIR ?= $(HOME)/Documents/Arduino
|
||||||
|
PACKAGE_DIR ?= $(HOME)/Library/Arduino15
|
||||||
|
else
|
||||||
|
SKETCHBOOK_DIR ?= $(HOME)/Arduino
|
||||||
|
PACKAGE_DIR ?= $(HOME)/.arduino15
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
ARDUINO_INSTALLED_ENV=$(shell ls -dt $(PACKAGE_DIR)/packages/keyboardio/hardware/avr 2>/dev/null |head -n 1)
|
||||||
|
MANUALLY_INSTALLED_ENV=$(shell ls -dt $(SKETCHBOOK_DIR)/hardware/keyboardio/avr 2>/dev/null |head -n 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ifneq ("$(wildcard $(ARDUINO_INSTALLED_ENV)/boards.txt)","")
|
||||||
|
|
||||||
|
ifneq ("$(wildcard $(MANUALLY_INSTALLED_ENV)/boards.txt)","")
|
||||||
|
|
||||||
|
$(info ***************************************************************************)
|
||||||
|
$(info It appears that you have installed two copies of Kaleidoscope. One copy was)
|
||||||
|
$(info installed using Arduino's "Board Manager", while the other was installed by)
|
||||||
|
$(info hand, probably using "git".)
|
||||||
|
$(info )
|
||||||
|
$(info This will likely cause some trouble as you try to build keyboard firmware)
|
||||||
|
$(info using Kaleidoscope. You may want to remove either: )
|
||||||
|
$(info )
|
||||||
|
$(info $(PACKAGE_DIR)/packages/keyboardio/ which was installed using Arduino)
|
||||||
|
$(info )
|
||||||
|
$(info or)
|
||||||
|
$(info )
|
||||||
|
$(info $(SKETCHBOOK_DIR)/hardware/keyboardio/ which was installed by hand.)
|
||||||
|
$(info )
|
||||||
|
$(info ***************************************************************************)
|
||||||
|
$(info )
|
||||||
|
|
||||||
|
endif
|
||||||
|
|
||||||
|
BOARD_HARDWARE_PATH = $(ARDUINO_INSTALLED_ENV)
|
||||||
|
KALEIDOSCOPE_PLUGIN_MAKEFILE_DIR ?= build-tools/makefiles/
|
||||||
|
KALEIDOSCOPE_BUILDER_DIR ?= $(ARDUINO_INSTALLED_ENV)/libraries/Kaleidoscope/bin/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
BOARD_HARDWARE_PATH ?= $(SKETCHBOOK_DIR)/hardware
|
||||||
|
KALEIDOSCOPE_PLUGIN_MAKEFILE_DIR ?= keyboardio/avr/build-tools/makefiles/
|
||||||
|
|
||||||
|
include $(BOARD_HARDWARE_PATH)/$(KALEIDOSCOPE_PLUGIN_MAKEFILE_DIR)/rules.mk
|
@ -0,0 +1,20 @@
|
|||||||
|
/* -*- mode: c++ -*-
|
||||||
|
* Kaleidoscope-FlashHelper -- Firmware-assisted flashing
|
||||||
|
* 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 <kaleidoscope/plugin/FlashHelper.h>
|
@ -0,0 +1,82 @@
|
|||||||
|
/* -*- mode: c++ -*-
|
||||||
|
* Kaleidoscope-FlashHelper -- Firmware-assisted flashing
|
||||||
|
* 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-FlashHelper.h>
|
||||||
|
#include <Kaleidoscope-FocusSerial.h>
|
||||||
|
|
||||||
|
#include "kaleidoscope/key_events.h"
|
||||||
|
|
||||||
|
namespace kaleidoscope {
|
||||||
|
namespace plugin {
|
||||||
|
|
||||||
|
uint8_t FlashHelper::timeout_ = 10;
|
||||||
|
uint32_t FlashHelper::start_time_;
|
||||||
|
bool FlashHelper::is_active_;
|
||||||
|
|
||||||
|
EventHandlerResult FlashHelper::onFocusEvent(const char *command) {
|
||||||
|
if (::Focus.handleHelp(command, PSTR("flash.prepare\nflash.resume")))
|
||||||
|
return EventHandlerResult::OK;
|
||||||
|
|
||||||
|
if (strncmp_P(command, PSTR("flash."), 6) != 0)
|
||||||
|
return EventHandlerResult::OK;
|
||||||
|
|
||||||
|
if (strcmp_P(command + 6, PSTR("prepare")) == 0) {
|
||||||
|
activate();
|
||||||
|
return EventHandlerResult::EVENT_CONSUMED;
|
||||||
|
}
|
||||||
|
if (strcmp_P(command + 6, PSTR("resume")) == 0) {
|
||||||
|
deactivate();
|
||||||
|
return EventHandlerResult::EVENT_CONSUMED;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EventHandlerResult::OK;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
EventHandlerResult FlashHelper::beforeEachCycle() {
|
||||||
|
if (!isActive())
|
||||||
|
goto end;
|
||||||
|
|
||||||
|
if (!Runtime.hasTimeExpired(start_time_, (uint16_t)(timeout_ * 1000)))
|
||||||
|
goto end;
|
||||||
|
|
||||||
|
deactivate();
|
||||||
|
|
||||||
|
end:
|
||||||
|
return EventHandlerResult::OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
EventHandlerResult FlashHelper::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state) {
|
||||||
|
if (!isActive())
|
||||||
|
return EventHandlerResult::OK;
|
||||||
|
|
||||||
|
const auto progKeys = Runtime.device().progKeyAddresses();
|
||||||
|
const uint8_t progKeyNum = Runtime.device().numProgKeys();
|
||||||
|
|
||||||
|
for (int8_t i = 0; i < progKeyNum; i++) {
|
||||||
|
if (key_addr.toInt() == progKeys[i]) {
|
||||||
|
return EventHandlerResult::EVENT_CONSUMED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EventHandlerResult::OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
kaleidoscope::plugin::FlashHelper FlashHelper;
|
@ -0,0 +1,60 @@
|
|||||||
|
/* -*- mode: c++ -*-
|
||||||
|
* Kaleidoscope-FlashHelper -- Firmware-assisted flashing
|
||||||
|
* 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 "kaleidoscope/Runtime.h"
|
||||||
|
|
||||||
|
namespace kaleidoscope {
|
||||||
|
namespace plugin {
|
||||||
|
|
||||||
|
class FlashHelper: public kaleidoscope::Plugin {
|
||||||
|
public:
|
||||||
|
FlashHelper() {}
|
||||||
|
|
||||||
|
static void setTimeout(uint8_t timeout) {
|
||||||
|
timeout_ = timeout;
|
||||||
|
}
|
||||||
|
static uint8_t getTimeout() {
|
||||||
|
return timeout_;
|
||||||
|
}
|
||||||
|
static void activate() {
|
||||||
|
is_active_ = true;
|
||||||
|
start_time_ = Runtime.millisAtCycleStart();
|
||||||
|
}
|
||||||
|
static void deactivate() {
|
||||||
|
is_active_ = false;
|
||||||
|
}
|
||||||
|
static bool isActive() {
|
||||||
|
return is_active_;
|
||||||
|
}
|
||||||
|
|
||||||
|
EventHandlerResult onFocusEvent(const char *command);
|
||||||
|
EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state);
|
||||||
|
EventHandlerResult beforeEachCycle();
|
||||||
|
|
||||||
|
private:
|
||||||
|
static bool is_active_;
|
||||||
|
static uint8_t timeout_;
|
||||||
|
|
||||||
|
static uint32_t start_time_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extern kaleidoscope::plugin::FlashHelper FlashHelper;
|
Loading…
Reference in new issue