-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpyRadioHeadNRF24.py
executable file
·157 lines (121 loc) · 3.46 KB
/
pyRadioHeadNRF24.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
#!/usr/bin/python
from cffi import FFI
import os
ffi = FFI()
buf = ffi.new("char*")
l = ffi.new("uint8_t*")
l[0] = 0
src = ffi.new("uint8_t*")
src[0] = 0
class nRF24:
# Data Rate Values
DataRate1Mbps = 1
DataRate2Mbps = 2
# Transmit Power Values
TransmitPower0dBm = 0
TransmitPowerm6dBm = 6
TransmitPowerm12dBm = 12
TransmitPowerm18dBm = 18
def __init__(self):
ffi.cdef("int init();\
int setChannel(int c);\
int setRF(int dr, int tp);\
int send(uint8_t* data, uint8_t len);\
int waitPacketSent();\
int waitAvailableTimeout(int ms);\
int available();\
int recv(char* buf, uint8_t* len);\
int maxMessageLength();\
int isSending();\
int printRegisters();\
int setNetworkAddress(uint8_t* address, uint8_t len);\
int enterSleepMode();\
int setModeIdle();\
int setModeTx();\
int setModeRx();\
\
int managerInit(int address);\
int sendtoWait(uint8_t* data, uint8_t len, uint8_t dst);\
int recvfromAck(char* buf, uint8_t* len, uint8_t* form);\
int recvfromAckTimeout(char* buf, uint8_t* len, uint16_t timeout, uint8_t* form);\
int setTimeout(uint16_t timeout);\
int retries();\
int setRetries(uint8_t retries);\
int retransmissions();\
int resetRetransmissions();")
global radiohead
#radiohead = ffi.dlopen("./libradiohead.so")
path_string = os.path.dirname(__file__) + "/libradiohead.so"
radiohead = ffi.dlopen(path_string)
def init(self):
r = radiohead.init()
if r != 0:
raise RuntimeError("nRF24 init failed")
def managerInit(self, address):
radiohead.managerInit(address)
def setChannel(self, channel):
r = radiohead.setChannel(channel)
if r != 0:
raise RuntimeError("nRF24 setChannel failed")
def setRF(self, datarate, transmitpower):
r = radiohead.setRF(datarate, transmitpower)
if r != 0:
raise RuntimeError("nRF24 setRF failed")
def send(self, data, l):
r = radiohead.send(data, l)
if r != 0:
raise RuntimeError("nRF24 send failed")
def waitPacketSent(self):
radiohead.waitPacketSent()
def waitAvailableTimeout(self):
radiohead.waitAvailableTimeout()
def available(self):
b = radiohead.available()
if (b == 1):
return True
else:
return False
def recv(self):
radiohead.recv(buf, l)
return (ffi.string(buf), l[0])
def maxMessageLength(self):
return radiohead.maxMessageLength()
def isSending(self):
b = radiohead.isSending()
if b == 1:
return True
else:
return False
def printRegisters(self):
radiohead.printRegisters()
def setNetworkAddress(self, address, l):
radiohead.setNetworkAddress(address, l)
def sleep(self):
radiohead.enterSleepMode()
def recvfromAck(self):
radiohead.recvfromAck(buf, l, src)
return (ffi.string(buf), l[0], src[0])
def recvfromAckTimeout(self, timeout):
ris = radiohead.recvfromAck(buf, l, timeout, src)
if ris > 0:
return (ffi.string(buf), l[0], src[0])
else:
return ("", -1, -1)
def sendtoWait(self, data, l, dst):
return radiohead.sendtoWait(data, l, dst)
def retries(self):
return radiohead.retries()
def setRetries(self, retries):
radiohead.setRetries(retries)
def retransmissions(self):
return radiohead.retransmissions()
def resetRetransmissions(self):
radiohead.resetRetransmissions()
def setTimeout(self, timeout):
radiohead.setTimeout(timeout)
def setModeIdle(self):
radiohead.setModeIdle()
def setModeTx(self):
radiohead.setModeTx()
def setModeRx(self):
radiohead.setModeRx()