mirror of
https://github.com/miketeachman/micropython-rotary.git
synced 2024-11-14 03:56:28 +00:00
2fa3f1b054
Every encoder click changes the count by "incr". "incr" default=1 for backwards compatibility with existing operation.
71 lines
1.8 KiB
Python
71 lines
1.8 KiB
Python
# MIT License (MIT)
|
|
# Copyright (c) 2020 Mike Teachman
|
|
# Copyright (c) 2021 Eric Moyer
|
|
# https://opensource.org/licenses/MIT
|
|
|
|
# Platform-specific MicroPython code for the rotary encoder module
|
|
# Raspberry Pi Pico implementation
|
|
|
|
# Documentation:
|
|
# https://github.com/MikeTeachman/micropython-rotary
|
|
|
|
from machine import Pin
|
|
from rotary import Rotary
|
|
|
|
IRQ_RISING_FALLING = Pin.IRQ_RISING | Pin.IRQ_FALLING
|
|
|
|
|
|
class RotaryIRQ(Rotary):
|
|
def __init__(
|
|
self,
|
|
pin_num_clk,
|
|
pin_num_dt,
|
|
min_val=0,
|
|
max_val=10,
|
|
incr=1,
|
|
reverse=False,
|
|
range_mode=Rotary.RANGE_UNBOUNDED,
|
|
pull_up=False,
|
|
half_step=False,
|
|
invert=False
|
|
):
|
|
super().__init__(min_val, max_val, incr, reverse, range_mode, half_step, invert)
|
|
|
|
if pull_up:
|
|
self._pin_clk = Pin(pin_num_clk, Pin.IN, Pin.PULL_UP)
|
|
self._pin_dt = Pin(pin_num_dt, Pin.IN, Pin.PULL_UP)
|
|
else:
|
|
self._pin_clk = Pin(pin_num_clk, Pin.IN)
|
|
self._pin_dt = Pin(pin_num_dt, Pin.IN)
|
|
|
|
self._hal_enable_irq()
|
|
|
|
def _enable_clk_irq(self):
|
|
self._pin_clk.irq(self._process_rotary_pins, IRQ_RISING_FALLING)
|
|
|
|
def _enable_dt_irq(self):
|
|
self._pin_dt.irq(self._process_rotary_pins, IRQ_RISING_FALLING)
|
|
|
|
def _disable_clk_irq(self):
|
|
self._pin_clk.irq(None, 0)
|
|
|
|
def _disable_dt_irq(self):
|
|
self._pin_dt.irq(None, 0)
|
|
|
|
def _hal_get_clk_value(self):
|
|
return self._pin_clk.value()
|
|
|
|
def _hal_get_dt_value(self):
|
|
return self._pin_dt.value()
|
|
|
|
def _hal_enable_irq(self):
|
|
self._enable_clk_irq()
|
|
self._enable_dt_irq()
|
|
|
|
def _hal_disable_irq(self):
|
|
self._disable_clk_irq()
|
|
self._disable_dt_irq()
|
|
|
|
def _hal_close(self):
|
|
self._hal_disable_irq()
|