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:5},
  EN_RXADDR:   {address: 0x02,len:5},
  SETUP_AW:    {address: 0x03},
  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: 5, format: :hex },
  RX_ADDR_P1:  {address: 0x0B, bytes: 5, format: :hex },
  RX_ADDR_P2:  {address: 0x0C, format: :hex},
  RX_ADDR_P3:  {address: 0x0D, format: :hex},
  RX_ADDR_P4:  {address: 0x0E, format: :hex},
  RX_ADDR_P5:  {address: 0x0F, format: :hex},
  TX_ADDR:     {address: 0x10, bytes: 5 , format: :hex},
  RX_PW_P0:    {address: 0x11, format: :dec},
  RX_PW_P1:    {address: 0x12, format: :dec},
  FIFO_STATUS: {address: 0x17, poll: 1, len:7},
  DYNPD:       {address: 0x1C,len:5},
  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 =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ NRF24

Returns a new instance of NRF24.



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/nRF24-ruby.rb', line 337

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.



260
261
262
# File 'lib/nRF24-ruby.rb', line 260

def log
  @log
end

#rcntObject

Returns the value of attribute rcnt.



259
260
261
# File 'lib/nRF24-ruby.rb', line 259

def rcnt
  @rcnt
end

#recv_qObject

Returns the value of attribute recv_q.



260
261
262
# File 'lib/nRF24-ruby.rb', line 260

def recv_q
  @recv_q
end

#rfullObject

Returns the value of attribute rfull.



259
260
261
# File 'lib/nRF24-ruby.rb', line 259

def rfull
  @rfull
end

#scntObject

Returns the value of attribute scnt.



259
260
261
# File 'lib/nRF24-ruby.rb', line 259

def scnt
  @scnt
end

#send_qObject

Returns the value of attribute send_q.



260
261
262
# File 'lib/nRF24-ruby.rb', line 260

def send_q
  @send_q
end

Class Method Details

.all_devicesObject



262
263
264
# File 'lib/nRF24-ruby.rb', line 262

def self.all_devices
  @@all
end

.get_logObject



286
287
288
# File 'lib/nRF24-ruby.rb', line 286

def self.get_log 
  @@log
end

.jsonObject



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/nRF24-ruby.rb', line 266

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

.note(str, *args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/nRF24-ruby.rb', line 55

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



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

def self.register_table
  @@regs
end

Instance Method Details

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nRF24-ruby.rb', line 83

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/nRF24-ruby.rb', line 243

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



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

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



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

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



176
177
178
179
180
181
182
# File 'lib/nRF24-ruby.rb', line 176

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



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/nRF24-ruby.rb', line 290

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,0x7f
    wreg :DYNPD,0x03
    wreg :FEATURE,0x00
  else
    wreg :SETUP_RETR,0x00
    wreg :EN_AA,0x00
    wreg :DYNPD,0x00
    wreg :FEATURE,0x01
  end
  wreg :SETUP_AW,0x03
  wreg :STATUS,0x70
  wreg :RX_PW_P0,@@PAYLOAD_SIZE
  wreg :RX_PW_P1,@@PAYLOAD_SIZE
  wreg :TX_ADDR,[0x12,0x34,0x56,0x78,0x9a]
  wreg :RX_ADDR_P0,[0x12,0x34,0x56,0x78,0x9a]

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

  cmd :FLUSH_TX
  cmd :FLUSH_RX
end

#jsonObject



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

def json 
  @s
end

#recvObject



169
170
171
172
173
174
# File 'lib/nRF24-ruby.rb', line 169

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

#rf_serverObject



184
185
186
187
188
189
190
191
192
193
194
195
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
# File 'lib/nRF24-ruby.rb', line 184

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
         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 
            send msg 
            @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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nRF24-ruby.rb', line 103

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) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/nRF24-ruby.rb', line 150

def send packet
  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 @s[:params]
    cmd :W_TX_PAYLOAD,pac
  else
    cmd :W_TX_PAYLOAD_NOACK,pac
  end
  @ce.on
  sleep 0.0005
  @ce.off
  wreg :CONFIG,0x0b
  @ce.on
end

#wreg(reg, data) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/nRF24-ruby.rb', line 127

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