drivers/nrf24l01: Make nRF24L01 test script more portable.
This commit is contained in:
parent
1394258f37
commit
54acd0b0f0
|
@ -1,8 +1,8 @@
|
||||||
"""Test for nrf24l01 module."""
|
"""Test for nrf24l01 module."""
|
||||||
|
|
||||||
import struct
|
import struct
|
||||||
import pyb
|
import utime
|
||||||
from pyb import Pin, SPI
|
from machine import Pin, SPI
|
||||||
from nrf24l01 import NRF24L01
|
from nrf24l01 import NRF24L01
|
||||||
|
|
||||||
pipes = (b'\xf0\xf0\xf0\xf0\xe1', b'\xf0\xf0\xf0\xf0\xd2')
|
pipes = (b'\xf0\xf0\xf0\xf0\xe1', b'\xf0\xf0\xf0\xf0\xd2')
|
||||||
|
@ -24,7 +24,7 @@ def master():
|
||||||
while num_successes < num_needed and num_failures < num_needed:
|
while num_successes < num_needed and num_failures < num_needed:
|
||||||
# stop listening and send packet
|
# stop listening and send packet
|
||||||
nrf.stop_listening()
|
nrf.stop_listening()
|
||||||
millis = pyb.millis()
|
millis = utime.ticks_ms()
|
||||||
led_state = max(1, (led_state << 1) & 0x0f)
|
led_state = max(1, (led_state << 1) & 0x0f)
|
||||||
print('sending:', millis, led_state)
|
print('sending:', millis, led_state)
|
||||||
try:
|
try:
|
||||||
|
@ -36,10 +36,10 @@ def master():
|
||||||
nrf.start_listening()
|
nrf.start_listening()
|
||||||
|
|
||||||
# wait for response, with 250ms timeout
|
# wait for response, with 250ms timeout
|
||||||
start_time = pyb.millis()
|
start_time = utime.ticks_ms()
|
||||||
timeout = False
|
timeout = False
|
||||||
while not nrf.any() and not timeout:
|
while not nrf.any() and not timeout:
|
||||||
if pyb.elapsed_millis(start_time) > 250:
|
if utime.ticks_diff(utime.ticks_ms(), start_time) > 250:
|
||||||
timeout = True
|
timeout = True
|
||||||
|
|
||||||
if timeout:
|
if timeout:
|
||||||
|
@ -51,11 +51,11 @@ def master():
|
||||||
got_millis, = struct.unpack('i', nrf.recv())
|
got_millis, = struct.unpack('i', nrf.recv())
|
||||||
|
|
||||||
# print response and round-trip delay
|
# print response and round-trip delay
|
||||||
print('got response:', got_millis, '(delay', pyb.millis() - got_millis, 'ms)')
|
print('got response:', got_millis, '(delay', utime.ticks_diff(utime.ticks_ms(), got_millis), 'ms)')
|
||||||
num_successes += 1
|
num_successes += 1
|
||||||
|
|
||||||
# delay then loop
|
# delay then loop
|
||||||
pyb.delay(250)
|
utime.sleep_ms(250)
|
||||||
|
|
||||||
print('master finished sending; successes=%d, failures=%d' % (num_successes, num_failures))
|
print('master finished sending; successes=%d, failures=%d' % (num_successes, num_failures))
|
||||||
|
|
||||||
|
@ -69,18 +69,19 @@ def slave():
|
||||||
print('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)')
|
print('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)')
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
pyb.wfi()
|
machine.idle()
|
||||||
if nrf.any():
|
if nrf.any():
|
||||||
while nrf.any():
|
while nrf.any():
|
||||||
buf = nrf.recv()
|
buf = nrf.recv()
|
||||||
millis, led_state = struct.unpack('ii', buf)
|
millis, led_state = struct.unpack('ii', buf)
|
||||||
print('received:', millis, led_state)
|
print('received:', millis, led_state)
|
||||||
for i in range(4):
|
for led in leds:
|
||||||
if led_state & (1 << i):
|
if led_state & 1:
|
||||||
pyb.LED(i + 1).on()
|
led.on()
|
||||||
else:
|
else:
|
||||||
pyb.LED(i + 1).off()
|
led.off()
|
||||||
pyb.delay(15)
|
led_state >>= 1
|
||||||
|
utime.sleep_ms(15)
|
||||||
|
|
||||||
nrf.stop_listening()
|
nrf.stop_listening()
|
||||||
try:
|
try:
|
||||||
|
@ -90,6 +91,12 @@ def slave():
|
||||||
print('sent response')
|
print('sent response')
|
||||||
nrf.start_listening()
|
nrf.start_listening()
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pyb
|
||||||
|
leds = [pyb.LED(i + 1) for i in range(4)]
|
||||||
|
except:
|
||||||
|
leds = []
|
||||||
|
|
||||||
print('NRF24L01 test module loaded')
|
print('NRF24L01 test module loaded')
|
||||||
print('NRF24L01 pinout for test:')
|
print('NRF24L01 pinout for test:')
|
||||||
print(' CE on Y4')
|
print(' CE on Y4')
|
||||||
|
|
Loading…
Reference in New Issue