15
15
import random
16
16
import time
17
17
import adafruit_bus_device .spi_device as spidev
18
- from digitalio import DigitalInOut
19
18
from micropython import const
20
- from busio import SPI
21
19
22
20
HAS_SUPERVISOR = False
23
21
30
28
pass
31
29
32
30
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
+
34
36
except ImportError :
35
37
pass
36
38
@@ -206,11 +208,11 @@ def __init__(self, address: int, *, offset: int = 0, bits: int = 1) -> None:
206
208
self ._mask <<= offset
207
209
self ._offset = offset
208
210
209
- def __get__ (self , obj , objtype ) -> int :
211
+ def __get__ (self , obj : "RFM9x" , objtype : Type [ "RFM9x" ] ) -> int :
210
212
reg_value = obj ._read_u8 (self ._address )
211
213
return (reg_value & self ._mask ) >> self ._offset
212
214
213
- def __set__ (self , obj , val ) -> None :
215
+ def __set__ (self , obj : "RFM9x" , val : int ) -> None :
214
216
reg_value = obj ._read_u8 (self ._address )
215
217
reg_value &= ~ self ._mask
216
218
reg_value |= (val & 0xFF ) << self ._offset
@@ -252,11 +254,11 @@ def __set__(self, obj, val) -> None:
252
254
def __init__ (
253
255
self ,
254
256
spi : SPI ,
255
- cs : Optional [ DigitalInOut ] ,
257
+ cs : DigitalInOut ,
256
258
reset : DigitalInOut ,
257
259
frequency : int ,
258
260
* ,
259
- preamble_length = 8 ,
261
+ preamble_length : int = 8 ,
260
262
high_power : bool = True ,
261
263
baudrate : int = 5000000 ,
262
264
agc : bool = False ,
@@ -374,7 +376,7 @@ def __init__(
374
376
# pylint: disable=no-member
375
377
# Reconsider pylint: disable when this can be tested
376
378
def _read_into (
377
- self , address : int , buf : bytearray , length : Optional [int ] = None
379
+ self , address : int , buf : WriteableBuffer , length : Optional [int ] = None
378
380
) -> None :
379
381
# Read a number of bytes from the specified address into the provided
380
382
# buffer. If length is not specified (the default) the entire buffer
@@ -393,7 +395,7 @@ def _read_u8(self, address: int) -> int:
393
395
return self ._BUFFER [0 ]
394
396
395
397
def _write_from (
396
- self , address : int , buf : bytearray , length : Optional [int ] = None
398
+ self , address : int , buf : ReadableBuffer , length : Optional [int ] = None
397
399
) -> None :
398
400
# Write a number of bytes to the provided address and taken from the
399
401
# provided buffer. If no length is specified (the default) the entire
@@ -464,7 +466,7 @@ def preamble_length(self, val: int) -> None:
464
466
self ._write_u8 (_RH_RF95_REG_21_PREAMBLE_LSB , val & 0xFF )
465
467
466
468
@property
467
- def frequency_mhz (self ) -> float :
469
+ def frequency_mhz (self ) -> Literal [ 433.0 , 915.0 ] :
468
470
"""The frequency of the radio in Megahertz. Only the allowed values for
469
471
your radio must be specified (i.e. 433 vs. 915 mhz)!
470
472
"""
@@ -476,7 +478,7 @@ def frequency_mhz(self) -> float:
476
478
return frequency
477
479
478
480
@frequency_mhz .setter
479
- def frequency_mhz (self , val : int ) -> None :
481
+ def frequency_mhz (self , val : Literal [ 433.0 , 915.0 ] ) -> None :
480
482
if val < 240 or val > 960 :
481
483
raise RuntimeError ("frequency_mhz must be between 240 and 960" )
482
484
# Calculate FRF register 24-bit value.
@@ -596,7 +598,7 @@ def signal_bandwidth(self, val: int) -> None:
596
598
self ._write_u8 (0x30 , 0 )
597
599
598
600
@property
599
- def coding_rate (self ) -> int :
601
+ def coding_rate (self ) -> Literal [ 5 , 6 , 7 , 8 ] :
600
602
"""The coding rate used by the radio to control forward error
601
603
correction (try setting to a higher value to increase tolerance of
602
604
short bursts of interference or to a lower value to increase bit
@@ -606,7 +608,7 @@ def coding_rate(self) -> int:
606
608
return denominator
607
609
608
610
@coding_rate .setter
609
- def coding_rate (self , val : int ) -> None :
611
+ def coding_rate (self , val : Literal [ 5 , 6 , 7 , 8 ] ) -> None :
610
612
# Set coding rate (set to 5 to match RadioHead Cr45).
611
613
denominator = min (max (val , 5 ), 8 )
612
614
cr_id = denominator - 4
@@ -616,7 +618,7 @@ def coding_rate(self, val: int) -> None:
616
618
)
617
619
618
620
@property
619
- def spreading_factor (self ) -> int :
621
+ def spreading_factor (self ) -> Literal [ 6 , 7 , 8 , 9 , 10 , 11 , 12 ] :
620
622
"""The spreading factor used by the radio (try setting to a higher
621
623
value to increase the receiver's ability to distinguish signal from
622
624
noise or to a lower value to increase the data transmission rate).
@@ -625,7 +627,7 @@ def spreading_factor(self) -> int:
625
627
return sf_id
626
628
627
629
@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 :
629
631
# Set spreading factor (set to 7 to match RadioHead Sf128).
630
632
val = min (max (val , 6 ), 12 )
631
633
@@ -664,24 +666,24 @@ def enable_crc(self, val: bool) -> None:
664
666
self ._read_u8 (_RH_RF95_REG_1E_MODEM_CONFIG2 ) & 0xFB ,
665
667
)
666
668
667
- def tx_done (self ) -> int :
669
+ def tx_done (self ) -> bool :
668
670
"""Transmit status"""
669
671
return (self ._read_u8 (_RH_RF95_REG_12_IRQ_FLAGS ) & 0x8 ) >> 3
670
672
671
- def rx_done (self ) -> int :
673
+ def rx_done (self ) -> bool :
672
674
"""Receive status"""
673
675
return (self ._read_u8 (_RH_RF95_REG_12_IRQ_FLAGS ) & 0x40 ) >> 6
674
676
675
- def crc_error (self ) -> int :
677
+ def crc_error (self ) -> bool :
676
678
"""crc status"""
677
679
return (self ._read_u8 (_RH_RF95_REG_12_IRQ_FLAGS ) & 0x20 ) >> 5
678
680
679
681
# pylint: disable=too-many-branches
680
682
def send (
681
683
self ,
682
- data ,
684
+ data : ReadableBuffer ,
683
685
* ,
684
- keep_listening : bool = False ,
686
+ keep_listening : Optional [ int ] = False ,
685
687
destination = None ,
686
688
node = None ,
687
689
identifier = None ,
@@ -798,7 +800,7 @@ def send_with_ack(self, data) -> bool:
798
800
def receive (
799
801
self ,
800
802
* ,
801
- keep_listening : bool = True ,
803
+ keep_listening : Optional [ int ] = True ,
802
804
with_header : bool = False ,
803
805
with_ack : bool = False ,
804
806
timeout : Optional [float ] = None
0 commit comments