-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathencoder_input.hpp
123 lines (107 loc) · 3.47 KB
/
encoder_input.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#pragma once
#include <algorithm>
#include <functional>
#include "lvgl.h"
#include "sdkconfig.h"
#include "logger.hpp"
namespace espp {
/**
* @brief Light wrapper around LVGL input device driver, specifically
* designed for encoders with optional home buttons.
*/
class EncoderInput {
public:
typedef std::function<void(int *enc_diff, bool button_pressed)> read_fn;
/**
* @brief Configuration structure, containing the read function for the
* encoder itself.
*/
struct Config {
read_fn read; /**< Input function for the encoder and button itself. */
Logger::Verbosity log_level{
Logger::Verbosity::WARN}; /**< Log verbosity for the input driver. */
};
/**
* @brief Initialize and register the input drivers associated with the
* encoder.
* @param config Configuration structure for the EncoderInput.
*/
explicit EncoderInput(const Config &config)
: read_(config.read), logger_({.tag = "EncoderInput", .level = config.log_level}) {
init();
}
/**
* @brief Unregister the input drivers associated with the Encoder.
*/
~EncoderInput() {
if (indev_encoder_) {
lv_indev_delete(indev_encoder_);
}
if (indev_button_) {
lv_indev_delete(indev_button_);
}
}
/**
* @brief Get the input device driver associated with the encoder.
* @return The input device driver associated with the encoder.
*/
lv_indev_t *get_encoder_input_device() { return indev_encoder_; }
/**
* @brief Get the input device driver associated with the button.
* @return The input device driver associated with the button.
*/
lv_indev_t *get_button_input_device() { return indev_button_; }
protected:
static void encoder_read(lv_indev_drv_t *drv, lv_indev_data_t *data) {
EncoderInput *ei = (EncoderInput *)drv->user_data;
if (ei) {
ei->encoder_read_impl(data);
}
}
void encoder_read_impl(lv_indev_data_t *data) {
int enc_diff;
bool button_pressed;
if (!read_) {
logger_.error("Invalid read function!");
return;
}
read_(&enc_diff, &button_pressed);
if (button_pressed) {
button_pressed_ = true;
}
data->state = (button_pressed) ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
data->enc_diff = enc_diff;
}
static void button_read(lv_indev_drv_t *drv, lv_indev_data_t *data) {
EncoderInput *ei = (EncoderInput *)drv->user_data;
if (ei) {
ei->home_button_read_impl(data);
}
}
void button_read_impl(lv_indev_data_t *data) const {
data->state = button_pressed_ ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
}
void init() {
using namespace std::placeholders;
logger_.info("Add encoder input device to LVGL");
lv_indev_drv_init(&indev_drv_enc_);
indev_drv_enc_.type = LV_INDEV_TYPE_POINTER;
indev_drv_enc_.read_cb = &EncoderInput::encoder_read;
indev_drv_enc_.user_data = (void *)this;
indev_encoder_ = lv_indev_drv_register(&indev_drv_enc_);
logger_.info("Add button input to LVGL");
lv_indev_drv_init(&indev_drv_btn_);
indev_drv_btn_.type = LV_INDEV_TYPE_BUTTON;
indev_drv_btn_.read_cb = &EncoderInput::button_read;
indev_drv_btn_.user_data = (void *)this;
indev_button_ = lv_indev_drv_register(&indev_drv_btn_);
}
encoder_read_fn encoder_read_;
std::atomic<bool> button_pressed_{false};
lv_indev_drv_t indev_drv_enc_;
lv_indev_t *indev_encoder_;
lv_indev_drv_t indev_drv_btn_;
lv_indev_t *indev_button_;
Logger logger_;
};
} // namespace espp