Skip to content

Commit

Permalink
[#19] Add type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
process1183 committed Oct 28, 2021
1 parent 390426b commit b1fcdd8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
6 changes: 3 additions & 3 deletions adafruit_pm25/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class PM25:
"""

def __init__(self):
def __init__(self) -> None:
# rad, ok make our internal buffer!
self._buffer = bytearray(32)
self.aqi_reading = {
Expand All @@ -68,11 +68,11 @@ def __init__(self):
"particles 100um": None,
}

def _read_into_buffer(self):
def _read_into_buffer(self) -> None:
"""Low level buffer filling function, to be overridden"""
raise NotImplementedError()

def read(self):
def read(self) -> dict:
"""Read any available data from the air quality sensor and
return a dictionary with available particulate/quality data"""
self._read_into_buffer()
Expand Down
15 changes: 12 additions & 3 deletions adafruit_pm25/i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@

# imports
import time
from digitalio import Direction
from digitalio import Direction, DigitalInOut
from adafruit_bus_device.i2c_device import I2CDevice
from . import PM25

try:
# Used only for typing
import typing # pylint: disable=unused-import
from busio import I2C
except ImportError:
pass


class PM25_I2C(PM25):
"""
Expand Down Expand Up @@ -77,7 +84,9 @@ class PM25_I2C(PM25):
"""

def __init__(self, i2c_bus, reset_pin=None, address=0x12):
def __init__(
self, i2c_bus: I2C, reset_pin: DigitalInOut = None, address: int = 0x12
) -> None:
if reset_pin:
# Reset device
reset_pin.direction = Direction.OUTPUT
Expand All @@ -98,7 +107,7 @@ def __init__(self, i2c_bus, reset_pin=None, address=0x12):
raise RuntimeError("Unable to find PM2.5 device")
super().__init__()

def _read_into_buffer(self):
def _read_into_buffer(self) -> None:
with self.i2c_device as i2c:
try:
i2c.readinto(self._buffer)
Expand Down
13 changes: 10 additions & 3 deletions adafruit_pm25/uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@
"""

import time
from digitalio import Direction
from digitalio import Direction, DigitalInOut
from . import PM25

try:
# Used only for typing
import typing # pylint: disable=unused-import
from busio import UART
except ImportError:
pass


class PM25_UART(PM25):
"""
Expand Down Expand Up @@ -73,7 +80,7 @@ class PM25_UART(PM25):
"""

def __init__(self, uart, reset_pin=None):
def __init__(self, uart: UART, reset_pin: DigitalInOut = None):
if reset_pin:
# Reset device
reset_pin.direction = Direction.OUTPUT
Expand All @@ -86,7 +93,7 @@ def __init__(self, uart, reset_pin=None):
self._uart = uart
super().__init__()

def _read_into_buffer(self):
def _read_into_buffer(self) -> None:
while True:
b = self._uart.read(1)
if not b:
Expand Down

0 comments on commit b1fcdd8

Please sign in to comment.