summaryrefslogtreecommitdiff
path: root/kvmd/gpio.py
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2018-09-26 02:57:24 +0300
committerDevaev Maxim <[email protected]>2018-09-26 02:57:24 +0300
commit81a5311349564a1016c4af2bf18ae872b650e85b (patch)
treed01fd027948494e22ae2c14334b98c1515e5e8a4 /kvmd/gpio.py
parentf3946f102fc167efdc53c73412b2c0d6ac6c72c5 (diff)
moved kvmd to the root
Diffstat (limited to 'kvmd/gpio.py')
-rw-r--r--kvmd/gpio.py38
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)