Compare commits
No commits in common. "pwm" and "main" have entirely different histories.
|
|
@ -1,8 +1,7 @@
|
|||
from gpiozero import PWMLED
|
||||
import gpiod
|
||||
|
||||
from . import config
|
||||
|
||||
GPIO_LED_MAX_PWM_VALUE = 0.8
|
||||
|
||||
class Leds:
|
||||
def __enter__(self):
|
||||
|
|
@ -17,41 +16,38 @@ class Leds:
|
|||
def off(self):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
class GpioLed:
|
||||
def __init__(self, gpio_pin: int):
|
||||
self.gpio_pin = gpio_pin
|
||||
self.led = None
|
||||
|
||||
def enter(self):
|
||||
self.led = PWMLED(
|
||||
self.gpio_pin,
|
||||
initial_value=1.0,
|
||||
frequency=200,
|
||||
)
|
||||
def enter(self, chip: gpiod.Chip):
|
||||
self.led = chip.get_line(self.gpio_pin)
|
||||
self.led.request(consumer=str(self), type=gpiod.LINE_REQ_DIR_OUT)
|
||||
self.off()
|
||||
|
||||
def exit(self):
|
||||
self.off()
|
||||
self.led.close()
|
||||
self.led.release()
|
||||
self.led = None
|
||||
|
||||
def set_value(self, value: float):
|
||||
value = min(1.0, max(0.0, value))
|
||||
self.led.set_value(1.0 - GPIO_LED_MAX_PWM_VALUE * value)
|
||||
|
||||
def on(self):
|
||||
self.set_value(0)
|
||||
self.led.set_value(0)
|
||||
|
||||
def off(self):
|
||||
self.set_value(1)
|
||||
self.led.set_value(1)
|
||||
|
||||
def __str__(self):
|
||||
return f'LED{self.gpio_pin:02}'
|
||||
|
||||
|
||||
class GpioLeds(Leds):
|
||||
def __init__(self, gpio_pins: list[int]):
|
||||
def __init__(self, chip: str, gpio_pins: list[int]):
|
||||
self._entered = False
|
||||
self.chip = gpiod.Chip(chip)
|
||||
self.leds = []
|
||||
|
||||
for pin in gpio_pins:
|
||||
|
|
@ -61,7 +57,7 @@ class GpioLeds(Leds):
|
|||
if not self._entered:
|
||||
self._entered = True
|
||||
for led in self.leds:
|
||||
led.enter()
|
||||
led.enter(self.chip)
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
|
|
@ -147,7 +143,7 @@ class DummyLeds(Leds):
|
|||
raise ValueError(f"No LED with UUID {uuid}")
|
||||
|
||||
|
||||
_leds = GpioLeds(config.LEDS_UUIDS) if config.GPIO_CHIP is not None else DummyLeds(config.LEDS_UUIDS)
|
||||
_leds = GpioLeds(config.GPIO_CHIP, config.LEDS_UUIDS) if config.GPIO_CHIP is not None else DummyLeds(config.LEDS_UUIDS)
|
||||
|
||||
|
||||
def get() -> Leds:
|
||||
|
|
|
|||
Loading…
Reference in New Issue