Class: NRF24

Inherits:
Object
  • Object
show all
Defined in:
lib/nRF24-ruby.rb

Constant Summary collapse

@@all =
[]
@@PAYLOAD_SIZE =
32
@@SPI_CLOCK =
250000
@@regs =
{
  CONFIG:      {address: 0x00,len:7},
  EN_AA:       {address: 0x01,len:6},
  EN_RXADDR:   {address: 0x02,len:6},
  SETUP_AW:    {address: 0x03,len:2},
  SETUP_RETR:  {address: 0x04},
  RF_CH:       {address: 0x05,format: :dec},
  RF_SETUP:    {address: 0x06,len:4},
  STATUS:      {address: 0x07, poll: 1,len: 7},
  OBSERVE_TX:  {address: 0x08, poll: 1},
  CD:          {address: 0x09, poll: 1, len: 1},
  RX_ADDR_P0:  {address: 0x0A, bytes: 3, format: :hex },
  RX_ADDR_P1:  {address: 0x0B, bytes: 3, format: :hex },
  RX_ADDR_P2:  {address: 0x0C, format: :hex,hide: true},
  RX_ADDR_P3:  {address: 0x0D, format: :hex,hide: true},
  RX_ADDR_P4:  {address: 0x0E, format: :hex,hide: true},
  RX_ADDR_P5:  {address: 0x0F, format: :hex,hide: true},
  TX_ADDR:     {address: 0x10, bytes: 3 , format: :hex},
  RX_PW_P0:    {address: 0x11, format: :dec,hide: true},
  RX_PW_P1:    {address: 0x12, format: :dec,hide: true},
  RX_PW_P2:    {address: 0x13, format: :dec,hide: true},
  RX_PW_P3:    {address: 0x14, format: :dec,hide: true},
  RX_PW_P4:    {address: 0x15, format: :dec,hide: true},
  RX_PW_P5:    {address: 0x16, format: :dec,hide: true},
  FIFO_STATUS: {address: 0x17, poll: 1, len:7},
  DYNPD:       {address: 0x1C,len:6}, 
  FEATURE:     {address: 0x1D,len:3},
}
@@cmds =
{
  R_REGISTER: 0x00,
  W_REGISTER: 0x20,
  ACTIVATE:   0x50,
  R_RX_PL_WID: 0x60,
  R_RX_PAYLOAD: 0x61,
  W_TX_PAYLOAD: 0xA0,
  FLUSH_TX:   0xe1,
  FLUSH_RX:   0xe2,
  W_TX_PAYLOAD_NOACK: 0xB0,
  ACTIVATE2:  0x73,
}
@@sem =
Mutex.new
@@log =
[]
@@bmac =
"45:45:45:45:45"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ NRF24



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/nRF24-ruby.rb', line 384

def initialize(hash={})
  @semh=Mutex.new 

 
  @ce=PiPiper::Pin.new(:pin => hash[:ce], :direction => :out)
  @cs=PiPiper::Pin.new(:pin => hash[:cs], :direction => :out)

  @ce.on
  @cs.on
  @@all<<self

  @recv_q=Queue.new
  @send_q=Queue.new

  hw_init hash

  @server_t=rf_server 
  @monitor_t=do_monitor
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



276
277
278
# File 'lib/nRF24-ruby.rb', line 276

def log
  @log
end

#macObject

Returns the value of attribute mac.



276
277
278
# File 'lib/nRF24-ruby.rb', line 276

def mac
  @mac
end

#rcntObject

Returns the value of attribute rcnt.



275
276
277
# File 'lib/nRF24-ruby.rb', line 275

def rcnt
  @rcnt
end

#recv_qObject

Returns the value of attribute recv_q.



276
277
278
# File 'lib/nRF24-ruby.rb', line 276

def recv_q
  @recv_q
end

#rfullObject

Returns the value of attribute rfull.



275
276
277
# File 'lib/nRF24-ruby.rb', line 275

def rfull
  @rfull
end

#scntObject

Returns the value of attribute scnt.



275
276
277
# File 'lib/nRF24-ruby.rb', line 275

def scnt
  @scnt
end

#send_qObject

Returns the value of attribute send_q.



276
277
278
# File 'lib/nRF24-ruby.rb', line 276

def send_q
  @send_q
end

Class Method Details

.all_devicesObject



278
279
280
# File 'lib/nRF24-ruby.rb', line 278

def self.all_devices
  @@all
end

.get_bmacObject



282
283
284
# File 'lib/nRF24-ruby.rb', line 282

def self.get_bmac
  @@bmac
end

.get_logObject



306
307
308
# File 'lib/nRF24-ruby.rb', line 306

def self.get_log 
  @@log
end

.jsonObject



286
287
288
289
290
291
292
293
294
295
296
# File 'lib/nRF24-ruby.rb', line 286

