SDL2

import micropython from pygame

Jul 15th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.86 KB | None | 0 0
  1. """Test for nrf24l01 module.  Portable between MicroPython targets."""
  2.  
  3. import sys
  4. import ustruct as struct
  5. import utime
  6. from machine import Pin, SPI
  7. from nrf24l01 import NRF24L01
  8. from micropython import const
  9.  
  10. # Slave pause between receiving data and checking for further packets.
  11. _RX_POLL_DELAY = const(15)
  12. # Slave pauses an additional _SLAVE_SEND_DELAY ms after receiving data and before
  13. # transmitting to allow the (remote) master time to get into receive mode. The
  14. # master may be a slow device. Value tested with Pyboard, ESP32 and ESP8266.
  15. _SLAVE_SEND_DELAY = const(10)
  16.  
  17. if sys.platform == "pyboard":
  18.     cfg = {"spi": 2, "miso": "Y7", "mosi": "Y8", "sck": "Y6", "csn": "Y5", "ce": "Y4"}
  19. elif sys.platform == "esp8266":  # Hardware SPI
  20.     cfg = {"spi": 1, "miso": 12, "mosi": 13, "sck": 14, "csn": 4, "ce": 5}
  21. elif sys.platform == "esp32":  # Software SPI
  22.     cfg = {"spi": -1, "miso": 32, "mosi": 33, "sck": 25, "csn": 26, "ce": 27}
  23. else:
  24.     raise ValueError("Unsupported platform {}".format(sys.platform))
  25.  
  26. # Addresses are in little-endian format. They correspond to big-endian
  27. # 0xf0f0f0f0e1, 0xf0f0f0f0d2
  28. pipes = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd2\xf0\xf0\xf0\xf0")
  29.  
  30.  
  31. def master():
  32.     csn = Pin(cfg["csn"], mode=Pin.OUT, value=1)
  33.     ce = Pin(cfg["ce"], mode=Pin.OUT, value=0)
  34.     if cfg["spi"] == -1:
  35.         spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"]))
  36.         nrf = NRF24L01(spi, csn, ce, payload_size=8)
  37.     else:
  38.         nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8)
  39.  
  40.     nrf.open_tx_pipe(pipes[0])
  41.     nrf.open_rx_pipe(1, pipes[1])
  42.     nrf.start_listening()
  43.  
  44.     num_needed = 16
  45.     num_successes = 0
  46.     num_failures = 0
  47.     led_state = 0
  48.  
  49.     print("NRF24L01 master mode, sending %d packets..." % num_needed)
  50.  
  51.     while num_successes < num_needed and num_failures < num_needed:
  52.         # stop listening and send packet
  53.         nrf.stop_listening()
  54.         millis = utime.ticks_ms()
  55.         led_state = max(1, (led_state << 1) & 0x0F)
  56.         print("sending:", millis, led_state)
  57.         try:
  58.             nrf.send(struct.pack("ii", millis, led_state))
  59.         except OSError:
  60.             pass
  61.  
  62.         # start listening again
  63.         nrf.start_listening()
  64.  
  65.         # wait for response, with 250ms timeout
  66.         start_time = utime.ticks_ms()
  67.         timeout = False
  68.         while not nrf.any() and not timeout:
  69.             if utime.ticks_diff(utime.ticks_ms(), start_time) > 250:
  70.                 timeout = True
  71.  
  72.         if timeout:
  73.             print("failed, response timed out")
  74.             num_failures += 1
  75.  
  76.         else:
  77.             # recv packet
  78.             (got_millis,) = struct.unpack("i", nrf.recv())
  79.  
  80.             # print response and round-trip delay
  81.             print(
  82.                 "got response:",
  83.                 got_millis,
  84.                 "(delay",
  85.                 utime.ticks_diff(utime.ticks_ms(), got_millis),
  86.                 "ms)",
  87.             )
  88.             num_successes += 1
  89.  
  90.         # delay then loop
  91.         utime.sleep_ms(250)
  92.  
  93.     print("master finished sending; successes=%d, failures=%d" % (num_successes, num_failures))
  94.  
  95.  
  96. def slave():
  97.     csn = Pin(cfg["csn"], mode=Pin.OUT, value=1)
  98.     ce = Pin(cfg["ce"], mode=Pin.OUT, value=0)
  99.     if cfg["spi"] == -1:
  100.         spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"]))
  101.         nrf = NRF24L01(spi, csn, ce, payload_size=8)
  102.     else:
  103.         nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8)
  104.  
  105.     nrf.open_tx_pipe(pipes[1])
  106.     nrf.open_rx_pipe(1, pipes[0])
  107.     nrf.start_listening()
  108.  
  109.     print("NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)")
  110.  
  111.     while True:
  112.         if nrf.any():
  113.             while nrf.any():
  114.                 buf = nrf.recv()
  115.                 millis, led_state = struct.unpack("ii", buf)
  116.                 print("received:", millis, led_state)
  117.                 for led in leds:
  118.                     if led_state & 1:
  119.                         led.on()
  120.                     else:
  121.                         led.off()
  122.                     led_state >>= 1
  123.                 utime.sleep_ms(_RX_POLL_DELAY)
  124.  
  125.             # Give master time to get into receive mode.
  126.             utime.sleep_ms(_SLAVE_SEND_DELAY)
  127.             nrf.stop_listening()
  128.             try:
  129.                 nrf.send(struct.pack("i", millis))
  130.             except OSError:
  131.                 pass
  132.             print("sent response")
  133.             nrf.start_listening()
  134.  
  135.  
  136. try:
  137.     import pyb
  138.  
  139.     leds = [pyb.LED(i + 1) for i in range(4)]
  140. except:
  141.     leds = []
  142.  
  143. print("NRF24L01 test module loaded")
  144. print("NRF24L01 pinout for test:")
  145. print("    CE on", cfg["ce"])
  146. print("    CSN on", cfg["csn"])
  147. print("    SCK on", cfg["sck"])
  148. print("    MISO on", cfg["miso"])
  149. print("    MOSI on", cfg["mosi"])
  150. print("run nrf24l01test.slave() on slave, then nrf24l01test.master() on master")
Add Comment
Please, Sign In to add comment