From ed82063af5f83530afa5cfb5bf5bd516f3d05f2a Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 10 Apr 2024 16:56:10 +1000 Subject: WIP decompose our giant LVGL driver into smaller classes --- src/input/include/feedback_device.hpp | 32 ++++++++++++++++++ src/input/include/feedback_haptics.hpp | 26 +++++++++++++++ src/input/include/input_device.hpp | 33 +++++++++++++++++++ src/input/include/input_touch_dpad.hpp | 30 +++++++++++++++++ src/input/include/input_touch_wheel.hpp | 37 +++++++++++++++++++++ src/input/include/input_volume_buttons.hpp | 30 +++++++++++++++++ src/input/include/lvgl_input_driver.hpp | 53 ++++++++++++++++++++++++++++++ 7 files changed, 241 insertions(+) create mode 100644 src/input/include/feedback_device.hpp create mode 100644 src/input/include/feedback_haptics.hpp create mode 100644 src/input/include/input_device.hpp create mode 100644 src/input/include/input_touch_dpad.hpp create mode 100644 src/input/include/input_touch_wheel.hpp create mode 100644 src/input/include/input_volume_buttons.hpp create mode 100644 src/input/include/lvgl_input_driver.hpp (limited to 'src/input/include') diff --git a/src/input/include/feedback_device.hpp b/src/input/include/feedback_device.hpp new file mode 100644 index 00000000..4faeeafd --- /dev/null +++ b/src/input/include/feedback_device.hpp @@ -0,0 +1,32 @@ +/* + * Copyright 2024 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include + +namespace input { + +/* + * Interface for providing non-visual feedback to the user as a result of LVGL + * events. 'Feedback Devices' are able to observe all events that are generated + * by LVGL as a result of Input Devices. + * + * Implementations of this interface are a mix of hardware features (e.g. a + * haptic motor buzzing when your selection changes) and firmware features + * (e.g. playing audio feedback that describes the selected element). + */ +class IFeedbackDevice { + public: + virtual ~IFeedbackDevice() {} + + virtual auto feedback(uint8_t event_type) -> void = 0; + + // TODO: Add configuration; likely the same shape of interface that + // IInputDevice uses. +}; + +} // namespace input diff --git a/src/input/include/feedback_haptics.hpp b/src/input/include/feedback_haptics.hpp new file mode 100644 index 00000000..a307a429 --- /dev/null +++ b/src/input/include/feedback_haptics.hpp @@ -0,0 +1,26 @@ +/* + * Copyright 2024 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include + +#include "feedback_device.hpp" +#include "haptics.hpp" + +namespace input { + +class Haptics : public IFeedbackDevice { + public: + Haptics(drivers::Haptics& haptics_); + + auto feedback(uint8_t event_type) -> void override; + + private: + drivers::Haptics& haptics_; +}; + +} // namespace input diff --git a/src/input/include/input_device.hpp b/src/input/include/input_device.hpp new file mode 100644 index 00000000..09cf9193 --- /dev/null +++ b/src/input/include/input_device.hpp @@ -0,0 +1,33 @@ +/* + * Copyright 2024 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include +#include +#include + +#include "hal/lv_hal_indev.h" + +namespace input { + +/* + * Interface for all device input methods. Each 'Input Device' is polled by + * LVGL at regular intervals, and can effect the device either via LVGL's input + * device driver API, or by emitting events for other parts of the system to + * react to (e.g. issuing a play/pause event, or altering the volume). + */ +class IInputDevice { + public: + virtual ~IInputDevice() {} + + virtual auto read(lv_indev_data_t* data) -> void = 0; + + // TODO: Add hooks and configuration (or are hooks just one kind of + // configuration?) +}; + +} // namespace input diff --git a/src/input/include/input_touch_dpad.hpp b/src/input/include/input_touch_dpad.hpp new file mode 100644 index 00000000..fdb52db9 --- /dev/null +++ b/src/input/include/input_touch_dpad.hpp @@ -0,0 +1,30 @@ +/* + * Copyright 2024 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include +#include + +#include "hal/lv_hal_indev.h" + +#include "haptics.hpp" +#include "input_device.hpp" +#include "touchwheel.hpp" + +namespace input { + +class TouchDPad : public IInputDevice { + public: + TouchDPad(drivers::TouchWheel&); + + auto read(lv_indev_data_t* data) -> void override; + + private: + drivers::TouchWheel& wheel_; +}; + +} // namespace input diff --git a/src/input/include/input_touch_wheel.hpp b/src/input/include/input_touch_wheel.hpp new file mode 100644 index 00000000..a4923f21 --- /dev/null +++ b/src/input/include/input_touch_wheel.hpp @@ -0,0 +1,37 @@ +/* + * Copyright 2024 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include + +#include "hal/lv_hal_indev.h" + +#include "haptics.hpp" +#include "input_device.hpp" +#include "touchwheel.hpp" + +namespace input { + +class TouchWheel : public IInputDevice { + public: + TouchWheel(drivers::TouchWheel&); + + auto read(lv_indev_data_t* data) -> void override; + + private: + auto calculate_ticks(const drivers::TouchWheelData& data) -> int8_t; + + drivers::TouchWheel& wheel_; + + bool is_scrolling_; + uint8_t sensitivity_; + uint8_t threshold_; + bool is_first_read_; + uint8_t last_angle_; +}; + +} // namespace input diff --git a/src/input/include/input_volume_buttons.hpp b/src/input/include/input_volume_buttons.hpp new file mode 100644 index 00000000..4ca8c16b --- /dev/null +++ b/src/input/include/input_volume_buttons.hpp @@ -0,0 +1,30 @@ +/* + * Copyright 2024 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include + +#include "gpios.hpp" +#include "hal/lv_hal_indev.h" + +#include "haptics.hpp" +#include "input_device.hpp" +#include "touchwheel.hpp" + +namespace input { + +class VolumeButtons : public IInputDevice { + public: + VolumeButtons(drivers::IGpios&); + + auto read(lv_indev_data_t* data) -> void override; + + private: + drivers::IGpios& gpios_; +}; + +} // namespace input diff --git a/src/input/include/lvgl_input_driver.hpp b/src/input/include/lvgl_input_driver.hpp new file mode 100644 index 00000000..be452368 --- /dev/null +++ b/src/input/include/lvgl_input_driver.hpp @@ -0,0 +1,53 @@ +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include +#include +#include +#include + +#include "core/lv_group.h" +#include "feedback_device.hpp" +#include "gpios.hpp" +#include "hal/lv_hal_indev.h" + +#include "input_device.hpp" +#include "nvs.hpp" +#include "service_locator.hpp" +#include "touchwheel.hpp" + +namespace input { + +/* + * Implementation of an LVGL input device. This class composes multiple + * IInputDevice and IFeedbackDevice instances together into a single LVGL + * device. + */ +class LvglInputDriver { + public: + LvglInputDriver(std::shared_ptr); + + auto read(lv_indev_data_t* data) -> void; + auto feedback(uint8_t) -> void; + + auto registration() -> lv_indev_t* { return registration_; } + auto lock(bool l) -> void { is_locked_ = l; } + + private: + std::shared_ptr services_; + + lv_indev_drv_t driver_; + lv_indev_t* registration_; + + std::vector> inputs_; + std::vector> feedbacks_; + + bool is_locked_; +}; + +} // namespace input -- cgit v1.2.3 From 33919e9e3f419e13318fa6b8217d8c8dcd86c1eb Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 11 Apr 2024 15:16:35 +1000 Subject: Migrate all existing control schemes to the cool new world --- src/input/include/device_factory.hpp | 39 ++++++++++++++++++++++++++++++ src/input/include/input_device.hpp | 6 +++++ src/input/include/input_nav_buttons.hpp | 34 ++++++++++++++++++++++++++ src/input/include/input_touch_dpad.hpp | 6 +++++ src/input/include/input_touch_wheel.hpp | 20 ++++++++++++--- src/input/include/input_trigger.hpp | 37 ++++++++++++++++++++++++++++ src/input/include/input_volume_buttons.hpp | 4 +++ src/input/include/lvgl_input_driver.hpp | 17 ++++++++----- 8 files changed, 154 insertions(+), 9 deletions(-) create mode 100644 src/input/include/device_factory.hpp create mode 100644 src/input/include/input_nav_buttons.hpp create mode 100644 src/input/include/input_trigger.hpp (limited to 'src/input/include') diff --git a/src/input/include/device_factory.hpp b/src/input/include/device_factory.hpp new file mode 100644 index 00000000..dd9c7133 --- /dev/null +++ b/src/input/include/device_factory.hpp @@ -0,0 +1,39 @@ +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include +#include + +#include "feedback_device.hpp" +#include "input_device.hpp" +#include "input_touch_wheel.hpp" +#include "nvs.hpp" +#include "service_locator.hpp" + +namespace input { + +class DeviceFactory { + public: + DeviceFactory(std::shared_ptr); + + auto createInputs(drivers::NvsStorage::InputModes mode) + -> std::vector>; + + auto createFeedbacks() -> std::vector>; + + auto touch_wheel() -> std::shared_ptr { return wheel_; } + + private: + std::shared_ptr services_; + + // HACK: the touchwheel is current a special case, since it's the only input + // device that has some kind of setting/configuration; scroll sensitivity. + std::shared_ptr wheel_; +}; + +} // namespace input diff --git a/src/input/include/input_device.hpp b/src/input/include/input_device.hpp index 09cf9193..5fc3e066 100644 --- a/src/input/include/input_device.hpp +++ b/src/input/include/input_device.hpp @@ -11,6 +11,7 @@ #include #include "hal/lv_hal_indev.h" +#include "property.hpp" namespace input { @@ -28,6 +29,11 @@ class IInputDevice { // TODO: Add hooks and configuration (or are hooks just one kind of // configuration?) + + virtual auto settings() + -> std::vector> { + return {}; + } }; } // namespace input diff --git a/src/input/include/input_nav_buttons.hpp b/src/input/include/input_nav_buttons.hpp new file mode 100644 index 00000000..29a19a16 --- /dev/null +++ b/src/input/include/input_nav_buttons.hpp @@ -0,0 +1,34 @@ +/* + * Copyright 2024 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include + +#include "gpios.hpp" +#include "hal/lv_hal_indev.h" + +#include "haptics.hpp" +#include "input_device.hpp" +#include "input_trigger.hpp" +#include "touchwheel.hpp" + +namespace input { + +class NavButtons : public IInputDevice { + public: + NavButtons(drivers::IGpios&); + + auto read(lv_indev_data_t* data) -> void override; + + private: + drivers::IGpios& gpios_; + + Trigger up_; + Trigger down_; +}; + +} // namespace input diff --git a/src/input/include/input_touch_dpad.hpp b/src/input/include/input_touch_dpad.hpp index fdb52db9..03936acb 100644 --- a/src/input/include/input_touch_dpad.hpp +++ b/src/input/include/input_touch_dpad.hpp @@ -13,6 +13,7 @@ #include "haptics.hpp" #include "input_device.hpp" +#include "input_trigger.hpp" #include "touchwheel.hpp" namespace input { @@ -25,6 +26,11 @@ class TouchDPad : public IInputDevice { private: drivers::TouchWheel& wheel_; + + Trigger up_; + Trigger right_; + Trigger down_; + Trigger left_; }; } // namespace input diff --git a/src/input/include/input_touch_wheel.hpp b/src/input/include/input_touch_wheel.hpp index a4923f21..c81cbb1a 100644 --- a/src/input/include/input_touch_wheel.hpp +++ b/src/input/include/input_touch_wheel.hpp @@ -6,29 +6,43 @@ #pragma once +#include #include #include "hal/lv_hal_indev.h" #include "haptics.hpp" #include "input_device.hpp" +#include "input_trigger.hpp" +#include "nvs.hpp" +#include "property.hpp" #include "touchwheel.hpp" namespace input { class TouchWheel : public IInputDevice { public: - TouchWheel(drivers::TouchWheel&); + TouchWheel(drivers::NvsStorage&, drivers::TouchWheel&); auto read(lv_indev_data_t* data) -> void override; + auto sensitivity() -> lua::Property&; + private: - auto calculate_ticks(const drivers::TouchWheelData& data) -> int8_t; + auto calculateTicks(const drivers::TouchWheelData& data) -> int8_t; + auto calculateThreshold(uint8_t sensitivity) -> uint8_t; + drivers::NvsStorage& nvs_; drivers::TouchWheel& wheel_; + lua::Property sensitivity_; + + Trigger up_; + Trigger right_; + Trigger down_; + Trigger left_; + bool is_scrolling_; - uint8_t sensitivity_; uint8_t threshold_; bool is_first_read_; uint8_t last_angle_; diff --git a/src/input/include/input_trigger.hpp b/src/input/include/input_trigger.hpp new file mode 100644 index 00000000..599b796b --- /dev/null +++ b/src/input/include/input_trigger.hpp @@ -0,0 +1,37 @@ +/* + * Copyright 2024 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include +#include + +#include "hal/lv_hal_indev.h" + +namespace input { + +const uint16_t kLongPressDelayMs = LV_INDEV_DEF_LONG_PRESS_TIME; +const uint16_t kRepeatDelayMs = LV_INDEV_DEF_LONG_PRESS_REP_TIME; + +class Trigger { + public: + enum class State { + kNone, + kClick, + kLongPress, + kRepeatPress, + }; + + Trigger(); + + auto update(bool is_pressed) -> State; + + private: + std::optional touch_time_ms_; + uint16_t times_fired_; +}; + +} // namespace input diff --git a/src/input/include/input_volume_buttons.hpp b/src/input/include/input_volume_buttons.hpp index 4ca8c16b..162ca8d9 100644 --- a/src/input/include/input_volume_buttons.hpp +++ b/src/input/include/input_volume_buttons.hpp @@ -13,6 +13,7 @@ #include "haptics.hpp" #include "input_device.hpp" +#include "input_trigger.hpp" #include "touchwheel.hpp" namespace input { @@ -25,6 +26,9 @@ class VolumeButtons : public IInputDevice { private: drivers::IGpios& gpios_; + + Trigger up_; + Trigger down_; }; } // namespace input diff --git a/src/input/include/lvgl_input_driver.hpp b/src/input/include/lvgl_input_driver.hpp index be452368..257ccb28 100644 --- a/src/input/include/lvgl_input_driver.hpp +++ b/src/input/include/lvgl_input_driver.hpp @@ -6,19 +6,20 @@ #pragma once -#include +#include #include #include #include #include "core/lv_group.h" +#include "device_factory.hpp" #include "feedback_device.hpp" #include "gpios.hpp" #include "hal/lv_hal_indev.h" #include "input_device.hpp" #include "nvs.hpp" -#include "service_locator.hpp" +#include "property.hpp" #include "touchwheel.hpp" namespace input { @@ -30,7 +31,9 @@ namespace input { */ class LvglInputDriver { public: - LvglInputDriver(std::shared_ptr); + LvglInputDriver(drivers::NvsStorage& nvs, DeviceFactory&); + + auto mode() -> lua::Property& { return mode_; } auto read(lv_indev_data_t* data) -> void; auto feedback(uint8_t) -> void; @@ -39,13 +42,15 @@ class LvglInputDriver { auto lock(bool l) -> void { is_locked_ = l; } private: - std::shared_ptr services_; + drivers::NvsStorage& nvs_; + DeviceFactory& factory_; + lua::Property mode_; lv_indev_drv_t driver_; lv_indev_t* registration_; - std::vector> inputs_; - std::vector> feedbacks_; + std::vector> inputs_; + std::vector> feedbacks_; bool is_locked_; }; -- cgit v1.2.3