Compare commits
1 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
71a017f8ae |
|
|
@ -1,7 +1,8 @@
|
||||||
import gpiod
|
from gpiozero import PWMLED
|
||||||
|
|
||||||
from . import config
|
from . import config
|
||||||
|
|
||||||
|
GPIO_LED_MAX_PWM_VALUE = 0.8
|
||||||
|
|
||||||
class Leds:
|
class Leds:
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
|
@ -16,38 +17,41 @@ class Leds:
|
||||||
def off(self):
|
def off(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class GpioLed:
|
class GpioLed:
|
||||||
def __init__(self, gpio_pin: int):
|
def __init__(self, gpio_pin: int):
|
||||||
self.gpio_pin = gpio_pin
|
self.gpio_pin = gpio_pin
|
||||||
self.led = None
|
self.led = None
|
||||||
|
|
||||||
def enter(self, chip: gpiod.Chip):
|
def enter(self):
|
||||||
self.led = chip.get_line(self.gpio_pin)
|
self.led = PWMLED(
|
||||||
self.led.request(consumer=str(self), type=gpiod.LINE_REQ_DIR_OUT)
|
self.gpio_pin,
|
||||||
|
initial_value=1.0,
|
||||||
|
frequency=200,
|
||||||
|
)
|
||||||
self.off()
|
self.off()
|
||||||
|
|
||||||
def exit(self):
|
def exit(self):
|
||||||
self.off()
|
self.off()
|
||||||
self.led.release()
|
self.led.close()
|
||||||
self.led = None
|
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):
|
def on(self):
|
||||||
self.led.set_value(0)
|
self.set_value(0)
|
||||||
|
|
||||||
def off(self):
|
def off(self):
|
||||||
self.led.set_value(1)
|
self.set_value(1)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'LED{self.gpio_pin:02}'
|
return f'LED{self.gpio_pin:02}'
|
||||||
|
|
||||||
|
|
||||||
class GpioLeds(Leds):
|
class GpioLeds(Leds):
|
||||||
def __init__(self, chip: str, gpio_pins: list[int]):
|
def __init__(self, gpio_pins: list[int]):
|
||||||
self._entered = False
|
self._entered = False
|
||||||
self.chip = gpiod.Chip(chip)
|
|
||||||
self.leds = []
|
self.leds = []
|
||||||
|
|
||||||
for pin in gpio_pins:
|
for pin in gpio_pins:
|
||||||
|
|
@ -57,7 +61,7 @@ class GpioLeds(Leds):
|
||||||
if not self._entered:
|
if not self._entered:
|
||||||
self._entered = True
|
self._entered = True
|
||||||
for led in self.leds:
|
for led in self.leds:
|
||||||
led.enter(self.chip)
|
led.enter()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
|
|
@ -143,7 +147,7 @@ class DummyLeds(Leds):
|
||||||
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:
|
def get() -> Leds:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue