-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmain.py
More file actions
189 lines (170 loc) · 6.34 KB
/
main.py
File metadata and controls
189 lines (170 loc) · 6.34 KB
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
"""A simple script to take all data dumped from the nRF24L01 registers and
output it in human readable form.
Notes:
* The radio's power state is represented under the assumption that
the radio's CE pin is inactive low.
"""
# pylint: disable=consider-using-f-string
import struct
import argparse
import time
from arduino.app_utils import *
bufferByteArray = bytearray()
def RF24Callback(payload):
bufferByteArray.append(payload)
if(len(bufferByteArray)) == 43:
print_details(bufferByteArray)
Bridge.provide("RF24Callback", RF24Callback)
def address_repr(buf, reverse: bool = True, delimit: str = "") -> str:
"""Convert a buffer into a hexadecimal string."""
order = range(len(buf) - 1, -1, -1) if reverse else range(len(buf))
return delimit.join(["%02X" % buf[byte] for byte in order])
# pylint: disable=too-many-locals,too-many-statements
def print_details(encoded_buf: bytearray):
"""This debugging function outputs all details about the nRF24L01."""
# declare sequences
pipes = [bytearray(5)] * 2 + [0] * 4
pl_len = [0] * 6
# unpack bytearray
(
config, # 0x00
auto_ack, # 0x01
open_pipes, # 0x02
addr_len, # 0x03
retry_setup, # 0x04
channel, # 0x05
rf_setup, # 0x06
status, # 0x07
observer, # 0x08
rpd, # 0x09
) = struct.unpack("10B", encoded_buf[:10])
pipes[0] = encoded_buf[10:15] # 0x0A
pipes[1] = encoded_buf[15:20] # 0x0B
(
pipes[2], # 0x0C
pipes[3], # 0x0D
pipes[4], # 0x0E
pipes[5], # 0x0F
) = struct.unpack("4B", encoded_buf[20:24])
tx_address = encoded_buf[24:29] # 0x10
(
pl_len[0], # 0x11
pl_len[1], # 0x12
pl_len[2], # 0x13
pl_len[3], # 0x14
pl_len[4], # 0x15
pl_len[5], # 0x16
fifo, # 0x17
dyn_pl, # 0x1C
features, # 0x1D
) = struct.unpack("9B", encoded_buf[29:38])
ce_pin, csn_pin, spi_speed = struct.unpack(">2H1B", encoded_buf[38:44])
# do some deciphering arithmetic
addr_len += 2
crc = (2 if config & 4 else 1) if auto_ack else max(0, ((config & 0x0C) >> 2) - 1)
d_rate = rf_setup & 0x28
d_rate = (2 if d_rate == 8 else 250) if d_rate else 1
pa_level = (3 - ((rf_setup & 6) >> 1)) * -6
pa_level = (
"MIN"
if pa_level == -18
else ("LOW" if pa_level == -12 else ("HIGH" if pa_level == -6 else "MAX"))
)
dyn_p = (
("_Enabled" if dyn_pl else "Disabled")
if dyn_pl == 0x3F or not dyn_pl
else "0b" + "0" * (8 - len(bin(dyn_pl))) + bin(dyn_pl)[2:]
)
auto_ack = (
("Enabled" if auto_ack else "Disabled")
if auto_ack == 0x3F or not auto_ack
else "0b" + "0" * (8 - len(bin(auto_ack))) + bin(auto_ack)[2:]
)
pwr = "Standby" if config & 2 else "Off"
is_plus_variant = bool(spi_speed >> 4)
spi_speed = spi_speed & 0xF
# print it all out
print("CE pin____________________{}".format(ce_pin))
print("CSN pin___________________{}".format(csn_pin))
print("SPI speed_________________{} MHz".format(spi_speed))
print("Is a plus variant_________{}".format(is_plus_variant))
print(
"Channel___________________{}".format(channel),
"~ {} GHz".format((channel + 2400) / 1000),
)
print(
"RF Data Rate______________{}".format(d_rate),
"Mbps" if d_rate != 250 else "Kbps",
)
print("RF Power Amplifier________PA_{}".format(pa_level))
print(
"RF Low Noise Amplifier____{}abled".format(
"En" if bool(rf_setup & 1) else "Dis"
)
)
print("CRC Length________________{} bits".format(crc * 8))
print("Address length____________{} bytes".format(addr_len))
print("TX Payload lengths________{} bytes".format(pl_len[0]))
print(
"Auto retry delay__________{} microseconds".format(
((retry_setup & 0xF0) >> 4) * 250 + 250
)
)
print("Auto retry attempts_______{} maximum".format(retry_setup & 0x0F))
print("Re-use TX FIFO____________{}".format(bool(fifo & 64)))
print("Received Power Detected___{}".format(bool(rpd)))
print(
"Packets lost on current channel_____________________{}".format(observer >> 4)
)
print(
"Retry attempts made for last transmission___________{}".format(observer & 0xF)
)
print(
"IRQ on Data Ready__{}abled".format("Dis" if config & 64 else "_En"),
" Data Ready___________{}".format(bool(status & 0x40)),
)
print(
"IRQ on Data Sent___{}abled".format("Dis" if config & 32 else "_En"),
" Data Sent____________{}".format(bool(status & 0x20)),
)
print(
"IRQ on Data Fail___{}abled".format("Dis" if config & 16 else "_En"),
" Data Failed__________{}".format(bool(status & 0x10)),
)
print(
"TX FIFO full__________{}e".format("_Tru" if fifo & 0x20 else "Fals"),
" TX FIFO empty________{}".format(bool(fifo & 0x10)),
)
print(
"RX FIFO full__________{}e".format("_Tru" if fifo & 2 else "Fals"),
" RX FIFO empty________{}".format(bool(fifo & 1)),
)
print(
"Multicast__________{}ed Custom ACK Payload___{}abled".format(
"_Allow" if features & 1 else "Disabl",
"En" if features & 2 else "Dis",
),
)
print("Dynamic Payloads___{} Auto Acknowledgment__{}".format(dyn_p, auto_ack))
print(
"Primary Mode_____________{}X".format("R" if config & 1 else "T"),
" Power Mode___________{}".format(pwr),
)
print("TX address____________ 0x{}".format(address_repr(tx_address)))
for i in range(6):
is_open = open_pipes & (1 << i)
address = pipes[i] if i < 2 else bytes([pipes[i]]) + pipes[1][1:]
print(
"Pipe {} ({}) bound: 0x{}".format(
i, " open " if is_open else "closed", address_repr(address)
),
)
if is_open and not dyn_pl & (1 << i):
print("\t\texpecting {} byte static payloads".format(pl_len[i]))
# pylint: enable=too-many-locals,too-many-statements
def loop():
"""This function is called repeatedly by the App framework."""
# You can replace this with any code you want your App to run repeatedly.
time.sleep(10)
# See: https://docs.arduino.cc/software/app-lab/tutorials/getting-started/#app-run
App.run(user_loop=loop)