mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-04 14:47:37 +03:00
Some checks failed
CI / build (push) Has been cancelled
## Summary Extracted some changes from https://github.com/crosspoint-reader/crosspoint-reader/pull/500 to make reviewing easier This PR adds HAL (Hardware Abstraction Layer) for display and GPIO components, making it easier to write a stub or an emulated implementation of the hardware. SD card HAL will be added via another PR, because it's a bit more tricky. --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? **NO**
92 lines
3.3 KiB
C++
92 lines
3.3 KiB
C++
#include "MappedInputManager.h"
|
|
|
|
#include "CrossPointSettings.h"
|
|
|
|
namespace {
|
|
using ButtonIndex = uint8_t;
|
|
|
|
struct FrontLayoutMap {
|
|
ButtonIndex back;
|
|
ButtonIndex confirm;
|
|
ButtonIndex left;
|
|
ButtonIndex right;
|
|
};
|
|
|
|
struct SideLayoutMap {
|
|
ButtonIndex pageBack;
|
|
ButtonIndex pageForward;
|
|
};
|
|
|
|
// Order matches CrossPointSettings::FRONT_BUTTON_LAYOUT.
|
|
constexpr FrontLayoutMap kFrontLayouts[] = {
|
|
{HalGPIO::BTN_BACK, HalGPIO::BTN_CONFIRM, HalGPIO::BTN_LEFT, HalGPIO::BTN_RIGHT},
|
|
{HalGPIO::BTN_LEFT, HalGPIO::BTN_RIGHT, HalGPIO::BTN_BACK, HalGPIO::BTN_CONFIRM},
|
|
{HalGPIO::BTN_CONFIRM, HalGPIO::BTN_LEFT, HalGPIO::BTN_BACK, HalGPIO::BTN_RIGHT},
|
|
{HalGPIO::BTN_BACK, HalGPIO::BTN_CONFIRM, HalGPIO::BTN_RIGHT, HalGPIO::BTN_LEFT},
|
|
};
|
|
|
|
// Order matches CrossPointSettings::SIDE_BUTTON_LAYOUT.
|
|
constexpr SideLayoutMap kSideLayouts[] = {
|
|
{HalGPIO::BTN_UP, HalGPIO::BTN_DOWN},
|
|
{HalGPIO::BTN_DOWN, HalGPIO::BTN_UP},
|
|
};
|
|
} // namespace
|
|
|
|
bool MappedInputManager::mapButton(const Button button, bool (HalGPIO::*fn)(uint8_t) const) const {
|
|
const auto frontLayout = static_cast<CrossPointSettings::FRONT_BUTTON_LAYOUT>(SETTINGS.frontButtonLayout);
|
|
const auto sideLayout = static_cast<CrossPointSettings::SIDE_BUTTON_LAYOUT>(SETTINGS.sideButtonLayout);
|
|
const auto& front = kFrontLayouts[frontLayout];
|
|
const auto& side = kSideLayouts[sideLayout];
|
|
|
|
switch (button) {
|
|
case Button::Back:
|
|
return (gpio.*fn)(front.back);
|
|
case Button::Confirm:
|
|
return (gpio.*fn)(front.confirm);
|
|
case Button::Left:
|
|
return (gpio.*fn)(front.left);
|
|
case Button::Right:
|
|
return (gpio.*fn)(front.right);
|
|
case Button::Up:
|
|
return (gpio.*fn)(HalGPIO::BTN_UP);
|
|
case Button::Down:
|
|
return (gpio.*fn)(HalGPIO::BTN_DOWN);
|
|
case Button::Power:
|
|
return (gpio.*fn)(HalGPIO::BTN_POWER);
|
|
case Button::PageBack:
|
|
return (gpio.*fn)(side.pageBack);
|
|
case Button::PageForward:
|
|
return (gpio.*fn)(side.pageForward);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool MappedInputManager::wasPressed(const Button button) const { return mapButton(button, &HalGPIO::wasPressed); }
|
|
|
|
bool MappedInputManager::wasReleased(const Button button) const { return mapButton(button, &HalGPIO::wasReleased); }
|
|
|
|
bool MappedInputManager::isPressed(const Button button) const { return mapButton(button, &HalGPIO::isPressed); }
|
|
|
|
bool MappedInputManager::wasAnyPressed() const { return gpio.wasAnyPressed(); }
|
|
|
|
bool MappedInputManager::wasAnyReleased() const { return gpio.wasAnyReleased(); }
|
|
|
|
unsigned long MappedInputManager::getHeldTime() const { return gpio.getHeldTime(); }
|
|
|
|
MappedInputManager::Labels MappedInputManager::mapLabels(const char* back, const char* confirm, const char* previous,
|
|
const char* next) const {
|
|
const auto layout = static_cast<CrossPointSettings::FRONT_BUTTON_LAYOUT>(SETTINGS.frontButtonLayout);
|
|
|
|
switch (layout) {
|
|
case CrossPointSettings::LEFT_RIGHT_BACK_CONFIRM:
|
|
return {previous, next, back, confirm};
|
|
case CrossPointSettings::LEFT_BACK_CONFIRM_RIGHT:
|
|
return {previous, back, confirm, next};
|
|
case CrossPointSettings::BACK_CONFIRM_RIGHT_LEFT:
|
|
return {back, confirm, next, previous};
|
|
case CrossPointSettings::BACK_CONFIRM_LEFT_RIGHT:
|
|
default:
|
|
return {back, confirm, previous, next};
|
|
}
|
|
} |