diff --git a/src/BleInput.cpp b/src/BleInput.cpp index 8fdf4bc6..eaa1e5a9 100644 --- a/src/BleInput.cpp +++ b/src/BleInput.cpp @@ -77,7 +77,7 @@ const char* specialName(uint8_t value) { } } // namespace -void showConnectingUntilLinked(GfxRenderer& renderer, MappedInputManager& input) { +void showConnectingUntilLinked(const GfxRenderer& renderer, const MappedInputManager& input) { if (!BleHid.isRunning() || BleHid.isConnected()) return; // drawPopup refreshes the panel itself, so draw once and let e-ink hold it while we // pump the host. Holds until the remote links, the user presses a button to bail, or diff --git a/src/BleInput.h b/src/BleInput.h index f0bba35a..a46b1e56 100644 --- a/src/BleInput.h +++ b/src/BleInput.h @@ -44,6 +44,6 @@ void describeKey(uint8_t kind, uint8_t value, char* out, size_t outLen); // Draw a "BT Connecting..." popup and pump the BLE host until the bonded remote // links, the user presses a button to dismiss, or a timeout. No-op if BLE isn't // running or is already connected. The caller must redraw afterward to clear it. -void showConnectingUntilLinked(GfxRenderer& renderer, MappedInputManager& input); +void showConnectingUntilLinked(const GfxRenderer& renderer, const MappedInputManager& input); } // namespace bleinput diff --git a/src/JsonSettingsIO.cpp b/src/JsonSettingsIO.cpp index 233d4bb0..14b9b4ec 100644 --- a/src/JsonSettingsIO.cpp +++ b/src/JsonSettingsIO.cpp @@ -5,7 +5,9 @@ #include #include +#include #include +#include #include #include "BookmarkEntry.h" @@ -256,7 +258,7 @@ bool JsonSettingsIO::loadSettings(CrossPointSettings& s, const char* json, bool* // Bluetooth — managed by BluetoothSettingsActivity, not in SettingsList. s.bluetoothEnabled = clamp(doc["bluetoothEnabled"] | (uint8_t)0, 2, 0); - for (auto& e : s.bleKeyMap) e = CrossPointSettings::BleKeyMapEntry{}; // reset to empty + std::fill(std::begin(s.bleKeyMap), std::end(s.bleKeyMap), CrossPointSettings::BleKeyMapEntry{}); // reset to empty JsonArrayConst bleMap = doc["bleKeyMap"]; if (!bleMap.isNull()) { uint8_t slot = 0; diff --git a/src/activities/settings/BleButtonMapActivity.cpp b/src/activities/settings/BleButtonMapActivity.cpp index e094c05c..5f34bdee 100644 --- a/src/activities/settings/BleButtonMapActivity.cpp +++ b/src/activities/settings/BleButtonMapActivity.cpp @@ -2,7 +2,9 @@ #include +#include #include +#include #include "BleInput.h" #include "CrossPointSettings.h" @@ -31,7 +33,7 @@ void BleButtonMapActivity::onEnter() { // Start every mapping session from a clean slate: the user re-maps each remote // button once, so a button can't be left bound to a stale action and there's no // separate "clear mappings" step to remember. - for (auto& e : SETTINGS.bleKeyMap) e = CrossPointSettings::BleKeyMapEntry{}; + std::fill(std::begin(SETTINGS.bleKeyMap), std::end(SETTINGS.bleKeyMap), CrossPointSettings::BleKeyMapEntry{}); SETTINGS.saveToFile(); mappedInput.setBleCaptureMode(true); requestUpdate(); @@ -44,32 +46,32 @@ void BleButtonMapActivity::onExit() { bool BleButtonMapActivity::assignCapturedKey(MappedInputManager::Button button) { const uint8_t btn = static_cast(button); + auto& map = SETTINGS.bleKeyMap; + using Entry = CrossPointSettings::BleKeyMapEntry; + const uint8_t kind = capturedKind; + const uint8_t value = capturedValue; + // One key per action: drop any other key currently bound to this action so the same // action can't be triggered by two different remote buttons. - for (auto& e : SETTINGS.bleKeyMap) { - if (e.button == btn && !(e.keyKind == capturedKind && e.keyValue == capturedValue)) { - e = CrossPointSettings::BleKeyMapEntry{}; - } + std::replace_if( + std::begin(map), std::end(map), + [&](const Entry& e) { return e.button == btn && !(e.keyKind == kind && e.keyValue == value); }, Entry{}); + + // Reuse the slot already bound to this key, else the first free slot. + auto* slot = std::find_if(std::begin(map), std::end(map), [&](const Entry& e) { + return e.button != 0xFF && e.keyKind == kind && e.keyValue == value; + }); + if (slot == std::end(map)) { + slot = std::find_if(std::begin(map), std::end(map), + [](const Entry& e) { return e.button == 0xFF || e.keyKind == 0xFF; }); } - // Update an existing binding for this key, if present. - for (auto& e : SETTINGS.bleKeyMap) { - if (e.button != 0xFF && e.keyKind == capturedKind && e.keyValue == capturedValue) { - e.button = btn; - SETTINGS.saveToFile(); - return true; - } - } - // Otherwise take a free slot. - for (auto& e : SETTINGS.bleKeyMap) { - if (e.button == 0xFF || e.keyKind == 0xFF) { - e.keyKind = capturedKind; - e.keyValue = capturedValue; - e.button = btn; - SETTINGS.saveToFile(); - return true; - } - } - return false; // table full + if (slot == std::end(map)) return false; // table full + + slot->keyKind = kind; + slot->keyValue = value; + slot->button = btn; + SETTINGS.saveToFile(); + return true; } void BleButtonMapActivity::loop() {