-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpyRadioHeadRF95.py
executable file
·169 lines (132 loc) · 4.16 KB
/
pyRadioHeadRF95.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
#!/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 RF95:
# Bandwidth values
Bandwidth7K8HZ = 7800
Bandwidth10K4HZ = 10400
Bandwidth15K6HZ = 15600
Bandwidth20K8HZ = 20800
Bandwidth31K25HZ = 31250
Bandwidth41K7HZ = 41700
Bandwidth62K5HZ = 62500
Bandwidth125KHZ = 125000
Bandwidth250KHZ = 250000
Bandwidth500KHZ = 500000
# Spreading factor values
SpreadingFactor6 = 6
SpreadingFactor7 = 7
SpreadingFactor8 = 8
SpreadingFactor9 = 9
SpreadingFactor10 = 10
SpreadingFactor11 = 11
SpreadingFactor12 = 12
# Coding rate denominator values
CodingRate4_5 = 5
CodingRate4_6 = 6
CodingRate4_7 = 7
CodingRate4_8 = 8
def __init__(self):
ffi.cdef("int init();\
void setTxPower(int8_t power, bool useRFO);\
bool setFrequency(float centre);\
void setSpreadingFactor(int8_t sf);\
void setSignalBandwidth(long sbw);\
void setCodingRate4(int8_t denominator);\
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 printRegisters();\
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
path_string = os.path.dirname(__file__) + "/libradiohead.so"
radiohead = ffi.dlopen(path_string)
def init(self):
r = radiohead.init()
if r != 0:
raise RuntimeError("RF95 init failed - value: " + str(r))
def setTxPower(self, power, useRFO):
radiohead.setTxPower(power, useRFO)
def setFrequency(self, centre):
r = radiohead.setFrequency(centre)
return r
def setSpreadingFactor(self, sf):
radiohead.setSpreadingFactor(sf)
def setSignalBandwidth(self, sbw):
radiohead.setSignalBandwidth(sbw)
def setCodingRate4(seld, denominator):
radiohead.setCodingRate4(denominator)
def managerInit(self, address):
radiohead.managerInit(address)
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 printRegisters(self):
radiohead.printRegisters()
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()