@@ -20,7 +20,7 @@ typedef struct {
2020 button_handle_t btn_handle ; /* Encoder button handlers */
2121 lv_indev_t * indev ; /* LVGL input device driver */
2222 bool btn_enter ; /* Encoder button enter state */
23- knob_event_t event ; /* Encoder event */
23+ int32_t diff ; /* Encoder diff */
2424} lvgl_port_encoder_ctx_t ;
2525
2626/*******************************************************************************
@@ -32,6 +32,7 @@ static void lvgl_port_encoder_btn_down_handler(void *arg, void *arg2);
3232static void lvgl_port_encoder_btn_up_handler (void * arg , void * arg2 );
3333static void lvgl_port_encoder_left_handler (void * arg , void * arg2 );
3434static void lvgl_port_encoder_right_handler (void * arg , void * arg2 );
35+ static int32_t lvgl_port_calculate_diff (knob_handle_t knob , knob_event_t event );
3536
3637/*******************************************************************************
3738* Public API functions
@@ -70,6 +71,7 @@ lv_indev_t *lvgl_port_add_encoder(const lvgl_port_encoder_cfg_t *encoder_cfg)
7071 ESP_ERROR_CHECK (iot_button_register_cb (encoder_ctx -> btn_handle , BUTTON_PRESS_UP , lvgl_port_encoder_btn_up_handler , encoder_ctx ));
7172
7273 encoder_ctx -> btn_enter = false;
74+ encoder_ctx -> diff = 0 ;
7375
7476 lvgl_port_lock (0 );
7577 /* Register a encoder input device */
@@ -132,28 +134,13 @@ esp_err_t lvgl_port_remove_encoder(lv_indev_t *encoder)
132134
133135static void lvgl_port_encoder_read (lv_indev_t * indev_drv , lv_indev_data_t * data )
134136{
135- static int32_t last_v = 0 ;
136-
137137 assert (indev_drv );
138138 lvgl_port_encoder_ctx_t * ctx = (lvgl_port_encoder_ctx_t * )lv_indev_get_driver_data (indev_drv );
139139 assert (ctx );
140140
141- int32_t invd = iot_knob_get_count_value (ctx -> knob_handle );
142- knob_event_t event = ctx -> event ;
143-
144- if (last_v ^ invd ) {
145-
146- int32_t diff = (int32_t )((uint32_t )invd - (uint32_t )last_v );
147-
148- diff += (event == KNOB_RIGHT && invd < last_v ) ? CONFIG_KNOB_HIGH_LIMIT :
149- (event == KNOB_LEFT && invd > last_v ) ? CONFIG_KNOB_LOW_LIMIT : 0 ;
150-
151- data -> enc_diff = diff ;
152- last_v = invd ;
153- } else {
154- data -> enc_diff = 0 ;
155- }
141+ data -> enc_diff = ctx -> diff ;
156142 data -> state = (true == ctx -> btn_enter ) ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED ;
143+ ctx -> diff = 0 ;
157144}
158145
159146static void lvgl_port_encoder_btn_down_handler (void * arg , void * arg2 )
@@ -193,7 +180,7 @@ static void lvgl_port_encoder_left_handler(void *arg, void *arg2)
193180 if (ctx && knob ) {
194181 /* LEFT */
195182 if (knob == ctx -> knob_handle ) {
196- ctx -> event = KNOB_LEFT ;
183+ ctx -> diff += lvgl_port_calculate_diff ( knob , KNOB_LEFT ) ;
197184 }
198185 /* Wake LVGL task, if needed */
199186 lvgl_port_task_wake (LVGL_PORT_EVENT_TOUCH , ctx -> indev );
@@ -207,9 +194,28 @@ static void lvgl_port_encoder_right_handler(void *arg, void *arg2)
207194 if (ctx && knob ) {
208195 /* RIGHT */
209196 if (knob == ctx -> knob_handle ) {
210- ctx -> event = KNOB_RIGHT ;
197+ ctx -> diff += lvgl_port_calculate_diff ( knob , KNOB_RIGHT ) ;
211198 }
212199 /* Wake LVGL task, if needed */
213200 lvgl_port_task_wake (LVGL_PORT_EVENT_TOUCH , ctx -> indev );
214201 }
215202}
203+
204+
205+ static int32_t lvgl_port_calculate_diff (knob_handle_t knob , knob_event_t event )
206+ {
207+ static int32_t last_v = 0 ;
208+
209+ int32_t diff = 0 ;
210+ int32_t invd = iot_knob_get_count_value (knob );
211+
212+ if (last_v ^ invd ) {
213+
214+ diff = (int32_t )((uint32_t )invd - (uint32_t )last_v );
215+ diff += (event == KNOB_RIGHT && invd < last_v ) ? CONFIG_KNOB_HIGH_LIMIT :
216+ (event == KNOB_LEFT && invd > last_v ) ? CONFIG_KNOB_LOW_LIMIT : 0 ;
217+ last_v = invd ;
218+ }
219+
220+ return diff ;
221+ }
0 commit comments