File: line_settings.py

package info (click to toggle)
libgpiod 2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,108 kB
  • sloc: ansic: 26,612; sh: 7,554; cpp: 4,944; python: 2,426; makefile: 811; xml: 49
file content (72 lines) | stat: -rw-r--r-- 2,560 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
# SPDX-License-Identifier: LGPL-2.1-or-later
# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>

from . import _ext
from dataclasses import dataclass
from datetime import timedelta
from gpiod.line import Direction, Bias, Drive, Edge, Clock, Value

__all__ = "LineSettings"


@dataclass(repr=False)
class LineSettings:
    """
    Stores a set of line properties.
    """

    direction: Direction = Direction.AS_IS
    """Line direction."""
    edge_detection: Edge = Edge.NONE
    """Edge detection setting."""
    bias: Bias = Bias.AS_IS
    """Line bias setting."""
    drive: Drive = Drive.PUSH_PULL
    """Line drive setting."""
    active_low: bool = False
    """Active-low switch."""
    debounce_period: timedelta = timedelta()
    """Debounce period of the line."""
    event_clock: Clock = Clock.MONOTONIC
    """Edge event timestamping clock setting."""
    output_value: Value = Value.INACTIVE
    """Output value of the line."""

    # __repr__ generated by @dataclass uses repr for enum members resulting in
    # an unusable representation as those are of the form: <NAME: $value>
    def __repr__(self):
        return "gpiod.LineSettings(direction=gpiod.line.{}, edge_detection=gpiod.line.{}, bias=gpiod.line.{}, drive=gpiod.line.{}, active_low={}, debounce_period={}, event_clock=gpiod.line.{}, output_value=gpiod.line.{})".format(
            str(self.direction),
            str(self.edge_detection),
            str(self.bias),
            str(self.drive),
            self.active_low,
            repr(self.debounce_period),
            str(self.event_clock),
            str(self.output_value),
        )

    def __str__(self):
        return "<LineSettings direction={} edge_detection={} bias={} drive={} active_low={} debounce_period={} event_clock={} output_value={}>".format(
            self.direction,
            self.edge_detection,
            self.bias,
            self.drive,
            self.active_low,
            self.debounce_period,
            self.event_clock,
            self.output_value,
        )


def _line_settings_to_ext(settings: LineSettings) -> _ext.LineSettings:
    return _ext.LineSettings(
        direction=settings.direction.value,
        edge_detection=settings.edge_detection.value,
        bias=settings.bias.value,
        drive=settings.drive.value,
        active_low=settings.active_low,
        debounce_period=int(settings.debounce_period.total_seconds() * 1000000),
        event_clock=settings.event_clock.value,
        output_value=settings.output_value.value,
    )