Skip to content

Commit 7100a1e

Browse files
authored
Merge pull request #67 from RossK1/adding_type_hints
Adding type annotations and fixing float incompatibility
2 parents 252d3d0 + 409c013 commit 7100a1e

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

simpleio.py

+37-16
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@
1010
1111
* Author(s): Scott Shawcroft
1212
"""
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+
1520
import array
21+
import sys
22+
import time
23+
from math import floor
24+
1625
import digitalio
1726
import pwmio
1827

@@ -34,13 +43,13 @@
3443
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SimpleIO.git"
3544

3645

37-
def tone(pin, frequency, duration=1, length=100):
46+
def tone(pin: Pin, frequency: float, duration: int = 1, length: float = 100) -> None:
3847
"""
3948
Generates a square wave of the specified frequency on a pin
4049
4150
:param ~microcontroller.Pin pin: Pin on which to output the tone
4251
:param float frequency: Frequency of tone in Hz
43-
:param int length: Variable size buffer (optional)
52+
:param float length: Variable size buffer (optional)
4453
:param int duration: Duration of tone in seconds (optional)
4554
"""
4655
if length * frequency > 350000:
@@ -56,9 +65,9 @@ def tone(pin, frequency, duration=1, length=100):
5665
# pylint: enable=no-member
5766
except ValueError:
5867
# pin without PWM
59-
sample_length = length
68+
sample_length = floor(length)
6069
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)):
6271
square_wave[i] = 0xFFFF
6372
square_wave_sample = audiocore.RawSample(square_wave)
6473
square_wave_sample.sample_rate = int(len(square_wave) * frequency)
@@ -69,7 +78,7 @@ def tone(pin, frequency, duration=1, length=100):
6978
dac.stop()
7079

7180

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
7382
"""
7483
Based on the Arduino bitWrite function, changes a specific bit of a value to 0 or 1.
7584
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
8695
return x
8796

8897

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:
90103
"""
91104
Shifts in a byte of data one bit at a time. Starts from either the LSB or
92105
MSB.
@@ -116,7 +129,13 @@ def shift_in(data_pin, clock, msb_first=True):
116129
return value
117130

118131

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:
120139
"""
121140
Shifts out a byte of data one bit at a time. Data gets written to a data
122141
pin. Then, the clock pulses hi then low
@@ -190,17 +209,17 @@ class DigitalOut:
190209
:param drive_mode digitalio.DriveMode: drive mode for the output
191210
"""
192211

193-
def __init__(self, pin, **kwargs):
212+
def __init__(self, pin: Pin, **kwargs: Any) -> None:
194213
self.iopin = digitalio.DigitalInOut(pin)
195214
self.iopin.switch_to_output(**kwargs)
196215

197216
@property
198-
def value(self):
217+
def value(self) -> bool:
199218
"""The digital logic level of the output pin."""
200219
return self.iopin.value
201220

202221
@value.setter
203-
def value(self, value):
222+
def value(self, value: bool) -> None:
204223
self.iopin.value = value
205224

206225

@@ -212,21 +231,23 @@ class DigitalIn:
212231
:param pull digitalio.Pull: pull configuration for the input
213232
"""
214233

215-
def __init__(self, pin, **kwargs):
234+
def __init__(self, pin: Pin, **kwargs: Any) -> None:
216235
self.iopin = digitalio.DigitalInOut(pin)
217236
self.iopin.switch_to_input(**kwargs)
218237

219238
@property
220-
def value(self):
239+
def value(self) -> bool:
221240
"""The digital logic level of the input pin."""
222241
return self.iopin.value
223242

224243
@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
226245
raise AttributeError("Cannot set the value on a digital input.")
227246

228247

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:
230251
"""
231252
Maps a number from one range to another.
232253
Note: This implementation handles values < in_min differently than arduino's map function does.

0 commit comments

Comments
 (0)