| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from . import _ext |
| 5 |
from enum import Enum |
| 6 |
from typing import Optional |
| 7 |
|
| 8 |
|
| 9 |
class Chip: |
| 10 |
""" |
| 11 |
Represents a simulated GPIO chip. |
| 12 |
""" |
| 13 |
|
| 14 |
class Pull(Enum): |
| 15 |
DOWN = _ext.PULL_DOWN |
| 16 |
UP = _ext.PULL_UP |
| 17 |
|
| 18 |
class Value(Enum): |
| 19 |
INACTIVE = _ext.VALUE_INACTIVE |
| 20 |
ACTIVE = _ext.VALUE_ACTIVE |
| 21 |
|
| 22 |
class Direction(Enum): |
| 23 |
INPUT = _ext.DIRECTION_INPUT |
| 24 |
OUTPUT_HIGH = _ext.DIRECTION_OUTPUT_HIGH |
| 25 |
OUTPUT_LOW = _ext.DIRECTION_OUTPUT_LOW |
| 26 |
|
| 27 |
def __init__( |
| 28 |
self, |
| 29 |
label: Optional[str] = None, |
| 30 |
num_lines: Optional[int] = None, |
| 31 |
line_names: Optional[dict[int, str]] = None, |
| 32 |
hogs: Optional[dict[int, tuple[str, Direction]]] = None, |
| 33 |
): |
| 34 |
self._chip = _ext.Chip() |
| 35 |
|
| 36 |
if label: |
| 37 |
self._chip.set_label(label) |
| 38 |
|
| 39 |
if num_lines: |
| 40 |
self._chip.set_num_lines(num_lines) |
| 41 |
|
| 42 |
if line_names: |
| 43 |
for off, name in line_names.items(): |
| 44 |
self._chip.set_line_name(off, name) |
| 45 |
|
| 46 |
if hogs: |
| 47 |
for off, (name, direction) in hogs.items(): |
| 48 |
self._chip.set_hog(off, name, direction.value) |
| 49 |
|
| 50 |
self._chip.enable() |
| 51 |
|
| 52 |
def get_value(self, offset: int) -> Value: |
| 53 |
val = self._chip.get_value(offset) |
| 54 |
return Chip.Value(val) |
| 55 |
|
| 56 |
def set_pull(self, offset: int, pull: Pull) -> None: |
| 57 |
self._chip.set_pull(offset, pull.value) |
| 58 |
|
| 59 |
@property |
| 60 |
def dev_path(self) -> str: |
| 61 |
return self._chip.dev_path |
| 62 |
|
| 63 |
@property |
| 64 |
def name(self) -> str: |
| 65 |
return self._chip.name |