Skip to main content

An extension of RPi.GPIO to easily use interrupts on the Raspberry Pi

Project description

RPIO extends RPi.GPIO with easy interrupt handling.

Interrupts are used to receive notifications from the kernel when GPIO state changes occur. Advantages include minimized cpu consumption, very fast notification times, and the ability to trigger on specific edge transitions (‘rising|falling|both’). This is an example of how to use RPIO:

import logging
logging.basicConfig(level=logging.DEBUG)
import RPIO

def do_something(gpio_id, value):
    print("New value for GPIO %s: %s" % (gpio_id, value))

RPIO.add_interrupt_callback(17, do_something, edge='rising')
RPIO.add_interrupt_callback(18, do_something, edge='falling')
RPIO.add_interrupt_callback(19, do_something, edge='both')
RPIO.wait_for_interrupts()

If you want to receive a callback inside a Thread (which won’t block anything else on the system), set threaded_callback to True when adding an interrupt- callback. Here is an example:

RPIO.add_interrupt_callback(17, do_something, edge='rising',
        threaded_callback=True)

Make sure to double-check the value returned from the interrupt, since it is not necessarily corresponding to the edge (eg. 0 may come in as value, even if edge=”rising”). To remove all callbacks from a certain gpio pin, use RPIO.del_interrupt_callback(gpio_id). To stop the wait_for_interrupts() loop you can either set RPIO.is_waiting_for_interrupts to False, or call RPIO.stop_waiting_for_interrupts().

If an interrupt occurs while your callback function does something blocking (like time.sleep() outside a thread), events will not arrive until you release the block. Only one process can receive interrupts for a specific GPIO pin, since the read on /sys/class/gpio/gpio<N>/value destroys the value for subsequent reads. RPIO uses epoll to receive interrupts from the kernel.

RPi.GPIO

You can use all of RPi.GPIO’s functionality through RPIO. Note that RPIO uses GPIO.BCM pin numbering by default

import RPIO

# set up GPIO output channel
RPIO.setup(17, RPIO.OUT)

# set gpio 17 to high
RPIO.output(17, RPIO.HIGH)

# set up output channel with an initial state
RPIO.setup(18, RPIO.OUT, initial=RPIO.LOW)

# set up input channel with pull-up control
#   (pull_up_down be PUD_OFF, PUD_UP or PUD_DOWN, default PUD_OFF)
RPIO.setup(19, RPIO.IN, pull_up_down=RPIO.PUD_UP)

# read input from gpio 19
input_value = RPIO.input(19)

# set up rising edge detection (EXPERIMENTAL)
set_rising_event(19)

# check for an event (EXPERIMENTAL)
if RPIO.event_detected(19):
    print('Rising edge detected!')

# set up falling edge detection (EXPERIMENTAL)
RPIO.set_rising_event(19, enable=False)  # disable rising edge detection (as set above)
RPIO.set_falling_event(19)

# set up high detection (EXPERIMENTAL)
RPIO.set_falling_event(19, enable=False)  # disable falling edge detection (as set above)
RPIO.set_high_event(19)

# set up low detection (EXPERIMENTAL)
RPIO.set_high_event(19, enable=False)  # disable high detection (as set above)
RPIO.set_low_event(19)

# to change to BCM RPIO numbering
RPIO.setmode(RPIO.BCM)

# to reset every channel that has been set up by this program to INPUT with no pullup/pulldown and no event detection
RPIO.cleanup()

Feedback

Chris Hager (<chris@linuxuser.at>)

License

RPIO is free software, released under the MIT license.

Copyright (c) 2013 Chris Hager <chris@linuxuser.at>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

RPIO-0.1.7.tar.gz (5.8 kB view details)

Uploaded Source

Built Distributions

RPIO-0.1.7-py3.2.egg (9.5 kB view details)

Uploaded Egg

RPIO-0.1.7-py2.7.egg (9.4 kB view details)

Uploaded Egg

RPIO-0.1.7-py2.6.egg (9.5 kB view details)

Uploaded Egg

File details

Details for the file RPIO-0.1.7.tar.gz.

File metadata

  • Download URL: RPIO-0.1.7.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for RPIO-0.1.7.tar.gz
Algorithm Hash digest
SHA256 4b9281aa8b758917fafad68c630f0098492aa235b53e021e35df787bea7a1090
MD5 c7e2b356b8147d4f505ab3221d6aaf7d
BLAKE2b-256 a6b8a19771433e67f680a79df13eb1449304f239a63eaa3c84fbbf57db9bb5ad

See more details on using hashes here.

File details

Details for the file RPIO-0.1.7-py3.2.egg.

File metadata

  • Download URL: RPIO-0.1.7-py3.2.egg
  • Upload date:
  • Size: 9.5 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for RPIO-0.1.7-py3.2.egg
Algorithm Hash digest
SHA256 9ba2c13b921ad515cda96540e70fe6ecaa3f4901c9754046d63643d1141bd387
MD5 88ddd6ac7693e9f4967dc0c64b8f3aa3
BLAKE2b-256 b327486dc024fd23d5c2480fe1b61e290eabd26da8e0edae0b9e5771b0484902

See more details on using hashes here.

File details

Details for the file RPIO-0.1.7-py2.7.egg.

File metadata

  • Download URL: RPIO-0.1.7-py2.7.egg
  • Upload date:
  • Size: 9.4 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for RPIO-0.1.7-py2.7.egg
Algorithm Hash digest
SHA256 883136910b61bdfc1bd1429cd5909ad59ed5db46deb946473574255711b66f4e
MD5 8fa4088ef665049ad9aeafd16a88f15e
BLAKE2b-256 a9cfcbffcd1e13a35189cf1f3e69eb973e9ad04a26df02e44fe2db279ec27bfa

See more details on using hashes here.

File details

Details for the file RPIO-0.1.7-py2.6.egg.

File metadata

  • Download URL: RPIO-0.1.7-py2.6.egg
  • Upload date:
  • Size: 9.5 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for RPIO-0.1.7-py2.6.egg
Algorithm Hash digest
SHA256 3c83a9712c93b31f1b8ca953f8993efc3acb1e5940217fff38ade9f43dab053c
MD5 05a798ff7e51662e6e0e87941900c9c3
BLAKE2b-256 e0c8116b689e04f42835f8d4e55149de9b1018465ba312a07cbb4c3bad4b464f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page