Commit 031d3888 authored by mz-fuzzy's avatar mz-fuzzy

Added possibility/demonstration of IRQ pin usage for RPi/Python.

Clean-up/fix of RF24 configuration examples in RPi/Python example.
Signed-off-by: default avatarmz-fuzzy <mzfuzzy800@gmail.com>
parent 72fe373e
......@@ -9,18 +9,17 @@
from __future__ import print_function
import time
from RF24 import *
import RPi.GPIO as GPIO
irq_gpio_pin = None
########### USER CONFIGURATION ###########
# See https://github.com/TMRh20/RF24/blob/master/RPi/pyRF24/readme.md
# CE Pin, CSN Pin, SPI Speed
# Setup for GPIO 22 CE and GPIO 25 CSN with SPI Speed @ 1Mhz
#radio = RF24(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_18, BCM2835_SPI_SPEED_1MHZ)
# Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
#radio = RF24(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ)
# Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 8Mhz
#radio = RF24(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ)
#RPi B
# Setup for GPIO 15 CE and CE1 CSN with SPI Speed @ 8Mhz
......@@ -28,9 +27,27 @@ from RF24 import *
#RPi B+
# Setup for GPIO 22 CE and CE0 CSN for RPi B+ with SPI Speed @ 8Mhz
radio = RF24(RPI_BPLUS_GPIO_J8_22, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ)
radio = RF24(RPI_BPLUS_GPIO_J8_15, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ)
# Setup for connected IRQ pin, GPIO 24 on RPi B+; uncomment to activate
#irq_gpio_pin = RPI_BPLUS_GPIO_J8_18
##########################################
def try_read_data(channel=0):
if radio.available():
while radio.available():
len = radio.getDynamicPayloadSize()
receive_payload = radio.read(len)
print('Got payload size={} value="{}"'.format(len, receive_payload.decode('utf-8')))
# First, stop listening so we can talk
radio.stopListening()
# Send the final one back.
radio.write(receive_payload)
print('Sent response.')
# Now, resume listening so we catch the next packets.
radio.startListening()
pipes = [0xF0F0F0F0E1, 0xF0F0F0F0D2]
min_payload_size = 4
......@@ -53,6 +70,12 @@ while (inp_role !='0') and (inp_role !='1'):
if inp_role == '0':
print('Role: Pong Back, awaiting transmission')
if irq_gpio_pin is not None:
# set up callback for irq pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(irq_gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(irq_gpio_pin, GPIO.FALLING, callback=try_read_data)
radio.openWritingPipe(pipes[1])
radio.openReadingPipe(1,pipes[0])
radio.startListening()
......@@ -103,22 +126,11 @@ while 1:
# Pong back role. Receive each packet, dump it out, and send it back
# if there is data ready
if radio.available():
while radio.available():
# Fetch the payload, and see if this was the last one.
len = radio.getDynamicPayloadSize()
receive_payload = radio.read(len)
# Spew it
print('Got payload size={} value="{}"'.format(len, receive_payload.decode('utf-8')))
# First, stop listening so we can talk
radio.stopListening()
# Send the final one back.
radio.write(receive_payload)
print('Sent response.')
# Now, resume listening so we catch the next packets.
radio.startListening()
if irq_gpio_pin is None:
# no irq pin is set up -> poll it
try_read_data()
else:
# callback routine set for irq pin takes care for reading -
# do nothing, just sleeps in order not to burn cpu by looping
time.sleep(1000)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment