77
88import time
99import board
10- import digitalio
10+ from digitalio import DigitalInOut
1111
1212# if running this on a ATSAMD21 M0 based board
1313# from circuitpython_nrf24l01.rf24_lite import RF24
1414from circuitpython_nrf24l01 .rf24 import RF24
1515
16- # select your digital input pin that's connected to the IRQ pin on the nRF4L01
17- irq_pin = digitalio .DigitalInOut (board .D12 )
18- irq_pin .switch_to_input () # make sure its an input object
19- # change these (digital output) pins accordingly
20- CE_PIN = digitalio .DigitalInOut (board .D4 )
21- CSN_PIN = digitalio .DigitalInOut (board .D5 )
16+ # invalid default values for scoping
17+ SPI_BUS , CSN_PIN , CE_PIN = (None , None , None )
18+
19+ try : # on Linux
20+ import spidev
21+
22+ SPI_BUS = spidev .SpiDev () # for a faster interface on linux
23+ CSN_PIN = 0 # use CE0 on default bus (even faster than using any pin)
24+ CE_PIN = DigitalInOut (board .D22 ) # using pin gpio22 (BCM numbering)
25+ IRQ_PIN = DigitalInOut (board .D24 ) # using gpio24 (BCM numbering)
26+
27+ except ImportError : # on CircuitPython only
28+ # using board.SPI() automatically selects the MCU's
29+ # available SPI pins, board.SCK, board.MOSI, board.MISO
30+ SPI_BUS = board .SPI () # init spi bus object
2231
23- # using board.SPI() automatically selects the MCU's
24- # available SPI pins, board.SCK, board.MOSI, board.MISO
25- SPI_BUS = board .SPI () # init spi bus object
32+ # change these (GPIO) pins accordingly
33+ CE_PIN = DigitalInOut (board .D4 )
34+ CSN_PIN = DigitalInOut (board .D5 )
35+ IRQ_PIN = DigitalInOut (board .D12 )
36+
37+ # select your digital input pin that's connected to the IRQ pin on the nRF4L01
38+ IRQ_PIN .switch_to_input () # make sure its an input object
2639
2740# we'll be using the dynamic payload size feature (enabled by default)
2841# initialize the nRF24L01 on the spi bus object
5366nrf .open_rx_pipe (1 , address [not radio_number ]) # using pipe 1
5467
5568
56- def _ping_and_prompt ():
69+ def _ping_and_prompt () -> bool :
5770 """transmit 1 payload, wait till irq_pin goes active, print IRQ status
5871 flags."""
59- nrf .ce_pin = 1 # tell the nRF24L01 to prepare sending a single packet
72+ nrf .ce_pin = True # tell the nRF24L01 to prepare sending a single packet
6073 time .sleep (0.00001 ) # mandatory 10 microsecond pulse starts transmission
61- nrf .ce_pin = 0 # end 10 us pulse; use only 1 buffer from TX FIFO
62- while irq_pin .value : # IRQ pin is active when LOW
63- pass
64- print ("IRQ pin went active LOW." )
74+ nrf .ce_pin = False # end 10 us pulse; use only 1 buffer from TX FIFO
75+ timeout = time .monotonic () + 1
76+ while IRQ_PIN .value :
77+ # IRQ pin is active when LOW
78+ if time .monotonic () > timeout :
79+ break
80+ if IRQ_PIN .value :
81+ print (" IRQ pin was not triggered!" )
82+ return False
83+ # else:
84+ print (" IRQ pin went active LOW." )
6585 nrf .update () # update irq_d? status flags
6686 print (
67- "\t irq_ds : {}, irq_dr: {}, irq_df: {}" .format (
87+ " irq_ds : {}, irq_dr: {}, irq_df: {}" .format (
6888 nrf .irq_ds , nrf .irq_dr , nrf .irq_df
6989 )
7090 )
91+ return True
7192
7293
7394def master ():
@@ -83,16 +104,16 @@ def master():
83104 # on data ready test
84105 print ("\n Configuring IRQ pin to only ignore 'on data sent' event" )
85106 nrf .interrupt_config (data_sent = False )
86- print (" Pinging slave node for an ACK payload..." , end = " " )
87- _ping_and_prompt () # CE pin is managed by this function
88- print ('\t "on data ready" event test {}successful' . format ( "un" * nrf .irq_dr ) )
107+ print (" Pinging slave node for an ACK payload..." )
108+ if _ping_and_prompt (): # CE pin is managed by this function
109+ print (' "on data ready" event test' , "passed" if nrf .irq_dr else "failed" )
89110
90111 # on data sent test
91112 print ("\n Configuring IRQ pin to only ignore 'on data ready' event" )
92113 nrf .interrupt_config (data_recv = False )
93- print (" Pinging slave node again... " , end = " " )
94- _ping_and_prompt () # CE pin is managed by this function
95- print ('\t "on data sent" event test {}successful' . format ( "un" * nrf .irq_ds ) )
114+ print (" Pinging slave node again..." )
115+ if _ping_and_prompt (): # CE pin is managed by this function
116+ print (' "on data sent" event test' , "passed" if nrf .irq_ds else "failed" )
96117
97118 # trigger slave node to exit by filling the slave node's RX FIFO
98119 print ("\n Sending one extra payload to fill RX FIFO on slave node." )
@@ -108,11 +129,11 @@ def master():
108129 # on data fail test
109130 print ("\n Configuring IRQ pin to go active for all events." )
110131 nrf .interrupt_config ()
111- print (" Sending a ping to inactive slave node..." , end = " " )
132+ print (" Sending a ping to inactive slave node..." )
112133 nrf .flush_tx () # just in case any previous tests failed
113134 nrf .write (b"Dummy" , write_only = True ) # CE pin is left LOW
114- _ping_and_prompt () # CE pin is managed by this function
115- print ('\t "on data failed" event test {}successful' . format ( "un" * nrf .irq_df ) )
135+ if _ping_and_prompt (): # CE pin is managed by this function
136+ print (' "on data failed" event test' , "passed" if nrf .irq_df else "failed" )
116137 nrf .flush_tx () # flush artifact payload in TX FIFO from last test
117138 # all 3 ACK payloads received were 4 bytes each, and RX FIFO is full
118139 # so, fetching 12 bytes from the RX FIFO also flushes RX FIFO
@@ -130,6 +151,7 @@ def slave(timeout=6): # will listen for 6 seconds before timing out
130151 while not nrf .fifo (0 , 0 ) and time .monotonic () - start_timer < timeout :
131152 # if RX FIFO is not full and timeout is not reached, then keep going
132153 pass
154+ time .sleep (0.0005 ) # wait for ACK packet to finish transmitting
133155 # recommended behavior is to keep in TX mode when in idle
134156 nrf .listen = False # enter inactive TX mode
135157 # entering TX mode (when ACK payloads are enabled) also flushes the TX FIFO
0 commit comments