diff options
Diffstat (limited to 'kvmd/gpio.py')
-rw-r--r-- | kvmd/gpio.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/kvmd/gpio.py b/kvmd/gpio.py new file mode 100644 index 00000000..deb583ca --- /dev/null +++ b/kvmd/gpio.py @@ -0,0 +1,38 @@ +import contextlib + +from typing import Generator + +from RPi import GPIO + +from .logging import get_logger + + +# ===== +def bcm() -> Generator[None, None, None]: + logger = get_logger(2) + GPIO.setmode(GPIO.BCM) + logger.info("Configured GPIO mode as BCM") + try: + yield + finally: + GPIO.cleanup() + logger.info("GPIO cleaned") + + +def set_output(pin: int, initial: bool=False) -> int: + GPIO.setup(pin, GPIO.OUT, initial=initial) + return pin + + +def set_input(pin: int) -> int: + GPIO.setup(pin, GPIO.IN) + return pin + + +def read(pin: int) -> bool: + return bool(GPIO.input(pin)) + + +def write(pin: int, flag: bool) -> None: + GPIO.output(pin, flag) |