parent
							
								
									f2bad894cd
								
							
						
					
					
						commit
						7d5536e2ab
					
				@ -0,0 +1,46 @@ | 
				
			||||
/*
 | 
				
			||||
 * Copyright 2023 jacqueline <me@jacqueline.id.au> | 
				
			||||
 * | 
				
			||||
 * SPDX-License-Identifier: GPL-3.0-only | 
				
			||||
 */ | 
				
			||||
 | 
				
			||||
#include "encoder_input.hpp" | 
				
			||||
 | 
				
			||||
#include <sys/_stdint.h> | 
				
			||||
#include <memory> | 
				
			||||
 | 
				
			||||
#include "core/lv_group.h" | 
				
			||||
#include "gpios.hpp" | 
				
			||||
#include "hal/lv_hal_indev.h" | 
				
			||||
#include "relative_wheel.hpp" | 
				
			||||
#include "touchwheel.hpp" | 
				
			||||
 | 
				
			||||
namespace ui { | 
				
			||||
 | 
				
			||||
static void encoder_read(lv_indev_drv_t* drv, lv_indev_data_t* data) { | 
				
			||||
  EncoderInput* instance = reinterpret_cast<EncoderInput*>(drv->user_data); | 
				
			||||
  instance->Read(data); | 
				
			||||
} | 
				
			||||
 | 
				
			||||
EncoderInput::EncoderInput(drivers::IGpios& gpios, drivers::TouchWheel& wheel) | 
				
			||||
    : gpios_(gpios), | 
				
			||||
      raw_wheel_(wheel), | 
				
			||||
      relative_wheel_(std::make_unique<drivers::RelativeWheel>(wheel)) { | 
				
			||||
  lv_indev_drv_init(&driver_); | 
				
			||||
  driver_.type = LV_INDEV_TYPE_ENCODER; | 
				
			||||
  driver_.read_cb = encoder_read; | 
				
			||||
  driver_.user_data = this; | 
				
			||||
 | 
				
			||||
  registration_ = lv_indev_drv_register(&driver_); | 
				
			||||
} | 
				
			||||
 | 
				
			||||
auto EncoderInput::Read(lv_indev_data_t* data) -> void { | 
				
			||||
  raw_wheel_.Update(); | 
				
			||||
  relative_wheel_->Update(); | 
				
			||||
 | 
				
			||||
  data->enc_diff = relative_wheel_->ticks(); | 
				
			||||
  data->state = relative_wheel_->is_clicking() ? LV_INDEV_STATE_PRESSED | 
				
			||||
                                               : LV_INDEV_STATE_RELEASED; | 
				
			||||
} | 
				
			||||
 | 
				
			||||
}  // namespace ui
 | 
				
			||||
@ -0,0 +1,47 @@ | 
				
			||||
/*
 | 
				
			||||
 * Copyright 2023 jacqueline <me@jacqueline.id.au> | 
				
			||||
 * | 
				
			||||
 * SPDX-License-Identifier: GPL-3.0-only | 
				
			||||
 */ | 
				
			||||
 | 
				
			||||
#pragma once | 
				
			||||
 | 
				
			||||
#include <memory> | 
				
			||||
 | 
				
			||||
#include "core/lv_group.h" | 
				
			||||
#include "gpios.hpp" | 
				
			||||
#include "hal/lv_hal_indev.h" | 
				
			||||
 | 
				
			||||
#include "relative_wheel.hpp" | 
				
			||||
#include "touchwheel.hpp" | 
				
			||||
 | 
				
			||||
namespace ui { | 
				
			||||
 | 
				
			||||
/*
 | 
				
			||||
 * Main input device abstracting that handles turning lower-level input device | 
				
			||||
 * drivers into events and LVGL inputs. | 
				
			||||
 * | 
				
			||||
 * As far as LVGL is concerned, this class represents an ordinary rotary | 
				
			||||
 * encoder, supporting only left and right ticks, and clicking. | 
				
			||||
 */ | 
				
			||||
class EncoderInput { | 
				
			||||
 public: | 
				
			||||
  EncoderInput(drivers::IGpios& gpios, drivers::TouchWheel& wheel); | 
				
			||||
 | 
				
			||||
  auto Read(lv_indev_data_t* data) -> void; | 
				
			||||
  auto registration() -> lv_indev_t* { return registration_; } | 
				
			||||
 | 
				
			||||
  auto lock(bool l) -> void { is_locked_ = l; } | 
				
			||||
 | 
				
			||||
 private: | 
				
			||||
  lv_indev_drv_t driver_; | 
				
