18
18
from micropython import const
19
19
from adafruit_onewire .device import OneWireDevice
20
20
21
+ try :
22
+ import typing # pylint: disable=unused-import
23
+ from typing_extensions import Literal
24
+ from adafruit_onewire .bus import OneWireBus # pylint: disable=ungrouped-imports
25
+ except ImportError :
26
+ pass
27
+
21
28
_DS2413_ACCESS_READ = b"\xF5 "
22
29
_DS2413_ACCESS_WRITE = b"\x5A "
23
30
_DS2413_ACK_SUCCESS = b"\xAA "
29
36
class DS2413Pin :
30
37
"""Class which provides interface to single DS2413 GPIO pin."""
31
38
32
- def __init__ (self , number , host , direction = OUTPUT ):
39
+ def __init__ (
40
+ self , number : Literal [0 , 1 ], host , direction : Literal [0 , 1 ] = OUTPUT
41
+ ) -> None :
33
42
if number not in (0 , 1 ):
34
43
raise ValueError ("Incorrect pin number." )
35
44
self ._number = number
@@ -39,20 +48,20 @@ def __init__(self, number, host, direction=OUTPUT):
39
48
self .direction = direction # set it through setter
40
49
41
50
@property
42
- def direction (self ):
51
+ def direction (self ) -> Literal [ 0 , 1 ] :
43
52
"""The direction of the pin, either INPUT or OUTPUT."""
44
53
return self ._direction
45
54
46
55
@direction .setter
47
- def direction (self , direction ) :
56
+ def direction (self , direction : Literal [ 0 , 1 ]) -> None :
48
57
if direction not in (INPUT , OUTPUT ):
49
58
raise ValueError ("Incorrect direction setting." )
50
59
self ._direction = OUTPUT
51
60
self .value = False
52
61
self ._direction = direction
53
62
54
63
@property
55
- def value (self ):
64
+ def value (self ) -> bool :
56
65
"""The pin state if configured as INPUT. The output latch state
57
66
if configured as OUTPUT. True is HIGH/ON, False is LOW/OFF."""
58
67
# return Pin State if configured for INPUT
@@ -61,7 +70,7 @@ def value(self):
61
70
return not self ._host .pio_state & (self ._mask << self ._direction )
62
71
63
72
@value .setter
64
- def value (self , state ) :
73
+ def value (self , state : bool ) -> None :
65
74
# This only makes sense if the pin is configured for OUTPUT.
66
75
if self ._direction == INPUT :
67
76
raise RuntimeError ("Can't set value when pin is set to input." )
@@ -84,7 +93,7 @@ def value(self, state):
84
93
class DS2413 :
85
94
"""Class which provides interface to DS2413 GPIO breakout."""
86
95
87
- def __init__ (self , bus , address ) :
96
+ def __init__ (self , bus : OneWireBus , address : int ) -> None :
88
97
if address .family_code == 0x3A :
89
98
self ._address = address
90
99
self ._device = OneWireDevice (bus , address )
@@ -95,35 +104,35 @@ def __init__(self, bus, address):
95
104
raise RuntimeError ("Incorrect family code in device address." )
96
105
97
106
@property
98
- def IOA (self ):
107
+ def IOA (self ) -> DS2413Pin :
99
108
"""The pin object for channel A."""
100
109
if self ._IOA is None :
101
110
self ._IOA = DS2413Pin (0 , self )
102
111
return self ._IOA
103
112
104
113
@property
105
- def IOB (self ):
114
+ def IOB (self ) -> DS2413Pin :
106
115
"""The pin object for channel B."""
107
116
if self ._IOB is None :
108
117
self ._IOB = DS2413Pin (1 , self )
109
118
return self ._IOB
110
119
111
120
@property
112
- def pio_state (self ):
121
+ def pio_state (self ) -> int :
113
122
"""The state of both PIO channels."""
114
123
return self ._read_status ()
115
124
116
125
@pio_state .setter
117
- def pio_state (self , value ) :
126
+ def pio_state (self , value : int ) -> None :
118
127
return self ._write_latches (value )
119
128
120
- def _read_status (self ):
129
+ def _read_status (self ) -> int :
121
130
with self ._device as dev :
122
131
dev .write (_DS2413_ACCESS_READ )
123
132
dev .readinto (self ._buf , end = 1 )
124
133
return self ._buf [0 ]
125
134
126
- def _write_latches (self , value ) :
135
+ def _write_latches (self , value : int ) -> None :
127
136
# top six bits must be 1
128
137
value |= 0xFC
129
138
self ._buf [0 ] = value
0 commit comments