Skip to content

Commit

Permalink
Cleanup RPi Interrupt example, addresses adafruit#36
Browse files Browse the repository at this point in the history
  • Loading branch information
mtai committed May 30, 2023
1 parent c977cbc commit befa74b
Showing 1 changed file with 41 additions and 42 deletions.
83 changes: 41 additions & 42 deletions examples/rfm69_rpi_interrupt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,6 @@
import RPi.GPIO as io
import adafruit_rfm69


# setup interrupt callback function
def rfm69_callback(rfm69_irq):
global packet_received # pylint: disable=global-statement
print(
"IRQ detected on pin {0} payload_ready {1} ".format(
rfm69_irq, rfm69_client.payload_ready
)
)
# see if this was a payload_ready interrupt ignore if not
if rfm69_client.payload_ready:
packet = rfm69_client.receive(timeout=None)
if packet is not None:
# Received a packet!
packet_received = True
print_packet(packet)


# Define radio parameters.
RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your
# module! Can be a value like 915.0, 433.0, etc.
Expand All @@ -37,6 +19,33 @@ def rfm69_callback(rfm69_irq):
RFM69_CS = board.CE1
RFM69_RESET = board.D25

SRC_ADDRESS = adafruit_rfm69.RH_BROADCAST_ADDRESS
DEST_ADDRESS = adafruit_rfm69.RH_BROADCAST_ADDRESS

def init_client():
# Initialize SPI device.
in_spi = board.SPI()
in_spi_device = adafruit_rfm69.RFM69.spi_device(in_spi, RFM69_CS)

# Initialze RFM radio
rfm69_client = adafruit_rfm69.RFM69(in_spi_device, RFM69_IRQ, RFM69_RESET, RADIO_FREQ_MHZ)
rfm69_client.address = SRC_ADDRESS


# Optionally set an encryption key (16 byte AES key). MUST match both
# on the transmitter and receiver (or be set to None to disable/the default).
rfm69_client.set_encryption_key(
b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
)

print("===== Initializing RFM69 client =====")
print(f"Temperature: {rfm69_client.get_temperature()}C")
print(f"Frequency: {rfm69_client.get_frequency_mhz()}mhz")
print(f"Bit rate: {rfm69_client.get_bitrate() / 1000}kbit/s")
print(f"Frequency deviation: {rfm69_client.get_frequency_deviation()}hz")
return rfm69_client


def print_packet(packet):
if not packet:
return
Expand All @@ -52,44 +61,33 @@ def print_packet(packet):
print(f"RSSI: {packet.rssi}")
print(f"Time: {packet.time}")

# Initialize SPI device.
in_spi = board.SPI()
in_spi_device = adafruit_rfm69.RFM69.spi_device(in_spi, RFM69_CS)
rfm69_client = init_client()

# Initialze RFM radio
rfm69_client = adafruit_rfm69.RFM69(in_spi_device, RFM69_IRQ, RFM69_RESET, RADIO_FREQ_MHZ)
# Magic incantation that tells us we can support interrupts
rfm69_client.platform_supports_interrupts = True

# Optionally set an encryption key (16 byte AES key). MUST match both
# on the transmitter and receiver (or be set to None to disable/the default).
rfm69_client.set_encryption_key(
b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
)

# Print out some chip state:
print(f"Temperature: {rfm69_client.get_temperature()}C")
print(f"Frequency: {rfm69_client.get_frequency_mhz()}mhz")
print(f"Bit rate: {rfm69_client.get_bitrate() / 1000}kbit/s")
print(f"Frequency deviation: {rfm69_client.get_frequency_deviation()}hz")
# setup interrupt callback function
def rfm69_callback(rfm69_irq):
print(f"IRQ detected on pin {rfm69_irq}")
rfm69_client.handle_interrupt()

# configure the interrupt pin and event handling.
io.setmode(io.BCM)
io.setup(RFM69_IRQ, io.IN, pull_up_down=io.PUD_DOWN) # activate input
io.add_event_detect(RFM69_IRQ, io.RISING)
io.add_event_callback(RFM69_IRQ, rfm69_callback)
packet_received = False

# Send a packet. Note you can only send a packet up to 60 bytes in length.
# This is a limitation of the radio packet size, so if you need to send larger
# amounts of data you will need to break it into smaller send calls. Each send
# call will wait for the previous one to finish before continuing.
rfm69_client.send(
adafruit_rfm69.RH_BROADCAST_ADDRESS,
DEST_ADDRESS,
bytes("Hello world!\r\n", "utf-8")
)
rfm69_client.set_mode_rx()
print("Sent hello world message!")
# If you don't wawnt to send a message to start you can just start lintening
# rmf69.listen()
# Just start listening
rfm69_client.available()

# Wait to receive packets. Note that this library can't receive data at a fast
# rate, in fact it can only receive and process one 60 byte packet at a time.
Expand All @@ -100,9 +98,10 @@ def print_packet(packet):
# the loop is where you can do any desire processing
# the global variable packet_received can be used to determine if a packet was received.
while True:
# the sleep time is arbitrary since any incomming packe will trigger an interrupt
# the sleep time is arbitrary since any incoming packet will trigger an interrupt
# and be received.
time.sleep(0.1)
if packet_received:
if rfm69_client.rx_packet:
print("received message!")
packet_received = False
print_packet(rfm69_client.rx_packet)
rfm69_client.rx_packet = None

0 comments on commit befa74b

Please sign in to comment.