Skip to content

Commit 7d4653d

Browse files
committed
Fix enable_internal_pullup to always use pull-ups
Previously, the pull direction was based on btn_pressed_level, which caused pull-downs to be applied to encoder pins when btn_pressed_level was 1. Encoder pins always need pull-ups since they are wired with common to ground. Now always uses pull-ups and documents that the button pin may need a separate pull-down when btn_pressed_level is 1. Fixes #12
1 parent 10a01cd commit 7d4653d

3 files changed

Lines changed: 14 additions & 13 deletions

File tree

encoder.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,8 @@ esp_err_t rotary_encoder_create(const rotary_encoder_config_t *config, rotary_en
230230
io_conf.mode = GPIO_MODE_INPUT;
231231
if (config->enable_internal_pullup)
232232
{
233-
if (re->btn_pressed_level == 0)
234-
{
235-
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
236-
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
237-
}
238-
else
239-
{
240-
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
241-
io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
242-
}
233+
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
234+
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
243235
}
244236
else
245237
{

encoder.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,17 @@ typedef struct
9696
gpio_num_t pin_a; //!< Encoder pin A
9797
gpio_num_t pin_b; //!< Encoder pin B
9898
gpio_num_t pin_btn; //!< Button pin, or GPIO_NUM_NC if unused
99-
uint8_t btn_pressed_level; //!< GPIO level when button is pressed (0 or 1)
100-
bool enable_internal_pullup; //!< Enable internal pull-up/pull-down resistors on GPIO pins
99+
uint8_t btn_pressed_level; //!< GPIO level when button is pressed (0 or 1).
100+
//!< When set to 1, the button pin may need a pull-down
101+
//!< resistor, either external or set via \c gpio_set_pull_mode()
102+
//!< before creating the encoder, as \c enable_internal_pullup
103+
//!< only enables pull-ups.
104+
bool enable_internal_pullup; //!< Enable internal pull-up resistors on all encoder and
105+
//!< button GPIO pins. Pull-ups are always used, which is
106+
//!< correct for encoders wired with common to ground. When
107+
//!< \c btn_pressed_level is 1, the button pin may need a
108+
//!< pull-down instead — use an external resistor or call
109+
//!< \c gpio_set_pull_mode() before creating the encoder.
101110
uint32_t btn_dead_time_us; //!< Button dead time in microseconds
102111
uint32_t btn_long_press_time_us; //!< Long press threshold in microseconds
103112
uint32_t acceleration_threshold_ms; //!< Acceleration threshold in milliseconds (acceleration starts below this interval)

idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
version: 3.0.0
2+
version: 3.0.1
33
description: HW timer-based driver for incremental rotary encoders
44
license: BSD-3
55
targets:

0 commit comments

Comments
 (0)