From 031d3888e837fa2ef23e92e3923ddc10defbcbf8 Mon Sep 17 00:00:00 2001 From: mz-fuzzy Date: Mon, 25 Jan 2016 00:30:05 +0000 Subject: [PATCH] Added possibility/demonstration of IRQ pin usage for RPi/Python. Clean-up/fix of RF24 configuration examples in RPi/Python example. Signed-off-by: mz-fuzzy --- examples_RPi/pingpair_dyn.py | 60 +++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/examples_RPi/pingpair_dyn.py b/examples_RPi/pingpair_dyn.py index 0459ef4cb..76ea5141d 100755 --- a/examples_RPi/pingpair_dyn.py +++ b/examples_RPi/pingpair_dyn.py @@ -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 @@ #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 @@ 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 @@ # 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)