Skip to content

Commit a29a385

Browse files
Jeremy CeriseJeremy Cerise
Jeremy Cerise
authored and
Jeremy Cerise
committed
Fix some typing based on feedback, add more specific types elsewhere
1 parent 2a3455a commit a29a385

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

adafruit_rfm9x.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import random
1616
import time
1717
import adafruit_bus_device.spi_device as spidev
18-
from digitalio import DigitalInOut
1918
from micropython import const
20-
from busio import SPI
2119

2220
HAS_SUPERVISOR = False
2321

@@ -30,7 +28,11 @@
3028
pass
3129

3230
try:
33-
from typing import Optional
31+
from typing import Optional, Type, Literal
32+
from digitalio import DigitalInOut
33+
from busio import SPI
34+
from circuitpython_typing import WriteableBuffer, ReadableBuffer
35+
3436
except ImportError:
3537
pass
3638

@@ -206,11 +208,11 @@ def __init__(self, address: int, *, offset: int = 0, bits: int = 1) -> None:
206208
self._mask <<= offset
207209
self._offset = offset
208210

209-
def __get__(self, obj, objtype) -> int:
211+
def __get__(self, obj: "RFM9x", objtype: Type["RFM9x"]) -> int:
210212
reg_value = obj._read_u8(self._address)
211213
return (reg_value & self._mask) >> self._offset
212214

213-
def __set__(self, obj, val) -> None:
215+
def __set__(self, obj: "RFM9x", val: int) -> None:
214216
reg_value = obj._read_u8(self._address)
215217
reg_value &= ~self._mask
216218
reg_value |= (val & 0xFF) << self._offset
@@ -252,11 +254,11 @@ def __set__(self, obj, val) -> None:
252254
def __init__(
253255
self,
254256
spi: SPI,
255-
cs: Optional[DigitalInOut],
257+
cs: DigitalInOut,
256258
reset: DigitalInOut,
257259
frequency: int,
258260
*,
259-
preamble_length=8,
261+
preamble_length: int = 8,
260262
high_power: bool = True,
261263
baudrate: int = 5000000,
262264
agc: bool = False,
@@ -374,7 +376,7 @@ def __init__(
374376
# pylint: disable=no-member
375377
# Reconsider pylint: disable when this can be tested
376378
def _read_into(
377-
self, address: int, buf: bytearray, length: Optional[int] = None
379+
self, address: int, buf: WriteableBuffer, length: Optional[int] = None
378380
) -> None:
379381
# Read a number of bytes from the specified address into the provided
380382
# buffer. If length is not specified (the default) the entire buffer
@@ -393,7 +395,7 @@ def _read_u8(self, address: int) -> int:
393395
return self._BUFFER[0]
394396

395397
def _write_from(
396-
self, address: int, buf: bytearray, length: Optional[int] = None
398+
self, address: int, buf: ReadableBuffer, length: Optional[int] = None
397399
) -> None:
398400
# Write a number of bytes to the provided address and taken from the
399401
# provided buffer. If no length is specified (the default) the entire
@@ -464,7 +466,7 @@ def preamble_length(self, val: int) -> None:
464466
self._write_u8(_RH_RF95_REG_21_PREAMBLE_LSB, val & 0xFF)
465467

466468
@property
467-
def frequency_mhz(self) -> float:
469+
def frequency_mhz(self) -> Literal[433.0, 915.0]:
468470
"""The frequency of the radio in Megahertz. Only the allowed values for
469471
your radio must be specified (i.e. 433 vs. 915 mhz)!
470472
"""
@@ -476,7 +478,7 @@ def frequency_mhz(self) -> float:
476478
return frequency
477479

478480
@frequency_mhz.setter
479-
def frequency_mhz(self, val: int) -> None:
481+
def frequency_mhz(self, val: Literal[433.0, 915.0]) -> None:
480482
if val < 240 or val > 960:
481483
raise RuntimeError("frequency_mhz must be between 240 and 960")
482484
# Calculate FRF register 24-bit value.
@@ -596,7 +598,7 @@ def signal_bandwidth(self, val: int) -> None:
596598
self._write_u8(0x30, 0)
597599

598600
@property
599-
def coding_rate(self) -> int:
601+
def coding_rate(self) -> Literal[5, 6, 7, 8]:
600602
"""The coding rate used by the radio to control forward error
601603
correction (try setting to a higher value to increase tolerance of
602604
short bursts of interference or to a lower value to increase bit
@@ -606,7 +608,7 @@ def coding_rate(self) -> int:
606608
return denominator
607609

608610
@coding_rate.setter
609-
def coding_rate(self, val: int) -> None:
611+
def coding_rate(self, val: Literal[5, 6, 7, 8]) -> None:
610612
# Set coding rate (set to 5 to match RadioHead Cr45).
611613
denominator = min(max(val, 5), 8)
612614
cr_id = denominator - 4
@@ -616,7 +618,7 @@ def coding_rate(self, val: int) -> None:
616618
)
617619

618620
@property
619-
def spreading_factor(self) -> int:
621+
def spreading_factor(self) -> Literal[6, 7, 8, 9, 10, 11, 12]:
620622
"""The spreading factor used by the radio (try setting to a higher
621623
value to increase the receiver's ability to distinguish signal from
622624
noise or to a lower value to increase the data transmission rate).
@@ -625,7 +627,7 @@ def spreading_factor(self) -> int:
625627
return sf_id
626628

627629
@spreading_factor.setter
628-
def spreading_factor(self, val: int) -> None:
630+
def spreading_factor(self, val: Literal[6, 7, 8, 9, 10, 11, 12]) -> None:
629631
# Set spreading factor (set to 7 to match RadioHead Sf128).
630632
val = min(max(val, 6), 12)
631633

@@ -664,24 +666,24 @@ def enable_crc(self, val: bool) -> None:
664666
self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) & 0xFB,
665667
)
666668

