Replace range-based for loop with std::fill
Use standard algorithm std::fill instead of manual loop to clear bleKeyMap array. This is more idiomatic C++ and expresses intent more clearly.
This commit is contained in:
+1
-1
@@ -77,7 +77,7 @@ const char* specialName(uint8_t value) {
|
|||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void showConnectingUntilLinked(GfxRenderer& renderer, MappedInputManager& input) {
|
void showConnectingUntilLinked(const GfxRenderer& renderer, const MappedInputManager& input) {
|
||||||
if (!BleHid.isRunning() || BleHid.isConnected()) return;
|
if (!BleHid.isRunning() || BleHid.isConnected()) return;
|
||||||
// drawPopup refreshes the panel itself, so draw once and let e-ink hold it while we
|
// 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
|
// pump the host. Holds until the remote links, the user presses a button to bail, or
|
||||||
|
|||||||
+1
-1
@@ -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
|
// 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
|
// 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.
|
// 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
|
} // namespace bleinput
|
||||||
|
|||||||
@@ -5,7 +5,9 @@
|
|||||||
#include <Logging.h>
|
#include <Logging.h>
|
||||||
#include <ObfuscationUtils.h>
|
#include <ObfuscationUtils.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <iterator>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "BookmarkEntry.h"
|
#include "BookmarkEntry.h"
|
||||||
@@ -256,7 +258,7 @@ bool JsonSettingsIO::loadSettings(CrossPointSettings& s, const char* json, bool*
|
|||||||
|
|
||||||
// Bluetooth — managed by BluetoothSettingsActivity, not in SettingsList.
|
// Bluetooth — managed by BluetoothSettingsActivity, not in SettingsList.
|
||||||
s.bluetoothEnabled = clamp(doc["bluetoothEnabled"] | (uint8_t)0, 2, 0);
|
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"];
|
JsonArrayConst bleMap = doc["bleKeyMap"];
|
||||||
if (!bleMap.isNull()) {
|
if (!bleMap.isNull()) {
|
||||||
uint8_t slot = 0;
|
uint8_t slot = 0;
|
||||||
|
|||||||
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
#include <GfxRenderer.h>
|
#include <GfxRenderer.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
#include "BleInput.h"
|
#include "BleInput.h"
|
||||||
#include "CrossPointSettings.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
|
// 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
|
// button once, so a button can't be left bound to a stale action and there's no
|
||||||
// separate "clear mappings" step to remember.
|
// 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();
|
SETTINGS.saveToFile();
|
||||||
mappedInput.setBleCaptureMode(true);
|
mappedInput.setBleCaptureMode(true);
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
@@ -44,32 +46,32 @@ void BleButtonMapActivity::onExit() {
|
|||||||
|
|
||||||
bool BleButtonMapActivity::assignCapturedKey(MappedInputManager::Button button) {
|
bool BleButtonMapActivity::assignCapturedKey(MappedInputManager::Button button) {
|
||||||
const uint8_t btn = static_cast<uint8_t>(button);
|
const uint8_t btn = static_cast<uint8_t>(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
|
// 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.
|
// action can't be triggered by two different remote buttons.
|
||||||
for (auto& e : SETTINGS.bleKeyMap) {
|
std::replace_if(
|
||||||
if (e.button == btn && !(e.keyKind == capturedKind && e.keyValue == capturedValue)) {
|
std::begin(map), std::end(map),
|
||||||
e = CrossPointSettings::BleKeyMapEntry{};
|
[&](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.
|
if (slot == std::end(map)) return false; // table full
|
||||||
for (auto& e : SETTINGS.bleKeyMap) {
|
|
||||||
if (e.button != 0xFF && e.keyKind == capturedKind && e.keyValue == capturedValue) {
|
slot->keyKind = kind;
|
||||||
e.button = btn;
|
slot->keyValue = value;
|
||||||
SETTINGS.saveToFile();
|
slot->button = btn;
|
||||||
return true;
|
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BleButtonMapActivity::loop() {
|
void BleButtonMapActivity::loop() {
|
||||||
|
|||||||
Reference in New Issue
Block a user