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
custom
jacqueline 2 years ago
parent c124c8f94d
commit 9763cc955c
  1. 5
      src/drivers/include/relative_wheel.hpp
  2. 3
      src/drivers/include/touchwheel.hpp
  3. 16
      src/drivers/relative_wheel.cpp
  4. 5
      src/drivers/touchwheel.cpp
  5. 3
      src/ui/include/wheel_encoder.hpp
  6. 40
      src/ui/screen_menu.cpp
  7. 11
      src/ui/wheel_encoder.cpp

@ -32,12 +32,13 @@ class RelativeWheel {
auto Update() -> void; auto Update() -> void;
auto is_pressed() -> bool; auto is_clicking() -> bool;
auto ticks() -> std::int_fast16_t; auto ticks() -> std::int_fast16_t;
private: private:
TouchWheel* touch_; TouchWheel* touch_;
bool is_pressed_; bool is_clicking_;
bool was_clicking_;
bool is_first_read_; bool is_first_read_;
std::int_fast16_t ticks_; std::int_fast16_t ticks_;
uint8_t last_angle_; uint8_t last_angle_;

@ -17,7 +17,8 @@
namespace drivers { namespace drivers {
struct TouchWheelData { struct TouchWheelData {
bool is_touched = false; bool is_wheel_touched = false;
bool is_button_touched = false;
uint8_t wheel_position = -1; uint8_t wheel_position = -1;
}; };

@ -15,7 +15,8 @@ namespace drivers {
RelativeWheel::RelativeWheel(TouchWheel* touch) RelativeWheel::RelativeWheel(TouchWheel* touch)
: touch_(touch), : touch_(touch),
is_pressed_(false), is_clicking_(false),
was_clicking_(false),
is_first_read_(true), is_first_read_(true),
ticks_(0), ticks_(0),
last_angle_(0) {} last_angle_(0) {}
@ -23,7 +24,12 @@ RelativeWheel::RelativeWheel(TouchWheel* touch)
auto RelativeWheel::Update() -> void { auto RelativeWheel::Update() -> void {
touch_->Update(); touch_->Update();
TouchWheelData d = touch_->GetTouchWheelData(); 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; uint8_t new_angle = d.wheel_position;
if (is_first_read_) { if (is_first_read_) {
@ -61,8 +67,10 @@ auto RelativeWheel::Update() -> void {
ticks_ = change; ticks_ = change;
} }
auto RelativeWheel::is_pressed() -> bool { auto RelativeWheel::is_clicking() -> bool {
return is_pressed_; bool ret = is_clicking_;
is_clicking_ = 0;
return ret;
} }
auto RelativeWheel::ticks() -> std::int_fast16_t { auto RelativeWheel::ticks() -> std::int_fast16_t {

@ -105,8 +105,9 @@ void TouchWheel::Update() {
} }
if (status & 0b1) { if (status & 0b1) {
// Key detect. Note that the touchwheel keys also trigger this. // Key detect. Note that the touchwheel keys also trigger this.
// TODO(daniel): Do something with this. uint8_t reg = ReadRegister(Register::KEY_STATUS_A);
// bool centre_key = ReadRegister(Register::KEY_STATUS_A) & 0b1000; data_.is_button_touched = reg & 0b1000;
data_.is_wheel_touched = reg & 0b111;
} }
} }

@ -8,6 +8,7 @@
#include <memory> #include <memory>
#include "core/lv_group.h"
#include "hal/lv_hal_indev.h" #include "hal/lv_hal_indev.h"
#include "relative_wheel.hpp" #include "relative_wheel.hpp"
@ -24,6 +25,8 @@ class TouchWheelEncoder {
private: private:
lv_indev_drv_t driver_; lv_indev_drv_t driver_;
lv_indev_t* registration_; lv_indev_t* registration_;
lv_key_t last_key_;
std::weak_ptr<drivers::RelativeWheel> wheel_; std::weak_ptr<drivers::RelativeWheel> wheel_;
}; };

@ -6,8 +6,12 @@
#include "screen_menu.hpp" #include "screen_menu.hpp"
#include "core/lv_event.h"
#include "esp_log.h"
#include "core/lv_group.h" #include "core/lv_group.h"
#include "core/lv_obj_pos.h" #include "core/lv_obj_pos.h"
#include "extra/widgets/list/lv_list.h"
#include "extra/widgets/menu/lv_menu.h" #include "extra/widgets/menu/lv_menu.h"
#include "extra/widgets/spinner/lv_spinner.h" #include "extra/widgets/spinner/lv_spinner.h"
#include "hal/lv_hal_disp.h" #include "hal/lv_hal_disp.h"
@ -17,33 +21,25 @@
namespace ui { namespace ui {
namespace screens { namespace screens {
Menu::Menu() { static void item_click_cb(lv_event_t* ev) {
lv_obj_t* menu = lv_menu_create(root_); ESP_LOGI("menu", "clicked!");
lv_obj_set_size(menu, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL)); }
lv_obj_center(menu);
lv_obj_t* main_page = lv_menu_page_create(menu, NULL);
lv_obj_t* container;
lv_obj_t* label;
container = lv_menu_cont_create(main_page); Menu::Menu() {
label = lv_label_create(container); lv_obj_t* list = lv_list_create(root_);
lv_label_set_text(label, "I am an item"); lv_obj_set_size(list, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));
lv_obj_center(list);
container = lv_menu_cont_create(main_page); lv_obj_t* button;
label = lv_label_create(container);
lv_label_set_text(label, "I am also an item");
container = lv_menu_cont_create(main_page); button = lv_list_add_btn(list, NULL, "hi");
label = lv_label_create(container); lv_obj_add_event_cb(button, item_click_cb, LV_EVENT_CLICKED, NULL);
lv_label_set_text(label, "Item #3");
container = lv_menu_cont_create(main_page); button = lv_list_add_btn(list, NULL, "second");
label = lv_label_create(container); lv_obj_add_event_cb(button, item_click_cb, LV_EVENT_CLICKED, NULL);
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() {} Menu::~Menu() {}

@ -5,6 +5,8 @@
*/ */
#include "wheel_encoder.hpp" #include "wheel_encoder.hpp"
#include <sys/_stdint.h>
#include "core/lv_group.h"
#include "hal/lv_hal_indev.h" #include "hal/lv_hal_indev.h"
namespace ui { namespace ui {
@ -17,7 +19,7 @@ void encoder_read(lv_indev_drv_t* drv, lv_indev_data_t* data) {
TouchWheelEncoder::TouchWheelEncoder( TouchWheelEncoder::TouchWheelEncoder(
std::weak_ptr<drivers::RelativeWheel> wheel) std::weak_ptr<drivers::RelativeWheel> wheel)
: wheel_(wheel) { : last_key_(0), wheel_(wheel) {
lv_indev_drv_init(&driver_); lv_indev_drv_init(&driver_);
driver_.type = LV_INDEV_TYPE_ENCODER; driver_.type = LV_INDEV_TYPE_ENCODER;
driver_.read_cb = encoder_read; driver_.read_cb = encoder_read;
@ -29,15 +31,14 @@ TouchWheelEncoder::TouchWheelEncoder(
auto TouchWheelEncoder::Read(lv_indev_data_t* data) -> void { auto TouchWheelEncoder::Read(lv_indev_data_t* data) -> void {
auto lock = wheel_.lock(); auto lock = wheel_.lock();
if (lock == nullptr) { if (lock == nullptr) {
data->state = LV_INDEV_STATE_RELEASED;
data->enc_diff = 0;
return; return;
} }
lock->Update(); lock->Update();
data->state =
lock->is_pressed() ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
data->enc_diff = lock->ticks(); data->enc_diff = lock->ticks();
data->state =
lock->is_clicking() ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
} }
} // namespace ui } // namespace ui

Loading…
Cancel
Save