Skip to content
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

Add Missing Type Annotations #14

Merged
merged 3 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
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
20 changes: 14 additions & 6 deletions adafruit_tfmini.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
import time
import struct

try:
import typing # pylint: disable=unused-import
from typing_extensions import Literal
from circuitpython_typing import ReadableBuffer
from busio import UART
except ImportError:
pass

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TFmini.git"

Expand All @@ -46,7 +54,7 @@ class TFmini:
:param timeout: how long we'll wait for valid data or response, in seconds. Default is 1
"""

def __init__(self, uart, *, timeout=1):
def __init__(self, uart: UART, *, timeout: float = 1) -> None:
self._uart = uart
self._uart.baudrate = 115200
self._uart.reset_input_buffer()
Expand All @@ -55,7 +63,7 @@ def __init__(self, uart, *, timeout=1):
self._mode = None

@property
def distance(self):
def distance(self) -> int:
"""The most recent distance measurement in centimeters"""
try:
self._uart.reset_input_buffer()
Expand Down Expand Up @@ -87,24 +95,24 @@ def distance(self):
raise RuntimeError("Timed out looking for valid data")

@property
def strength(self):
def strength(self) -> int:
"""The signal validity, higher value means better measurement"""
_ = self.distance # trigger distance measurement
return self._strength

@property
def mode(self):
def mode(self) -> Literal[2, 7]:
"""The measurement mode can be MODE_SHORT (2) or MODE_LONG (7)"""
_ = self.distance # trigger distance measurement
return self._mode

@mode.setter
def mode(self, newmode):
def mode(self, newmode: Literal[2, 7]) -> None:
if not newmode in (MODE_LONG, MODE_SHORT):
raise ValueError("Invalid mode")
self._set_config(_CONFIGPARAM + bytes([0, 0, newmode, 0x11]))

def _set_config(self, command):
def _set_config(self, command: ReadableBuffer) -> None:
"""Manager for sending commands, put sensor into config mode, config,
then exit configuration mode!"""
self._uart.write(_STARTCONFIG)
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

Adafruit-Blinka
pyserial
adafruit-circuitpython-typing
typing-extensions~=4.0