nRF24-ruby 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/nRF24-demo.rb +18 -8
- data/http/coffee/nRF24.coffee +6 -5
- data/lib/nRF24-ruby.rb +79 -32
- metadata +1 -1
data/examples/nRF24-demo.rb
CHANGED
@@ -19,11 +19,12 @@ if http
|
|
19
19
|
puts "\n"
|
20
20
|
end
|
21
21
|
|
22
|
+
bmac="B2:B2:B3"
|
23
|
+
NRF24::set_bmac bmac
|
24
|
+
r0=NRF24.new id: :eka, ce: 22,cs: 27, irq: 17, chan:3, ack: false, mac: "00:A7:A7"
|
25
|
+
r1=NRF24.new id: :toka, ce: 24,cs: 23, irq: 22, chan: 3, ack: false, mac: "00:A5:A5"
|
22
26
|
|
23
|
-
|
24
|
-
r1=NRF24.new id: :toka, ce: 24,cs: 23, irq: 22, chan: 3, ack: true
|
25
|
-
|
26
|
-
puts "Main Loop Starts:"
|
27
|
+
puts "Main Loop Starts: bmac: #{NRF24::get_bmac}"
|
27
28
|
|
28
29
|
loopc=0;
|
29
30
|
sc=0;
|
@@ -36,10 +37,19 @@ loop do
|
|
36
37
|
str.each_byte do |b|
|
37
38
|
msg<<b
|
38
39
|
end
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
if sc&2==0
|
41
|
+
if sc&1==0
|
42
|
+
r0.send_q << {msg: msg, tx_mac: bmac}
|
43
|
+
else
|
44
|
+
r1.send_q << {msg: msg, tx_mac: bmac}
|
45
|
+
end
|
46
|
+
else
|
47
|
+
if sc&1==0
|
48
|
+
#r0.send_q << {msg: msg, tx_mac: r1.mac,ack:true}
|
49
|
+
r0.send_q << {msg: msg, tx_mac: "FF:A5:A5",ack:true}
|
50
|
+
else
|
51
|
+
r1.send_q << {msg: msg, tx_mac: r0.mac,ack:true}
|
52
|
+
end
|
43
53
|
end
|
44
54
|
NRF24::note "sent '#{str}' to #{sc&1}"
|
45
55
|
end
|
data/http/coffee/nRF24.coffee
CHANGED
@@ -39,11 +39,12 @@ update_status = (data) ->
|
|
39
39
|
@build_regs = (regs) ->
|
40
40
|
ret="<table>"
|
41
41
|
for k,v of regs
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
if not v.hide
|
43
|
+
ret+="<tr>"
|
44
|
+
ret+="<td >#{v.name}<td>"
|
45
|
+
ret+="<td id='d0r#{k}'>d0r#{k}<td>"
|
46
|
+
ret+="<td id='d1r#{k}'>d1r#{k}<td>"
|
47
|
+
ret+="</tr>"
|
47
48
|
ret+="</table>"
|
48
49
|
$(".regs").html(ret)
|
49
50
|
ret="<table>"
|
data/lib/nRF24-ruby.rb
CHANGED
@@ -13,26 +13,30 @@ class NRF24
|
|
13
13
|
|
14
14
|
@@regs={
|
15
15
|
CONFIG: {address: 0x00,len:7},
|
16
|
-
EN_AA: {address: 0x01,len:
|
17
|
-
EN_RXADDR: {address: 0x02,len:
|
18
|
-
SETUP_AW: {address: 0x03},
|
16
|
+
EN_AA: {address: 0x01,len:6},
|
17
|
+
EN_RXADDR: {address: 0x02,len:6},
|
18
|
+
SETUP_AW: {address: 0x03,len:2},
|
19
19
|
SETUP_RETR: {address: 0x04},
|
20
20
|
RF_CH: {address: 0x05,format: :dec},
|
21
21
|
RF_SETUP: {address: 0x06,len:4},
|
22
22
|
STATUS: {address: 0x07, poll: 1,len: 7},
|
23
23
|
OBSERVE_TX: {address: 0x08, poll: 1},
|
24
24
|
CD: {address: 0x09, poll: 1, len: 1},
|
25
|
-
RX_ADDR_P0: {address: 0x0A, bytes:
|
26
|
-
RX_ADDR_P1: {address: 0x0B, bytes:
|
27
|
-
RX_ADDR_P2: {address: 0x0C, format: :hex},
|
28
|
-
RX_ADDR_P3: {address: 0x0D, format: :hex},
|
29
|
-
RX_ADDR_P4: {address: 0x0E, format: :hex},
|
30
|
-
RX_ADDR_P5: {address: 0x0F, format: :hex},
|
31
|
-
TX_ADDR: {address: 0x10, bytes:
|
32
|
-
RX_PW_P0: {address: 0x11, format: :dec},
|
33
|
-
RX_PW_P1: {address: 0x12, format: :dec},
|
25
|
+
RX_ADDR_P0: {address: 0x0A, bytes: 3, format: :hex },
|
26
|
+
RX_ADDR_P1: {address: 0x0B, bytes: 3, format: :hex },
|
27
|
+
RX_ADDR_P2: {address: 0x0C, format: :hex,hide: true},
|
28
|
+
RX_ADDR_P3: {address: 0x0D, format: :hex,hide: true},
|
29
|
+
RX_ADDR_P4: {address: 0x0E, format: :hex,hide: true},
|
30
|
+
RX_ADDR_P5: {address: 0x0F, format: :hex,hide: true},
|
31
|
+
TX_ADDR: {address: 0x10, bytes: 3 , format: :hex},
|
32
|
+
RX_PW_P0: {address: 0x11, format: :dec,hide: true},
|
33
|
+
RX_PW_P1: {address: 0x12, format: :dec,hide: true},
|
34
|
+
RX_PW_P2: {address: 0x13, format: :dec,hide: true},
|
35
|
+
RX_PW_P3: {address: 0x14, format: :dec,hide: true},
|
36
|
+
RX_PW_P4: {address: 0x15, format: :dec,hide: true},
|
37
|
+
RX_PW_P5: {address: 0x16, format: :dec,hide: true},
|
34
38
|
FIFO_STATUS: {address: 0x17, poll: 1, len:7},
|
35
|
-
DYNPD: {address: 0x1C,len:
|
39
|
+
DYNPD: {address: 0x1C,len:6},
|
36
40
|
FEATURE: {address: 0x1D,len:3},
|
37
41
|
}
|
38
42
|
|
@@ -51,6 +55,12 @@ class NRF24
|
|
51
55
|
|
52
56
|
@@sem=Mutex.new
|
53
57
|
@@log=[]
|
58
|
+
@@bmac="45:45:45:45:45"
|
59
|
+
|
60
|
+
def self.set_bmac mac
|
61
|
+
@@bmac=mac
|
62
|
+
puts "set bmac to #{mac}"
|
63
|
+
end
|
54
64
|
|
55
65
|
def self.note str,*args
|
56
66
|
begin
|
@@ -147,20 +157,22 @@ class NRF24
|
|
147
157
|
[@s[:status]]
|
148
158
|
end
|
149
159
|
|
150
|
-
def send packet
|
160
|
+
def send packet,hash={}
|
151
161
|
pac=Array.new(@@PAYLOAD_SIZE, 0)
|
152
162
|
packet.each_with_index do |byte,i|
|
153
163
|
pac[i]=packet[i] if i<@@PAYLOAD_SIZE
|
154
164
|
end
|
155
165
|
@ce.off
|
156
166
|
wreg :CONFIG,0x0a
|
157
|
-
if @s[:params]
|
167
|
+
if hash[:ack] and @s[:params][:ack]
|
158
168
|
cmd :W_TX_PAYLOAD,pac
|
169
|
+
#puts "with ack"
|
159
170
|
else
|
160
171
|
cmd :W_TX_PAYLOAD_NOACK,pac
|
172
|
+
#puts "with NOack"
|
161
173
|
end
|
162
174
|
@ce.on
|
163
|
-
sleep 0.
|
175
|
+
sleep 0.001
|
164
176
|
@ce.off
|
165
177
|
wreg :CONFIG,0x0b
|
166
178
|
@ce.on
|
@@ -202,7 +214,9 @@ class NRF24
|
|
202
214
|
@s[:sfail]+=1
|
203
215
|
end
|
204
216
|
if (d&0x01)==0x00
|
205
|
-
|
217
|
+
pipe=(s>>1)&0x05
|
218
|
+
NRF24::note "pipe: #{pipe}"
|
219
|
+
ret=cmd :R_RX_PAYLOAD,Array.new(@@PAYLOAD_SIZE, 0xff)
|
206
220
|
@recv_q<<ret
|
207
221
|
@s[:rcnt]+=1
|
208
222
|
donesome=true
|
@@ -226,8 +240,10 @@ class NRF24
|
|
226
240
|
@s[:sarc]+=d&0x0f
|
227
241
|
end
|
228
242
|
|
229
|
-
msg=@send_q.pop
|
230
|
-
|
243
|
+
msg=@send_q.pop
|
244
|
+
wreg :TX_ADDR,NRF24::mac2a(msg[:tx_mac])
|
245
|
+
#puts "send mac: #{msg[:tx_mac]}"
|
246
|
+
send msg[:msg], ack:msg[:ack]
|
231
247
|
@s[:scnt]+=1
|
232
248
|
end
|
233
249
|
end
|
@@ -257,12 +273,16 @@ class NRF24
|
|
257
273
|
end
|
258
274
|
|
259
275
|
attr_accessor :rcnt,:scnt,:rfull
|
260
|
-
attr_accessor :send_q,:recv_q,:log
|
276
|
+
attr_accessor :send_q,:recv_q,:log,:mac
|
261
277
|
|
262
278
|
def self.all_devices
|
263
279
|
@@all
|
264
280
|
end
|
265
281
|
|
282
|
+
def self.get_bmac
|
283
|
+
@@bmac
|
284
|
+
end
|
285
|
+
|
266
286
|
def self.json
|
267
287
|
devs=[]
|
268
288
|
NRF24::all_devices.each do |data|
|
@@ -287,6 +307,14 @@ class NRF24
|
|
287
307
|
@@log
|
288
308
|
end
|
289
309
|
|
310
|
+
def self.mac2a mac
|
311
|
+
a=[]
|
312
|
+
mac.split(":").each do |b|
|
313
|
+
a<<b.hex
|
314
|
+
end
|
315
|
+
a
|
316
|
+
end
|
317
|
+
|
290
318
|
def hw_init hash
|
291
319
|
@s={
|
292
320
|
stamp: 0,
|
@@ -308,28 +336,47 @@ class NRF24
|
|
308
336
|
wreg :RF_CH,hash[:chan]||2
|
309
337
|
if hash[:ack]
|
310
338
|
wreg :SETUP_RETR,0x8f
|
311
|
-
wreg :EN_AA,
|
312
|
-
wreg :DYNPD,0x03
|
313
|
-
wreg :FEATURE,0x00
|
339
|
+
wreg :EN_AA,0x3e # no acks on broadcast
|
314
340
|
else
|
315
341
|
wreg :SETUP_RETR,0x00
|
316
342
|
wreg :EN_AA,0x00
|
317
|
-
wreg :DYNPD,0x00
|
318
|
-
wreg :FEATURE,0x01
|
319
343
|
end
|
320
|
-
wreg :
|
344
|
+
wreg :FEATURE,0x01
|
345
|
+
wreg :SETUP_AW,0x01
|
346
|
+
wreg :DYNPD,0x00
|
321
347
|
wreg :STATUS,0x70
|
348
|
+
wreg :EN_RXADDR,0x3f
|
322
349
|
wreg :RX_PW_P0,@@PAYLOAD_SIZE
|
323
350
|
wreg :RX_PW_P1,@@PAYLOAD_SIZE
|
324
|
-
wreg :
|
325
|
-
wreg :
|
326
|
-
|
327
|
-
|
328
|
-
|
351
|
+
wreg :RX_PW_P2,@@PAYLOAD_SIZE
|
352
|
+
wreg :RX_PW_P3,@@PAYLOAD_SIZE
|
353
|
+
wreg :RX_PW_P4,@@PAYLOAD_SIZE
|
354
|
+
wreg :RX_PW_P5,@@PAYLOAD_SIZE
|
355
|
+
wreg :TX_ADDR,NRF24::mac2a(@@bmac)
|
356
|
+
wreg :RX_ADDR_P0,NRF24::mac2a(@@bmac)
|
357
|
+
wreg :RX_ADDR_P2,0xfc
|
358
|
+
wreg :RX_ADDR_P3,0xfd
|
359
|
+
wreg :RX_ADDR_P4,0xfe
|
360
|
+
wreg :RX_ADDR_P5,0xff
|
361
|
+
if hash[:mac] #keep old if not defined
|
362
|
+
wreg :RX_ADDR_P1,NRF24::mac2a(hash[:mac])
|
363
|
+
@mac=hash[:mac]
|
329
364
|
else
|
330
|
-
|
365
|
+
s,d,bytes,code =rreg :RX_ADDR_P1
|
366
|
+
mac=""
|
367
|
+
d.each do |b|
|
368
|
+
mac+=":" if mac!=""
|
369
|
+
mac+=sprintf "%02X",b
|
370
|
+
end
|
371
|
+
@mac=mac
|
331
372
|
end
|
332
373
|
|
374
|
+
# if hash[:ack]
|
375
|
+
# cmd :ACTIVATE,[ 0]
|
376
|
+
# else
|
377
|
+
cmd :ACTIVATE,[ get_ccode(:ACTIVATE2)]
|
378
|
+
# end
|
379
|
+
|
333
380
|
cmd :FLUSH_TX
|
334
381
|
cmd :FLUSH_RX
|
335
382
|
end
|