|
| 1 | +# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +""" |
| 5 | +CircuitPython Feather RP2040 RFM69 Packet Receive Demo |
| 6 | +
|
| 7 | +This demo waits for a "button" packet. When the first packet is received, the NeoPixel LED |
| 8 | +lights up red. The next packet changes it to green. The next packet changes it to blue. |
| 9 | +Subsequent button packets cycle through the same colors in the same order. |
| 10 | +
|
| 11 | +This example is meant to be paired with the Packet Send Demo code running |
| 12 | +on a second Feather RP2040 RFM69 board. |
| 13 | +""" |
| 14 | + |
| 15 | +import board |
| 16 | +import digitalio |
| 17 | +import neopixel |
| 18 | +import adafruit_rfm69 |
| 19 | + |
| 20 | +# Set up NeoPixel. |
| 21 | +pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) |
| 22 | +pixel.brightness = 0.5 |
| 23 | + |
| 24 | +# Define the possible NeoPixel colors. You can add as many colors to this list as you like! |
| 25 | +# Simply follow the format shown below. Make sure you include the comma after the color tuple! |
| 26 | +color_values = [ |
| 27 | + (255, 0, 0), |
| 28 | + (0, 255, 0), |
| 29 | + (0, 0, 255), |
| 30 | +] |
| 31 | + |
| 32 | +# Define radio frequency in MHz. Must match your |
| 33 | +# module. Can be a value like 915.0, 433.0, etc. |
| 34 | +RADIO_FREQ_MHZ = 915.0 |
| 35 | + |
| 36 | +# Define Chip Select and Reset pins for the radio module. |
| 37 | +CS = digitalio.DigitalInOut(board.RFM_CS) |
| 38 | +RESET = digitalio.DigitalInOut(board.RFM_RST) |
| 39 | + |
| 40 | +# Initialise RFM69 radio |
| 41 | +rfm69 = adafruit_rfm69.RFM69(board.SPI(), CS, RESET, RADIO_FREQ_MHZ) |
| 42 | + |
| 43 | +color_index = 0 |
| 44 | +# Wait to receive packets. |
| 45 | +print("Waiting for packets...") |
| 46 | +while True: |
| 47 | + # Look for a new packet - wait up to 5 seconds: |
| 48 | + packet = rfm69.receive(timeout=5.0) |
| 49 | + # If no packet was received during the timeout then None is returned. |
| 50 | + if packet is not None: |
| 51 | + print("Received a packet!") |
| 52 | + # If the received packet is b'button'... |
| 53 | + if packet == b'button': |
| 54 | + # ...cycle the NeoPixel LED color through the color_values list. |
| 55 | + pixel.fill(color_values[color_index]) |
| 56 | + color_index = (color_index + 1) % len(color_values) |
0 commit comments