32
32
import typing # pylint: disable=unused-import
33
33
from microcontroller import Pin
34
34
import busio
35
+ from circuitpython_typing import ReadableBuffer
35
36
except ImportError :
36
37
pass
37
38
@@ -46,11 +47,16 @@ class DigitalInOut:
46
47
direction as input will raise an exception.
47
48
"""
48
49
50
+ _pin : Pin
51
+ _byte_pos : int
52
+ _byte_pin : int
53
+ _shift_register : "ShiftRegister74HC595"
54
+
49
55
def __init__ (
50
56
self ,
51
57
pin_number : Pin ,
52
58
shift_register_74hc595 : "ShiftRegister74HC595" ,
53
- ):
59
+ ) -> None :
54
60
"""Specify the pin number of the shift register (0...7) and
55
61
ShiftRegister74HC595 instance.
56
62
"""
@@ -64,27 +70,26 @@ def __init__(
64
70
# is unused by this class). Do not remove them, instead turn off pylint
65
71
# in this case.
66
72
# pylint: disable=unused-argument
67
- def switch_to_output (self , value : bool = False , ** kwargs ):
73
+ def switch_to_output (self , value : bool = False , ** kwargs ) -> None :
68
74
"""``DigitalInOut switch_to_output``"""
69
75
self .direction = digitalio .Direction .OUTPUT
70
76
self .value = value
71
77
72
- def switch_to_input (self , ** kwargs ): # pylint: disable=no-self-use
78
+ def switch_to_input (self , ** kwargs ) -> None : # pylint: disable=no-self-use
73
79
"""``switch_to_input`` is not supported."""
74
80
raise RuntimeError ("Digital input not supported." )
75
81
76
82
# pylint: enable=unused-argument
77
83
78
84
@property
79
- def value (self ):
85
+ def value (self ) -> bool :
80
86
"""The value of the pin, either True for high or False for low."""
81
87
return self ._shift_register .gpio [self ._byte_pos ] & (1 << self ._byte_pin ) == (
82
88
1 << self ._byte_pin
83
89
)
84
90
85
91
@value .setter
86
- def value (self , val : bool ):
87
-
92
+ def value (self , val : bool ) -> None :
88
93
if (
89
94
self ._pin >= 0
90
95
and self ._pin < self ._shift_register .number_of_shift_registers * 8
@@ -97,23 +102,26 @@ def value(self, val: bool):
97
102
self ._shift_register .gpio = gpio
98
103
99
104
@property
100
- def direction (self ):
105
+ def direction (self ) -> digitalio . Direction . OUTPUT :
101
106
"""``Direction`` can only be set to ``OUTPUT``."""
102
107
return digitalio .Direction .OUTPUT
103
108
104
109
@direction .setter
105
- def direction (self , val : digitalio .Direction ): # pylint: disable=no-self-use
110
+ def direction ( # pylint: disable=no-self-use
111
+ self ,
112
+ val : digitalio .Direction .OUTPUT ,
113
+ ) -> None :
106
114
"""``Direction`` can only be set to ``OUTPUT``."""
107
115
if val != digitalio .Direction .OUTPUT :
108
116
raise RuntimeError ("Digital input not supported." )
109
117
110
118
@property
111
- def pull (self ):
119
+ def pull (self ) -> None :
112
120
"""Pull-up/down not supported, return None for no pull-up/down."""
113
121
return None
114
122
115
123
@pull .setter
116
- def pull (self , val : digitalio . Pull ) : # pylint: disable=no-self-use
124
+ def pull (self , val : None ) -> None : # pylint: disable=no-self-use
117
125
"""Only supports null/no pull state."""
118
126
if val is not None :
119
127
raise RuntimeError ("Pull-up and pull-down not supported." )
@@ -124,38 +132,42 @@ class ShiftRegister74HC595:
124
132
number of shift registers being used and optional baudrate.
125
133
"""
126
134
135
+ _device : spi_device .SPIDevice
136
+ _number_of_shift_registers : int
137
+ _gpio : ReadableBuffer
138
+
127
139
def __init__ (
128
140
self ,
129
141
spi : busio .SPI ,
130
142
latch : digitalio .DigitalInOut ,
131
143
number_of_shift_registers : int = 1 ,
132
144
baudrate : int = 1000000 ,
133
- ):
145
+ ) -> None :
134
146
self ._device = spi_device .SPIDevice (spi , latch , baudrate = baudrate )
135
147
self ._number_of_shift_registers = number_of_shift_registers
136
148
self ._gpio = bytearray (self ._number_of_shift_registers )
137
149
138
150
@property
139
- def number_of_shift_registers (self ):
151
+ def number_of_shift_registers (self ) -> int :
140
152
"""The number of shift register chips"""
141
153
return self ._number_of_shift_registers
142
154
143
155
@property
144
- def gpio (self ):
156
+ def gpio (self ) -> ReadableBuffer :
145
157
"""The raw GPIO output register. Each bit represents the
146
158
output value of the associated pin (0 = low, 1 = high).
147
159
"""
148
160
return self ._gpio
149
161
150
162
@gpio .setter
151
- def gpio (self , val : bytearray ) :
163
+ def gpio (self , val : ReadableBuffer ) -> None :
152
164
self ._gpio = val
153
165
154
166
with self ._device as spi :
155
167
# pylint: disable=no-member
156
168
spi .write (self ._gpio )
157
169
158
- def get_pin (self , pin : int ) -> Pin :
170
+ def get_pin (self , pin : int ) -> DigitalInOut :
159
171
"""Convenience function to create an instance of the DigitalInOut class
160
172
pointing at the specified pin of this 74HC595 device .
161
173
"""
0 commit comments