Skip to content

Commit

Permalink
Merge pull request #29 from tcfranks/main
Browse files Browse the repository at this point in the history
Add missing type annotations
  • Loading branch information
tekktrik authored Sep 13, 2022
2 parents 1b1f7f4 + 42a2866 commit cc696b8
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions adafruit_hcsr04.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
import time
from digitalio import DigitalInOut, Direction

try:
from typing import Optional, Type
from types import TracebackType
from microcontroller import Pin
except ImportError:
pass

_USE_PULSEIO = False
try:
from pulseio import PulseIn
Expand Down Expand Up @@ -68,7 +75,9 @@ class HCSR04:
time.sleep(0.1)
"""

def __init__(self, trigger_pin, echo_pin, *, timeout=0.1):
def __init__(
self, trigger_pin: Pin, echo_pin: Pin, *, timeout: float = 0.1
) -> None:
"""
:param trigger_pin: The pin on the microcontroller that's connected to the
``Trig`` pin on the HC-SR04.
Expand All @@ -92,21 +101,26 @@ def __init__(self, trigger_pin, echo_pin, *, timeout=0.1):
self._echo = DigitalInOut(echo_pin)
self._echo.direction = Direction.INPUT

def __enter__(self):
def __enter__(self) -> "HCSR04":
"""Allows for use in context managers."""
return self

def __exit__(self, exc_type, exc_val, exc_tb):
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> None:
"""Automatically de-initialize after a context manager."""
self.deinit()

def deinit(self):
def deinit(self) -> None:
"""De-initialize the trigger and echo pins."""
self._trig.deinit()
self._echo.deinit()

@property
def distance(self):
def distance(self) -> float:
"""Return the distance measured by the sensor in cm.
This is the function that will be called most often in user code. The
Expand All @@ -126,7 +140,7 @@ def distance(self):
"""
return self._dist_two_wire() # at this time we only support 2-wire meausre

def _dist_two_wire(self):
def _dist_two_wire(self) -> float:
if _USE_PULSEIO:
self._echo.clear() # Discard any previous pulse values
self._trig.value = True # Set trig high
Expand Down

0 comments on commit cc696b8

Please sign in to comment.