Skip to content

Fix Annotations #17

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 2 commits into from
Jan 16, 2023
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
39 changes: 24 additions & 15 deletions adafruit_sgp40/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
from struct import unpack_from
from adafruit_bus_device import i2c_device

try:
from typing import List, Optional
from circuitpython_typing import ReadableBuffer
from busio import I2C
except ImportError:
pass

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

Expand Down Expand Up @@ -107,15 +114,15 @@ class SGP40:

"""

def __init__(self, i2c, address=0x59):
def __init__(self, i2c: I2C, address: int = 0x59) -> None:
self.i2c_device = i2c_device.I2CDevice(i2c, address)
self._command_buffer = bytearray(2)
self._measure_command = _READ_CMD
self._voc_algorithm = None

self.initialize()

def initialize(self):
def initialize(self) -> None:
"""Reset the sensor to it's initial unconfigured state and configure it with sensible
defaults so it can be used"""
# check serial number
Expand All @@ -132,7 +139,7 @@ def initialize(self):
featureset = self._read_word_from_command()
if featureset[0] != 0x3220:

raise RuntimeError("Feature set does not match: %s" % hex(featureset[0]))
raise RuntimeError(f"Feature set does not match: {featureset[0]:#x}")

# Self Test
self._command_buffer[0] = 0x28
Expand All @@ -142,7 +149,7 @@ def initialize(self):
raise RuntimeError("Self test failed")
self._reset()

def _reset(self):
def _reset(self) -> None:
# This is a general call Reset. Several sensors may see this and it doesn't appear to
# ACK before resetting
self._command_buffer[0] = 0x00
Expand All @@ -156,7 +163,7 @@ def _reset(self):
sleep(1)

@staticmethod
def _celsius_to_ticks(temperature):
def _celsius_to_ticks(temperature: float) -> List[int]:
"""
Converts Temperature in Celsius to 'ticks' which are an input parameter
the sgp40 can use
Expand All @@ -175,7 +182,7 @@ def _celsius_to_ticks(temperature):
return [most_sig_temp_ticks, least_sig_temp_ticks]

@staticmethod
def _relative_humidity_to_ticks(humidity):
def _relative_humidity_to_ticks(humidity: float) -> List[int]:
"""
Converts Relative Humidity in % to 'ticks' which are an input parameter
the sgp40 can use
Expand All @@ -194,15 +201,15 @@ def _relative_humidity_to_ticks(humidity):
return [most_sig_rhumidity_ticks, least_sig_rhumidity_ticks]

@property
def raw(self):
def raw(self) -> int:
"""The raw gas value"""
# recycle a single buffer
self._command_buffer = self._measure_command
read_value = self._read_word_from_command(delay_ms=250)
self._command_buffer = bytearray(2)
return read_value[0]

def measure_raw(self, temperature=25, relative_humidity=50):
def measure_raw(self, temperature: float = 25, relative_humidity: float = 50):
"""
A humidity and temperature compensated raw gas value which helps
address fluctuations in readings due to changing humidity.
Expand All @@ -225,7 +232,9 @@ def measure_raw(self, temperature=25, relative_humidity=50):
self._measure_command = bytearray(_cmd)
return self.raw

def measure_index(self, temperature=25, relative_humidity=50):
def measure_index(
self, temperature: float = 25, relative_humidity: float = 50
) -> int:
"""Measure VOC index after humidity compensation
:param float temperature: The temperature in degrees Celsius, defaults to :const:`25`
:param float relative_humidity: The relative humidity in percentage, defaults to :const:`50`
Expand All @@ -234,7 +243,7 @@ def measure_index(self, temperature=25, relative_humidity=50):
:note 0-100, no need to ventilate, purify
:note 100-200, no need to ventilate, purify
:note 200-400, ventilate, purify
:note 00-500, ventilate, purify intensely
:note 400-500, ventilate, purify intensely
:return int The VOC index measured, ranged from 0 to 500
"""
# import/setup algorithm only on use of index
Expand All @@ -255,9 +264,9 @@ def measure_index(self, temperature=25, relative_humidity=50):

def _read_word_from_command(
self,
delay_ms=10,
readlen=1,
):
delay_ms: int = 10,
readlen: Optional[int] = 1,
) -> Optional[List[int]]:
"""_read_word_from_command - send a given command code and read the result back

Args:
Expand Down Expand Up @@ -291,15 +300,15 @@ def _read_word_from_command(

return readdata_buffer

def _check_crc8(self, crc_buffer, crc_value):
def _check_crc8(self, crc_buffer: ReadableBuffer, crc_value: int) -> bool:
"""
Checks that the 8 bit CRC Checksum value from the sensor matches the
received data
"""
return crc_value == self._generate_crc(crc_buffer)

@staticmethod
def _generate_crc(crc_buffer):
def _generate_crc(crc_buffer: ReadableBuffer) -> int:
"""
Generates an 8 bit CRC Checksum from the input buffer.

Expand Down
Loading