|
| 1 | +"""GPIO driver — digital I/O abstraction via gpiozero. |
| 2 | + |
| 3 | +gpiozero ships a :class:`~gpiozero.pins.mock.MockFactory` that replaces real |
| 4 | +hardware in CI. The ``mock_gpio`` fixture in ``conftest.py`` activates it |
| 5 | +automatically for all tests — no extra setup needed. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from typing import Protocol |
| 11 | + |
| 12 | + |
| 13 | +class DigitalOutput(Protocol): |
| 14 | + """Abstract digital output pin (LED, relay, buzzer …).""" |
| 15 | + |
| 16 | + @property |
| 17 | + def value(self) -> int: ... |
| 18 | + def on(self) -> None: ... |
| 19 | + def off(self) -> None: ... |
| 20 | + def toggle(self) -> None: ... |
| 21 | + def close(self) -> None: ... |
| 22 | + |
| 23 | + |
| 24 | +class DigitalInput(Protocol): |
| 25 | + """Abstract digital input pin (button, switch, sensor …).""" |
| 26 | + |
| 27 | + @property |
| 28 | + def value(self) -> int: ... |
| 29 | + def close(self) -> None: ... |
| 30 | + |
| 31 | + |
| 32 | +def led(pin: int) -> DigitalOutput: |
| 33 | + """Create a digital output on a BCM pin number. |
| 34 | + |
| 35 | + Args: |
| 36 | + pin: BCM pin number (e.g. ``17``). |
| 37 | + |
| 38 | + Returns: |
| 39 | + A :class:`DigitalOutput` backed by :class:`~gpiozero.LED`. |
| 40 | + """ |
| 41 | + from gpiozero import LED # type: ignore[import-untyped] |
| 42 | + |
| 43 | + return LED(pin) # type: ignore[return-value] |
| 44 | + |
| 45 | + |
| 46 | +def button(pin: int, pull_up: bool = True) -> DigitalInput: |
| 47 | + """Create a digital input on a BCM pin number. |
| 48 | + |
| 49 | + Args: |
| 50 | + pin: BCM pin number (e.g. ``27``). |
| 51 | + pull_up: Use internal pull-up resistor. Set to ``False`` for pull-down. |
| 52 | + |
| 53 | + Returns: |
| 54 | + A :class:`DigitalInput` backed by :class:`~gpiozero.Button`. |
| 55 | + """ |
| 56 | + from gpiozero import Button # type: ignore[import-untyped] |
| 57 | + |
| 58 | + return Button(pin, pull_up=pull_up) # type: ignore[return-value] |
0 commit comments