def begin(self, major, minor, ce_pin, irq_pin): # Initialize SPI bus self.spidev = spidev.SpiDev() self.spidev.open(major,minor) self.ce_pin = ce_pin self.irq_pin = irq_pin GPIO.setup(self.ce_pin, GPIO.OUT) GPIO.setup(self.irq_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) time.sleep(5/1000000.0) # Set 1500uS (minimum for 32B payload in ESB@250KBPS) timeouts, to make testing a little easier # WARNING: If this is ever lowered, either 250KBS mode with AA is broken or maximum packet # sizes must never be used. See documentation for a more complete explanation. self.write_register(NRF24.SETUP_RETR,(int('0100',2) << NRF24.ARD) | (int('1111',2) << NRF24.ARC)) # Restore our default PA level self.setPALevel( NRF24.PA_MAX ) # Determine if this is a p or non-p RF24 module and then # reset our data rate back to default value. This works # because a non-P variant won't allow the data rate to # be set to 250Kbps. if self.setDataRate( NRF24.BR_250KBPS ): self.p_variant = True # Then set the data rate to the slowest (and most reliable) speed supported by all # hardware. self.setDataRate( NRF24.BR_1MBPS ) # Initialize CRC and request 2-byte (16bit) CRC self.setCRCLength( NRF24.CRC_16 ) # Disable dynamic payloads, to match dynamic_payloads_enabled setting self.write_register( NRF24.DYNPD,0) # Reset current status # Notice reset and flush is the last thing we do self.write_register( NRF24.STATUS, _BV(NRF24.RX_DR) | _BV(NRF24.TX_DS) | _BV(NRF24.MAX_RT) ) # Set up default configuration. Callers can always change it later. # This channel should be universally safe and not bleed over into adjacent # spectrum. self.setChannel(self.channel) # Flush buffers self.flush_rx() self.flush_tx()
def ce(self, level): if level == NRF24.HIGH: GPIO.output(self.ce_pin, GPIO.HIGH) else: GPIO.output(self.ce_pin, GPIO.LOW) return
def irqWait(self): # A race condition may occur here. # Should set a timeout if GPIO.input(self.irq_pin) == 0: return
def ce(self, level): if level == NRF24.HIGH: GPIO.output(self.ce_pin, GPIO.HIGH) else: GPIO.output(self.ce_pin, GPIO.LOW) return def irqWait(self): # A race condition may occur here. # Should set a timeout if GPIO.input(self.irq_pin) == 0: return GPIO.wait_for_edge(self.irq_pin, GPIO.FALLING) def read_register(self, reg, blen = 1): buffer = [NRF24.R_REGISTER | ( NRF24.REGISTER_MASK & reg )] for col in range(blen): buffer.append(NRF24.NOP) resp = self.spidev.xfer2(buffer) if blen == 1: return resp[1] return resp[1:blen+1] def write_register(self, reg, value, length = -1): buffer = [NRF24.W_REGISTER | ( NRF24.REGISTER_MASK & reg )] if isinstance(value,(int, long)):