Skip to content

Commit

Permalink
added invert argument
Browse files Browse the repository at this point in the history
  • Loading branch information
miketeachman committed Apr 17, 2022
1 parent 419ac44 commit e767312
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ ampy -pCOMx put rotary_irq_pyb.py
reverse=False,
range_mode=RotaryIRQ.RANGE_UNBOUNDED,
pull_up=False,
half_step=False)
half_step=False,
invert=False)
```
| argument | description | value |
|-------------|-------------|---------|
Expand All @@ -50,8 +51,9 @@ ampy -pCOMx put rotary_irq_pyb.py
| max_val | maximum value in the encoder range (not used when range_mode = RANGE_UNBOUNDED) | integer |
| reverse | reverse count direction | True or False(default) |
| range_mode | count behavior at min_val and max_val | RotaryIRQ.RANGE_UNBOUNDED(default) RotaryIRQ.RANGE_WRAP RotaryIRQ.RANGE_BOUNDED |
| pull_up | enable internal pull up resistors (use when rotary encoder hardware lacks pull up resistors) | True or False(default) |
| pull_up | enable internal pull up resistors. Use when rotary encoder hardware lacks pull up resistors | True or False(default) |
| half_step | half-step mode | True or False(default) |
| invert | invert the CLK and DT signals. Use when encoder resting value is CLK, DT = 00 | True or False(default) |

| range_mode | description |
| ------------- | ------------- |
Expand Down Expand Up @@ -126,12 +128,7 @@ while True:
#### Rotary Encoders
* KY-040 rotary encoder

#### MicroPython versions
* MicroPython v1.12
* MicroPython v1.13
* MicroPython v1.14

### Rotary Encoder Wiring
### Wiring for KY-040 encoder
| Encoder Pin | Connection |
| ------------- |:-------------:|
| + | 3.3V |
Expand Down
13 changes: 10 additions & 3 deletions rotary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# The MIT License (MIT)
# Copyright (c) 2020 Mike Teachman
# Copyright (c) 2022 Mike Teachman
# https://opensource.org/licenses/MIT

# Platform-independent MicroPython code for the rotary encoder module
Expand Down Expand Up @@ -42,7 +42,9 @@
[_R_CW_3 | _DIR_CW, _R_CW_2, _R_START, _R_START],
[_R_CW_3, _R_CCW_2, _R_CCW_1, _R_START],
[_R_CW_3, _R_CW_2, _R_CCW_1, _R_START | _DIR_CW],
[_R_CW_3, _R_CCW_2, _R_CW_3, _R_START | _DIR_CCW]]
[_R_CW_3, _R_CCW_2, _R_CW_3, _R_START | _DIR_CCW],
[_R_START, _R_START, _R_START, _R_START],
[_R_START, _R_START, _R_START, _R_START]]

_STATE_MASK = const(0x07)
_DIR_MASK = const(0x30)
Expand Down Expand Up @@ -73,14 +75,15 @@ class Rotary(object):
RANGE_WRAP = const(2)
RANGE_BOUNDED = const(3)

def __init__(self, min_val, max_val, reverse, range_mode, half_step):
def __init__(self, min_val, max_val, reverse, range_mode, half_step, invert):
self._min_val = min_val
self._max_val = max_val
self._reverse = -1 if reverse else 1
self._range_mode = range_mode
self._value = min_val
self._state = _R_START
self._half_step = half_step
self._invert = invert
self._listener = []

def set(self, value=None, min_val=None,
Expand Down Expand Up @@ -124,6 +127,10 @@ def _process_rotary_pins(self, pin):
old_value = self._value
clk_dt_pins = (self._hal_get_clk_value() <<
1) | self._hal_get_dt_value()

if self._invert:
clk_dt_pins = ~clk_dt_pins & 0x03

# Determine next state
if self._half_step:
self._state = _transition_table_half_step[self._state &
Expand Down
4 changes: 2 additions & 2 deletions rotary_irq_esp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class RotaryIRQ(Rotary):

def __init__(self, pin_num_clk, pin_num_dt, min_val=0, max_val=10,
reverse=False, range_mode=Rotary.RANGE_UNBOUNDED, pull_up=False, half_step=False):
reverse=False, range_mode=Rotary.RANGE_UNBOUNDED, pull_up=False, half_step=False, invert=False):

if platform == 'esp8266':
if pin_num_clk in _esp8266_deny_pins:
Expand All @@ -30,7 +30,7 @@ def __init__(self, pin_num_clk, pin_num_dt, min_val=0, max_val=10,
'%s: Pin %d not allowed. Not Available for Interrupt: %s' %
(platform, pin_num_dt, _esp8266_deny_pins))

super().__init__(min_val, max_val, reverse, range_mode, half_step)
super().__init__(min_val, max_val, reverse, range_mode, half_step, invert)

if pull_up == True:
self._pin_clk = Pin(pin_num_clk, Pin.IN, Pin.PULL_UP)
Expand Down
4 changes: 2 additions & 2 deletions rotary_irq_pyb.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
class RotaryIRQ(Rotary):

def __init__(self, pin_num_clk, pin_num_dt, min_val=0, max_val=10,
reverse=False, range_mode=Rotary.RANGE_UNBOUNDED, pull_up=False, half_step=False):
super().__init__(min_val, max_val, reverse, range_mode, half_step)
reverse=False, range_mode=Rotary.RANGE_UNBOUNDED, pull_up=False, half_step=False, invert=False):
super().__init__(min_val, max_val, reverse, range_mode, half_step, invert)

if pull_up == True:
self._pin_clk = Pin(pin_num_clk, Pin.IN, Pin.PULL_UP)
Expand Down
3 changes: 2 additions & 1 deletion rotary_irq_rp2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ def __init__(
range_mode=Rotary.RANGE_UNBOUNDED,
pull_up=False,
half_step=False,
invert=False,
):
super().__init__(min_val, max_val, reverse, range_mode, half_step)
super().__init__(min_val, max_val, reverse, range_mode, half_step, invert)

if pull_up:
self._pin_clk = Pin(pin_num_clk, Pin.IN, Pin.PULL_UP)
Expand Down

0 comments on commit e767312

Please sign in to comment.