10
10
11
11
* Author(s): Scott Shawcroft
12
12
"""
13
- import time
14
- import sys
13
+ try :
14
+ from typing import Any
15
+
16
+ from microcontroller import Pin
17
+ except ImportError :
18
+ pass
19
+
15
20
import array
21
+ import sys
22
+ import time
23
+ from math import floor
24
+
16
25
import digitalio
17
26
import pwmio
18
27
34
43
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SimpleIO.git"
35
44
36
45
37
- def tone (pin , frequency , duration = 1 , length = 100 ):
46
+ def tone (pin : Pin , frequency : float , duration : int = 1 , length : float = 100 ) -> None :
38
47
"""
39
48
Generates a square wave of the specified frequency on a pin
40
49
41
50
:param ~microcontroller.Pin pin: Pin on which to output the tone
42
51
:param float frequency: Frequency of tone in Hz
43
- :param int length: Variable size buffer (optional)
52
+ :param float length: Variable size buffer (optional)
44
53
:param int duration: Duration of tone in seconds (optional)
45
54
"""
46
55
if length * frequency > 350000 :
@@ -56,9 +65,9 @@ def tone(pin, frequency, duration=1, length=100):
56
65
# pylint: enable=no-member
57
66
except ValueError :
58
67
# pin without PWM
59
- sample_length = length
68
+ sample_length = floor ( length )
60
69
square_wave = array .array ("H" , [0 ] * sample_length )
61
- for i in range (sample_length / 2 ):
70
+ for i in range (floor ( sample_length / 2 ) ):
62
71
square_wave [i ] = 0xFFFF
63
72
square_wave_sample = audiocore .RawSample (square_wave )
64
73
square_wave_sample .sample_rate = int (len (square_wave ) * frequency )
@@ -69,7 +78,7 @@ def tone(pin, frequency, duration=1, length=100):
69
78
dac .stop ()
70
79
71
80
72
- def bitWrite (x , n , b ) : # pylint: disable-msg=invalid-name
81
+ def bitWrite (x : int , n : int , b : int ) -> int : # pylint: disable-msg=invalid-name
73
82
"""
74
83
Based on the Arduino bitWrite function, changes a specific bit of a value to 0 or 1.
75
84
The return value is the original value with the changed bit.
@@ -86,7 +95,11 @@ def bitWrite(x, n, b): # pylint: disable-msg=invalid-name
86
95
return x
87
96
88
97
89
- def shift_in (data_pin , clock , msb_first = True ):
98
+ def shift_in (
99
+ data_pin : digitalio .DigitalInOut ,
100
+ clock : digitalio .DigitalInOut ,
101
+ msb_first : bool = True ,
102
+ ) -> int :
90
103
"""
91
104
Shifts in a byte of data one bit at a time. Starts from either the LSB or
92
105
MSB.
@@ -116,7 +129,13 @@ def shift_in(data_pin, clock, msb_first=True):
116
129
return value
117
130
118
131
119
- def shift_out (data_pin , clock , value , msb_first = True , bitcount = 8 ):
132
+ def shift_out (
133
+ data_pin : digitalio .DigitalInOut ,
134
+ clock : digitalio .DigitalInOut ,
135
+ value : int ,
136
+ msb_first : bool = True ,
137
+ bitcount : int = 8 ,
138
+ ) -> None :
120
139
"""
121
140
Shifts out a byte of data one bit at a time. Data gets written to a data
122
141
pin. Then, the clock pulses hi then low
@@ -190,17 +209,17 @@ class DigitalOut:
190
209
:param drive_mode digitalio.DriveMode: drive mode for the output
191
210
"""
192
211
193
- def __init__ (self , pin , ** kwargs ) :
212
+ def __init__ (self , pin : Pin , ** kwargs : Any ) -> None :
194
213
self .iopin = digitalio .DigitalInOut (pin )
195
214
self .iopin .switch_to_output (** kwargs )
196
215
197
216
@property
198
- def value (self ):
217
+ def value (self ) -> bool :
199
218
"""The digital logic level of the output pin."""
200
219
return self .iopin .value
201
220
202
221
@value .setter
203
- def value (self , value ) :
222
+ def value (self , value : bool ) -> None :
204
223
self .iopin .value = value
205
224
206
225
@@ -212,21 +231,23 @@ class DigitalIn:
212
231
:param pull digitalio.Pull: pull configuration for the input
213
232
"""
214
233
215
- def __init__ (self , pin , ** kwargs ) :
234
+ def __init__ (self , pin : Pin , ** kwargs : Any ) -> None :
216
235
self .iopin = digitalio .DigitalInOut (pin )
217
236
self .iopin .switch_to_input (** kwargs )
218
237
219
238
@property
220
- def value (self ):
239
+ def value (self ) -> bool :
221
240
"""The digital logic level of the input pin."""
222
241
return self .iopin .value
223
242
224
243
@value .setter
225
- def value (self , value ) : # pylint: disable-msg =no-self-use, unused-argument
244
+ def value (self , value : bool ) -> None : # pylint: disable=no-self-use
226
245
raise AttributeError ("Cannot set the value on a digital input." )
227
246
228
247
229
- def map_range (x , in_min , in_max , out_min , out_max ):
248
+ def map_range (
249
+ x : float , in_min : float , in_max : float , out_min : float , out_max : float
250
+ ) -> float :
230
251
"""
231
252
Maps a number from one range to another.
232
253
Note: This implementation handles values < in_min differently than arduino's map function does.
0 commit comments