			||||
  lv_indev_t* registration_; | 
				
			||||
 | 
				
			||||
  drivers::IGpios& gpios_; | 
				
			||||
  drivers::TouchWheel& raw_wheel_; | 
				
			||||
  std::unique_ptr<drivers::RelativeWheel> relative_wheel_; | 
				
			||||
 | 
				
			||||
  bool is_locked_; | 
				
			||||
}; | 
				
			||||
 | 
				
			||||
}  // namespace ui
 | 
				
			||||
@ -1,33 +0,0 @@ | 
				
			||||
/*
 | 
				
			||||
 * Copyright 2023 jacqueline <me@jacqueline.id.au> | 
				
			||||
 * | 
				
			||||
 * SPDX-License-Identifier: GPL-3.0-only | 
				
			||||
 */ | 
				
			||||
 | 
				
			||||
#pragma once | 
				
			||||
 | 
				
			||||
#include <memory> | 
				
			||||
 | 
				
			||||
#include "core/lv_group.h" | 
				
			||||
#include "hal/lv_hal_indev.h" | 
				
			||||
 | 
				
			||||
#include "relative_wheel.hpp" | 
				
			||||
 | 
				
			||||
namespace ui { | 
				
			||||
 | 
				
			||||
class TouchWheelEncoder { | 
				
			||||
 public: | 
				
			||||
  explicit TouchWheelEncoder(std::unique_ptr<drivers::RelativeWheel> wheel); | 
				
			||||
 | 
				
			||||
  auto Read(lv_indev_data_t* data) -> void; | 
				
			||||
  auto registration() -> lv_indev_t* { return registration_; } | 
				
			||||
 | 
				
			||||
 private: | 
				
			||||
  lv_indev_drv_t driver_; | 
				
			||||
  lv_indev_t* registration_; | 
				
			||||
 | 
				
			||||
  lv_key_t last_key_; | 
				
			||||
  std::unique_ptr<drivers::RelativeWheel> wheel_; | 
				
			||||
}; | 
				
			||||
 | 
				
			||||
}  // namespace ui
 | 
				
			||||
@ -1,44 +0,0 @@ | 
				
			||||
/*
 | 
				
			||||
 * Copyright 2023 jacqueline <me@jacqueline.id.au> | 
				
			||||
 * | 
				
			||||
 * SPDX-License-Identifier: GPL-3.0-only | 
				
			||||
 */ | 
				
			||||
 | 
				
			||||
#include "wheel_encoder.hpp" | 
				
			||||
#include <sys/_stdint.h> | 
				
			||||
#include "core/lv_group.h" | 
				
			||||
#include "hal/lv_hal_indev.h" | 
				
			||||
 | 
				
			||||
namespace ui { | 
				
			||||
 | 
				
			||||
void encoder_read(lv_indev_drv_t* drv, lv_indev_data_t* data) { | 
				
			||||
  TouchWheelEncoder* instance = | 
				
			||||
      reinterpret_cast<TouchWheelEncoder*>(drv->user_data); | 
				
			||||
  instance->Read(data); | 
				
			||||
} | 
				
			||||
 | 
				
			||||
void encoder_feedback(lv_indev_drv_t* drv, uint8_t event_code) { | 
				
			||||
  ESP_LOGI("Touchwheel Event", "Event code: %d", event_code); | 
				
			||||
} | 
				
			||||
 | 
				
			||||
TouchWheelEncoder::TouchWheelEncoder( | 
				
			||||
    std::unique_ptr<drivers::RelativeWheel> wheel) | 
				
			||||
    : last_key_(0), wheel_(std::move(wheel)) { | 
				
			||||
  lv_indev_drv_init(&driver_); | 
				
			||||
  driver_.type = LV_INDEV_TYPE_ENCODER; | 
				
			||||
  driver_.read_cb = encoder_read; | 
				
			||||
  // driver_.feedback_cb = encoder_feedback;
 | 
				
			||||
  driver_.user_data = this; | 
				
			||||
 | 
				
			||||
  registration_ = lv_indev_drv_register(&driver_); | 
				
			||||
} | 
				
			||||
 | 
				
			||||
auto TouchWheelEncoder::Read(lv_indev_data_t* data) -> void { | 
				
			||||
  wheel_->Update(); | 
				
			||||
 | 
				
			||||
  data->enc_diff = wheel_->ticks(); | 
				
			||||
  data->state = | 
				
			||||
      wheel_->is_clicking() ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED; | 
				
			||||
} | 
				
			||||
 | 
				
			||||
}  // namespace ui
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue