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