Support PWM
This commit is contained in:
parent
90bb0e69c0
commit
71a017f8ae
|
|
@ -1,7 +1,8 @@
|
|||
import gpiod
|
||||
from gpiozero import PWMLED
|
||||
|
||||
from . import config
|
||||
|
||||
GPIO_LED_MAX_PWM_VALUE = 0.8
|
||||
|
||||
class Leds:
|
||||
def __enter__(self):
|
||||
|
|
@ -15,39 +16,42 @@ 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, chip: gpiod.Chip):
|
||||
self.led = chip.get_line(self.gpio_pin)
|
||||
self.led.request(consumer=str(self), type=gpiod.LINE_REQ_DIR_OUT)
|
||||
def enter(self):
|
||||
self.led = PWMLED(
|
||||
self.gpio_pin,
|
||||
initial_value=1.0,
|
||||
frequency=200,
|
||||
)
|
||||
self.off()
|
||||
|
||||
def exit(self):
|
||||
self.off()
|
||||
self.led.release()
|
||||
self.led.close()
|
||||
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.led.set_value(0)
|
||||
self.set_value(0)
|
||||
|
||||
def off(self):
|
||||
self.led.set_value(1)
|
||||
self.set_value(1)
|
||||
|
||||
def __str__(self):
|
||||
return f'LED{self.gpio_pin:02}'
|
||||
|
||||
|
||||
class GpioLeds(Leds):
|
||||
def __init__(self, chip: str, gpio_pins: list[int]):
|
||||
def __init__(self, gpio_pins: list[int]):
|
||||
self._entered = False
|
||||
self.chip = gpiod.Chip(chip)
|
||||
self.leds = []
|
||||
|
||||
for pin in gpio_pins:
|
||||
|
|
@ -57,7 +61,7 @@ class GpioLeds(Leds):
|
|||
if not self._entered:
|
||||
self._entered = True
|
||||
for led in self.leds:
|
||||
led.enter(self.chip)
|
||||
led.enter()
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
|
|
@ -73,7 +77,7 @@ class GpioLeds(Leds):
|
|||
def on(self):
|
||||
for led in self.leds:
|
||||
led.on()
|
||||
|
||||
|
||||
def enter(self):
|
||||
return self.__enter__()
|
||||
|
||||
|
|
@ -84,7 +88,7 @@ class GpioLeds(Leds):
|
|||
for led in self.leds:
|
||||
if led.gpio_pin == uuid:
|
||||
return led
|
||||
raise ValueError(f"No LED with UUID {uuid}")
|
||||
raise ValueError(f"No LED with UUID {uuid}")
|
||||
|
||||
class DummyLed:
|
||||
def __init__(self, gpio_pin: int):
|
||||
|
|
@ -129,7 +133,7 @@ class DummyLeds(Leds):
|
|||
def on(self):
|
||||
for led in self.leds:
|
||||
led.on()
|
||||
|
||||
|
||||
def enter(self):
|
||||
return self.__enter__()
|
||||
|
||||
|
|
@ -140,10 +144,10 @@ class DummyLeds(Leds):
|
|||
for led in self.leds:
|
||||
if led.gpio_pin == uuid:
|
||||
return led
|
||||
raise ValueError(f"No LED with UUID {uuid}")
|
||||
raise ValueError(f"No LED with UUID {uuid}")
|
||||
|
||||
|
||||
_leds = GpioLeds(config.GPIO_CHIP, config.LEDS_UUIDS) if config.GPIO_CHIP is not None else DummyLeds(config.LEDS_UUIDS)
|
||||
_leds = GpioLeds(config.LEDS_UUIDS) if config.GPIO_CHIP is not None else DummyLeds(config.LEDS_UUIDS)
|
||||
|
||||
|
||||
def get() -> Leds:
|
||||
|
|
|
|||
Loading…
Reference in New Issue