8000 Adding type annotations and fixing float incompatibility by RossK1 · Pull Request #67 · adafruit/Adafruit_CircuitPython_SimpleIO · GitHub
[go: up one dir, main page]

Skip to content

Adding type annotations and fixing float incompatibility #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions simpleio.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@

* Author(s): Scott Shawcroft
"""
import time
import sys
try:
from typing import Any

from microcontroller import Pin
except ImportError:
pass

import array
import sys
import time
from math import floor

import digitalio
import pwmio

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


def tone(pin, frequency, duration=1, length=100):
def tone(pin: Pin, frequency: float, duration: int = 1, length: float = 100) -> None:
"""
Generates a square wave of the specified frequency on a pin

:param ~microcontroller.Pin pin: Pin on which to output the tone
:param float frequency: Frequency of tone in Hz
:param int length: Variable size buffer (optional)
:param float length: Variable size buffer (optional)
:param int duration: Duration of tone in seconds (optional)
"""
if length * frequency > 350000:
Expand All @@ -56,9 +65,9 @@ def tone(pin, frequency, duration=1, length=100):
# pylint: enable=no-member
except ValueError:
# pin without PWM
sample_length = length
sample_length = floor(length)
square_wave = array.array("H", [0] * sample_length)
for i in range(sample_length / 2):
for i in range(floor(sample_length / 2)):
square_wave[i] = 0xFFFF
square_wave_sample = audiocore.RawSample(square_wave)
square_wave_sample.sample_rate = int(len(square_wave) * frequency)
Expand All @@ -69,7 +78,7 @@ def tone(pin, frequency, duration=1, length=100):
dac.stop()


def bitWrite(x, n, b): # pylint: disable-msg=invalid-name
def bitWrite(x: int, n: int, b: int) -> int: # pylint: disable-msg=invalid-name
"""
Based on the Arduino bitWrite function, changes a specific bit of a value to 0 or 1.
The return value is the original value with the changed bit.
Expand All @@ -86,7 +95,11 @@ def bitWrite(x, n, b): # pylint: disable-msg=invalid-name
return x


def shift_in(data_pin, clock, msb_first=True):
def shift_in(
data_pin: digitalio.DigitalInOut,
clock: digitalio.DigitalInOut,
msb_first: bool = True,
) -> int:
"""
Shifts in a byte of data one bit at a time. Starts from either the LSB or
MSB.
Expand Down Expand Up @@ -116,7 +129,13 @@ def shift_in(data_pin, clock, msb_first=True):
return value


def shift_out(data_pin, clock, value, msb_first=True, bitcount=8):
def shift_out(
data_pin: digitalio.DigitalInOut,
clock: digitalio.DigitalInOut,
value: int,
msb_first: bool = True,
bitcount: int = 8,
) -> None:
"""
Shifts out a byte of data one bit at a time. Data gets written to a data
pin. Then, the clock pulses hi then low
Expand Down Expand Up @@ -190,17 +209,17 @@ class DigitalOut:
:param drive_mode digitalio.DriveMode: drive mode for the output
"""

def __init__(self, pin, **kwargs):
def __init__(self, pin: Pin, **kwargs: Any) -> None:
self.iopin = digitalio.DigitalInOut(pin)
self.iopin.switch_to_output(**kwargs)

@property
def value(self):
def value(self) -> bool:
"""The digital logic level of the output pin."""
return self.iopin.value

@value.setter
def value(self, value):
def value(self, value: bool) -> None:
self.iopin.value = value


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

def __init__(self, pin, **kwargs):
def __init__(self, pin: Pin, **kwargs: Any) -> None:
self.iopin = digitalio.DigitalInOut(pin)
self.iopin.switch_to_input(**kwargs)

@property
def value(self):
def value(self) -> bool:
"""The digital logic level of the input pin."""
return self.iopin.value

@value.setter
def value(self, value): # pylint: disable-msg=no-self-use, unused-argument
def value(self, value: bool) -> None: # pylint: disable=no-self-use
raise AttributeError("Cannot set the value on a digital input.")


def map_range(x, in_min, in_max, out_min, out_max):
def map_range(
x: float, in_min: float, in_max: float, out_min: float, out_max: float
) -> float:
"""
Maps a number from one range to another.
Note: This implementation handles values < in_min differently than arduino's map function does.
Expand Down
0