Class: NRF24
- Inherits:
-
Object
- Object
- NRF24
- 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
-
#log ⇒ Object
Returns the value of attribute log.
-
#mac ⇒ Object
Returns the value of attribute mac.
-
#rcnt ⇒ Object
Returns the value of attribute rcnt.
-
#recv_q ⇒ Object
Returns the value of attribute recv_q.
-
#rfull ⇒ Object
Returns the value of attribute rfull.
-
#scnt ⇒ Object
Returns the value of attribute scnt.
-
#send_q ⇒ Object
Returns the value of attribute send_q.
Class Method Summary collapse
- .a2mac(a, hash = {}) ⇒ Object
- .all_devices ⇒ Object
- .get_bmac ⇒ Object
- .get_log ⇒ Object
- .json ⇒ Object
- .mac2a(mac, hash = {}) ⇒ Object
- .note(str, *args) ⇒ Object
- .register_table ⇒ Object
- .set_bmac(mac) ⇒ Object
Instance Method Summary collapse
- #calc_crc(str) ⇒ Object
- #cmd(c, data = []) ⇒ Object
- #do_monitor ⇒ Object
- #get_address(reg) ⇒ Object
- #get_ccode(c) ⇒ Object
- #get_regs(all) ⇒ Object
- #hw_init(hash) ⇒ Object
-
#initialize(hash = {}) ⇒ NRF24
constructor
A new instance of NRF24.
- #json ⇒ Object
- #rf_server ⇒ Object
- #rreg(reg) ⇒ Object
- #send(packet, hash = {}) ⇒ Object
- #wreg(reg, data) ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ NRF24
Returns a new instance of NRF24.
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/nRF24-ruby.rb', line 415 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 @server_t.priority=100 @monitor_t=do_monitor end |
Instance Attribute Details
#log ⇒ Object
Returns the value of attribute log.
306 307 308 |
# File 'lib/nRF24-ruby.rb', line 306 def log @log end |
#mac ⇒ Object
Returns the value of attribute mac.
306 307 308 |
# File 'lib/nRF24-ruby.rb', line 306 def mac @mac end |
#rcnt ⇒ Object
Returns the value of attribute rcnt.
305 306 307 |
# File 'lib/nRF24-ruby.rb', line 305 def rcnt @rcnt end |
#recv_q ⇒ Object
Returns the value of attribute recv_q.
306 307 308 |
# File 'lib/nRF24-ruby.rb', line 306 def recv_q @recv_q end |
#rfull ⇒ Object
Returns the value of attribute rfull.
305 306 307 |
# File 'lib/nRF24-ruby.rb', line 305 def rfull @rfull end |
#scnt ⇒ Object
Returns the value of attribute scnt.
305 306 307 |
# File 'lib/nRF24-ruby.rb', line 305 def scnt @scnt end |
#send_q ⇒ Object
Returns the value of attribute send_q.
306 307 308 |
# File 'lib/nRF24-ruby.rb', line 306 def send_q @send_q end |
Class Method Details
.a2mac(a, hash = {}) ⇒ Object
340 341 342 343 344 345 346 347 |
# File 'lib/nRF24-ruby.rb', line 340 def self.a2mac a,hash={} mac="" a.each do |e| mac+=":" if mac!="" mac+=sprintf "%02X",e end mac end |
.all_devices ⇒ Object
308 309 310 |
# File 'lib/nRF24-ruby.rb', line 308 def self.all_devices @@all end |
.get_bmac ⇒ Object
312 313 314 |
# File 'lib/nRF24-ruby.rb', line 312 def self.get_bmac @@bmac end |
.get_log ⇒ Object
336 337 338 |
# File 'lib/nRF24-ruby.rb', line 336 def self.get_log @@log end |
.json ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/nRF24-ruby.rb', line 316 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, hash = {}) ⇒ Object
349 350 351 352 353 354 355 356 357 |
# File 'lib/nRF24-ruby.rb', line 349 def self.mac2a mac,hash={} a=[] mac.split(":").each do |b| a<<b.hex end a.unshift hash[:socket]||0 if a.size==2 and not hash[:short] #pp a a end |
.note(str, *args) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/nRF24-ruby.rb', line 66 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_table ⇒ Object
328 329 330 |
# File 'lib/nRF24-ruby.rb', line 328 def self.register_table @@regs end |
.set_bmac(mac) ⇒ Object
62 63 64 |
# File 'lib/nRF24-ruby.rb', line 62 def self.set_bmac mac @@bmac=mac end |
Instance Method Details
#calc_crc(str) ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'lib/nRF24-ruby.rb', line 161 def calc_crc str checksum=0 str.unpack("c*").each do |c| checksum+=c.ord end checksum&=0xff #puts "cc calc: '#{str}'' -> #{checksum}" return checksum end |
#cmd(c, data = []) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/nRF24-ruby.rb', line 94 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_monitor ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/nRF24-ruby.rb', line 289 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
86 87 88 89 90 91 92 |
# File 'lib/nRF24-ruby.rb', line 86 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
77 78 79 80 81 82 83 84 |
# File 'lib/nRF24-ruby.rb', line 77 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
196 197 198 199 200 201 202 |
# File 'lib/nRF24-ruby.rb', line 196 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
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/nRF24-ruby.rb', line 359 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) if hash[:mac] #keep old if not defined wreg :RX_ADDR_P1,NRF24::mac2a(hash[:mac]) @mac=hash[:mac] else end wreg :RX_ADDR_P2,1 wreg :RX_ADDR_P3,2 wreg :RX_ADDR_P4,3 wreg :RX_ADDR_P5,4 cmd :ACTIVATE,[ get_ccode(:ACTIVATE2)] cmd :FLUSH_TX cmd :FLUSH_RX end |
#json ⇒ Object
332 333 334 |
# File 'lib/nRF24-ruby.rb', line 332 def json @s end |
#rf_server ⇒ Object
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/nRF24-ruby.rb', line 204 def rf_server Thread.new do begin loop do donesome=false cflag=0 s,d,b=rreg :FIFO_STATUS if (s&0x40) == 0x40 #NRF24::note "got RX_DR --received something" cflag|=0x40 end if (s&0x20) == 0x20 #NRF24::note "got TX_DS --sent something" wreg :STATUS,0x20 cflag|=0x20 end if (s&0x10) == 0x10 NRF24::note "****************** got MAX_RT --send fails..." cflag|=0x10 @s[:sfail]+=1 end wreg(:STATUS,cflag) if cflag>0 while (d&0x01)==0x00 @s[:rfull]+=1 if (d&0x02)==0x02 pipe=(s>>1)&0x05 if pipe==0 socket=:broadcast else socket=pipe-1 end ret=cmd :R_RX_PAYLOAD,Array.new(@@PAYLOAD_SIZE, 0xff) if @s[:params][:mac_header] sender=NRF24::a2mac(ret[0..1]) checksum=ret[2] ret.shift(3) check= calc_crc(ret.pack("c*")) if check!=checksum puts "Error: Checksum error! Message Ignored!" s,d,b=rreg :FIFO_STATUS next end end msg={msg:ret,socket:socket,from:sender,to:@mac,dir: :in,checksum:checksum,check:check} @recv_q << msg NRF24::note "i #{msg}" @s[:rcnt]+=1 s,d,b=rreg :FIFO_STATUS 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 #puts "msg:#{msg}" if msg[:socket]==:broadcast to=NRF24::mac2a(NRF24::get_bmac) else to=NRF24::mac2a(msg[:to],socket: msg[:socket]) end wreg :TX_ADDR, to msg[:from]=@mac msg[:dir]=:out send msg[:msg], ack:msg[:ack] #msg[:msg]=msg[:msg].pack("c*") NRF24::note "o #{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
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/nRF24-ruby.rb', line 114 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
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/nRF24-ruby.rb', line 171 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 #prepend our mac? if @s[:params][:mac_header] checksum= calc_crc(pac.pack("c*")) pac= NRF24::mac2a(@mac, short:true)+[checksum]+pac pac=pac[0...32] end if hash[:ack] and @s[:params][:ack] cmd :W_TX_PAYLOAD,pac else cmd :W_TX_PAYLOAD_NOACK,pac end @ce.on sleep 0.001 @ce.off wreg :CONFIG,0x0b @ce.on end |
#wreg(reg, data) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/nRF24-ruby.rb', line 138 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 |