From c060c5ef7e76f5bce5524d66921a1179724b260d Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Wed, 1 Nov 2017 12:05:54 -0500 Subject: [PATCH] Rearranged and commented Layer.on() and Layer.off() The boolean wasOn was unnecessary, and there was no need to call bitSet() (or bitClear(), in the case of Layer.off()) if the test passed. Mostly, I just added a few explanatory comments. (Aslo reversed the sense of the on/off test in Layer.on() and .off()) @algernon likes it better this way, and I agree. --- src/layers.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/layers.cpp b/src/layers.cpp index 27d34e88..4f868832 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -136,30 +136,40 @@ void Layer_::on(uint8_t layer) { if (layer_count != 0 && layer >= layer_count) return; - bool wasOn = isOn(layer); + // If the target layer was already on, return + if (isOn(layer)) + return; + // Otherwise, turn on its bit in LayerState bitSet(LayerState, layer); + + // If the target layer is above the previous highest active layer, + // update highestLayer if (layer > highestLayer) updateHighestLayer(); - /* If the layer did turn on, update the keymap cache. See layers.h for an - * explanation about the caches we have. */ - if (!wasOn) - updateActiveLayers(); + // Update the keymap cache (but not liveCompositeKeymap; that gets + // updated separately, when keys toggle on or off. See layers.h) + updateActiveLayers(); } // Deactivate a given layer void Layer_::off(uint8_t layer) { - bool wasOn = isOn(layer); + // If the target layer was already off, return + if (!bitRead(LayerState, layer)) + return; + // Turn off its bit in LayerState bitClear(LayerState, layer); + + // If the target layer was the previous highest active layer, + // update highestLayer if (layer == highestLayer) updateHighestLayer(); - /* If the layer did turn off, update the keymap cache. See layers.h for an - * explanation about the caches we have. */ - if (wasOn) - updateActiveLayers(); + // Update the keymap cache (but not liveCompositeKeymap; that gets + // updated separately, when keys toggle on or off. See layers.h) + updateActiveLayers(); } boolean Layer_::isOn(uint8_t layer) {