-
Notifications
You must be signed in to change notification settings - Fork 9
Add Misssing Type Annotations #22
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b97615c
Add Misssing Type Annotations
tcfranks 3b47d19
corrected pylint invalid name errors
tcfranks 37fec22
fixed error with SPI object
tcfranks a1c2c03
added Literal definitions as suggested and fixed pylint issues
tcfranks 07a439b
added Literal definitions as suggested and fixed pylint issues
tcfranks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,13 @@ | |
from micropython import const | ||
from adafruit_bus_device.spi_device import SPIDevice | ||
|
||
try: | ||
from typing import Dict, Tuple | ||
from busio import SPI | ||
from digitalio import DigitalInOut | ||
except ImportError: | ||
pass | ||
|
||
try: | ||
from struct import unpack | ||
except ImportError: | ||
|
@@ -151,7 +158,9 @@ class MAX31856: | |
# Tony says this isn't re-entrant or thread safe! | ||
_BUFFER = bytearray(4) | ||
|
||
def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K): | ||
def __init__( | ||
self, spi: SPI, cs: DigitalInOut, thermocouple_type: int = ThermocoupleType.K | ||
) -> None: | ||
self._device = SPIDevice(spi, cs, baudrate=500000, polarity=0, phase=1) | ||
|
||
# assert on any fault | ||
|
@@ -162,7 +171,7 @@ def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K): | |
# set thermocouple type | ||
self._set_thermocouple_type(thermocouple_type) | ||
|
||
def _set_thermocouple_type(self, thermocouple_type: ThermocoupleType): | ||
def _set_thermocouple_type(self, thermocouple_type: ThermocoupleType) -> None: | ||
# get current value of CR1 Reg | ||
conf_reg_1 = self._read_register(_MAX31856_CR1_REG, 1)[0] | ||
conf_reg_1 &= 0xF0 # mask off bottom 4 bits | ||
|
@@ -184,7 +193,7 @@ def averaging(self) -> int: | |
raise KeyError(f"AVGSEL bit pattern was not recognised ({avgsel:>08b})") | ||
|
||
@averaging.setter | ||
def averaging(self, num_samples: int): | ||
def averaging(self, num_samples: int) -> None: | ||
# This option is set in bits 4-6 of register CR1. | ||
if num_samples not in _AVGSEL_CONSTS: | ||
raise ValueError("Num_samples must be one of 1,2,4,8,16") | ||
|
@@ -208,7 +217,7 @@ def noise_rejection(self) -> int: | |
return 60 | ||
|
||
@noise_rejection.setter | ||
def noise_rejection(self, frequency: int): | ||
def noise_rejection(self, frequency: int) -> None: | ||
conf_reg_0 = self._read_register(_MAX31856_CR0_REG, 1)[0] | ||
if frequency == 50: | ||
conf_reg_0 |= _MAX31856_CR0_50HZ # set the 50hz bit | ||
|
@@ -219,7 +228,7 @@ def noise_rejection(self, frequency: int): | |
self._write_u8(_MAX31856_CR0_REG, conf_reg_0) | ||
|
||
@property | ||
def temperature(self): | ||
def temperature(self) -> float: | ||
"""Measure the temperature of the sensor and wait for the result. | ||
Return value is in degrees Celsius. (read-only)""" | ||
self._perform_one_shot_measurement() | ||
|
@@ -241,7 +250,7 @@ def unpack_temperature(self) -> float: | |
return temp_float | ||
|
||
@property | ||
def reference_temperature(self): | ||
def reference_temperature(self) -> float: | ||
"""Wait to retrieve temperature of the cold junction in degrees Celsius. (read-only)""" | ||
self._perform_one_shot_measurement() | ||
return self.unpack_reference_temperature() | ||
|
@@ -256,7 +265,7 @@ def unpack_reference_temperature(self) -> float: | |
return cold_junction_temp | ||
|
||
@property | ||
def temperature_thresholds(self): | ||
def temperature_thresholds(self) -> Tuple[float, float]: | ||
"""The thermocouple's low and high temperature thresholds | ||
as a ``(low_temp, high_temp)`` tuple | ||
""" | ||
|
@@ -267,7 +276,7 @@ def temperature_thresholds(self): | |
return (round(raw_low[0] / 16.0, 1), round(raw_high[0] / 16.0, 1)) | ||
|
||
@temperature_thresholds.setter | ||
def temperature_thresholds(self, val): | ||
def temperature_thresholds(self, val: Tuple[float, float]) -> None: | ||
|
||
int_low = int(val[0] * 16) | ||
int_high = int(val[1] * 16) | ||
|
@@ -279,7 +288,9 @@ def temperature_thresholds(self, val): | |
self._write_u8(_MAX31856_LTLFTL_REG, int_low) | ||
|
||
@property | ||
def reference_temperature_thresholds(self): # pylint: disable=invalid-name | ||
def reference_temperature_thresholds( # pylint: disable=invalid-name, | ||
self, | ||
) -> Tuple[float, float]: # pylint: disable=invalid-name | ||
tcfranks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""The cold junction's low and high temperature thresholds | ||
as a ``(low_temp, high_temp)`` tuple | ||
""" | ||
|
@@ -289,13 +300,15 @@ def reference_temperature_thresholds(self): # pylint: disable=invalid-name | |
) | ||
|
||
@reference_temperature_thresholds.setter | ||
def reference_temperature_thresholds(self, val): # pylint: disable=invalid-name | ||
def reference_temperature_thresholds( # pylint: disable=invalid-name, | ||
self, val: Tuple[float, float] | ||
) -> None: # pylint: disable=invalid-name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here, I think it's this second |
||
|
||
self._write_u8(_MAX31856_CJLF_REG, int(val[0])) | ||
self._write_u8(_MAX31856_CJHF_REG, int(val[1])) | ||
|
||
@property | ||
def fault(self): | ||
def fault(self) -> Dict[str, bool]: | ||
"""A dictionary with the status of each fault type where the key is the fault type and the | ||
value is a bool if the fault is currently active | ||
|
||
|
@@ -326,12 +339,12 @@ def fault(self): | |
"open_tc": bool(faults & _MAX31856_FAULT_OPEN), | ||
} | ||
|
||
def _perform_one_shot_measurement(self): | ||
def _perform_one_shot_measurement(self) -> None: | ||
self.initiate_one_shot_measurement() | ||
# wait for the measurement to complete | ||
self._wait_for_oneshot() | ||
|
||
def initiate_one_shot_measurement(self): | ||
def initiate_one_shot_measurement(self) -> None: | ||
"""Starts a one-shot measurement and returns immediately. | ||
A measurement takes approximately 160ms. | ||
Check the status of the measurement with `oneshot_pending`; when it is false, | ||
|
@@ -358,11 +371,11 @@ def oneshot_pending(self) -> bool: | |
) | ||
return bool(oneshot_flag) | ||
|
||
def _wait_for_oneshot(self): | ||
def _wait_for_oneshot(self) -> None: | ||
while self.oneshot_pending: | ||
sleep(0.01) | ||
|
||
def _read_register(self, address, length): | ||
def _read_register(self, address: int, length: int) -> int: | ||
tcfranks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# pylint: disable=no-member | ||
# Read a 16-bit BE unsigned value from the specified 8-bit address. | ||
with self._device as device: | ||
|
@@ -371,7 +384,7 @@ def _read_register(self, address, length): | |
device.readinto(self._BUFFER, end=length) | ||
return self._BUFFER[:length] | ||
|
||
def _write_u8(self, address, val): | ||
def _write_u8(self, address: int, val: int) -> None: | ||
# Write an 8-bit unsigned value to the specified 8-bit address. | ||
with self._device as device: | ||
self._BUFFER[0] = (address | 0x80) & 0xFF | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.