<commit_before># -*- coding: utf-8 -*-
"""MicroPython rotary encoder library."""

import pyb


ENC_STATES = (0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0)


class Encoder(object):
    def __init__(self, pin_x, pin_y, pin_mode=pyb.Pin.PULL_NONE, scale=1,
                 min=0, max=100, reverse=False):
        self.pin_x = (pin_x if isinstance(pin_x, pyb.Pin) else
                      pyb.Pin(pin_x, pyb.Pin.IN, pin_mode))
        self.pin_y = (pin_y if isinstance(pin_y, pyb.Pin) else
                      pyb.Pin(pin_y, pyb.Pin.IN, pin_mode))

        self.pin_mode = pin_mode
        self.scale = scale
        self.min = min
        self.max = max
        self.reverse = 1 if reverse else -1

        # The following variables are assigned to in the interrupt callback,
        # so we have to allocate them here.
        self._pos = -1
        self._readings = 0
        self._state = 0

        self.set_callbacks(self._callback)

    def _callback(self, line):
        self._readings = (self._readings << 2 | self.pin_x.value() << 1 |
                          self.pin_y.value()) & 0x0f

        self._state = ENC_STATES[self._readings] * self.reverse

        if self._state:
            self._pos = min(max(self.min, self._pos + self._state), self.max)

    def set_callbacks(self, callback=None):
        self.irq_x = pyb.ExtInt(self.pin_x, pyb.ExtInt.IRQ_RISING_FALLING,
                                self.pin_mode, callback)
        self.irq_y = pyb.ExtInt(self.pin_y, pyb.ExtInt.IRQ_RISING_FALLING,
                                self.pin_mode, callback)

    @property
    def position(self):
        return self._pos * self.scale

    def reset(self):
        self._pos = 0<commit_msg>Pyboard/stmhal version of encoder lib inherits from standard/esp version<commit_after># -*- coding: utf-8 -*-
"""MicroPython rotary encoder library for Pyboard/STMHal.

Usage:

    from time import sleep_ms
    from pyb_encoder import Encoder

    enc = Encoder(pin_clk='X11', pin_dt='X12')

    def readloop(e):
        oldval = 0
        while True:
            val = enc.value
            if oldval != val:
                print(val)
                oldval = val
            sleep_ms(50)

    readloop(e)
"""

from machine import Pin
from pyb import ExtInt
from encoder import Encoder as BaseEncoder, test as _test


class Encoder(BaseEncoder):
    def __init__(self, *args, **kwargs):
        self.pin_mode = kwargs.setdefault('pin_mode', Pin.PULL_NONE)
        super().__init__(*args, **kwargs)

    def set_callbacks(self, callback=None):
        mode = ExtInt.IRQ_RISING_FALLING
        self.irq_clk = ExtInt(self.pin_clk, mode, self.pin_mode, callback)
        self.irq_dt = ExtInt(self.pin_dt, mode, self.pin_mode, callback)


def test(enc=None, **kwargs):
    if not enc:
        kwargs.setdefault('pin_clk', 'X11')
        kwargs.setdefault('pin_dt', 'X12')
        kwargs.setdefault('encoder_cls', Encoder)
    _test(enc, **kwargs)
{'fraction_non_alphanumeric': 0.11633412242137302, 'fraction_numerical': 0.014879945891105851, 'mean_word_length': 2.2047670639219934, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3062342', 'n_tokens_mistral': 1030, 'n_tokens_neox': 970, 'n_words': 237}