1818from micropython import const
1919from adafruit_onewire .device import OneWireDevice
2020
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+
2128_DS2413_ACCESS_READ = b"\xF5 "
2229_DS2413_ACCESS_WRITE = b"\x5A "
2330_DS2413_ACK_SUCCESS = b"\xAA "
2936class DS2413Pin :
3037 """Class which provides interface to single DS2413 GPIO pin."""
3138
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 :
3342 if number not in (0 , 1 ):
3443 raise ValueError ("Incorrect pin number." )
3544 self ._number = number
@@ -39,20 +48,20 @@ def __init__(self, number, host, direction=OUTPUT):
3948 self .direction = direction # set it through setter
4049
4150 @property
42- def direction (self ):
51+ def direction (self ) -> Literal [ 0 , 1 ] :
4352 """The direction of the pin, either INPUT or OUTPUT."""
4453 return self ._direction
4554
4655 @direction .setter
47- def direction (self , direction ) :
56+ def direction (self , direction : Literal [ 0 , 1 ]) -> None :
4857 if direction not in (INPUT , OUTPUT ):
4958 raise ValueError ("Incorrect direction setting." )
5059 self ._direction = OUTPUT
5160 self .value = False
5261 self ._direction = direction
5362
5463 @property
55- def value (self ):
64+ def value (self ) -> bool :
5665 """The pin state if configured as INPUT. The output latch state
5766 if configured as OUTPUT. True is HIGH/ON, False is LOW/OFF."""
5867 # return Pin State if configured for INPUT
@@ -61,7 +70,7 @@ def value(self):
6170 return not self ._host .pio_state & (self ._mask << self ._direction )
6271
6372 @value .setter
64- def value (self , state ) :
73+ def value (self , state : bool ) -> None :
6574 # This only makes sense if the pin is configured for OUTPUT.
6675 if self ._direction == INPUT :
6776 raise RuntimeError ("Can't set value when pin is set to input." )
@@ -84,7 +93,7 @@ def value(self, state):
8493class DS2413 :
8594 """Class which provides interface to DS2413 GPIO breakout."""
8695
87- def __init__ (self , bus , address ) :
96+ def __init__ (self , bus : OneWireBus , address : int ) -> None :
8897 if address .family_code == 0x3A :
8998 self ._address = address
9099 self ._device = OneWireDevice (bus , address )
@@ -95,35 +104,35 @@ def __init__(self, bus, address):
95104 raise RuntimeError ("Incorrect family code in device address." )
96105
97106 @property
98- def IOA (self ):
107+ def IOA (self ) -> DS2413Pin :
99108 """The pin object for channel A."""
100109 if self ._IOA is None :
101110 self ._IOA = DS2413Pin (0 , self )
102111 return self ._IOA
103112
104113 @property
105- def IOB (self ):
114+ def IOB (self ) -> DS2413Pin :
106115 """The pin object for channel B."""
107116 if self ._IOB is None :
108117 self ._IOB = DS2413Pin (1 , self )
109118 return self ._IOB
110119
111120 @property
112- def pio_state (self ):
121+ def pio_state (self ) -> int :
113122 """The state of both PIO channels."""
114123 return self ._read_status ()
115124
116125 @pio_state .setter
117- def pio_state (self , value ) :
126+ def pio_state (self , value : int ) -> None :
118127 return self ._write_latches (value )
119128
120- def _read_status (self ):
129+ def _read_status (self ) -> int :
121130 with self ._device as dev :
122131 dev .write (_DS2413_ACCESS_READ )
123132 dev .readinto (self ._buf , end = 1 )
124133 return self ._buf [0 ]
125134
126- def _write_latches (self , value ) :
135+ def _write_latches (self , value : int ) -> None :
127136 # top six bits must be 1
128137 value |= 0xFC
129138 self ._buf [0 ] = value
0 commit comments