667-
def tx_done(self) -> int:
669+
def tx_done(self) -> bool:
668670
"""Transmit status"""
669671
return (self._read_u8(_RH_RF95_REG_12_IRQ_FLAGS) & 0x8) >> 3
670672

671-
def rx_done(self) -> int:
673+
def rx_done(self) -> bool:
672674
"""Receive status"""
673675
return (self._read_u8(_RH_RF95_REG_12_IRQ_FLAGS) & 0x40) >> 6
674676

675-
def crc_error(self) -> int:
677+
def crc_error(self) -> bool:
676678
"""crc status"""
677679
return (self._read_u8(_RH_RF95_REG_12_IRQ_FLAGS) & 0x20) >> 5
678680

679681
# pylint: disable=too-many-branches
680682
def send(
681683
self,
682-
data,
684+
data: ReadableBuffer,
683685
*,
684-
keep_listening: bool = False,
686+
keep_listening: Optional[int] = False,
685687
destination=None,
686688
node=None,
687689
identifier=None,
@@ -798,7 +800,7 @@ def send_with_ack(self, data) -> bool:
798800
def receive(
799801
self,
800802
*,
801-
keep_listening: bool = True,
803+
keep_listening: Optional[int] = True,
802804
with_header: bool = False,
803805
with_ack: bool = False,
804806
timeout: Optional[float] = None

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5-
Adafruit-Blinka
5+
Adafruit-Blinka>=7.2.3
66
adafruit-circuitpython-busdevice

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
# Author details
3535
author="Adafruit Industries",
3636
author_email="[email protected]",
37-
install_requires=["Adafruit-Blinka", "adafruit-circuitpython-busdevice"],
37+
install_requires=["Adafruit-Blinka>=7.2.3", "adafruit-circuitpython-busdevice"],
3838
# Choose your license
3939
license="MIT",
4040
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers

0 commit comments

Comments
 (0)