6161except ImportError :
6262 pass
6363
64+ try :
65+ from typing import Callable , Optional , Type
66+ from circuitpython_typing import WriteableBuffer , ReadableBuffer
67+ from digitalio import DigitalInOut
68+ from busio import SPI
69+ except ImportError :
70+ pass
71+
6472__version__ = "0.0.0+auto.0"
6573__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM69.git"
6674
134142# pylint: disable=too-many-instance-attributes
135143
136144
137- def ticks_diff (ticks1 , ticks2 ) :
145+ def ticks_diff (ticks1 : int , ticks2 : int ) -> int :
138146 """Compute the signed difference between two ticks values
139147 assuming that they are within 2**28 ticks
140148 """
@@ -143,7 +151,7 @@ def ticks_diff(ticks1, ticks2):
143151 return diff
144152
145153
146- def check_timeout (flag , limit ) :
154+ def check_timeout (flag : Callable , limit : float ) -> bool :
147155 """test for timeout waiting for specified flag"""
148156 timed_out = False
149157 if HAS_SUPERVISOR :
@@ -224,7 +232,7 @@ class _RegisterBits:
224232 # check from pylint.
225233 # pylint: disable=protected-access
226234
227- def __init__ (self , address , * , offset = 0 , bits = 1 ) :
235+ def __init__ (self , address : int , * , offset : int = 0 , bits : int = 1 ) -> None :
228236 assert 0 <= offset <= 7
229237 assert 1 <= bits <= 8
230238 assert (offset + bits ) <= 8
@@ -236,11 +244,11 @@ def __init__(self, address, *, offset=0, bits=1):
236244 self ._mask <<= offset
237245 self ._offset = offset
238246
239- def __get__ (self , obj , objtype ):
247+ def __get__ (self , obj : Optional [ "RFM69" ] , objtype : Type [ "RFM69" ] ):
240248 reg_value = obj ._read_u8 (self ._address )
241249 return (reg_value & self ._mask ) >> self ._offset
242250
243- def __set__ (self , obj , val ) :
251+ def __set__ (self , obj : Optional [ "RFM69" ] , val : int ) -> None :
244252 reg_value = obj ._read_u8 (self ._address )
245253 reg_value &= ~ self ._mask
246254 reg_value |= (val & 0xFF ) << self ._offset
@@ -274,19 +282,19 @@ def __set__(self, obj, val):
274282 dio_0_mapping = _RegisterBits (_REG_DIO_MAPPING1 , offset = 6 , bits = 2 )
275283
276284 # pylint: disable=too-many-statements
277- def __init__ (
285+ def __init__ ( # pylint: disable=invalid-name
278286 self ,
279- spi ,
280- cs ,
281- reset ,
282- frequency ,
287+ spi : SPI ,
288+ cs : DigitalInOut ,
289+ reset : DigitalInOut ,
290+ frequency : int ,
283291 * ,
284- sync_word = b"\x2D \xD4 " ,
285- preamble_length = 4 ,
286- encryption_key = None ,
287- high_power = True ,
288- baudrate = 2000000
289- ):
292+ sync_word : bytes = b"\x2D \xD4 " ,
293+ preamble_length : int = 4 ,
294+ encryption_key : Optional [ bytes ] = None ,
295+ high_power : bool = True ,
296+ baudrate : int = 2000000
297+ ) -> None :
290298 self ._tx_power = 13
291299 self .high_power = high_power
292300 # Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
@@ -389,7 +397,9 @@ def __init__(
389397
390398 # pylint: disable=no-member
391399 # Reconsider this disable when it can be tested.
392- def _read_into (self , address , buf , length = None ):
400+ def _read_into (
401+ self , address : int , buf : WriteableBuffer , length : Optional [int ] = None
402+ ) -> None :
393403 # Read a number of bytes from the specified address into the provided
394404 # buffer. If length is not specified (the default) the entire buffer
395405 # will be filled.
@@ -401,12 +411,14 @@ def _read_into(self, address, buf, length=None):
401411 device .write (self ._BUFFER , end = 1 )
402412 device .readinto (buf , end = length )
403413
404- def _read_u8 (self , address ) :
414+ def _read_u8 (self , address : int ) -> int :
405415 # Read a single byte from the provided address and return it.
406416 self ._read_into (address , self ._BUFFER , length = 1 )
407417 return self ._BUFFER [0 ]
408418
409- def _write_from (self , address , buf , length = None ):
419+ def _write_from (
420+ self , address : int , buf : ReadableBuffer , length : Optional [int ] = None
421+ ) -> None :
410422 # Write a number of bytes to the provided address and taken from the
411423 # provided buffer. If no length is specified (the default) the entire
412424 # buffer is written.
@@ -418,7 +430,7 @@ def _write_from(self, address, buf, length=None):
418430 device .write (self ._BUFFER , end = 1 )
419431 device .write (buf , end = length ) # send data
420432
421- def _write_u8 (self , address , val ) :
433+ def _write_u8 (self , address : int , val : int ) -> None :
422434 # Write a byte register to the chip. Specify the 7-bit address and the
423435 # 8-bit value to write to that address.
424436 with self ._device as device :
@@ -427,27 +439,27 @@ def _write_u8(self, address, val):
427439 self ._BUFFER [1 ] = val & 0xFF
428440 device .write (self ._BUFFER , end = 2 )
429441
430- def reset (self ):
442+ def reset (self ) -> None :
431443 """Perform a reset of the chip."""
432444 # See section 7.2.2 of the datasheet for reset description.
433445 self ._reset .value = True
434446 time .sleep (0.0001 ) # 100 us
435447 self ._reset .value = False
436448 time .sleep (0.005 ) # 5 ms
437449
438- def idle (self ):
450+ def idle (self ) -> None :
439451 """Enter idle standby mode (switching off high power amplifiers if necessary)."""
440452 # Like RadioHead library, turn off high power boost if enabled.
441453 if self ._tx_power >= 18 :
442454 self ._write_u8 (_REG_TEST_PA1 , _TEST_PA1_NORMAL )
443455 self ._write_u8 (_REG_TEST_PA2 , _TEST_PA2_NORMAL )
444456 self .operation_mode = STANDBY_MODE
445457
446- def sleep (self ):
458+ def sleep (self ) -> None :
447459 """Enter sleep mode."""
448460 self .operation_mode = SLEEP_MODE
449461
450- def listen (self ):
462+ def listen (self ) -> None :
451463 """Listen for packets to be received by the chip. Use :py:func:`receive` to listen, wait
452464 and retrieve packets as they're available.
453465 """
@@ -460,7 +472,7 @@ def listen(self):
460472 # Enter RX mode (will clear FIFO!).
461473 self .operation_mode = RX_MODE
462474
463- def transmit (self ):
475+ def transmit (self ) -> None :
464476 """Transmit a packet which is queued in the FIFO. This is a low level function for
465477 entering transmit mode and more. For generating and transmitting a packet of data use
466478 :py:func:`send` instead.
@@ -475,7 +487,7 @@ def transmit(self):
475487 self .operation_mode = TX_MODE
476488
477489 @property
478- def temperature (self ):
490+ def temperature (self ) -> float :
479491 """The internal temperature of the chip in degrees Celsius. Be warned this is not
480492 calibrated or very accurate.
481493
@@ -491,7 +503,7 @@ def temperature(self):
491503 return 166.0 - temp
492504
493505 @property
494- def operation_mode (self ):
506+ def operation_mode (self ) -> int :
495507 """The operation mode value. Unless you're manually controlling the chip you shouldn't
496508 change the operation_mode with this property as other side-effects are required for
497509 changing logical modes--use :py:func:`idle`, :py:func:`sleep`, :py:func:`transmit`,
@@ -501,7 +513,7 @@ def operation_mode(self):
501513 return (op_mode >> 2 ) & 0b111
502514
503515 @operation_mode .setter
504- def operation_mode (self , val ) :
516+ def operation_mode (self , val : int ) -> None :
505517 assert 0 <= val <= 4
506518 # Set the mode bits inside the operation mode register.
507519 op_mode = self ._read_u8 (_REG_OP_MODE )
@@ -521,7 +533,7 @@ def operation_mode(self, val):
521533 raise TimeoutError ("Operation Mode failed to set." )
522534
523535 @property
524- def sync_word (self ):
536+ def sync_word (self ) -> bytearray :
525537 """The synchronization word value. This is a byte string up to 8 bytes long (64 bits)
526538 which indicates the synchronization word for transmitted and received packets. Any
527539 received packet which does not include this sync word will be ignored. The default value
@@ -539,7 +551,7 @@ def sync_word(self):
539551 return sync_word
540552
541553 @sync_word .setter
542- def sync_word (self , val ) :
554+ def sync_word (self , val : Optional [ bytearray ]) -> None :
543555 # Handle disabling sync word when None value is set.
544556 if val is None :
545557 self .sync_on = 0
@@ -553,7 +565,7 @@ def sync_word(self, val):
553565 self .sync_on = 1
554566
555567 @property
556- def preamble_length (self ):
568+ def preamble_length (self ) -> int :
557569 """The length of the preamble for sent and received packets, an unsigned 16-bit value.
558570 Received packets must match this length or they are ignored! Set to 4 to match the
559571 RadioHead RFM69 library.
@@ -563,13 +575,13 @@ def preamble_length(self):
563575 return ((msb << 8 ) | lsb ) & 0xFFFF
564576
565577 @preamble_length .setter
566- def preamble_length (self , val ) :
578+ def preamble_length (self , val : int ) -> None :
567579 assert 0 <= val <= 65535
568580 self ._write_u8 (_REG_PREAMBLE_MSB , (val >> 8 ) & 0xFF )
569581 self ._write_u8 (_REG_PREAMBLE_LSB , val & 0xFF )
570582
571583 @property
572- def frequency_mhz (self ):
584+ def frequency_mhz (self ) -> float :
573585 """The frequency of the radio in Megahertz. Only the allowed values for your radio must be
574586 specified (i.e. 433 vs. 915 mhz)!
575587 """
@@ -584,7 +596,7 @@ def frequency_mhz(self):
584596 return frequency
585597
586598 @frequency_mhz .setter
587- def frequency_mhz (self , val ) :
599+ def frequency_mhz (self , val : float ) -> None :
588600 assert 290 <= val <= 1020
589601 # Calculate FRF register 24-bit value using section 6.2 of the datasheet.
590602 frf = int ((val * 1000000.0 ) / _FSTEP ) & 0xFFFFFF
@@ -597,7 +609,7 @@ def frequency_mhz(self, val):
597609 self ._write_u8 (_REG_FRF_LSB , lsb )
598610
599611 @property
600- def encryption_key (self ):
612+ def encryption_key (self ) -> bytearray :
601613 """The AES encryption key used to encrypt and decrypt packets by the chip. This can be set
602614 to None to disable encryption (the default), otherwise it must be a 16 byte long byte
603615 string which defines the key (both the transmitter and receiver must use the same key
@@ -612,7 +624,7 @@ def encryption_key(self):
612624 return key
613625
614626 @encryption_key .setter
615- def encryption_key (self , val ) :
627+ def encryption_key (self , val : bytearray ) -> None :
616628 # Handle if unsetting the encryption key (None value).
617629 if val is None :
618630 self .aes_on = 0
@@ -623,7 +635,7 @@ def encryption_key(self, val):
623635 self .aes_on = 1
624636
625637 @property
626- def tx_power (self ):
638+ def tx_power (self ) -> int :
627639 """The transmit power in dBm. Can be set to a value from -2 to 20 for high power devices
628640 (RFM69HCW, high_power=True) or -18 to 13 for low power devices. Only integer power
629641 levels are actually set (i.e. 12.5 will result in a value of 12 dBm).
@@ -648,7 +660,7 @@ def tx_power(self):
648660 raise RuntimeError ("Power amplifiers in unknown state!" )
649661
650662 @tx_power .setter
651- def tx_power (self , val ):
663+ def tx_power (self , val : float ):
652664 val = int (val )
653665 # Determine power amplifier and output power values depending on
654666 # high power state and requested power.
@@ -685,7 +697,7 @@ def tx_power(self, val):
685697 self ._tx_power = val
686698
687699 @property
688- def rssi (self ):
700+ def rssi (self ) -> float :
689701 """The received strength indicator (in dBm).
690702 May be inaccuate if not read immediatey. last_rssi contains the value read immediately
691703 receipt of the last packet.
@@ -694,7 +706,7 @@ def rssi(self):
694706 return - self ._read_u8 (_REG_RSSI_VALUE ) / 2.0
695707
696708 @property
697- def bitrate (self ):
709+ def bitrate (self ) -> float :
698710 """The modulation bitrate in bits/second (or chip rate if Manchester encoding is enabled).
699711 Can be a value from ~489 to 32mbit/s, but see the datasheet for the exact supported
700712 values.
@@ -704,47 +716,47 @@ def bitrate(self):
704716 return _FXOSC / ((msb << 8 ) | lsb )
705717
706718 @bitrate .setter
707- def bitrate (self , val ) :
719+ def bitrate (self , val : float ) -> None :
708720 assert (_FXOSC / 65535 ) <= val <= 32000000.0
709721 # Round up to the next closest bit-rate value with addition of 0.5.
710722 bitrate = int ((_FXOSC / val ) + 0.5 ) & 0xFFFF
711723 self ._write_u8 (_REG_BITRATE_MSB , bitrate >> 8 )
712724 self ._write_u8 (_REG_BITRATE_LSB , bitrate & 0xFF )
713725
714726 @property
715- def frequency_deviation (self ):
727+ def frequency_deviation (self ) -> float :
716728 """The frequency deviation in Hertz."""
717729 msb = self ._read_u8 (_REG_FDEV_MSB )
718730 lsb = self ._read_u8 (_REG_FDEV_LSB )
719731 return _FSTEP * ((msb << 8 ) | lsb )
720732
721733 @frequency_deviation .setter
722- def frequency_deviation (self , val ) :
734+ def frequency_deviation (self , val : float ) -> None :
723735 assert 0 <= val <= (_FSTEP * 16383 ) # fdev is a 14-bit unsigned value
724736 # Round up to the next closest integer value with addition of 0.5.
725737 fdev = int ((val / _FSTEP ) + 0.5 ) & 0x3FFF
726738 self ._write_u8 (_REG_FDEV_MSB , fdev >> 8 )
727739 self ._write_u8 (_REG_FDEV_LSB , fdev & 0xFF )
728740
729- def packet_sent (self ):
741+ def packet_sent (self ) -> bool :
730742 """Transmit status"""
731743 return (self ._read_u8 (_REG_IRQ_FLAGS2 ) & 0x8 ) >> 3
732744
733- def payload_ready (self ):
745+ def payload_ready (self ) -> bool :
734746 """Receive status"""
735747 return (self ._read_u8 (_REG_IRQ_FLAGS2 ) & 0x4 ) >> 2
736748
737749 # pylint: disable=too-many-branches
738750 def send (
739751 self ,
740- data ,
752+ data : ReadableBuffer ,
741753 * ,
742- keep_listening = False ,
743- destination = None ,
744- node = None ,
745- identifier = None ,
746- flags = None
747- ):
754+ keep_listening : bool = False ,
755+ destination : Optional [ int ] = None ,
756+ node : Optional [ int ] = None ,
757+ identifier : Optional [ int ] = None ,
758+ flags : Optional [ int ] = None
759+ ) -> bool :
748760 """Send a string of data using the transmitter.
749761 You can only send 60 bytes at a time
750762 (limited by chip's FIFO size and appended headers).
@@ -801,7 +813,7 @@ def send(
801813 self .idle ()
802814 return not timed_out
803815
804- def send_with_ack (self , data ) :
816+ def send_with_ack (self , data : int ) -> bool :
805817 """Reliable Datagram mode:
806818 Send a packet with data and wait for an ACK response.
807819 The packet header is automatically generated.
@@ -839,8 +851,13 @@ def send_with_ack(self, data):
839851 return got_ack
840852
841853 def receive (
842- self , * , keep_listening = True , with_ack = False , timeout = None , with_header = False
843- ):
854+ self ,
855+ * ,
856+ keep_listening : bool = True ,
857+ with_ack : bool = False ,
858+ timeout : Optional [float ] = None ,
859+ with_header : bool = False
860+ ) -> int :
844861 """Wait to receive a packet from the receiver. If a packet is found the payload bytes
845862 are returned, otherwise None is returned (which indicates the timeout elapsed with no
846863 reception).
0 commit comments