Make updateHighestLayer() use layer_count if it's set

Now that `layer_count` is (potentially) available, we can start
looking for active layers at the top _defined_ layer instead of the
top _possible_ layer. This ought to be more efficient, especially for
sketches that don't have lots of layers defined.

Also introduced the `MAX_LAYERS` constant (#define).
pull/254/head
Michael Richters 7 years ago
parent bd35b9fe4e
commit 00df66d30a

@ -9,9 +9,9 @@ uint8_t Layer_::activeLayers[ROWS][COLS];
Key(*Layer_::getKey)(uint8_t layer, byte row, byte col) = Layer.getKeyFromPROGMEM;
// The total number of defined layers in the firmware sketch keymaps[]
// array. If the keymap wasn't defined using CREATE_KEYMAP() in the
// sketch file, layer_count gets the default value of zero.
uint8_t layer_count __attribute__((weak)) = 0;
// array. If the keymap wasn't defined using KEYMAPS(), set it to the
// highest possible number of layers.
uint8_t layer_count __attribute__((weak)) = MAX_LAYERS;
static void handleKeymapKeyswitchEvent(Key keymapEntry, uint8_t keyState) {
if (keymapEntry.keyCode >= LAYER_SHIFT_OFFSET) {
@ -115,12 +115,15 @@ Layer_::updateActiveLayers(void) {
}
void Layer_::updateHighestLayer(void) {
for (int8_t i = 31; i >= 0; i--) {
// If layer_count is set, start there, otherwise search from the
// highest possible layer for the top active layer
for (int8_t i = (layer_count - 1); i > 0; i++) {
if (bitRead(LayerState, i)) {
highestLayer = i;
return;
}
}
// return 0 if no higher active layers were found
highestLayer = 0;
}
@ -133,7 +136,7 @@ void Layer_::move(uint8_t layer) {
void Layer_::on(uint8_t layer) {
// If we're trying to turn on a layer that doesn't exist, abort (but
// if the keymap wasn't defined using the KEYMAPS() macro, proceed anyway
if (layer_count != 0 && layer >= layer_count)
if (layer >= layer_count)
return;
bool wasOn = isOn(layer);

@ -11,6 +11,10 @@
const Key keymaps[][ROWS][COLS] PROGMEM = { layers }; \
uint8_t layer_count = sizeof(keymaps) / sizeof(*keymaps);
// The maximum number of layers allowed. `LayerState`, which stores
// the on/off status of the layers in a bitfield has only 32 bits, and
// that should be enough for almost any layout.
#define MAX_LAYERS 32
class Layer_ {
public:

Loading…
Cancel
Save