adafruit-circuitpython-rfm 1.0.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,575 @@
1
+ # SPDX-FileCopyrightText: 2024 Jerry Needell for Adafruit Industries
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ """
6
+ `adafruit_rfm.rfm9xfsk`
7
+ ====================================================
8
+
9
+ CircuitPython module for the RFM95/6/7/8 FSK 433/915mhz radio modules.
10
+
11
+ * Author(s): Jerry Needell
12
+ """
13
+
14
+ import time
15
+
16
+ from micropython import const
17
+
18
+ from adafruit_rfm.rfm_common import RFMSPI
19
+
20
+ try:
21
+ from typing import Optional
22
+
23
+ import busio
24
+ import digitalio
25
+ from circuitpython_typing import ReadableBuffer
26
+
27
+ try:
28
+ from typing import Literal
29
+ except ImportError:
30
+ from typing_extensions import Literal
31
+
32
+ except ImportError:
33
+ pass
34
+
35
+ __version__ = "1.0.4"
36
+ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM.git"
37
+
38
+ # pylint: disable=duplicate-code
39
+
40
+ # Internal constants:
41
+ # Register names (FSK Mode even though we use LoRa instead, from table 85)
42
+ _RF95_REG_00_FIFO = const(0x00)
43
+ _RF95_REG_01_OP_MODE = const(0x01)
44
+ _RF95_REG_02_BITRATE_MSB = const(0x02)
45
+ _RF95_REG_03_BITRATE_LSB = const(0x03)
46
+ _RF95_REG_04_FDEV_MSB = const(0x4)
47
+ _RF95_REG_05_FDEV_LSB = const(0x5)
48
+ _RF95_REG_06_FRF_MSB = const(0x06)
49
+ _RF95_REG_07_FRF_MID = const(0x07)
50
+ _RF95_REG_08_FRF_LSB = const(0x08)
51
+ _RF95_REG_09_PA_CONFIG = const(0x09)
52
+ _RF95_REG_0A_PA_RAMP = const(0x0A)
53
+ _RF95_REG_0B_OCP = const(0x0B)
54
+ _RF95_REG_0C_LNA = const(0x0C)
55
+ _RF95_REG_0D_RX_CFG = const(0x0D)
56
+ _RF95_REG_0E_RSSI_CFG = const(0x0E)
57
+ _RF95_REG_0F_RSSI_COLLISION = const(0x0F)
58
+ _RF95_REG_10_RSSI_THRESH = const(0x10)
59
+ _RF95_REG_11_RSSI_VALUE = const(0x11)
60
+ _RF95_REG_12_RX_BW = const(0x12)
61
+ _RF95_REG_13_AFC_BW = const(0x13)
62
+ _RF95_REG_14_OOK_PEAK = const(0x14)
63
+ _RF95_REG_15_OOK_FIX = const(0x15)
64
+ _RF95_REG_16_OOK_AVG = const(0x16)
65
+ _RF95_REG_1A_AFC_FEI_CTL = const(0x1A)
66
+ _RF95_REG_1B_AFC_MSB = const(0x1B)
67
+ _RF95_REG_1C_AFC_LSB = const(0x1C)
68
+ _RF95_REG_1D_FEI_MSB = const(0x1D)
69
+ _RF95_REG_1E_FEI_LSB = const(0x1E)
70
+ _RF95_REG_1F_PREAMBLE_DETECT = const(0x1F)
71
+ _RF95_REG_20_RX_TIMEOUT_1 = const(0x20)
72
+ _RF95_REG_21_RX_TIMEOUT_2 = const(0x21)
73
+ _RF95_REG_22_RX_TIMEOUT_3 = const(0x22)
74
+ _RF95_REG_23_RX_DELAY = const(0x23)
75
+ _RF95_REG_24_OSC = const(0x24)
76
+ _RF95_REG_25_PREAMBLE_MSB = const(0x25)
77
+ _RF95_REG_26_PREAMBLE_LSB = const(0x26)
78
+ _RF95_REG_27_SYNC_CONFIG = const(0x27)
79
+ _RF95_REG_28_SYNC_VALUE_1 = const(0x28)
80
+ _RF95_REG_29_SYNC_VALUE_2 = const(0x29)
81
+ _RF95_REG_2A_SYNC_VALUE_3 = const(0x2A)
82
+ _RF95_REG_2B_SYNC_VALUE_4 = const(0x2B)
83
+ _RF95_REG_2C_SYNC_VALUE_5 = const(0x2C)
84
+ _RF95_REG_2D_SYNC_VALUE_6 = const(0x2D)
85
+ _RF95_REG_2E_SYNC_VALUE_7 = const(0x2E)
86
+ _RF95_REG_2F_SYNC_VALUE_8 = const(0x2F)
87
+ _RF95_REG_30_PACKET_CONFIG_1 = const(0x30)
88
+ _RF95_REG_31_PACKET_CONFIG_2 = const(0x31)
89
+ _RF95_REG_32_PAYLOAD_LENGTH = const(0x32)
90
+ _RF95_REG_33_NODE_ADDR = const(0x33)
91
+ _RF95_REG_34_BROADCAST_ADDR = const(0x34)
92
+ _RF95_REG_35_FIFO_THRESH = const(0x35)
93
+ _RF95_REG_36_SEQ_CFG_1 = const(0x36)
94
+ _RF95_REG_37_SEQ_CFG_2 = const(0x37)
95
+ _RF95_REG_38_TIMER_RES = const(0x38)
96
+ _RF95_REG_39_TIMER1_COEF = const(0x39)
97
+ _RF95_REG_3A_TIMER2_COEF = const(0x3A)
98
+ _RF95_REG_3B_IMAGE_CAL = const(0x3B)
99
+ _RF95_REG_3C_TEMP = const(0x3C)
100
+ _RF95_REG_3D_LOW_BAT = const(0x3D)
101
+ _RF95_REG_3E_IRQ_FLAGS_1 = const(0x3E)
102
+ _RF95_REG_3F_IRQ_FLAGS_2 = const(0x3F)
103
+
104
+ _RF95_REG_40_DIO_MAPPING1 = const(0x40)
105
+ _RF95_REG_41_DIO_MAPPING2 = const(0x41)
106
+ _RF95_REG_42_VERSION = const(0x42)
107
+
108
+ _RF95_REG_44_PIII_IOP = const(0x44)
109
+
110
+ _RF95_REG_4B_TCXO = const(0x4B)
111
+ _RF95_REG_4D_PA_DAC = const(0x4D)
112
+ _RF95_REG_5B_FORMER_TEMP = const(0x5B)
113
+ _RF95_REG_5B_BIT_RATE_FRAC = const(0x5D)
114
+ _RF95_REG_61_AGC_REF = const(0x61)
115
+ _RF95_REG_62_AGC_THRESH1 = const(0x62)
116
+ _RF95_REG_63_AGC_THRESH2 = const(0x63)
117
+ _RF95_REG_64_AGC_THRESH3 = const(0x64)
118
+
119
+
120
+ _RF95_PA_DAC_DISABLE = const(0x04)
121
+ _RF95_PA_DAC_ENABLE = const(0x07)
122
+
123
+ # The crystal oscillator frequency of the module
124
+ _RF95_FXOSC = 32000000.0
125
+
126
+ # The Frequency Synthesizer step = RH_RF95_FXOSC / 2^^19
127
+ _RF95_FSTEP = _RF95_FXOSC / 524288
128
+
129
+ # RadioHead specific compatibility constants.
130
+ _RH_BROADCAST_ADDRESS = const(0xFF)
131
+
132
+ # The acknowledgement bit in the FLAGS
133
+ # The top 4 bits of the flags are reserved for RadioHead. The lower 4 bits are reserved
134
+ # for application layer use.
135
+ _RH_FLAGS_ACK = const(0x80)
136
+ _RH_FLAGS_RETRY = const(0x40)
137
+
138
+ # User facing constants:
139
+ SLEEP_MODE = 0b000
140
+ STANDBY_MODE = 0b001
141
+ FS_TX_MODE = 0b010
142
+ TX_MODE = 0b011
143
+ FS_RX_MODE = 0b100
144
+ RX_MODE = 0b101
145
+
146
+
147
+ # pylint: disable=too-many-instance-attributes
148
+ # pylint: disable=too-many-public-methods
149
+ class RFM9xFSK(RFMSPI):
150
+ """Interface to a RFM95/6/7/8 FSK radio module. Allows sending and
151
+ receiving bytes of data in FSK mode at a support board frequency
152
+ (433/915mhz).
153
+
154
+ :param busio.SPI spi: The SPI bus connected to the chip. Ensure SCK, MOSI, and MISO are
155
+ connected.
156
+ :param ~digitalio.DigitalInOut cs: A DigitalInOut object connected to the chip's CS/chip select
157
+ line.
158
+ :param ~digitalio.DigitalInOut reset: A DigitalInOut object connected to the chip's RST/reset
159
+ line.
160
+ :param int frequency: The center frequency to configure for radio transmission and reception.
161
+ Must be a frequency supported by your hardware (i.e. either 433 or 915mhz).
162
+ :param bytes sync_word: A byte string up to 8 bytes long which represents the syncronization
163
+ word used by received and transmitted packets. Read the datasheet for a full understanding
164
+ of this value! However by default the library will set a value that matches the RadioHead
165
+ Arduino library.
166
+ :param int preamble_length: The number of bytes to pre-pend to a data packet as a preamble.
167
+ This is by default 4 to match the RadioHead library.
168
+ :param bool high_power: Indicate if the chip is a high power variant that supports boosted
169
+ transmission power. The default is True as it supports the common RFM69HCW modules sold by
170
+ Adafruit.
171
+
172
+ Also note this library tries to be compatible with raw RadioHead Arduino
173
+ library communication. This means the library sets up the radio modulation
174
+ to match RadioHead's defaults.
175
+ Advanced RadioHead features like address/node specific packets
176
+ or "reliable datagram" delivery are supported however due to the
177
+ limitations noted, "reliable datagram" is still subject to missed packets.
178
+ """
179
+
180
+ operation_mode = RFMSPI.RegisterBits(_RF95_REG_01_OP_MODE, bits=3)
181
+ low_frequency_mode = RFMSPI.RegisterBits(_RF95_REG_01_OP_MODE, offset=3, bits=1)
182
+ modulation_type = RFMSPI.RegisterBits(_RF95_REG_01_OP_MODE, offset=5, bits=2)
183
+ modulation_shaping = RFMSPI.RegisterBits(_RF95_REG_0A_PA_RAMP, offset=5, bits=2)
184
+ # Long range/LoRa mode can only be set in sleep mode!
185
+ long_range_mode = RFMSPI.RegisterBits(_RF95_REG_01_OP_MODE, offset=7, bits=1)
186
+ sync_on = RFMSPI.RegisterBits(_RF95_REG_27_SYNC_CONFIG, offset=4, bits=1)
187
+ sync_size = RFMSPI.RegisterBits(_RF95_REG_27_SYNC_CONFIG, offset=0, bits=3)
188
+ output_power = RFMSPI.RegisterBits(_RF95_REG_09_PA_CONFIG, bits=4)
189
+ max_power = RFMSPI.RegisterBits(_RF95_REG_09_PA_CONFIG, offset=4, bits=3)
190
+ pa_select = RFMSPI.RegisterBits(_RF95_REG_09_PA_CONFIG, offset=7, bits=1)
191
+ pa_dac = RFMSPI.RegisterBits(_RF95_REG_4D_PA_DAC, bits=3)
192
+ dio0_mapping = RFMSPI.RegisterBits(_RF95_REG_40_DIO_MAPPING1, offset=6, bits=2)
193
+ lna_boost_hf = RFMSPI.RegisterBits(_RF95_REG_0C_LNA, offset=0, bits=2)
194
+ rx_bw_mantissa = RFMSPI.RegisterBits(_RF95_REG_12_RX_BW, offset=3, bits=2)
195
+ rx_bw_exponent = RFMSPI.RegisterBits(_RF95_REG_12_RX_BW, offset=0, bits=3)
196
+ afc_bw_mantissa = RFMSPI.RegisterBits(_RF95_REG_13_AFC_BW, offset=3, bits=2)
197
+ afc_bw_exponent = RFMSPI.RegisterBits(_RF95_REG_13_AFC_BW, offset=0, bits=3)
198
+ packet_format = RFMSPI.RegisterBits(_RF95_REG_30_PACKET_CONFIG_1, offset=7, bits=1)
199
+ dc_free = RFMSPI.RegisterBits(_RF95_REG_30_PACKET_CONFIG_1, offset=5, bits=2)
200
+ crc_on = RFMSPI.RegisterBits(_RF95_REG_30_PACKET_CONFIG_1, offset=4, bits=1)
201
+ crc_auto_clear_off = RFMSPI.RegisterBits(_RF95_REG_30_PACKET_CONFIG_1, offset=3, bits=1)
202
+ address_filter = RFMSPI.RegisterBits(_RF95_REG_30_PACKET_CONFIG_1, offset=1, bits=2)
203
+ crc_type = RFMSPI.RegisterBits(_RF95_REG_30_PACKET_CONFIG_1, offset=0, bits=1)
204
+ mode_ready = RFMSPI.RegisterBits(_RF95_REG_3E_IRQ_FLAGS_1, offset=7)
205
+ ook_bit_sync_on = RFMSPI.RegisterBits(_RF95_REG_14_OOK_PEAK, offset=5, bits=1)
206
+ ook_thresh_type = RFMSPI.RegisterBits(_RF95_REG_14_OOK_PEAK, offset=4, bits=2)
207
+ ook_thresh_step = RFMSPI.RegisterBits(_RF95_REG_14_OOK_PEAK, offset=0, bits=3)
208
+ ook_peak_thresh_dec = RFMSPI.RegisterBits(_RF95_REG_16_OOK_AVG, offset=5, bits=3)
209
+ ook_average_offset = RFMSPI.RegisterBits(_RF95_REG_16_OOK_AVG, offset=2, bits=2)
210
+ ook_average_thresh_filt = RFMSPI.RegisterBits(_RF95_REG_16_OOK_AVG, offset=0, bits=2)
211
+
212
+ def __init__( # noqa: PLR0913
213
+ self,
214
+ spi: busio.SPI,
215
+ cs: digitalio.DigitalInOut, # pylint: disable=invalid-name
216
+ rst: digitalio.DigitalInOut,
217
+ frequency: int,
218
+ *,
219
+ sync_word: bytes = b"\x2d\xd4",
220
+ preamble_length: int = 4,
221
+ high_power: bool = True,
222
+ baudrate: int = 5000000,
223
+ crc: bool = True,
224
+ ) -> None:
225
+ super().__init__(spi, cs, baudrate=baudrate)
226
+ self.module = "RFM9X"
227
+ self.max_packet_length = 252
228
+ self.high_power = high_power
229
+ # Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
230
+ # Set Default Baudrate to 5MHz to avoid problems
231
+ # self._device = spidev.SPIDevice(spi, cs, baudrate=baudrate, polarity=0, phase=0)
232
+ # Setup reset as a digital output - initially High
233
+ # This line is pulled low as an output quickly to trigger a reset.
234
+ self._rst = rst
235
+ # initialize Reset High
236
+ self._rst.switch_to_output(value=True)
237
+ self.reset()
238
+ # No device type check! Catch an error from the very first request and
239
+ # throw a nicer message to indicate possible wiring problems.
240
+ version = self.read_u8(address=_RF95_REG_42_VERSION)
241
+ if version != 18:
242
+ raise RuntimeError(
243
+ "Failed to find rfm9x with expected version -- check wiring. Version found:",
244
+ hex(version),
245
+ )
246
+
247
+ # Set sleep mode, wait 10s and confirm in sleep mode (basic device check).
248
+ # Also set long range mode (LoRa mode) as it can only be done in sleep.
249
+ self.sleep()
250
+ time.sleep(0.01)
251
+ self.long_range_mode = False
252
+ if self.operation_mode != SLEEP_MODE or self.long_range_mode:
253
+ raise RuntimeError("Failed to configure radio for FSK mode, check wiring!")
254
+ # clear default setting for access to LF registers if frequency > 525MHz
255
+ if frequency > 525:
256
+ self.low_frequency_mode = 0
257
+ # Set mode idle
258
+ self.idle()
259
+ # Setup the chip in a similar way to the RadioHead RFM69 library.
260
+ # Set FIFO TX condition to not empty and the default FIFO threshold to 15.
261
+ self.write_u8(_RF95_REG_35_FIFO_THRESH, 0b10001111)
262
+ # Set the syncronization word.
263
+ self.sync_word = sync_word
264
+ self.preamble_length = preamble_length # Set the preamble length.
265
+ self.frequency_mhz = frequency # Set frequency.
266
+ # Configure modulation for RadioHead library GFSK_Rb250Fd250 mode
267
+ # by default. Users with advanced knowledge can manually reconfigure
268
+ # for any other mode (consulting the datasheet is absolutely
269
+ # necessary!).
270
+ self.modulation_shaping = 0b01 # Gaussian filter, BT=1.0
271
+ self.bitrate = 250000 # 250kbs
272
+ self.frequency_deviation = 250000 # 250khz
273
+ self.rx_bw_mantissa = 0b00
274
+ self.rx_bw_exponent = 0b000
275
+ self.afc_bw_mantissa = 0b00
276
+ self.afc_bw_exponent = 0b000
277
+ self.packet_format = 1 # Variable length.
278
+ self.dc_free = 0b10 # Whitening
279
+ # Set transmit power to 13 dBm, a safe value any module supports.
280
+ self._tx_power = 13
281
+ self.tx_power = self._tx_power
282
+
283
+ # Default to enable CRC checking on incoming packets.
284
+ self.enable_crc = crc
285
+ """CRC Enable state"""
286
+ self.snr = None
287
+
288
+ def reset(self) -> None:
289
+ """Perform a reset of the chip."""
290
+ # See section 7.2.2 of the datasheet for reset description.
291
+ self._rst.value = False # Set Reset Low
292
+ time.sleep(0.0001) # 100 us
293
+ self._rst.value = True # set Reset High
294
+ time.sleep(0.005) # 5 ms
295
+
296
+ def idle(self) -> None:
297
+ """Enter idle standby mode."""
298
+ self.operation_mode = STANDBY_MODE
299
+
300
+ def sleep(self) -> None:
301
+ """Enter sleep mode."""
302
+ self.operation_mode = SLEEP_MODE
303
+
304
+ def listen(self) -> None:
305
+ """Listen for packets to be received by the chip. Use :py:func:`receive`
306
+ to listen, wait and retrieve packets as they're available.
307
+ """
308
+ self.operation_mode = RX_MODE
309
+ self.dio0_mapping = 0b00 # Interrupt on rx done.
310
+
311
+ def transmit(self) -> None:
312
+ """Transmit a packet which is queued in the FIFO. This is a low level
313
+ function for entering transmit mode and more. For generating and
314
+ transmitting a packet of data use :py:func:`send` instead.
315
+ """
316
+ self.operation_mode = TX_MODE
317
+ self.dio0_mapping = 0b00 # Interrupt on tx done.
318
+
319
+ @property
320
+ def sync_word(self) -> bytearray:
321
+ """The synchronization word value. This is a byte string up to 8 bytes long (64 bits)
322
+ which indicates the synchronization word for transmitted and received packets. Any
323
+ received packet which does not include this sync word will be ignored. The default value
324
+ is 0x2D, 0xD4 which matches the RadioHead RFM69 library. Setting a value of None will
325
+ disable synchronization word matching entirely.
326
+ """
327
+ # Handle when sync word is disabled..
328
+ if not self.sync_on:
329
+ return None
330
+ # Sync word is not disabled so read the current value.
331
+ sync_word_length = self.sync_size + 1 # Sync word size is offset by 1
332
+ # according to datasheet.
333
+ sync_word = bytearray(sync_word_length)
334
+ self.read_into(_RF95_REG_28_SYNC_VALUE_1, sync_word)
335
+ return sync_word
336
+
337
+ @sync_word.setter
338
+ def sync_word(self, val: Optional[bytearray]) -> None:
339
+ # Handle disabling sync word when None value is set.
340
+ if val is None:
341
+ self.sync_on = 0
342
+ else:
343
+ # Check sync word is at most 8 bytes.
344
+ assert 1 <= len(val) <= 8
345
+ # Update the value, size and turn on the sync word.
346
+ self.write_from(_RF95_REG_28_SYNC_VALUE_1, val)
347
+ self.sync_size = len(val) - 1 # Again sync word size is offset by
348
+ # 1 according to datasheet.
349
+ self.sync_on = 1
350
+
351
+ @property
352
+ def bitrate(self) -> float:
353
+ """The modulation bitrate in bits/second (or chip rate if Manchester encoding is enabled).
354
+ Can be a value from ~489 to 32mbit/s, but see the datasheet for the exact supported
355
+ values.
356
+ """
357
+ msb = self.read_u8(_RF95_REG_02_BITRATE_MSB)
358
+ lsb = self.read_u8(_RF95_REG_03_BITRATE_LSB)
359
+ return _RF95_FXOSC / ((msb << 8) | lsb)
360
+
361
+ @bitrate.setter
362
+ def bitrate(self, val: float) -> None:
363
+ assert (_RF95_FXOSC / 65535) <= val <= 32000000.0
364
+ # Round up to the next closest bit-rate value with addition of 0.5.
365
+ bitrate = int((_RF95_FXOSC / val) + 0.5) & 0xFFFF
366
+ self.write_u8(_RF95_REG_02_BITRATE_MSB, bitrate >> 8)
367
+ self.write_u8(_RF95_REG_03_BITRATE_LSB, bitrate & 0xFF)
368
+
369
+ @property
370
+ def frequency_deviation(self) -> float:
371
+ """The frequency deviation in Hertz."""
372
+ msb = self.read_u8(_RF95_REG_04_FDEV_MSB)
373
+ lsb = self.read_u8(_RF95_REG_05_FDEV_LSB)
374
+ return _RF95_FSTEP * ((msb << 8) | lsb)
375
+
376
+ @frequency_deviation.setter
377
+ def frequency_deviation(self, val: float) -> None:
378
+ assert 0 <= val <= (_RF95_FSTEP * 16383) # fdev is a 14-bit unsigned value
379
+ # Round up to the next closest integer value with addition of 0.5.
380
+ fdev = int((val / _RF95_FSTEP) + 0.5) & 0x3FFF
381
+ self.write_u8(_RF95_REG_04_FDEV_MSB, fdev >> 8)
382
+ self.write_u8(_RF95_REG_05_FDEV_LSB, fdev & 0xFF)
383
+
384
+ @property
385
+ def temperature(self) -> float:
386
+ """The internal temperature of the chip.. See Sec 5.5.7 of the DataSheet
387
+ calibrated or very accurate.
388
+ """
389
+ temp = self.read_u8(_RF95_REG_3C_TEMP)
390
+ return temp
391
+
392
+ @property
393
+ def preamble_length(self) -> int:
394
+ """The length of the preamble for sent and received packets, an unsigned
395
+ 16-bit value. Received packets must match this length or they are
396
+ ignored! Set to 4 to match the RF69.
397
+ """
398
+ msb = self.read_u8(_RF95_REG_25_PREAMBLE_MSB)
399
+ lsb = self.read_u8(_RF95_REG_26_PREAMBLE_LSB)
400
+ return ((msb << 8) | lsb) & 0xFFFF
401
+
402
+ @preamble_length.setter
403
+ def preamble_length(self, val: int) -> None:
404
+ assert 0 <= val <= 65535
405
+ self.write_u8(_RF95_REG_25_PREAMBLE_MSB, (val >> 8) & 0xFF)
406
+ self.write_u8(_RF95_REG_26_PREAMBLE_LSB, val & 0xFF)
407
+
408
+ @property
409
+ def frequency_mhz(self) -> Literal[433.0, 915.0]:
410
+ """The frequency of the radio in Megahertz. Only the allowed values for
411
+ your radio must be specified (i.e. 433 vs. 915 mhz)!
412
+ """
413
+ msb = self.read_u8(_RF95_REG_06_FRF_MSB)
414
+ mid = self.read_u8(_RF95_REG_07_FRF_MID)
415
+ lsb = self.read_u8(_RF95_REG_08_FRF_LSB)
416
+ frf = ((msb << 16) | (mid << 8) | lsb) & 0xFFFFFF
417
+ frequency = (frf * _RF95_FSTEP) / 1000000.0
418
+ return frequency
419
+
420
+ @frequency_mhz.setter
421
+ def frequency_mhz(self, val: Literal[433.0, 915.0]) -> None:
422
+ if val < 240 or val > 960:
423
+ raise RuntimeError("frequency_mhz must be between 240 and 960")
424
+ # Calculate FRF register 24-bit value.
425
+ frf = int((val * 1000000.0) / _RF95_FSTEP) & 0xFFFFFF
426
+ # Extract byte values and update registers.
427
+ msb = frf >> 16
428
+ mid = (frf >> 8) & 0xFF
429
+ lsb = frf & 0xFF
430
+ self.write_u8(_RF95_REG_06_FRF_MSB, msb)
431
+ self.write_u8(_RF95_REG_07_FRF_MID, mid)
432
+ self.write_u8(_RF95_REG_08_FRF_LSB, lsb)
433
+
434
+ @property
435
+ def tx_power(self) -> int:
436
+ """The transmit power in dBm. Can be set to a value from 5 to 23 for
437
+ high power devices (RFM95/96/97/98, high_power=True) or -1 to 14 for low
438
+ power devices. Only integer power levels are actually set (i.e. 12.5
439
+ will result in a value of 12 dBm).
440
+ The actual maximum setting for high_power=True is 20dBm but for values > 20
441
+ the PA_BOOST will be enabled resulting in an additional gain of 3dBm.
442
+ The actual setting is reduced by 3dBm.
443
+ The reported value will reflect the reduced setting.
444
+ """
445
+ if self.high_power:
446
+ return self.output_power + 5
447
+ return self.output_power - 1
448
+
449
+ @tx_power.setter
450
+ def tx_power(self, val: int) -> None:
451
+ val = int(val)
452
+ if self.high_power:
453
+ if val < 5 or val > 23:
454
+ raise RuntimeError("tx_power must be between 5 and 23")
455
+ # Enable power amp DAC if power is above 20 dB.
456
+ # Lower setting by 3db when PA_BOOST enabled - see Data Sheet Section 6.4
457
+ if val > 20:
458
+ self.pa_dac = _RF95_PA_DAC_ENABLE
459
+ val -= 3
460
+ else:
461
+ self.pa_dac = _RF95_PA_DAC_DISABLE
462
+ self.pa_select = True
463
+ self.output_power = (val - 5) & 0x0F
464
+ else:
465
+ assert -1 <= val <= 14
466
+ self.pa_select = False
467
+ self.max_power = 0b111 # Allow max power output.
468
+ self.output_power = (val + 1) & 0x0F
469
+
470
+ @property
471
+ def rssi(self) -> float:
472
+ """The received strength indicator (in dBm) of the last received message."""
473
+ # Read RSSI register and convert to value using formula in datasheet.
474
+ # Remember in LoRa mode the payload register changes function to RSSI!
475
+ raw_rssi = self.read_u8(_RF95_REG_11_RSSI_VALUE)
476
+ return -raw_rssi / 2.0
477
+
478
+ @property
479
+ def enable_crc(self) -> bool:
480
+ """Set to True to enable hardware CRC checking of incoming packets.
481
+ Incoming packets that fail the CRC check are not processed. Set to
482
+ False to disable CRC checking and process all incoming packets."""
483
+ return self.crc_on
484
+
485
+ @enable_crc.setter
486
+ def enable_crc(self, val: bool) -> None:
487
+ # Optionally enable CRC checking on incoming packets.
488
+ if val:
489
+ self.crc_on = 1
490
+ self.crc_type = 0 # use CCITT for RF69 compatibility
491
+ else:
492
+ self.crc_on = 0
493
+
494
+ @property
495
+ def crc_error(self) -> bool:
496
+ """crc status"""
497
+ return (self.read_u8(_RF95_REG_3F_IRQ_FLAGS_2) & 0x2) >> 1
498
+
499
+ @property
500
+ def enable_address_filter(self) -> bool:
501
+ """Set to True to enable address filtering.
502
+ Incoming packets that do no match the node address or broadcast address
503
+ will be ignored."""
504
+ return self.address_filter
505
+
506
+ @enable_address_filter.setter
507
+ def enable_address_filter(self, val: bool) -> None:
508
+ # Enable address filtering on incoming packets.
509
+ if val:
510
+ self.address_filter = 2 # accept node address or broadcast address
511
+ else:
512
+ self.address_filter = 0
513
+
514
+ @property
515
+ def fsk_node_address(self) -> int:
516
+ """Node Address for Address Filtering"""
517
+ return self.read_u8(_RF95_REG_33_NODE_ADDR)
518
+
519
+ @fsk_node_address.setter
520
+ def fsk_node_address(self, val: int) -> None:
521
+ assert 0 <= val <= 255
522
+ self.write_u8(_RF95_REG_33_NODE_ADDR, val)
523
+
524
+ @property
525
+ def fsk_broadcast_address(self) -> int:
526
+ """Node Address for Address Filtering"""
527
+ return self.read_u8(_RF95_REG_34_BROADCAST_ADDR)
528
+
529
+ @fsk_broadcast_address.setter
530
+ def fsk_broadcast_address(self, val: int) -> None:
531
+ assert 0 <= val <= 255
532
+ self.write_u8(_RF95_REG_34_BROADCAST_ADDR, val)
533
+
534
+ @property
535
+ def ook_fixed_threshold(self) -> int:
536
+ """Fixed threshold for data slicer in OOK mode"""
537
+ return self.read_u8(_RF95_REG_15_OOK_FIX)
538
+
539
+ @ook_fixed_threshold.setter
540
+ def ook_fixed_threshold(self, val: int) -> None:
541
+ assert 0 <= val <= 255
542
+ self.write_u8(_RF95_REG_15_OOK_FIX, val)
543
+
544
+ def packet_sent(self) -> bool:
545
+ """Transmit status"""
546
+ return (self.read_u8(_RF95_REG_3F_IRQ_FLAGS_2) & 0x8) >> 3
547
+
548
+ def payload_ready(self) -> bool:
549
+ """Receive status"""
550
+ return (self.read_u8(_RF95_REG_3F_IRQ_FLAGS_2) & 0x4) >> 2
551
+
552
+ def clear_interrupt(self) -> None:
553
+ """Clear interrupt Flags"""
554
+ self.write_u8(_RF95_REG_3E_IRQ_FLAGS_1, 0xFF)
555
+ self.write_u8(_RF95_REG_3F_IRQ_FLAGS_2, 0xFF)
556
+
557
+ def fill_fifo(self, payload: ReadableBuffer) -> None:
558
+ """Write the payload to the FIFO."""
559
+ complete_payload = bytearray(1) # prepend packet length to payload
560
+ complete_payload[0] = len(payload)
561
+ # put the payload lengthe in the beginning of the packet for RFM69
562
+ complete_payload = complete_payload + payload
563
+ # Write payload to transmit fifo
564
+ self.write_from(_RF95_REG_00_FIFO, complete_payload)
565
+
566
+ def read_fifo(self) -> Optional[bytearray]:
567
+ """Read the data from the FIFO."""
568
+ # Read the length of the FIFO.
569
+ fifo_length = self.read_u8(_RF95_REG_00_FIFO)
570
+ packet = None # return None if FIFO empty
571
+ if fifo_length > 0: # read and clear the FIFO if anything in it
572
+ packet = bytearray(fifo_length)
573
+ # read the packet
574
+ self.read_into(_RF95_REG_00_FIFO, packet, fifo_length)
575
+ return packet