Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extra construction support to Encoder.py to support pullup direction from Code.py #891

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/en/encoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ encoder_handler = EncoderHandler()
keyboard.modules = [layers, holdtap, encoder_handler]
```

2. Define the pins for each encoder: `pin_a`, `pin_b` for rotations, `pin_button` for the switch in the encoder. Set switch to `None` if the encoder's button is handled differently (as a part of matrix for example) or not at all. If you want to invert the direction of the encoder, set the 4th (optional) parameter `is_inverted` to `True`. 5th parameter is [encoder divisor](#encoder-resolution) (optional), it can be either `2` or `4`.
2. Define the pins for each encoder: `pin_a`, `pin_b` for rotations, `pin_button` for the switch in the encoder. Set switch to `None` if the encoder's button is handled differently (as a part of matrix for example) or not at all. If you want to invert the direction of the encoder, set the 4th (optional) parameter `is_inverted` to `True`. 5th parameter is [encoder divisor](#encoder-resolution) (optional), it can be either `2` or `4`. If your encoder button pull direction is not the default of `digitalio.Pull.UP`, you may specify the 6th (optional) parameter `button_pull` as `digitalio.Pull.DOWN`.

```python
# Regular GPIO Encoder
encoder_handler.pins = (
Expand Down
28 changes: 21 additions & 7 deletions kmk/modules/encoder.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,15 @@ def vel_report(self):


class GPIOEncoder(BaseEncoder):
def __init__(self, pin_a, pin_b, pin_button=None, is_inverted=False, divisor=None):
def __init__(
self,
pin_a,
pin_b,
pin_button=None,
is_inverted=False,
divisor=None,
button_pull=digitalio.Pull.UP,
):
super().__init__(is_inverted)

# Divisor can be 4 or 2 depending on whether the detent
Expand All @@ -121,9 +129,10 @@ def __init__(self, pin_a, pin_b, pin_button=None, is_inverted=False, divisor=Non

self.pin_a = EncoderPin(pin_a)
self.pin_b = EncoderPin(pin_b)
self.pin_button = (
EncoderPin(pin_button, button_type=True) if pin_button is not None else None
)
if pin_button:
self.pin_button = EncoderPin(pin_button, button_type=True, pull=button_pull)
else:
self.pin_button = None

self._state = (self.pin_a.get_value(), self.pin_b.get_value())
self._start_state = self._state
Expand All @@ -138,9 +147,10 @@ def button_event(self):


class EncoderPin:
def __init__(self, pin, button_type=False):
def __init__(self, pin, button_type=False, pull=digitalio.Pull.UP):
self.pin = pin
self.button_type = button_type
self.pull = pull
self.prepare_pin()

def prepare_pin(self):
Expand All @@ -150,12 +160,16 @@ def prepare_pin(self):
else:
self.io = digitalio.DigitalInOut(self.pin)
self.io.direction = digitalio.Direction.INPUT
self.io.pull = digitalio.Pull.UP
self.io.pull = self.pull
else:
self.io = None

def get_value(self):
return self.io.value
io = self.io
result = io.value
if digitalio.Pull.UP != io.pull:
result = not result
return result


class I2CEncoder(BaseEncoder):
Expand Down