|
| 1 | +# MIT License (MIT) |
| 2 | +# Copyright (c) 2022 Mike Teachman |
| 3 | +# https://opensource.org/licenses/MIT |
| 4 | + |
| 5 | +# Platform-independent MicroPython code for the rotary encoder module |
| 6 | + |
| 7 | +# Documentation: |
| 8 | +# https://github.com/MikeTeachman/micropython-rotary |
| 9 | + |
| 10 | +import micropython |
| 11 | + |
| 12 | +_DIR_CW = const(0x10) # Clockwise step |
| 13 | +_DIR_CCW = const(0x20) # Counter-clockwise step |
| 14 | + |
| 15 | +# Rotary Encoder States |
| 16 | +_R_START = const(0x0) |
| 17 | +_R_CW_1 = const(0x1) |
| 18 | +_R_CW_2 = const(0x2) |
| 19 | +_R_CW_3 = const(0x3) |
| 20 | +_R_CCW_1 = const(0x4) |
| 21 | +_R_CCW_2 = const(0x5) |
| 22 | +_R_CCW_3 = const(0x6) |
| 23 | +_R_ILLEGAL = const(0x7) |
| 24 | + |
| 25 | +_transition_table = [ |
| 26 | + |
| 27 | + # |------------- NEXT STATE -------------| |CURRENT STATE| |
| 28 | + # CLK/DT CLK/DT CLK/DT CLK/DT |
| 29 | + # 00 01 10 11 |
| 30 | + [_R_START, _R_CCW_1, _R_CW_1, _R_START], # _R_START |
| 31 | + [_R_CW_2, _R_START, _R_CW_1, _R_START], # _R_CW_1 |
| 32 | + [_R_CW_2, _R_CW_3, _R_CW_1, _R_START], # _R_CW_2 |
| 33 | + [_R_CW_2, _R_CW_3, _R_START, _R_START | _DIR_CW], # _R_CW_3 |
| 34 | + [_R_CCW_2, _R_CCW_1, _R_START, _R_START], # _R_CCW_1 |
| 35 | + [_R_CCW_2, _R_CCW_1, _R_CCW_3, _R_START], # _R_CCW_2 |
| 36 | + [_R_CCW_2, _R_START, _R_CCW_3, _R_START | _DIR_CCW], # _R_CCW_3 |
| 37 | + [_R_START, _R_START, _R_START, _R_START]] # _R_ILLEGAL |
| 38 | + |
| 39 | +_transition_table_half_step = [ |
| 40 | + [_R_CW_3, _R_CW_2, _R_CW_1, _R_START], |
| 41 | + [_R_CW_3 | _DIR_CCW, _R_START, _R_CW_1, _R_START], |
| 42 | + [_R_CW_3 | _DIR_CW, _R_CW_2, _R_START, _R_START], |
| 43 | + [_R_CW_3, _R_CCW_2, _R_CCW_1, _R_START], |
| 44 | + [_R_CW_3, _R_CW_2, _R_CCW_1, _R_START | _DIR_CW], |
| 45 | + [_R_CW_3, _R_CCW_2, _R_CW_3, _R_START | _DIR_CCW], |
| 46 | + [_R_START, _R_START, _R_START, _R_START], |
| 47 | + [_R_START, _R_START, _R_START, _R_START]] |
| 48 | + |
| 49 | +_STATE_MASK = const(0x07) |
| 50 | +_DIR_MASK = const(0x30) |
| 51 | + |
| 52 | + |
| 53 | +def _wrap(value, incr, lower_bound, upper_bound): |
| 54 | + range = upper_bound - lower_bound + 1 |
| 55 | + value = value + incr |
| 56 | + |
| 57 | + if value < lower_bound: |
| 58 | + value += range * ((lower_bound - value) // range + 1) |
| 59 | + |
| 60 | + return lower_bound + (value - lower_bound) % range |
| 61 | + |
| 62 | + |
| 63 | +def _bound(value, incr, lower_bound, upper_bound): |
| 64 | + return min(upper_bound, max(lower_bound, value + incr)) |
| 65 | + |
| 66 | + |
| 67 | +def _trigger(rotary_instance): |
| 68 | + for listener in rotary_instance._listener: |
| 69 | + listener() |
| 70 | + |
| 71 | + |
| 72 | +class Rotary(object): |
| 73 | + |
| 74 | + RANGE_UNBOUNDED = const(1) |
| 75 | + RANGE_WRAP = const(2) |
| 76 | + RANGE_BOUNDED = const(3) |
| 77 | + |
| 78 | + def __init__(self, min_val, max_val, incr, reverse, range_mode, half_step, invert): |
| 79 | + self._min_val = min_val |
| 80 | + self._max_val = max_val |
| 81 | + self._incr = incr |
| 82 | + self._reverse = -1 if reverse else 1 |
| 83 | + self._range_mode = range_mode |
| 84 | + self._value = min_val |
| 85 | + self._state = _R_START |
| 86 | + self._half_step = half_step |
| 87 | + self._invert = invert |
| 88 | + self._listener = [] |
| 89 | + |
| 90 | + def set(self, value=None, min_val=None, incr=None, |
| 91 | + max_val=None, reverse=None, range_mode=None): |
| 92 | + # disable DT and CLK pin interrupts |
| 93 | + self._hal_disable_irq() |
| 94 | + |
| 95 | + if value is not None: |
| 96 | + self._value = value |
| 97 | + if min_val is not None: |
| 98 | + self._min_val = min_val |
| 99 | + if max_val is not None: |
| 100 | + self._max_val = max_val |
| 101 | + if incr is not None: |
| 102 | + self._incr = incr |
| 103 | + if reverse is not None: |
| 104 | + self._reverse = -1 if reverse else 1 |
| 105 | + if range_mode is not None: |
| 106 | + self._range_mode = range_mode |
| 107 | + self._state = _R_START |
| 108 | + |
| 109 | + # enable DT and CLK pin interrupts |
| 110 | + self._hal_enable_irq() |
| 111 | + |
| 112 | + def value(self): |
| 113 | + return self._value |
| 114 | + |
| 115 | + def reset(self): |
| 116 | + self._value = 0 |
| 117 | + |
| 118 | + def close(self): |
| 119 | + self._hal_close() |
| 120 | + |
| 121 | + def add_listener(self, l): |
| 122 | + self._listener.append(l) |
| 123 | + |
| 124 | + def remove_listener(self, l): |
| 125 | + if l not in self._listener: |
| 126 | + raise ValueError('{} is not an installed listener'.format(l)) |
| 127 | + self._listener.remove(l) |
| 128 | + |
| 129 | + def _process_rotary_pins(self, pin): |
| 130 | + old_value = self._value |
| 131 | + clk_dt_pins = (self._hal_get_clk_value() << |
| 132 | + 1) | self._hal_get_dt_value() |
| 133 | + |
| 134 | + if self._invert: |
| 135 | + clk_dt_pins = ~clk_dt_pins & 0x03 |
| 136 | + |
| 137 | + # Determine next state |
| 138 | + if self._half_step: |
| 139 | + self._state = _transition_table_half_step[self._state & |
| 140 | + _STATE_MASK][clk_dt_pins] |
| 141 | + else: |
| 142 | + self._state = _transition_table[self._state & |
| 143 | + _STATE_MASK][clk_dt_pins] |
| 144 | + direction = self._state & _DIR_MASK |
| 145 | + |
| 146 | + incr = 0 |
| 147 | + if direction == _DIR_CW: |
| 148 | + incr = self._incr |
| 149 | + elif direction == _DIR_CCW: |
| 150 | + incr = -self._incr |
| 151 | + |
| 152 | + incr *= self._reverse |
| 153 | + |
| 154 | + if self._range_mode == self.RANGE_WRAP: |
| 155 | + self._value = _wrap( |
| 156 | + self._value, |
| 157 | + incr, |
| 158 | + self._min_val, |
| 159 | + self._max_val) |
| 160 | + elif self._range_mode == self.RANGE_BOUNDED: |
| 161 | + self._value = _bound( |
| 162 | + self._value, |
| 163 | + incr, |
| 164 | + self._min_val, |
| 165 | + self._max_val) |
| 166 | + else: |
| 167 | + self._value = self._value + incr |
| 168 | + |
| 169 | + try: |
| 170 | + if old_value != self._value and len(self._listener) != 0: |
| 171 | + _trigger(self) |
| 172 | + except: |
| 173 | + pass |
0 commit comments