Fixes and improvements (#620)
- Standardized keyboard input using Unicode-based key codes across supported devices and the simulator. Keyboards don't emit `LV_KEY_*` anymore. - Refactored lilygo encoder driver into a reusable GPIO rotary encoder driver (see `Drivers/gpio-encoder-module/`). Added more features to the config file. - Improved LVGL keyboard device management, including duplicate prevention and reliable reconnects. - LVGL file mutex now registers with lvgl start/stop - Improved LVGL startup/shutdown stability and memory allocation reliability. - Increased simulator LVGL memory capacity and improved USB device-class handling.
This commit is contained in:
committed by
GitHub
parent
4fea48f433
commit
db48dfe812
@@ -0,0 +1,12 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/../../Buildscripts/module.cmake")
|
||||
|
||||
file(GLOB_RECURSE SOURCE_FILES "source/*.c*")
|
||||
|
||||
tactility_add_module(gpio-encoder-module
|
||||
SRCS ${SOURCE_FILES}
|
||||
INCLUDE_DIRS include/
|
||||
REQUIRES TactilityKernel driver
|
||||
PRIV_REQUIRES esp_driver_pcnt
|
||||
)
|
||||
@@ -0,0 +1,195 @@
|
||||
Apache License
|
||||
==============
|
||||
|
||||
_Version 2.0, January 2004_
|
||||
_<<http://www.apache.org/licenses/>>_
|
||||
|
||||
### Terms and Conditions for use, reproduction, and distribution
|
||||
|
||||
#### 1. Definitions
|
||||
|
||||
“License” shall mean the terms and conditions for use, reproduction, and
|
||||
distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
“Licensor” shall mean the copyright owner or entity authorized by the copyright
|
||||
owner that is granting the License.
|
||||
|
||||
“Legal Entity” shall mean the union of the acting entity and all other entities
|
||||
that control, are controlled by, or are under common control with that entity.
|
||||
For the purposes of this definition, “control” means **(i)** the power, direct or
|
||||
indirect, to cause the direction or management of such entity, whether by
|
||||
contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or **(iii)** beneficial ownership of such entity.
|
||||
|
||||
“You” (or “Your”) shall mean an individual or Legal Entity exercising
|
||||
permissions granted by this License.
|
||||
|
||||
“Source” form shall mean the preferred form for making modifications, including
|
||||
but not limited to software source code, documentation source, and configuration
|
||||
files.
|
||||
|
||||
“Object” form shall mean any form resulting from mechanical transformation or
|
||||
translation of a Source form, including but not limited to compiled object code,
|
||||
generated documentation, and conversions to other media types.
|
||||
|
||||
“Work” shall mean the work of authorship, whether in Source or Object form, made
|
||||
available under the License, as indicated by a copyright notice that is included
|
||||
in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
“Derivative Works” shall mean any work, whether in Source or Object form, that
|
||||
is based on (or derived from) the Work and for which the editorial revisions,
|
||||
annotations, elaborations, or other modifications represent, as a whole, an
|
||||
original work of authorship. For the purposes of this License, Derivative Works
|
||||
shall not include works that remain separable from, or merely link (or bind by
|
||||
name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
“Contribution” shall mean any work of authorship, including the original version
|
||||
of the Work and any modifications or additions to that Work or Derivative Works
|
||||
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
||||
by the copyright owner or by an individual or Legal Entity authorized to submit
|
||||
on behalf of the copyright owner. For the purposes of this definition,
|
||||
“submitted” means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems, and
|
||||
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
||||
the purpose of discussing and improving the Work, but excluding communication
|
||||
that is conspicuously marked or otherwise designated in writing by the copyright
|
||||
owner as “Not a Contribution.”
|
||||
|
||||
“Contributor” shall mean Licensor and any individual or Legal Entity on behalf
|
||||
of whom a Contribution has been received by Licensor and subsequently
|
||||
incorporated within the Work.
|
||||
|
||||
#### 2. Grant of Copyright License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the Work and such
|
||||
Derivative Works in Source or Object form.
|
||||
|
||||
#### 3. Grant of Patent License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable (except as stated in this section) patent license to make, have
|
||||
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
||||
such license applies only to those patent claims licensable by such Contributor
|
||||
that are necessarily infringed by their Contribution(s) alone or by combination
|
||||
of their Contribution(s) with the Work to which such Contribution(s) was
|
||||
submitted. If You institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
||||
Contribution incorporated within the Work constitutes direct or contributory
|
||||
patent infringement, then any patent licenses granted to You under this License
|
||||
for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
#### 4. Redistribution
|
||||
|
||||
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
||||
in any medium, with or without modifications, and in Source or Object form,
|
||||
provided that You meet the following conditions:
|
||||
|
||||
* **(a)** You must give any other recipients of the Work or Derivative Works a copy of
|
||||
this License; and
|
||||
* **(b)** You must cause any modified files to carry prominent notices stating that You
|
||||
changed the files; and
|
||||
* **(c)** You must retain, in the Source form of any Derivative Works that You distribute,
|
||||
all copyright, patent, trademark, and attribution notices from the Source form
|
||||
of the Work, excluding those notices that do not pertain to any part of the
|
||||
Derivative Works; and
|
||||
* **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any
|
||||
Derivative Works that You distribute must include a readable copy of the
|
||||
attribution notices contained within such NOTICE file, excluding those notices
|
||||
that do not pertain to any part of the Derivative Works, in at least one of the
|
||||
following places: within a NOTICE text file distributed as part of the
|
||||
Derivative Works; within the Source form or documentation, if provided along
|
||||
with the Derivative Works; or, within a display generated by the Derivative
|
||||
Works, if and wherever such third-party notices normally appear. The contents of
|
||||
the NOTICE file are for informational purposes only and do not modify the
|
||||
License. You may add Your own attribution notices within Derivative Works that
|
||||
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
||||
provided that such additional attribution notices cannot be construed as
|
||||
modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide
|
||||
additional or different license terms and conditions for use, reproduction, or
|
||||
distribution of Your modifications, or for any such Derivative Works as a whole,
|
||||
provided Your use, reproduction, and distribution of the Work otherwise complies
|
||||
with the conditions stated in this License.
|
||||
|
||||
#### 5. Submission of Contributions
|
||||
|
||||
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
||||
for inclusion in the Work by You to the Licensor shall be under the terms and
|
||||
conditions of this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
||||
any separate license agreement you may have executed with Licensor regarding
|
||||
such Contributions.
|
||||
|
||||
#### 6. Trademarks
|
||||
|
||||
This License does not grant permission to use the trade names, trademarks,
|
||||
service marks, or product names of the Licensor, except as required for
|
||||
reasonable and customary use in describing the origin of the Work and
|
||||
reproducing the content of the NOTICE file.
|
||||
|
||||
#### 7. Disclaimer of Warranty
|
||||
|
||||
Unless required by applicable law or agreed to in writing, Licensor provides the
|
||||
Work (and each Contributor provides its Contributions) on an “AS IS” BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
||||
including, without limitation, any warranties or conditions of TITLE,
|
||||
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
||||
solely responsible for determining the appropriateness of using or
|
||||
redistributing the Work and assume any risks associated with Your exercise of
|
||||
permissions under this License.
|
||||
|
||||
#### 8. Limitation of Liability
|
||||
|
||||
In no event and under no legal theory, whether in tort (including negligence),
|
||||
contract, or otherwise, unless required by applicable law (such as deliberate
|
||||
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special, incidental,
|
||||
or consequential damages of any character arising as a result of this License or
|
||||
out of the use or inability to use the Work (including but not limited to
|
||||
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
||||
any and all other commercial damages or losses), even if such Contributor has
|
||||
been advised of the possibility of such damages.
|
||||
|
||||
#### 9. Accepting Warranty or Additional Liability
|
||||
|
||||
While redistributing the Work or Derivative Works thereof, You may choose to
|
||||
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
||||
other liability obligations and/or rights consistent with this License. However,
|
||||
in accepting such obligations, You may act only on Your own behalf and on Your
|
||||
sole responsibility, not on behalf of any other Contributor, and only if You
|
||||
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason of your
|
||||
accepting any such warranty or additional liability.
|
||||
|
||||
_END OF TERMS AND CONDITIONS_
|
||||
|
||||
### APPENDIX: How to apply the Apache License to your work
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate
|
||||
notice, with the fields enclosed by brackets `[]` replaced with your own
|
||||
identifying information. (Don't include the brackets!) The text should be
|
||||
enclosed in the appropriate comment syntax for the file format. We also
|
||||
recommend that a file or class name and description of purpose be included on
|
||||
the same “printed page” as the copyright notice for easier identification within
|
||||
third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
description: >
|
||||
GPIO-attached rotary encoder wheel - a 2-phase quadrature encoder (decoded via the ESP32
|
||||
hardware PCNT peripheral) plus an optional separate click/enter button. Exposes a
|
||||
KEYBOARD_TYPE device: each wheel detent is translated to an arrow up/down key, and the
|
||||
button (if present) press/release is translated to the enter key.
|
||||
|
||||
compatible: "tactility,gpio-encoder"
|
||||
|
||||
properties:
|
||||
pin-a:
|
||||
type: phandles
|
||||
required: true
|
||||
description: Quadrature phase A GPIO pin
|
||||
pin-b:
|
||||
type: phandles
|
||||
required: true
|
||||
description: Quadrature phase B GPIO pin
|
||||
pin-enter:
|
||||
type: phandles
|
||||
default: GPIO_PIN_SPEC_NONE
|
||||
description: Optional click/enter button GPIO pin (active low)
|
||||
pulses-per-detent:
|
||||
type: int
|
||||
min: 1
|
||||
max: 255
|
||||
default: 4
|
||||
description: Quadrature pulses per mechanical detent
|
||||
pending-capacity:
|
||||
type: int
|
||||
min: 2
|
||||
max: 255
|
||||
default: 16
|
||||
description: Capacity of the queue buffering key events between read_key() polls. Must be at least 2 to hold one wheel press/release pair.
|
||||
@@ -0,0 +1,3 @@
|
||||
dependencies:
|
||||
- TactilityKernel
|
||||
bindings: bindings
|
||||
@@ -0,0 +1,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/bindings/bindings.h>
|
||||
#include <drivers/gpio_encoder.h>
|
||||
|
||||
DEFINE_DEVICETREE(gpio_encoder, struct GpioEncoderConfig)
|
||||
@@ -0,0 +1,25 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <tactility/drivers/gpio.h>
|
||||
|
||||
struct GpioEncoderConfig {
|
||||
// First pin of encoder wheel
|
||||
struct GpioPinSpec pin_a;
|
||||
// Second pin of encoder wheel
|
||||
struct GpioPinSpec pin_b;
|
||||
// "Button" pin of encoder wheel. Optional: GPIO_PIN_SPEC_NONE when the wheel has no click/enter button.
|
||||
struct GpioPinSpec pin_enter;
|
||||
// Quadrature pulses per mechanical detent (x4 decode gives 4 pulses/detent for a standard EC11-style encoder).
|
||||
uint8_t pulses_per_detent;
|
||||
// Capacity of the queue buffering key events between read_key() polls.
|
||||
uint8_t pending_capacity;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/module.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern struct Module gpio_encoder_module;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,337 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <drivers/gpio_encoder.h>
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/gpio.h>
|
||||
#include <tactility/drivers/gpio_controller.h>
|
||||
#include <tactility/drivers/gpio_descriptor.h>
|
||||
#include <tactility/drivers/keyboard.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <driver/pulse_cnt.h>
|
||||
|
||||
#include <new>
|
||||
|
||||
#define TAG "gpio_encoder"
|
||||
#define GET_CONFIG(device) (static_cast<const GpioEncoderConfig*>((device)->config))
|
||||
#define GET_INTERNAL(device) (static_cast<GpioEncoderInternal*>(device_get_driver_data(device)))
|
||||
|
||||
struct GpioEncoderPendingEvent {
|
||||
uint32_t key;
|
||||
bool pressed;
|
||||
};
|
||||
|
||||
struct GpioEncoderInternal {
|
||||
pcnt_unit_handle_t pcnt_unit = nullptr;
|
||||
GpioDescriptor* pin_a = nullptr;
|
||||
GpioDescriptor* pin_b = nullptr;
|
||||
GpioDescriptor* pin_enter = nullptr;
|
||||
int32_t pulse_remainder = 0;
|
||||
bool button_pressed = false;
|
||||
int32_t pulses_per_detent = 0;
|
||||
GpioEncoderPendingEvent* pending = nullptr;
|
||||
uint32_t pending_capacity = 0;
|
||||
uint32_t pending_head = 0;
|
||||
uint32_t pending_count = 0;
|
||||
};
|
||||
|
||||
static bool push_pending(GpioEncoderInternal* internal, uint32_t key, bool pressed) {
|
||||
if (internal->pending_count >= internal->pending_capacity) {
|
||||
LOG_W(TAG, "Pending event queue full, dropping event");
|
||||
return false;
|
||||
}
|
||||
uint32_t tail = (internal->pending_head + internal->pending_count) % internal->pending_capacity;
|
||||
internal->pending[tail] = { .key = key, .pressed = pressed };
|
||||
internal->pending_count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool pop_pending(GpioEncoderInternal* internal, GpioEncoderPendingEvent* out_event) {
|
||||
if (internal->pending_count == 0) {
|
||||
return false;
|
||||
}
|
||||
*out_event = internal->pending[internal->pending_head];
|
||||
internal->pending_head = (internal->pending_head + 1) % internal->pending_capacity;
|
||||
internal->pending_count--;
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
// region Driver lifecycle
|
||||
|
||||
// Accumulating count makes over-/underflow automatically compensated; requires watch points at
|
||||
// the low and high limits (see pcnt_unit_add_watch_point() below). Ported from the deprecated
|
||||
// HAL's TpagerEncoder::initEncoder().
|
||||
static constexpr int PCNT_LOW_LIMIT = -127;
|
||||
static constexpr int PCNT_HIGH_LIMIT = 126;
|
||||
|
||||
static error_t init_pcnt_unit(int pin_a, int pin_b, pcnt_unit_handle_t* out_unit) {
|
||||
pcnt_unit_config_t unit_config = {
|
||||
.low_limit = PCNT_LOW_LIMIT,
|
||||
.high_limit = PCNT_HIGH_LIMIT,
|
||||
.intr_priority = 0,
|
||||
.flags = { .accum_count = 1 },
|
||||
};
|
||||
|
||||
pcnt_unit_handle_t unit = nullptr;
|
||||
if (pcnt_new_unit(&unit_config, &unit) != ESP_OK) {
|
||||
LOG_E(TAG, "Pulse counter initialization failed");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
pcnt_glitch_filter_config_t filter_config = { .max_glitch_ns = 1000 };
|
||||
if (pcnt_unit_set_glitch_filter(unit, &filter_config) != ESP_OK) {
|
||||
LOG_E(TAG, "Pulse counter glitch filter config failed");
|
||||
pcnt_del_unit(unit);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
pcnt_chan_config_t chan_a_config = {
|
||||
.edge_gpio_num = pin_b,
|
||||
.level_gpio_num = pin_a,
|
||||
.flags = {},
|
||||
};
|
||||
pcnt_chan_config_t chan_b_config = {
|
||||
.edge_gpio_num = pin_a,
|
||||
.level_gpio_num = pin_b,
|
||||
.flags = {},
|
||||
};
|
||||
|
||||
pcnt_channel_handle_t chan_a = nullptr;
|
||||
pcnt_channel_handle_t chan_b = nullptr;
|
||||
if (pcnt_new_channel(unit, &chan_a_config, &chan_a) != ESP_OK ||
|
||||
pcnt_new_channel(unit, &chan_b_config, &chan_b) != ESP_OK) {
|
||||
LOG_E(TAG, "Pulse counter channel config failed");
|
||||
pcnt_del_unit(unit);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
// Standard quadrature decode: each channel counts on its edge, direction decided by the
|
||||
// other channel's level.
|
||||
if (pcnt_channel_set_edge_action(chan_a, PCNT_CHANNEL_EDGE_ACTION_DECREASE, PCNT_CHANNEL_EDGE_ACTION_INCREASE) != ESP_OK ||
|
||||
pcnt_channel_set_edge_action(chan_b, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_DECREASE) != ESP_OK) {
|
||||
LOG_E(TAG, "Pulse counter edge action config failed");
|
||||
pcnt_del_unit(unit);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
if (pcnt_channel_set_level_action(chan_a, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_INVERSE) != ESP_OK ||
|
||||
pcnt_channel_set_level_action(chan_b, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_INVERSE) != ESP_OK) {
|
||||
LOG_E(TAG, "Pulse counter level action config failed");
|
||||
pcnt_del_unit(unit);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (pcnt_unit_add_watch_point(unit, PCNT_LOW_LIMIT) != ESP_OK ||
|
||||
pcnt_unit_add_watch_point(unit, PCNT_HIGH_LIMIT) != ESP_OK) {
|
||||
LOG_E(TAG, "Pulse counter watch point config failed");
|
||||
pcnt_del_unit(unit);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (pcnt_unit_enable(unit) != ESP_OK ||
|
||||
pcnt_unit_clear_count(unit) != ESP_OK ||
|
||||
pcnt_unit_start(unit) != ESP_OK) {
|
||||
LOG_E(TAG, "Pulse counter could not be started");
|
||||
pcnt_del_unit(unit);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
*out_unit = unit;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t start(Device* device) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
|
||||
// Backstop for values the devicetree compiler doesn't currently validate: 0 divides by
|
||||
// zero in poll_wheel(), and a capacity below 2 can never hold one press/release pair.
|
||||
if (config->pulses_per_detent == 0) {
|
||||
LOG_E(TAG, "pulses_per_detent must be > 0");
|
||||
return ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (config->pending_capacity < 2) {
|
||||
LOG_E(TAG, "pending_capacity must be >= 2");
|
||||
return ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
auto* internal = new (std::nothrow) GpioEncoderInternal();
|
||||
if (internal == nullptr) {
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
internal->pulses_per_detent = static_cast<int32_t>(config->pulses_per_detent);
|
||||
internal->pending_capacity = config->pending_capacity;
|
||||
|
||||
internal->pending = new (std::nothrow) GpioEncoderPendingEvent[internal->pending_capacity];
|
||||
if (internal->pending == nullptr) {
|
||||
delete internal;
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
internal->pin_a = gpio_descriptor_acquire(config->pin_a.gpio_controller, config->pin_a.pin, config->pin_a.flags | GPIO_FLAG_DIRECTION_INPUT, GPIO_OWNER_GPIO);
|
||||
if (internal->pin_a == nullptr) {
|
||||
LOG_E(TAG, "Failed to acquire pin_a");
|
||||
delete[] internal->pending;
|
||||
delete internal;
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
internal->pin_b = gpio_descriptor_acquire(config->pin_b.gpio_controller, config->pin_b.pin, config->pin_b.flags | GPIO_FLAG_DIRECTION_INPUT, GPIO_OWNER_GPIO);
|
||||
if (internal->pin_b == nullptr) {
|
||||
LOG_E(TAG, "Failed to acquire pin_b");
|
||||
gpio_descriptor_release(internal->pin_a);
|
||||
delete[] internal->pending;
|
||||
delete internal;
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
int native_pin_a = 0;
|
||||
int native_pin_b = 0;
|
||||
if (gpio_descriptor_get_native_pin_number(internal->pin_a, &native_pin_a) != ERROR_NONE ||
|
||||
gpio_descriptor_get_native_pin_number(internal->pin_b, &native_pin_b) != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to resolve native pin numbers");
|
||||
gpio_descriptor_release(internal->pin_b);
|
||||
gpio_descriptor_release(internal->pin_a);
|
||||
delete[] internal->pending;
|
||||
delete internal;
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
error_t error = init_pcnt_unit(native_pin_a, native_pin_b, &internal->pcnt_unit);
|
||||
if (error != ERROR_NONE) {
|
||||
gpio_descriptor_release(internal->pin_b);
|
||||
gpio_descriptor_release(internal->pin_a);
|
||||
delete[] internal->pending;
|
||||
delete internal;
|
||||
return error;
|
||||
}
|
||||
|
||||
if (config->pin_enter.gpio_controller != nullptr) {
|
||||
internal->pin_enter = gpio_descriptor_acquire(config->pin_enter.gpio_controller, config->pin_enter.pin, GPIO_FLAG_DIRECTION_INPUT | GPIO_FLAG_ACTIVE_LOW, GPIO_OWNER_GPIO);
|
||||
if (internal->pin_enter == nullptr) {
|
||||
pcnt_unit_stop(internal->pcnt_unit);
|
||||
pcnt_del_unit(internal->pcnt_unit);
|
||||
gpio_descriptor_release(internal->pin_b);
|
||||
gpio_descriptor_release(internal->pin_a);
|
||||
delete[] internal->pending;
|
||||
delete internal;
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
}
|
||||
|
||||
device_set_driver_data(device, internal);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop(Device* device) {
|
||||
auto* internal = GET_INTERNAL(device);
|
||||
|
||||
if (internal->pin_enter != nullptr) {
|
||||
gpio_descriptor_release(internal->pin_enter);
|
||||
}
|
||||
|
||||
if (pcnt_unit_stop(internal->pcnt_unit) != ESP_OK) {
|
||||
LOG_W(TAG, "Failed to stop encoder");
|
||||
}
|
||||
if (pcnt_del_unit(internal->pcnt_unit) != ESP_OK) {
|
||||
LOG_W(TAG, "Failed to delete encoder");
|
||||
}
|
||||
|
||||
gpio_descriptor_release(internal->pin_b);
|
||||
gpio_descriptor_release(internal->pin_a);
|
||||
|
||||
device_set_driver_data(device, nullptr);
|
||||
delete[] internal->pending;
|
||||
delete internal;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region KeyboardApi
|
||||
|
||||
// Wheel rotation is a discrete notch, not a held key, so each detent is reported as an
|
||||
// immediate press+release pair rather than a persistent pressed state.
|
||||
static void poll_wheel(GpioEncoderInternal* internal) {
|
||||
int pulses = 0;
|
||||
pcnt_unit_get_count(internal->pcnt_unit, &pulses);
|
||||
pcnt_unit_clear_count(internal->pcnt_unit);
|
||||
|
||||
int32_t total = internal->pulse_remainder + pulses;
|
||||
int32_t detents = total / internal->pulses_per_detent;
|
||||
internal->pulse_remainder = total % internal->pulses_per_detent;
|
||||
|
||||
uint32_t key = detents >= 0 ? CODEPOINT_ARROW_DOWN : CODEPOINT_ARROW_UP;
|
||||
int32_t count = detents >= 0 ? detents : -detents;
|
||||
for (int32_t i = 0; i < count; i++) {
|
||||
// A press without its matching release would leave the consumer thinking the key
|
||||
// is stuck down, so only enqueue the pair when both fit.
|
||||
if (internal->pending_count + 2 > internal->pending_capacity) {
|
||||
LOG_W(TAG, "Pending event queue full, dropping remaining wheel events");
|
||||
break;
|
||||
}
|
||||
push_pending(internal, key, true);
|
||||
push_pending(internal, key, false);
|
||||
}
|
||||
}
|
||||
|
||||
static void poll_button(GpioEncoderInternal* internal) {
|
||||
if (internal->pin_enter == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool pressed = false;
|
||||
if (gpio_descriptor_get_level(internal->pin_enter, &pressed) != ERROR_NONE) {
|
||||
return;
|
||||
}
|
||||
// Only commit the new state once its event is actually queued - a full FIFO here
|
||||
// leaves button_pressed unchanged so the same transition is retried next poll instead
|
||||
// of being lost.
|
||||
if (pressed != internal->button_pressed) {
|
||||
if (push_pending(internal, CODEPOINT_ENTER, pressed)) {
|
||||
internal->button_pressed = pressed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static error_t gpio_encoder_read_key(Device* device, KeyboardKeyData* data) {
|
||||
auto* internal = GET_INTERNAL(device);
|
||||
|
||||
poll_wheel(internal);
|
||||
poll_button(internal);
|
||||
|
||||
GpioEncoderPendingEvent event;
|
||||
if (pop_pending(internal, &event)) {
|
||||
data->key = event.key;
|
||||
data->pressed = event.pressed;
|
||||
data->continue_reading = internal->pending_count > 0;
|
||||
} else {
|
||||
data->key = 0;
|
||||
data->pressed = false;
|
||||
data->continue_reading = false;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
static constexpr KeyboardApi GPIO_ENCODER_API = {
|
||||
.read_key = gpio_encoder_read_key,
|
||||
};
|
||||
|
||||
extern Module gpio_encoder_module;
|
||||
|
||||
Driver gpio_encoder_driver = {
|
||||
.name = "gpio_encoder",
|
||||
.compatible = (const char*[]) { "tactility,gpio-encoder", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = &GPIO_ENCODER_API,
|
||||
.device_type = &KEYBOARD_TYPE,
|
||||
.owner = &gpio_encoder_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern Driver gpio_encoder_driver;
|
||||
|
||||
static Driver* const gpio_encoder_drivers[] = {
|
||||
&gpio_encoder_driver,
|
||||
nullptr
|
||||
};
|
||||
|
||||
Module gpio_encoder_module = {
|
||||
.name = "gpio-encoder",
|
||||
.drivers = gpio_encoder_drivers
|
||||
};
|
||||
|
||||
} // extern "C"
|
||||
Reference in New Issue
Block a user