Skip to content

Commit

Permalink
Added possibility/demonstration of IRQ pin usage for RPi/Python.
Browse files Browse the repository at this point in the history
Clean-up/fix of RF24 configuration examples in RPi/Python example.

Signed-off-by: mz-fuzzy <mzfuzzy800@gmail.com>
  • Loading branch information
martin-mat committed Jan 25, 2016
1 parent 72fe373 commit 031d388
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions examples_RPi/pingpair_dyn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,45 @@
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
#radio = RF24(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ)

#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
Expand All @@ -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()
Expand Down Expand Up @@ -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)

1 comment on commit 031d388

@martin-mat
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.