summaryrefslogtreecommitdiff
path: root/kvmd/atx.py
blob: 9ce973866ffde61e6a6b3cab139eba16f177d1e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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)