forked from nohcpy/MicroPython_RFM69
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfm69.py
812 lines (743 loc) · 32.7 KB
/
rfm69.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
# The MIT License (MIT)
#
# Copyright (c) 2017 Tony DiCola for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Based on work of nohcpy (https://github.com/nohcpy) who ported the original Adafruit lib to micropython
from micropython import const
import time
import random
from machine import Pin
# Internal constants:
_REG_FIFO = const(0x00)
_REG_OP_MODE = const(0x01)
_REG_DATA_MOD = const(0x02)
_REG_BITRATE_MSB = const(0x03)
_REG_BITRATE_LSB = const(0x04)
_REG_FDEV_MSB = const(0x05)
_REG_FDEV_LSB = const(0x06)
_REG_FRF_MSB = const(0x07)
_REG_FRF_MID = const(0x08)
_REG_FRF_LSB = const(0x09)
_REG_VERSION = const(0x10)
_REG_PA_LEVEL = const(0x11)
_REG_RX_BW = const(0x19)
_REG_AFC_BW = const(0x1A)
_REG_RSSI_VALUE = const(0x24)
_REG_DIO_MAPPING1 = const(0x25)
_REG_IRQ_FLAGS1 = const(0x27)
_REG_IRQ_FLAGS2 = const(0x28)
_REG_PREAMBLE_MSB = const(0x2C)
_REG_PREAMBLE_LSB = const(0x2D)
_REG_SYNC_CONFIG = const(0x2E)
_REG_SYNC_VALUE1 = const(0x2F)
_REG_PACKET_CONFIG1 = const(0x37)
_REG_FIFO_THRESH = const(0x3C)
_REG_PACKET_CONFIG2 = const(0x3D)
_REG_AES_KEY1 = const(0x3E)
_REG_TEMP1 = const(0x4E)
_REG_TEMP2 = const(0x4F)
_REG_TEST_PA1 = const(0x5A)
_REG_TEST_PA2 = const(0x5C)
_REG_TEST_DAGC = const(0x6F)
_TEST_PA1_NORMAL = const(0x55)
_TEST_PA1_BOOST = const(0x5D)
_TEST_PA2_NORMAL = const(0x70)
_TEST_PA2_BOOST = const(0x7C)
# The crystal oscillator frequency and frequency synthesizer step size.
# See the datasheet for details of this calculation.
_FXOSC = 32000000.0
_FSTEP = _FXOSC / 524288
# RadioHead specific compatibility constants.
_RH_BROADCAST_ADDRESS = const(0xFF)
# The acknowledgement bit in the FLAGS
# The top 4 bits of the flags are reserved for RadioHead. The lower 4 bits are reserved
# for application layer use.
_RH_FLAGS_ACK = const(0x80)
_RH_FLAGS_RETRY = const(0x40)
# User facing constants:
SLEEP_MODE = const(0b000)
STANDBY_MODE = const(0b001)
FS_MODE = const(0b010)
TX_MODE = const(0b011)
RX_MODE = const(0b100)
DEFAULT_TX_POWER = const(13)
DEFAULT_HIGH_POWER = const(False)
class RFM69:
class _RegisterBits:
def __init__(self, address, *, offset=0, bits=1):
assert 0 <= offset <= 7
assert 1 <= bits <= 8
assert (offset + bits) <= 8
self._address = address
self._mask = 0
for _ in range(bits):
self._mask <<= 1
self._mask |= 1
self._mask <<= offset
self._offset = offset
def __get__(self, obj, objtype):
"""
:type obj: RFM69
"""
reg_value = obj._read_u8(self._address)
return (reg_value & self._mask) >> self._offset
def __set__(self, obj, val):
"""
:type obj: RFM69
"""
reg_value = obj._read_u8(self._address)
reg_value &= ~self._mask
reg_value |= (val & 0xFF) << self._offset
obj._write_u8(self._address, reg_value)
# Control bits from the registers of the chip:
data_mode = _RegisterBits(_REG_DATA_MOD, offset=5, bits=2)
modulation_type = _RegisterBits(_REG_DATA_MOD, offset=3, bits=2)
modulation_shaping = _RegisterBits(_REG_DATA_MOD, offset=0, bits=2)
temp_start = _RegisterBits(_REG_TEMP1, offset=3)
temp_running = _RegisterBits(_REG_TEMP1, offset=2)
sync_on = _RegisterBits(_REG_SYNC_CONFIG, offset=7)
sync_size = _RegisterBits(_REG_SYNC_CONFIG, offset=3, bits=3)
aes_on = _RegisterBits(_REG_PACKET_CONFIG2, offset=0)
pa_0_on = _RegisterBits(_REG_PA_LEVEL, offset=7)
pa_1_on = _RegisterBits(_REG_PA_LEVEL, offset=6)
pa_2_on = _RegisterBits(_REG_PA_LEVEL, offset=5)
output_power = _RegisterBits(_REG_PA_LEVEL, offset=0, bits=5)
rx_bw_dcc_freq = _RegisterBits(_REG_RX_BW, offset=5, bits=3)
rx_bw_mantissa = _RegisterBits(_REG_RX_BW, offset=3, bits=2)
rx_bw_exponent = _RegisterBits(_REG_RX_BW, offset=0, bits=3)
afc_bw_dcc_freq = _RegisterBits(_REG_AFC_BW, offset=5, bits=3)
afc_bw_mantissa = _RegisterBits(_REG_AFC_BW, offset=3, bits=2)
afc_bw_exponent = _RegisterBits(_REG_AFC_BW, offset=0, bits=3)
packet_format = _RegisterBits(_REG_PACKET_CONFIG1, offset=7, bits=1)
dc_free = _RegisterBits(_REG_PACKET_CONFIG1, offset=5, bits=2)
crc_on = _RegisterBits(_REG_PACKET_CONFIG1, offset=4, bits=1)
crc_auto_clear_off = _RegisterBits(_REG_PACKET_CONFIG1, offset=3, bits=1)
address_filter = _RegisterBits(_REG_PACKET_CONFIG1, offset=1, bits=2)
mode_ready = _RegisterBits(_REG_IRQ_FLAGS1, offset=7)
rx_ready = _RegisterBits(_REG_IRQ_FLAGS1, offset=6)
tx_ready = _RegisterBits(_REG_IRQ_FLAGS1, offset=5)
dio_0_mapping = _RegisterBits(_REG_DIO_MAPPING1, offset=6, bits=2)
packet_sent = _RegisterBits(_REG_IRQ_FLAGS2, offset=3)
payload_ready = _RegisterBits(_REG_IRQ_FLAGS2, offset=2)
def __init__(
self,
spi,
cs: Pin,
reset: Pin,
frequency,
*,
sync_word=b"\x2D\xD4",
preamble_length=4,
encryption_key=None,
high_power=None,
):
self._spi = spi
self._tx_power = DEFAULT_TX_POWER
if high_power is None:
high_power = DEFAULT_HIGH_POWER
self.high_power = high_power
self._cs = cs
self._reset = reset
if self._reset is not None:
self._reset.init(mode=Pin.OUT, value=0)
self.reset() # Reset the chip.
# Check the version of the chip.
version = self._read_u8(_REG_VERSION)
if version != 0x24:
raise RuntimeError("Failed to find RFM69 with expected version, check wiring!")
self.idle() # Enter idle state
# Set up the chip in a similar way to the RadioHead RFM69 library
# Set FIFO TX condition to not empty and the default FIFO threshold to 15
self._write_u8(_REG_FIFO_THRESH, 0b10001111)
# Configure low beta off.
self._write_u8(_REG_TEST_DAGC, 0x30)
# Disable boost.
self._write_u8(_REG_TEST_PA1, _TEST_PA1_NORMAL)
self._write_u8(_REG_TEST_PA2, _TEST_PA2_NORMAL)
# Set the synchronization word.
self.sync_word = sync_word
self.preamble_length = preamble_length # Set the preamble length
self.frequency_mhz = frequency # Set frequency.
self.encryption_key = encryption_key # Set encryption key.
# set radio configuration parameters
self._configure_radio()
# initialize last RSSI reading
self.last_rssi = 0.0
"""The RSSI of the last received packet. Stored when the packet was received.
This instantaneous RSSI value may not be accurate once the
operating mode has been changed.
"""
# initialize timeouts and delays
self.ack_wait = 500
"""The delay time before attempting a retry after not receiving an ACK"""
self.receive_timeout = 500
"""The amount of time to poll for a received packet.
If no packet is received, the returned packet will be None
"""
self.xmit_timeout = 2000
"""The amount of time to wait for the HW to transmit the packet.
This is mainly used to prevent a hang due to a HW issue
"""
self.ack_retries = 5
"""The number of ACK retries before reporting a failure."""
self.ack_delay = None
"""The delay time before attempting to send an ACK.
If ACKs are being missed try setting this to .1 or .2.
"""
# initialize sequence number counter for reliabe datagram mode
self.sequence_number = 0
# create seen Ids list
self.seen_ids = bytearray(256)
# initialize packet header
# node address - default is broadcast
self.node = _RH_BROADCAST_ADDRESS
"""The default address of this Node. (0-255).
If not 255 (0xff) then only packets address to this node will be accepted.
First byte of the RadioHead header.
"""
# destination address - default is broadcast
self.destination = _RH_BROADCAST_ADDRESS
"""The default destination address for packet transmissions. (0-255).
If 255 (0xff) then any receiving node should accept the packet.
Second byte of the RadioHead header.
"""
# ID - contains seq count for reliable datagram mode
self.identifier = 0
"""Automatically set to the sequence number when send_with_ack() used.
Third byte of the RadioHead header.
"""
# flags - identifies ack/retry packet for reliable datagram mode
self.flags = 0
"""Upper 4 bits reserved for use by Reliable Datagram Mode.
Lower 4 bits may be used to pass information.
Fourth byte of the RadioHead header.
"""
def _configure_radio(self):
# Configure modulation for RadioHead library GFSK_Rb250Fd250 mode by default.
# Users with advanced knowledge can manually reconfigure for any other mode
# (consulting the datasheet is absolutely necessary!)
self.data_mode = 0b00 # Packet mode
self.modulation_type = 0b00 # FSK modulation
self.modulation_shaping = 0b01 # Gaussian filter, BT=1.0
self.bitrate = 250000 # 250kbs
self.frequency_deviation = 250000 # 250khz
self.rx_bw_dcc_freq = 0b111 # RxBw register = 0xE0
self.rx_bw_mantissa = 0b00
self.rx_bw_exponent = 0b000
self.afc_bw_dcc_freq = 0b111 # AfcBw register = 0xE0
self.afc_bw_mantissa = 0b00
self.afc_bw_exponent = 0b000
self.packet_format = 1 # Variable length
self.dc_free = 0b10 # Whitening
self.crc_on = 1 # CRC enabled
self.crc_auto_clear = 0 # Clear FIFO on CRC fail
self.address_filtering = 0b00 # No address filtering
# Set transmit power to 13 dBm, a safe value any module supports
self.tx_power = 13
# pylint: disable=no-member
# Reconsider this disable when it can be tested.
# read_into(_REG_FIFO, packet)
def _read_into(self, address, buf):
# Read from the specified address into the provided buffer
self._cs.value(0)
self._spi.write(bytes([address & 0x7F]))
self._spi.readinto(buf)
self._cs.value(1)
return buf
def _read_u8(self, address):
# Read a single byte from the provided address and return it.
self._cs.value(0)
self._spi.write(bytes([address & 0x7F]))
value = self._spi.read(1)
self._cs.value(1)
return value[0]
def _write_from(self, address, buf):
# Write to the provided address and taken from the provided buffer
self._cs.value(0)
self._spi.write(bytes([(address | 0x80) & 0xFF]))
self._spi.write(buf)
self._cs.value(1)
def _write_fifo_from(self, buf):
# Write to the transmit FIFO and taken from the provided buffer
length = len(buf)
buf1 = (_REG_FIFO | 0x80) & 0xFF # Set top bit to 1 to indicate a write
buf2 = length & 0xFF # Set packet length
self._cs.value(0)
self._spi.write(bytes([buf1, buf2])) # send address and length
self._spi.write(buf)
self._cs.value(1)
def _write_u8(self, address, val):
# Write a byte register to the chip. Specify the 7-bit address and the
# 8-bit value to write to that address.
address = (address | 0x80) & 0xFF # Set top bit to 1 to indicate a write
val = val & 0xFF
self._cs.value(0)
self._spi.write(bytes([address, val]))
self._cs.value(1)
def reset(self):
"""Perform a reset of the chip."""
if self._reset is not None:
# See section 7.2.2 of the datasheet for reset description.
self._reset.value(1) # Set Reset High
time.sleep(0.0001) # 100 us
self._reset.value(0) # set Reset Low
time.sleep(0.005) # 5 ms
def idle(self):
"""Enter idle standby mode (switching off high power amplifiers if necessary)."""
# Like RadioHead library, turn off high power boost if enabled.
if self._tx_power >= 18:
self._write_u8(_REG_TEST_PA1, _TEST_PA1_NORMAL)
self._write_u8(_REG_TEST_PA2, _TEST_PA2_NORMAL)
self.operation_mode = STANDBY_MODE
def sleep(self):
"""Enter sleep mode."""
self.operation_mode = SLEEP_MODE
def listen(self):
"""Listen for packets to be received by the chip. Use :py:func:`receive` to listen, wait
and retrieve packets as they're available.
"""
# Like RadioHead library, turn off high power boost if enabled.
if self._tx_power >= 18:
self._write_u8(_REG_TEST_PA1, _TEST_PA1_NORMAL)
self._write_u8(_REG_TEST_PA2, _TEST_PA2_NORMAL)
# Enable payload ready interrupt for D0 line.
self.dio_0_mapping = 0b01
# Enter RX mode (will clear FIFO!).
self.operation_mode = RX_MODE
def transmit(self):
"""Transmit a packet which is queued in the FIFO. This is a low level function for
entering transmit mode and more. For generating and transmitting a packet of data use
:py:func:`send` instead.
"""
# Like RadioHead library, turn on high power boost if enabled.
if self._tx_power >= 18:
self._write_u8(_REG_TEST_PA1, _TEST_PA1_BOOST)
self._write_u8(_REG_TEST_PA2, _TEST_PA2_BOOST)
# Enable packet sent interrupt for D0 line.
self.dio_0_mapping = 0b00
# Enter TX mode (will clear FIFO!).
self.operation_mode = TX_MODE
@property
def temperature(self):
"""The internal temperature of the chip in degrees Celsius. Be warned this is not
calibrated or very accurate.
... warning:: Reading this will STOP any receiving/sending that might be happening!
"""
# Start a measurement then poll the measurement finished bit.
self.temp_start = 1
while self.temp_running > 0:
pass
# Grab the temperature value and convert it to Celsius.
# This uses the same observed value formula from the RadioHead library.
temp = self._read_u8(_REG_TEMP2)
return 166.0 - temp
@property
def operation_mode(self):
"""The operation mode value. Unless you're manually controlling the chip you shouldn't
change the operation_mode with this property as other side effects are required for
changing logical modes--use :py:func:`idle`, :py:func:`sleep`, :py:func:`transmit`,
:py:func:`listen` instead to signal intent for explicit logical modes.
"""
op_mode = self._read_u8(_REG_OP_MODE)
return (op_mode >> 2) & 0b111
@operation_mode.setter
def operation_mode(self, val):
assert 0 <= val <= 4
# Set the mode bits inside the operation mode register
op_mode = self._read_u8(_REG_OP_MODE)
op_mode &= 0b11100011
op_mode |= val << 2
self._write_u8(_REG_OP_MODE, op_mode)
# Wait for mode to change by polling interrupt bit
while not self.mode_ready:
pass
@property
def sync_word(self):
"""The synchronization word value. This is a byte string up to 8 bytes long (64 bits)
which indicates the synchronization word for transmitted and received packets. Any
received packet which does not include this sync word will be ignored. The default value
is 0x2D, 0xD4 which matches the RadioHead RFM69 library. Setting a value of None will
disable synchronization word matching entirely.
"""
# Handle when sync word is disabled..
if not self.sync_on:
return None
# Sync word is not disabled so read the current value.
sync_word_length = self.sync_size + 1 # Sync word size is offset by 1
# according to datasheet.
sync_word = bytearray(sync_word_length)
self._read_into(_REG_SYNC_VALUE1, sync_word)
return sync_word
@sync_word.setter
def sync_word(self, val):
# Handle disabling sync word when None value is set.
if val is None:
self.sync_on = 0
else:
# Check sync word is at most 8 bytes.
assert 1 <= len(val) <= 8
# Update the value, size and turn on the sync word.
self._write_from(_REG_SYNC_VALUE1, val)
self.sync_size = len(val) - 1 # Again sync word size is offset by
# 1 according to datasheet.
self.sync_on = 1
@property
def preamble_length(self):
"""The length of the preamble for sent and received packets, an unsigned 16-bit value.
Received packets must match this length, or they are ignored! Set to 4 to match the
RadioHead RFM69 library.
"""
msb = self._read_u8(_REG_PREAMBLE_MSB)
lsb = self._read_u8(_REG_PREAMBLE_LSB)
return ((msb << 8) | lsb) & 0xFFFF
@preamble_length.setter
def preamble_length(self, val):
assert 0 <= val <= 65535
self._write_u8(_REG_PREAMBLE_MSB, (val >> 8) & 0xFF)
self._write_u8(_REG_PREAMBLE_LSB, val & 0xFF)
@property
def frequency_mhz(self):
"""The frequency of the radio in Megahertz. Only the allowed values for your radio must be
specified (i.e. 433 vs. 915 mhz)!
"""
# FRF register is computed from the frequency following the datasheet.
# See section 6.2 and FRF register description.
# Read bytes of FRF register and assemble into a 24-bit unsigned value.
msb = self._read_u8(_REG_FRF_MSB)
mid = self._read_u8(_REG_FRF_MID)
lsb = self._read_u8(_REG_FRF_LSB)
frf = ((msb << 16) | (mid << 8) | lsb) & 0xFFFFFF
frequency = (frf * _FSTEP) / 1000000.0
return frequency
@frequency_mhz.setter
def frequency_mhz(self, val):
assert 290 <= val <= 1020
# Calculate FRF register 24-bit value using section 6.2 of the datasheet
frf = int((val * 1000000.0) / _FSTEP) & 0xFFFFFF
# Extract byte values and update registers
msb = frf >> 16
mid = (frf >> 8) & 0xFF
lsb = frf & 0xFF
self._write_u8(_REG_FRF_MSB, msb)
self._write_u8(_REG_FRF_MID, mid)
self._write_u8(_REG_FRF_LSB, lsb)
@property
def encryption_key(self):
"""The AES encryption key used to encrypt and decrypt packets by the chip. This can be set
to None to disable encryption (the default), otherwise it must be a 16 byte long byte
string which defines the key (both the transmitter and receiver must use the same key
value).
"""
# Handle if encryption is disabled.
if self.aes_on == 0:
return None
# Encryption is enabled so read the key and return it.
key = bytearray(16)
self._read_into(_REG_AES_KEY1, key)
return key
@encryption_key.setter
def encryption_key(self, val):
# Handle if unsetting the encryption key (None value).
if val is None:
self.aes_on = 0
else:
# Set the encryption key and enable encryption.
assert len(val) == 16
self._write_from(_REG_AES_KEY1, val)
self.aes_on = 1
@property
def tx_power(self):
"""The transmit power in dBm. Can be set to a value from -2 to 20 for high power devices
(RFM69HCW, high_power=True) or -18 to 13 for low power devices. Only integer power
levels are actually set (i.e. 12.5 will result in a value of 12 dBm).
"""
# Follow table 10 truth table from the datasheet for determining power
# level from the individual PA level bits and output power register.
pa0 = self.pa_0_on
pa1 = self.pa_1_on
pa2 = self.pa_2_on
if pa0 and not pa1 and not pa2:
# -18 to 13 dBm range
return -18 + self.output_power
if not pa0 and pa1 and not pa2:
# -2 to 13 dBm range
return -18 + self.output_power
if not pa0 and pa1 and pa2 and not self.high_power:
# 2 to 17 dBm range
return -14 + self.output_power
if not pa0 and pa1 and pa2 and self.high_power:
# 5 to 20 dBm range
return -11 + self.output_power
raise RuntimeError("Power amplifiers in unknown state!")
@tx_power.setter
def tx_power(self, val):
val = int(val)
# Determine power amplifier and output power values depending on
# high power state and requested power.
pa_0_on = 0
pa_1_on = 0
pa_2_on = 0
output_power = 0
if self.high_power:
# Handle high power mode.
assert -2 <= val <= 20
if val <= 13:
pa_1_on = 1
output_power = val + 18
elif 13 < val <= 17:
pa_1_on = 1
pa_2_on = 1
output_power = val + 14
else: # power >= 18 dBm
# Note this also needs PA boost enabled separately!
pa_1_on = 1
pa_2_on = 1
output_power = val + 11
else:
# Handle non-high power mode.
assert -18 <= val <= 13
# Enable only power amplifier 0 and set output power.
pa_0_on = 1
output_power = val + 18
# Set power amplifiers and output power as computed above.
self.pa_0_on = pa_0_on
self.pa_1_on = pa_1_on
self.pa_2_on = pa_2_on
self.output_power = output_power
self._tx_power = val
@property
def rssi(self):
"""The received strength indicator (in dBm).
May be inaccurate if not read immediate. last_rssi contains the value read immediately
receipt of the last packet.
"""
# Read RSSI register and convert to value using formula in datasheet.
return -self._read_u8(_REG_RSSI_VALUE) / 2.0
@property
def bitrate(self):
"""The modulation bitrate in bits/second (or chip rate if Manchester encoding is enabled).
Can be a value from ~489 to 32 Mbs, but see the datasheet for the exact supported
values.
"""
msb = self._read_u8(_REG_BITRATE_MSB)
lsb = self._read_u8(_REG_BITRATE_LSB)
return _FXOSC / ((msb << 8) | lsb)
@bitrate.setter
def bitrate(self, val):
assert (_FXOSC / 65535) <= val <= 32000000.0
# Round up to the next closest bit-rate value with addition of 0.5.
bitrate = int((_FXOSC / val) + 0.5) & 0xFFFF
self._write_u8(_REG_BITRATE_MSB, bitrate >> 8)
self._write_u8(_REG_BITRATE_LSB, bitrate & 0xFF)
@property
def frequency_deviation(self):
"""The frequency deviation in Hertz."""
msb = self._read_u8(_REG_FDEV_MSB)
lsb = self._read_u8(_REG_FDEV_LSB)
return _FSTEP * ((msb << 8) | lsb)
@frequency_deviation.setter
def frequency_deviation(self, val):
assert 0 <= val <= (_FSTEP * 16383) # fdev is a 14-bit unsigned value
# Round up to the next closest integer value with addition of 0.5.
fdev = int((val / _FSTEP) + 0.5) & 0x3FFF
self._write_u8(_REG_FDEV_MSB, fdev >> 8)
self._write_u8(_REG_FDEV_LSB, fdev & 0xFF)
def send(
self,
data,
*,
keep_listening=False,
destination=None,
node=None,
identifier=None,
flags=None
):
"""Send a string of data using the transmitter.
You can only send 60 bytes at a time
(limited by chip's FIFO size and appended headers).
This appends a 4 byte header to be compatible with the RadioHead library.
The header defaults to using the initialized attributes:
(destination,node,identifier,flags)
It may be temporarily overidden via the kwargs - destination,node,identifier,flags.
Values passed via kwargs do not alter the attribute settings.
The keep_listening argument should be set to True if you want to start listening
automatically after the packet is sent. The default setting is False.
Returns: True if success or False if the send timed out.
"""
# Disable pylint warning to not use length as a check for zero.
# This is a puzzling warning as the below code is clearly the most
# efficient and proper way to ensure a precondition that the provided
# buffer be within an expected range of bounds. Disable this check.
# pylint: disable=len-as-condition
assert 0 < len(data) <= 60
# pylint: enable=len-as-condition
self.idle() # Stop receiving to clear FIFO and keep it clear.
# Fill the FIFO with a packet to send.
# Combine header and data to form payload
payload = bytearray(4)
if destination is None: # use attribute
payload[0] = self.destination
else: # use kwarg
payload[0] = destination
if node is None: # use attribute
payload[1] = self.node
else: # use kwarg
payload[1] = node
if identifier is None: # use attribute
payload[2] = self.identifier
else: # use kwarg
payload[2] = identifier
if flags is None: # use attribute
payload[3] = self.flags
else: # use kwarg
payload[3] = flags
payload = payload + data
# Write payload to transmit fifo
self._write_fifo_from(payload)
# Turn on transmit mode to send out the packet
self.transmit()
# Wait for packet sent interrupt with explicit polling
# (not ideal but best that can be done right now without interrupts)
start = time.ticks_ms()
timed_out = False
while not timed_out and not self.packet_sent:
if time.ticks_diff(time.ticks_ms(), start) >= self.xmit_timeout:
timed_out = True
# Listen again if requested.
if keep_listening:
self.listen()
else: # Enter idle mode to stop receiving other packets.
self.idle()
return not timed_out
def send_with_ack(self, data):
"""Reliable Datagram mode:
Send a packet with data and wait for an ACK response.
The packet header is automatically generated.
If enabled, the packet transmission will be retried on failure
"""
if self.ack_retries:
retries_remaining = self.ack_retries
else:
retries_remaining = 1
got_ack = False
self.sequence_number = (self.sequence_number + 1) & 0xFF
while not got_ack and retries_remaining:
self.identifier = self.sequence_number
self.send(data, keep_listening=True)
# Don't look for ACK from Broadcast message
if self.destination == _RH_BROADCAST_ADDRESS:
got_ack = True
else:
# wait for a packet from our destination
ack_packet = self.receive(timeout=self.ack_wait, with_header=True)
if ack_packet is not None:
if ack_packet[3] & _RH_FLAGS_ACK:
# check the ID
if ack_packet[2] == self.identifier:
got_ack = True
break
# pause before next retry -- random delay
if not got_ack:
# delay by random amount before next try
time.sleep_ms(self.ack_wait + int(self.ack_wait * random.random()))
retries_remaining = retries_remaining - 1
# set retry flag in packet header
self.flags |= _RH_FLAGS_RETRY
self.flags = 0 # clear flags
return got_ack
# pylint: disable=too-many-branches
def receive(
self, *, keep_listening=True, with_ack=False, timeout=None, with_header=False
):
"""Wait to receive a packet from the receiver. If a packet is found the payload bytes
are returned, otherwise None is returned (which indicates the timeout elapsed with no
reception).
If keep_listening is True (the default) the chip will immediately enter listening mode
after reception of a packet, otherwise it will fall back to idle mode and ignore any
future reception.
All packets must have a 4 byte header for compatibility with the RadioHead library.
The header consists of 4 bytes (To,From,ID,Flags). The default setting will strip
the header before returning the packet to the caller.
If with_header is True then the 4 byte header will be returned with the packet.
The payload then begins at packet[4].
If with_ack is True, send an ACK after receipt (Reliable Datagram mode)
"""
timed_out = False
if timeout is None:
timeout = self.receive_timeout
if timeout is not None:
# Wait for the payload_ready signal. This is not ideal and will
# surely miss or overflow the FIFO when packets aren't read fast
# enough, however it's the best that can be done from Python without
# interrupt supports.
# Make sure we are listening for packets.
self.listen()
start = time.ticks_ms()
timed_out = False
while not timed_out and not self.payload_ready:
if time.ticks_diff(time.ticks_ms(), start) >= timeout:
timed_out = True
# Payload ready is set, a packet is in the FIFO.
packet = None
# save last RSSI reading
self.last_rssi = self.rssi
# Enter idle mode to stop receiving other packets.
self.idle()
if not timed_out:
# Read the length of the FIFO.
fifo_length = self._read_u8(_REG_FIFO)
# Handle if the received packet is too small to include the 4 byte
# RadioHead header and at least one byte of data --reject this packet and ignore it.
if fifo_length > 0: # read and clear the FIFO if anything in it
packet = bytearray(fifo_length)
self._read_into(_REG_FIFO, packet)
if fifo_length < 5:
packet = None
else:
if self.node != _RH_BROADCAST_ADDRESS and packet[0] != _RH_BROADCAST_ADDRESS and packet[0] != self.node:
packet = None
# send ACK unless this was an ACK or a broadcast
elif with_ack and ((packet[3] & _RH_FLAGS_ACK) == 0) and (packet[0] != _RH_BROADCAST_ADDRESS):
# delay before sending Ack to give receiver a chance to get ready
if self.ack_delay is not None:
time.sleep_ms(self.ack_delay)
# send ACK packet to sender
data = bytes("!", "UTF-8")
self.send(
data,
destination=packet[1],
node=packet[0],
identifier=packet[2],
flags=(packet[3] | _RH_FLAGS_ACK),
)
# reject Retries if we have seen this idetifier from this source before
if self.seen_ids[packet[1]] == packet[2] and packet[3] & _RH_FLAGS_RETRY:
packet = None
else: # save the packet identifier for this source
self.seen_ids[packet[1]] = packet[2]
if not with_header and packet is not None: # skip the header if not wanted
packet = packet[4:]
# Listen again if necessary and return the result packet.
if keep_listening:
self.listen()
else:
# Enter idle mode to stop receiving other packets.
self.idle()
return packet