Improve encoder driver

It actually works and clicks now!

Still a bit rough though. Need to dive into lvgl internals to work out
what it's doing with enc_diff
This commit is contained in:
jacqueline
2023-06-26 12:55:46 +10:00
parent c124c8f94d
commit 9763cc955c
7 changed files with 47 additions and 36 deletions
+3 -2
View File
@@ -32,12 +32,13 @@ class RelativeWheel {
auto Update() -> void;
auto is_pressed() -> bool;
auto is_clicking() -> bool;
auto ticks() -> std::int_fast16_t;
private:
TouchWheel* touch_;
bool is_pressed_;
bool is_clicking_;
bool was_clicking_;
bool is_first_read_;
std::int_fast16_t ticks_;
uint8_t last_angle_;
+2 -1
View File
@@ -17,7 +17,8 @@
namespace drivers {
struct TouchWheelData {
bool is_touched = false;
bool is_wheel_touched = false;
bool is_button_touched = false;
uint8_t wheel_position = -1;
};
+12 -4
View File
@@ -15,7 +15,8 @@ namespace drivers {
RelativeWheel::RelativeWheel(TouchWheel* touch)
: touch_(touch),
is_pressed_(false),
is_clicking_(false),
was_clicking_(false),
is_first_read_(true),
ticks_(0),
last_angle_(0) {}
@@ -23,7 +24,12 @@ RelativeWheel::RelativeWheel(TouchWheel* touch)
auto RelativeWheel::Update() -> void {
touch_->Update();
TouchWheelData d = touch_->GetTouchWheelData();
is_pressed_ = d.is_touched;
is_clicking_ = d.is_button_touched;
if (!d.is_wheel_touched) {
is_first_read_ = true;
}
uint8_t new_angle = d.wheel_position;
if (is_first_read_) {
@@ -61,8 +67,10 @@ auto RelativeWheel::Update() -> void {
ticks_ = change;
}
auto RelativeWheel::is_pressed() -> bool {
return is_pressed_;
auto RelativeWheel::is_clicking() -> bool {
bool ret = is_clicking_;
is_clicking_ = 0;
return ret;
}
auto RelativeWheel::ticks() -> std::int_fast16_t {
+3 -2
View File
@@ -105,8 +105,9 @@ void TouchWheel::Update() {
}
if (status & 0b1) {
// Key detect. Note that the touchwheel keys also trigger this.
// TODO(daniel): Do something with this.
// bool centre_key = ReadRegister(Register::KEY_STATUS_A) & 0b1000;
uint8_t reg = ReadRegister(Register::KEY_STATUS_A);
data_.is_button_touched = reg & 0b1000;
data_.is_wheel_touched = reg & 0b111;
}
}
+3
View File
@@ -8,6 +8,7 @@
#include <memory>
#include "core/lv_group.h"
#include "hal/lv_hal_indev.h"
#include "relative_wheel.hpp"
@@ -24,6 +25,8 @@ class TouchWheelEncoder {
private:
lv_indev_drv_t driver_;
lv_indev_t* registration_;
lv_key_t last_key_;
std::weak_ptr<drivers::RelativeWheel> wheel_;
};
+18 -22
View File
@@ -6,8 +6,12 @@
#include "screen_menu.hpp"
#include "core/lv_event.h"
#include "esp_log.h"
#include "core/lv_group.h"
#include "core/lv_obj_pos.h"
#include "extra/widgets/list/lv_list.h"
#include "extra/widgets/menu/lv_menu.h"
#include "extra/widgets/spinner/lv_spinner.h"
#include "hal/lv_hal_disp.h"
@@ -17,33 +21,25 @@
namespace ui {
namespace screens {
static void item_click_cb(lv_event_t* ev) {
ESP_LOGI("menu", "clicked!");
}
Menu::Menu() {
lv_obj_t* menu = lv_menu_create(root_);
lv_obj_set_size(menu, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));
lv_obj_center(menu);
lv_obj_t* list = lv_list_create(root_);
lv_obj_set_size(list, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));
lv_obj_center(list);
lv_obj_t* main_page = lv_menu_page_create(menu, NULL);
lv_obj_t* button;
lv_obj_t* container;
lv_obj_t* label;
button = lv_list_add_btn(list, NULL, "hi");
lv_obj_add_event_cb(button, item_click_cb, LV_EVENT_CLICKED, NULL);
container = lv_menu_cont_create(main_page);
label = lv_label_create(container);
lv_label_set_text(label, "I am an item");
button = lv_list_add_btn(list, NULL, "second");
lv_obj_add_event_cb(button, item_click_cb, LV_EVENT_CLICKED, NULL);
container = lv_menu_cont_create(main_page);
label = lv_label_create(container);
lv_label_set_text(label, "I am also an item");
container = lv_menu_cont_create(main_page);
label = lv_label_create(container);
lv_label_set_text(label, "Item #3");
container = lv_menu_cont_create(main_page);
label = lv_label_create(container);
lv_label_set_text(label, "Yay!");
lv_menu_set_page(menu, main_page);
button = lv_list_add_btn(list, NULL, "third");
lv_obj_add_event_cb(button, item_click_cb, LV_EVENT_CLICKED, NULL);
}
Menu::~Menu() {}
+6 -5
View File
@@ -5,6 +5,8 @@
*/
#include "wheel_encoder.hpp"
#include <sys/_stdint.h>
#include "core/lv_group.h"
#include "hal/lv_hal_indev.h"
namespace ui {
@@ -17,7 +19,7 @@ void encoder_read(lv_indev_drv_t* drv, lv_indev_data_t* data) {
TouchWheelEncoder::TouchWheelEncoder(
std::weak_ptr<drivers::RelativeWheel> wheel)
: wheel_(wheel) {
: last_key_(0), wheel_(wheel) {
lv_indev_drv_init(&driver_);
driver_.type = LV_INDEV_TYPE_ENCODER;
driver_.read_cb = encoder_read;
@@ -29,15 +31,14 @@ TouchWheelEncoder::TouchWheelEncoder(
auto TouchWheelEncoder::Read(lv_indev_data_t* data) -> void {
auto lock = wheel_.lock();
if (lock == nullptr) {
data->state = LV_INDEV_STATE_RELEASED;
data->enc_diff = 0;
return;
}
lock->Update();
data->state =
lock->is_pressed() ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
data->enc_diff = lock->ticks();
data->state =
lock->is_clicking() ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
}
} // namespace ui