drivers/nrf24l01: Make nRF24L01 test script more portable.

crypto-aes
Damien George 5 years ago
parent 1394258f37
commit 54acd0b0f0
  1. 33
      drivers/nrf24l01/nrf24l01test.py

@ -1,8 +1,8 @@
"""Test for nrf24l01 module."""
import struct
import pyb
from pyb import Pin, SPI
import utime
from machine import Pin, SPI
from nrf24l01 import NRF24L01
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:
# stop listening and send packet
nrf.stop_listening()
millis = pyb.millis()
millis = utime.ticks_ms()
led_state = max(1, (led_state << 1) & 0x0f)
print('sending:', millis, led_state)
try:
@ -36,10 +36,10 @@ def master():
nrf.start_listening()
# wait for response, with 250ms timeout
start_time = pyb.millis()
start_time = utime.ticks_ms()
timeout = False
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
if timeout:
@ -51,11 +51,11 @@ def master():
got_millis, = struct.unpack('i', nrf.recv())
# 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
# delay then loop
pyb.delay(250)
utime.sleep_ms(250)
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)')
while True:
pyb.wfi()
machine.idle()
if nrf.any():
while nrf.any():
buf = nrf.recv()
millis, led_state = struct.unpack('ii', buf)
print('received:', millis, led_state)
for i in range(4):
if led_state & (1 << i):
pyb.LED(i + 1).on()
for led in leds:
if led_state & 1:
led.on()
else:
pyb.LED(i + 1).off()
pyb.delay(15)
led.off()
led_state >>= 1
utime.sleep_ms(15)
nrf.stop_listening()
try:
@ -90,6 +91,12 @@ def slave():
print('sent response')
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 pinout for test:')
print(' CE on Y4')

Loading…
Cancel
Save