26
26
HAS_SUPERVISOR = True
27
27
except ImportError :
28
28
pass
29
+
30
+ try :
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
+
36
+ except ImportError :
37
+ pass
38
+
29
39
__version__ = "0.0.0-auto.0"
30
40
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM9x.git"
31
41
120
130
# pylint: disable=too-many-statements
121
131
122
132
123
- def ticks_diff (ticks1 , ticks2 ) :
133
+ def ticks_diff (ticks1 : int , ticks2 : int ) -> int :
124
134
"""Compute the signed difference between two ticks values
125
135
assuming that they are within 2**28 ticks
126
136
"""
@@ -131,7 +141,7 @@ def ticks_diff(ticks1, ticks2):
131
141
132
142
class RFM9x :
133
143
"""Interface to a RFM95/6/7/8 LoRa radio module. Allows sending and
134
- receivng bytes of data in long range LoRa mode at a support board frequency
144
+ receiving bytes of data in long range LoRa mode at a support board frequency
135
145
(433/915mhz).
136
146
137
147
You must specify the following parameters:
@@ -186,7 +196,7 @@ class _RegisterBits:
186
196
# check from pylint.
187
197
# pylint: disable=protected-access
188
198
189
- def __init__ (self , address , * , offset = 0 , bits = 1 ) :
199
+ def __init__ (self , address : int , * , offset : int = 0 , bits : int = 1 ) -> None :
190
200
assert 0 <= offset <= 7
191
201
assert 1 <= bits <= 8
192
202
assert (offset + bits ) <= 8
@@ -198,11 +208,11 @@ def __init__(self, address, *, offset=0, bits=1):
198
208
self ._mask <<= offset
199
209
self ._offset = offset
200
210
201
- def __get__ (self , obj , objtype ) :
211
+ def __get__ (self , obj : "RFM9x" , objtype : Type [ "RFM9x" ]) -> int :
202
212
reg_value = obj ._read_u8 (self ._address )
203
213
return (reg_value & self ._mask ) >> self ._offset
204
214
205
- def __set__ (self , obj , val ) :
215
+ def __set__ (self , obj : "RFM9x" , val : int ) -> None :
206
216
reg_value = obj ._read_u8 (self ._address )
207
217
reg_value &= ~ self ._mask
208
218
reg_value |= (val & 0xFF ) << self ._offset
@@ -243,17 +253,17 @@ def __set__(self, obj, val):
243
253
244
254
def __init__ (
245
255
self ,
246
- spi ,
247
- cs ,
248
- reset ,
249
- frequency ,
256
+ spi : SPI ,
257
+ cs : DigitalInOut ,
258
+ reset : DigitalInOut ,
259
+ frequency : int ,
250
260
* ,
251
- preamble_length = 8 ,
252
- high_power = True ,
253
- baudrate = 5000000 ,
254
- agc = False ,
255
- crc = True
256
- ):
261
+ preamble_length : int = 8 ,
262
+ high_power : bool = True ,
263
+ baudrate : int = 5000000 ,
264
+ agc : bool = False ,
265
+ crc : bool = True
266
+ ) -> None :
257
267
self .high_power = high_power
258
268
# Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
259
269
# Set Default Baudrate to 5MHz to avoid problems
@@ -363,7 +373,9 @@ def __init__(
363
373
364
374
# pylint: disable=no-member
365
375
# Reconsider pylint: disable when this can be tested
366
- def _read_into (self , address , buf , length = None ):
376
+ def _read_into (
377
+ self , address : int , buf : WriteableBuffer , length : Optional [int ] = None
378
+ ) -> None :
367
379
# Read a number of bytes from the specified address into the provided
368
380
# buffer. If length is not specified (the default) the entire buffer
369
381
# will be filled.
@@ -375,12 +387,14 @@ def _read_into(self, address, buf, length=None):
375
387
device .write (self ._BUFFER , end = 1 )
376
388
device .readinto (buf , end = length )
377
389
378
- def _read_u8 (self , address ) :
390
+ def _read_u8 (self , address : int ) -> int :
379
391
# Read a single byte from the provided address and return it.
380
392
self ._read_into (address , self ._BUFFER , length = 1 )
381
393
return self ._BUFFER [0 ]
382
394
383
- def _write_from (self , address , buf , length = None ):
395
+ def _write_from (
396
+ self , address : int , buf : ReadableBuffer , length : Optional [int ] = None
397
+ ) -> None :
384
398
# Write a number of bytes to the provided address and taken from the
385
399
# provided buffer. If no length is specified (the default) the entire
386
400
# buffer is written.
@@ -392,39 +406,40 @@ def _write_from(self, address, buf, length=None):
392
406
device .write (self ._BUFFER , end = 1 )
393
407
device .write (buf , end = length )
394
408
395
- def _write_u8 (self , address , val ) :
409
+ def _write_u8 (self , address : int , val : int ) -> None :
396
410
# Write a byte register to the chip. Specify the 7-bit address and the
397
411
# 8-bit value to write to that address.
398
412
with self ._device as device :
399
- self ._BUFFER [0 ] = (address | 0x80 ) & 0xFF # Set top bit to 1 to
400
- # indicate a write.
413
+ self ._BUFFER [0 ] = (
414
+ address | 0x80
415
+ ) & 0xFF # Set top bit to 1 to indicate a write.
401
416
self ._BUFFER [1 ] = val & 0xFF
402
417
device .write (self ._BUFFER , end = 2 )
403
418
404
- def reset (self ):
419
+ def reset (self ) -> None :
405
420
"""Perform a reset of the chip."""
406
421
# See section 7.2.2 of the datasheet for reset description.
407
422
self ._reset .value = False # Set Reset Low
408
423
time .sleep (0.0001 ) # 100 us
409
424
self ._reset .value = True # set Reset High
410
425
time .sleep (0.005 ) # 5 ms
411
426
412
- def idle (self ):
427
+ def idle (self ) -> None :
413
428
"""Enter idle standby mode."""
414
429
self .operation_mode = STANDBY_MODE
415
430
416
- def sleep (self ):
431
+ def sleep (self ) -> None :
417
432
"""Enter sleep mode."""
418
433
self .operation_mode = SLEEP_MODE
419
434
420
- def listen (self ):
435
+ def listen (self ) -> None :
421
436
"""Listen for packets to be received by the chip. Use :py:func:`receive`
422
437
to listen, wait and retrieve packets as they're available.
423
438
"""
424
439
self .operation_mode = RX_MODE
425
440
self .dio0_mapping = 0b00 # Interrupt on rx done.
426
441
427
- def transmit (self ):
442
+ def transmit (self ) -> None :
428
443
"""Transmit a packet which is queued in the FIFO. This is a low level
429
444
function for entering transmit mode and more. For generating and
430
445
transmitting a packet of data use :py:func:`send` instead.
@@ -433,7 +448,7 @@ def transmit(self):
433
448
self .dio0_mapping = 0b01 # Interrupt on tx done.
434
449
435
450
@property
436
- def preamble_length (self ):
451
+ def preamble_length (self ) -> int :
437
452
"""The length of the preamble for sent and received packets, an unsigned
438
453
16-bit value. Received packets must match this length or they are
439
454
ignored! Set to 8 to match the RadioHead RFM95 library.
@@ -443,13 +458,13 @@ def preamble_length(self):
443
458
return ((msb << 8 ) | lsb ) & 0xFFFF
444
459
445
460
@preamble_length .setter
446
- def preamble_length (self , val ) :
461
+ def preamble_length (self , val : int ) -> None :
447
462
assert 0 <= val <= 65535
448
463
self ._write_u8 (_RH_RF95_REG_20_PREAMBLE_MSB , (val >> 8 ) & 0xFF )
449
464
self ._write_u8 (_RH_RF95_REG_21_PREAMBLE_LSB , val & 0xFF )
450
465
451
466
@property
452
- def frequency_mhz (self ):
467
+ def frequency_mhz (self ) -> Literal [ 433.0 , 915.0 ] :
453
468
"""The frequency of the radio in Megahertz. Only the allowed values for
454
469
your radio must be specified (i.e. 433 vs. 915 mhz)!
455
470
"""
@@ -461,7 +476,7 @@ def frequency_mhz(self):
461
476
return frequency
462
477
463
478
@frequency_mhz .setter
464
- def frequency_mhz (self , val ) :
479
+ def frequency_mhz (self , val : Literal [ 433.0 , 915.0 ]) -> None :
465
480
if val < 240 or val > 960 :
466
481
raise RuntimeError ("frequency_mhz must be between 240 and 960" )
467
482
# Calculate FRF register 24-bit value.
@@ -475,7 +490,7 @@ def frequency_mhz(self, val):
475
490
self ._write_u8 (_RH_RF95_REG_08_FRF_LSB , lsb )
476
491
477
492
@property
478
- def tx_power (self ):
493
+ def tx_power (self ) -> int :
479
494
"""The transmit power in dBm. Can be set to a value from 5 to 23 for
480
495
high power devices (RFM95/96/97/98, high_power=True) or -1 to 14 for low
481
496
power devices. Only integer power levels are actually set (i.e. 12.5
@@ -490,7 +505,7 @@ def tx_power(self):
490
505
return self .output_power - 1
491
506
492
507
@tx_power .setter
493
- def tx_power (self , val ) :
508
+ def tx_power (self , val : int ) -> None :
494
509
val = int (val )
495
510
if self .high_power :
496
511
if val < 5 or val > 23 :
@@ -511,7 +526,7 @@ def tx_power(self, val):
511
526
self .output_power = (val + 1 ) & 0x0F
512
527
513
528
@property
514
- def rssi (self ):
529
+ def rssi (self ) -> int :
515
530
"""The received strength indicator (in dBm) of the last received message."""
516
531
# Read RSSI register and convert to value using formula in datasheet.
517
532
# Remember in LoRa mode the payload register changes function to RSSI!
@@ -523,7 +538,7 @@ def rssi(self):
523
538
return raw_rssi
524
539
525
540
@property
526
- def snr (self ):
541
+ def snr (self ) -> float :
527
542
"""The SNR (in dB) of the last received message."""
528
543
# Read SNR 0x19 register and convert to value using formula in datasheet.
529
544
# SNR(dB) = PacketSnr [twos complement] / 4
@@ -533,7 +548,7 @@ def snr(self):
533
548
return snr_byte / 4
534
549
535
550
@property
536
- def signal_bandwidth (self ):
551
+ def signal_bandwidth (self ) -> int :
537
552
"""The signal bandwidth used by the radio (try setting to a higher
538
553
value to increase throughput or to a lower value to increase the
539
554
likelihood of successfully received payloads). Valid values are
@@ -546,7 +561,7 @@ def signal_bandwidth(self):
546
561
return current_bandwidth
547
562
548
563
@signal_bandwidth .setter
549
- def signal_bandwidth (self , val ) :
564
+ def signal_bandwidth (self , val : int ) -> None :
550
565
# Set signal bandwidth (set to 125000 to match RadioHead Bw125).
551
566
for bw_id , cutoff in enumerate (self .bw_bins ):
552
567
if val <= cutoff :
@@ -581,7 +596,7 @@ def signal_bandwidth(self, val):
581
596
self ._write_u8 (0x30 , 0 )
582
597
583
598
@property
584
- def coding_rate (self ):
599
+ def coding_rate (self ) -> Literal [ 5 , 6 , 7 , 8 ] :
585
600
"""The coding rate used by the radio to control forward error
586
601
correction (try setting to a higher value to increase tolerance of
587
602
short bursts of interference or to a lower value to increase bit
@@ -591,7 +606,7 @@ def coding_rate(self):
591
606
return denominator
592
607
593
608
@coding_rate .setter
594
- def coding_rate (self , val ) :
609
+ def coding_rate (self , val : Literal [ 5 , 6 , 7 , 8 ]) -> None :
595
610
# Set coding rate (set to 5 to match RadioHead Cr45).
596
611
denominator = min (max (val , 5 ), 8 )
597
612
cr_id = denominator - 4
@@ -601,7 +616,7 @@ def coding_rate(self, val):
601
616
)
602
617
603
618
@property
604
- def spreading_factor (self ):
619
+ def spreading_factor (self ) -> Literal [ 6 , 7 , 8 , 9 , 10 , 11 , 12 ] :
605
620
"""The spreading factor used by the radio (try setting to a higher
606
621
value to increase the receiver's ability to distinguish signal from
607
622
noise or to a lower value to increase the data transmission rate).
@@ -610,7 +625,7 @@ def spreading_factor(self):
610
625
return sf_id
611
626
612
627
@spreading_factor .setter
613
- def spreading_factor (self , val ) :
628
+ def spreading_factor (self , val : Literal [ 6 , 7 , 8 , 9 , 10 , 11 , 12 ]) -> None :
614
629
# Set spreading factor (set to 7 to match RadioHead Sf128).
615
630
val = min (max (val , 6 ), 12 )
616
631
@@ -629,14 +644,14 @@ def spreading_factor(self, val):
629
644
)
630
645
631
646
@property
632
- def enable_crc (self ):
647
+ def enable_crc (self ) -> bool :
633
648
"""Set to True to enable hardware CRC checking of incoming packets.
634
649
Incoming packets that fail the CRC check are not processed. Set to
635
650
False to disable CRC checking and process all incoming packets."""
636
651
return (self ._read_u8 (_RH_RF95_REG_1E_MODEM_CONFIG2 ) & 0x04 ) == 0x04
637
652
638
653
@enable_crc .setter
639
- def enable_crc (self , val ) :
654
+ def enable_crc (self , val : bool ) -> None :
640
655
# Optionally enable CRC checking on incoming packets.
641
656
if val :
642
657
self ._write_u8 (
@@ -649,29 +664,29 @@ def enable_crc(self, val):
649
664
self ._read_u8 (_RH_RF95_REG_1E_MODEM_CONFIG2 ) & 0xFB ,
650
665
)
651
666
652
- def tx_done (self ):
667
+ def tx_done (self ) -> bool :
653
668
"""Transmit status"""
654
669
return (self ._read_u8 (_RH_RF95_REG_12_IRQ_FLAGS ) & 0x8 ) >> 3
655
670
656
- def rx_done (self ):
671
+ def rx_done (self ) -> bool :
657
672
"""Receive status"""
658
673
return (self ._read_u8 (_RH_RF95_REG_12_IRQ_FLAGS ) & 0x40 ) >> 6
659
674
660
- def crc_error (self ):
675
+ def crc_error (self ) -> bool :
661
676
"""crc status"""
662
677
return (self ._read_u8 (_RH_RF95_REG_12_IRQ_FLAGS ) & 0x20 ) >> 5
663
678
664
679
# pylint: disable=too-many-branches
665
680
def send (
666
681
self ,
667
- data ,
682
+ data : ReadableBuffer ,
668
683
* ,
669
- keep_listening = False ,
670
- destination = None ,
671
- node = None ,
672
- identifier = None ,
673
- flags = None
674
- ):
684
+ keep_listening : bool = False ,
685
+ destination : Optional [ int ] = None ,
686
+ node : Optional [ int ] = None ,
687
+ identifier : Optional [ int ] = None ,
688
+ flags : Optional [ int ] = None
689
+ ) -> bool :
675
690
"""Send a string of data using the transmitter.
676
691
You can only send 252 bytes at a time
677
692
(limited by chip's FIFO size and appended headers).
@@ -743,7 +758,7 @@ def send(
743
758
self ._write_u8 (_RH_RF95_REG_12_IRQ_FLAGS , 0xFF )
744
759
return not timed_out
745
760
746
- def send_with_ack (self , data ) :
761
+ def send_with_ack (self , data : ReadableBuffer ) -> bool :
747
762
"""Reliable Datagram mode:
748
763
Send a packet with data and wait for an ACK response.
749
764
The packet header is automatically generated.
@@ -781,15 +796,20 @@ def send_with_ack(self, data):
781
796
return got_ack
782
797
783
798
def receive (
784
- self , * , keep_listening = True , with_header = False , with_ack = False , timeout = None
785
- ):
799
+ self ,
800
+ * ,
801
+ keep_listening : bool = True ,
802
+ with_header : bool = False ,
803
+ with_ack : bool = False ,
804
+ timeout : Optional [float ] = None
805
+ ) -> Optional [bytearray ]:
786
806
"""Wait to receive a packet from the receiver. If a packet is found the payload bytes
787
807
are returned, otherwise None is returned (which indicates the timeout elapsed with no
788
808
reception).
789
809
If keep_listening is True (the default) the chip will immediately enter listening mode
790
810
after reception of a packet, otherwise it will fall back to idle mode and ignore any
791
811
future reception.
792
- All packets must have a 4-byte header for compatibilty with the
812
+ All packets must have a 4-byte header for compatibility with the
793
813
RadioHead library.
794
814
The header consists of 4 bytes (To,From,ID,Flags). The default setting will strip
795
815
the header before returning the packet to the caller.
0 commit comments