Files
Spectrometer/spectrometer/encoder.py

36 lines
861 B
Python

import RPi.GPIO as gpio
from dataclasses import dataclass
@dataclass
class Encoder:
pin_a: int
pin_b: int
_curr_steps: int = 0
def setup(self):
if not gpio.getmode():
gpio.setmode(gpio.BCM)
gpio.setup(self.pin_a, gpio.IN)
gpio.setup(self.pin_b, gpio.IN)
gpio.add_event_detect(self.pin_a,
gpio.RISING,
callback=self._event_detect)
gpio.add_event_detect(self.pin_b,
gpio.RISING,
callback=self._event_detect)
def _event_detect(self, pin):
a = gpio.input(self.pin_a)
b = gpio.input(self.pin_b)
if a ^ b:
self._curr_steps += 1 if a else -1
@property
def angle(self) -> float:
return self._curr_steps / 5000 * 360