def self.json
  devs=[]
  NRF24::all_devices.each do |data|
    devs<<data.json
  end
  json ={
    now:Time.now.to_i,
    devs: devs,
  }
  return json
end

.mac2a(mac) ⇒ Object



310
311
312
313
314
315
316
# File 'lib/nRF24-ruby.rb', line 310

def self.mac2a mac
  a=[]
  mac.split(":").each do |b|
    a<<b.hex
  end
  a
end

.note(str, *args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/nRF24-ruby.rb', line 65

def self.note str,*args
  begin
    s=sprintf(str,*args)
    text=sprintf("%s: %s",Time.now.iso8601,s)
    @@log << {stamp: Time.now.to_i, text: text.encode("UTF-8", :invalid=>:replace, :replace=>"?")}
  rescue => e
    pp e.backtrace
    puts "note dies: #{e} '#{str}'"
  end
end

.register_tableObject



298
299
300
# File 'lib/nRF24-ruby.rb', line 298

def self.register_table
  @@regs
end

.set_bmac(mac) ⇒ Object



60
61
62
63
# File 'lib/nRF24-ruby.rb', line 60

def self.set_bmac mac
  @@bmac=mac
  puts "set bmac to #{mac}"
end

Instance Method Details

#cmd(c, data = []) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/nRF24-ruby.rb', line 93

def cmd c,data=[]
  ret=[]
  status=0
  cc=get_ccode(c)
  @@sem.synchronize do
    @cs.off 
    PiPiper::Spi.begin do 
      clock(@@SPI_CLOCK)
      status=write cc
      data.each do |byte|
        ret << write(byte)
      end
    end
    @cs.on
    @s[:status]=status
  end
  ret
end

#do_monitorObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/nRF24-ruby.rb', line 259

def do_monitor
  Thread.new do
    begin
      lc=0
      loop do
        get_regs(lc%10 == 0)
        sleep 1
        lc+=1
      end
    rescue Exception =>e
      puts "do_monitor fails #{e}"
      pp e.backtrace
    end
  end
end

#get_address(reg) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/nRF24-ruby.rb', line 85

def get_address reg
  rdata=@@regs[reg]
  if not rdata
    raise "Unknown Register : #{reg}"
  end
  [rdata[:address],rdata[:bytes]||1]
end

#get_ccode(c) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/nRF24-ruby.rb', line 76

def get_ccode c
  ccode=@@cmds[c]
  if not ccode
    printf("Error: Unkown Command %s\n",c);
    raise "Command Error"
  end
  ccode
end

#get_regs(all) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/nRF24-ruby.rb', line 188

def get_regs all
  @@regs.each do |k,r|
    next if not r[:poll] and not all
    s,d,bytes,code =rreg k
    @s[:regs][code]=d
  end
end

#hw_init(hash) ⇒ Object



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
# File 'lib/nRF24-ruby.rb', line 318

def hw_init hash
  @s={
    stamp: 0,
    params: hash,
    status: 0,
    regs: {},
    rcnt: 0,
    rfull: 0,
    scnt: 0,
    sarc: 0,
    sfail: 0,
  } 
  @id=hash[:id]
  wreg :CONFIG,0x0b
  rf_dr=(hash[:rf_dr]||1).to_i&0x01
  rf_pwr=(hash[:rf_pwr]||3).to_i&0x03
  lna_hcurr=(hash[:lna_hcurr]||1).to_i&0x01
  wreg :RF_SETUP,(rf_dr<<3)+(rf_pwr<<1)+lna_hcurr
  wreg :RF_CH,hash[:chan]||2
  if hash[:ack]
    wreg :SETUP_RETR,0x8f
    wreg :EN_AA,0x3e # no acks on broadcast
  else
    wreg :SETUP_RETR,0x00
    wreg :EN_AA,0x00
  end
  wreg :FEATURE,0x01
  wreg :SETUP_AW,0x01
  wreg :DYNPD,0x00
  wreg :STATUS,0x70
  wreg :EN_RXADDR,0x3f
  wreg :RX_PW_P0,@@PAYLOAD_SIZE
  wreg :RX_PW_P1,@@PAYLOAD_SIZE
  wreg :RX_PW_P2,@@PAYLOAD_SIZE
  wreg :RX_PW_P3,@@PAYLOAD_SIZE
  wreg :RX_PW_P4,@@PAYLOAD_SIZE
  wreg :RX_PW_P5,@@PAYLOAD_SIZE
  wreg :TX_ADDR,NRF24::mac2a(@@bmac)
  wreg :RX_ADDR_P0,NRF24::mac2a(@@bmac)
  wreg :RX_ADDR_P2,0xfc
  wreg :RX_ADDR_P3,0xfd
  wreg :RX_ADDR_P4,0xfe
  wreg :RX_ADDR_P5,0xff
  if hash[:mac] #keep old if not defined
    wreg :RX_ADDR_P1,NRF24::mac2a(hash[:mac]) 
    @mac=hash[:mac]
  else
    s,d,bytes,code =rreg :RX_ADDR_P1
    mac=""
    d.each do |b|
      mac+=":" if mac!=""
      mac+=sprintf "%02X",b
    end
    @mac=mac
  end

#    if hash[:ack]
 #     cmd :ACTIVATE,[ 0]
#  else
    cmd :ACTIVATE,[ get_ccode(:ACTIVATE2)]
 # end

  cmd :FLUSH_TX
  cmd :FLUSH_RX
end

#jsonObject



302
303
304
# File 'lib/nRF24-ruby.rb', line 302

def json 
  @s
end

#recvObject



181
182
183
184
185
186
# File 'lib/nRF24-ruby.rb', line 181

def recv 
  fifo_status,_=rreg :FIFO_STATUS
  if (fifo_status & 0x01) == 0x01
    puts "on dataa"
  end
end

#rf_serverObject



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
# File 'lib/nRF24-ruby.rb', line 196

def rf_server
  Thread.new do
    begin
      loop do
        donesome=false

        s,d,b=rreg :FIFO_STATUS
        if (s&0x40) == 0x40
          NRF24::note "got RX_DR --received something"
          wreg :STATUS,0x40
        end
        if (s&0x20) == 0x20
          NRF24::note "got TX_DS --sent something"
          wreg :STATUS,0x20
        end
        if (s&0x10) == 0x10
          NRF24::note "****************** got MAX_RT --send fails..."
          wreg :STATUS,0x10
          @s[:sfail]+=1
        end
        if (d&0x01)==0x00
          pipe=(s>>1)&0x05
          NRF24::note "pipe: #{pipe}"
          ret=cmd :R_RX_PAYLOAD,Array.new(@@PAYLOAD_SIZE, 0xff)
          @recv_q<<ret
          @s[:rcnt]+=1
          donesome=true
        end
        if (d&0x02)==0x02
          ret=cmd :R_RX_PAYLOAD,Array.new(@@PAYLOAD_SIZE, 0xff)
          @recv_q<<ret
          @s[:rcnt]+=1
          @s[:rfull]+=1
          donesome=true
        end

        #send rate limiter here :)
        if not @send_q.empty?
          if (d&0x20)==0x00


            s,d,b=rreg :OBSERVE_TX
            if (d&0x0f)!=0x00
              NRF24::note "got ARC_CNT:#{d&0x0f}******************"
              @s[:sarc]+=d&0x0f
            end

            msg=@send_q.pop
            wreg :TX_ADDR,NRF24::mac2a(msg[:tx_mac]) 
            #puts "send mac: #{msg[:tx_mac]}"
            send msg[:msg], ack:msg[:ack]
            @s[:scnt]+=1
          end
        end
        sleep 0.01 if not donesome
      end
    rescue Exception =>e
      puts "do_send fails #{e}"
      pp e.backtrace
    end
  end
end

#rreg(reg) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/nRF24-ruby.rb', line 113

def rreg reg
  status=data=0
  i,bytes =get_address reg
  cc=get_ccode(:R_REGISTER) +i
  @@sem.synchronize do
    @cs.off
    PiPiper::Spi.begin do 
      clock(@@SPI_CLOCK)
      status=write cc
      if bytes==1
        data=write(0xff)
      else
        data=[]
        bytes.times do 
          data << write(0xff)
        end
      end
    end
    @s[:status]=status
    @cs.on
  end
  [@s[:status],data,bytes,cc]
end

#send(packet, hash = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/nRF24-ruby.rb', line 160

def send packet,hash={}
  pac=Array.new(@@PAYLOAD_SIZE, 0)
  packet.each_with_index do |byte,i|
    pac[i]=packet[i] if i<@@PAYLOAD_SIZE
  end
  @ce.off
  wreg :CONFIG,0x0a
  if hash[:ack] and @s[:params][:ack]
    cmd :W_TX_PAYLOAD,pac
    #puts "with ack"
  else
    cmd :W_TX_PAYLOAD_NOACK,pac
    #puts "with NOack" 
  end
  @ce.on
  sleep 0.001
  @ce.off
  wreg :CONFIG,0x0b
  @ce.on
end

#wreg(reg, data) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/nRF24-ruby.rb', line 137

def wreg reg,data
  i,bytes=get_address reg
  cc=get_ccode(:W_REGISTER)+i
  @@sem.synchronize do
    status=0xff
    @cs.off
    PiPiper::Spi.begin do
      clock(@@SPI_CLOCK)
      status=write cc
      if bytes==1
        write(data)
      else
        data.each do |byte|
          write(byte)
        end
      end
    end
    @cs.on
    @s[:status]=status
  end
  [@s[:status]]
end