summaryrefslogtreecommitdiff
path: root/kvmd/atx.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/atx.py
parentf3946f102fc167efdc53c73412b2c0d6ac6c72c5 (diff)
moved kvmd to the root
Diffstat (limited to 'kvmd/atx.py')
-rw-r--r--kvmd/atx.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/kvmd/atx.py b/kvmd/atx.py
new file mode 100644
index 00000000..9ce97386
--- /dev/null
+++ b/kvmd/atx.py
@@ -0,0 +1,63 @@
+import asyncio
+
+from typing import Dict
+
+from .logging import get_logger
+
+from . import aioregion
+from . import gpio
+
+
+# =====
+class AtxIsBusy(aioregion.RegionIsBusyError):
+ pass
+
+
+class Atx:
+ def __init__(
+ self,
+ power_led: int,
+ hdd_led: int,
+
+ power_switch: int,
+ reset_switch: int,
+ click_delay: float,
+ long_click_delay: float,
+ ) -> None:
+
+ self.__power_led = gpio.set_input(power_led)
+ self.__hdd_led = gpio.set_input(hdd_led)
+
+ self.__power_switch = gpio.set_output(power_switch)
+ self.__reset_switch = gpio.set_output(reset_switch)
+ self.__click_delay = click_delay
+ self.__long_click_delay = long_click_delay
+
+ self.__region = aioregion.AioExclusiveRegion(AtxIsBusy)
+
+ def get_state(self) -> Dict:
+ return {
+ "busy": self.__region.is_busy(),
+ "leds": {
+ "power": (not gpio.read(self.__power_led)),
+ "hdd": (not gpio.read(self.__hdd_led)),
+ },
+ }
+
+ async def click_power(self) -> None:
+ await self.__click(self.__power_switch, self.__click_delay)
+ get_logger().info("Clicked power")
+
+ async def click_power_long(self) -> None:
+ await self.__click(self.__power_switch, self.__long_click_delay)
+ get_logger().info("Clicked power (long press)")
+
+ async def click_reset(self) -> None:
+ await self.__click(self.__reset_switch, self.__click_delay)
+ get_logger().info("Clicked reset")
+
+ async def __click(self, pin: int, delay: float) -> None:
+ with self.__region:
+ for flag in (True, False):
+ gpio.write(pin, flag)
+ await asyncio.sleep(delay)