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.6.tar.gz (5.8 kB view details)

Uploaded Source

Built Distributions

RPIO-0.1.6-py3.2.egg (15.7 kB view details)

Uploaded Egg

RPIO-0.1.6-py2.7.egg (15.6 kB view details)

Uploaded Egg

RPIO-0.1.6-py2.6.egg (15.8 kB view details)

Uploaded Egg

File details

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

File metadata

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

File hashes

Hashes for RPIO-0.1.6.tar.gz
Algorithm Hash digest
SHA256 a2ca1b1a72e027046ba808ed6c91ffeb06c81a8dd45d9c9d50b62e2aeac522cb
MD5 4c2c29e72af3a47b3a8e4194009ac48a
BLAKE2b-256 e07c43ce65da9c0bb6e02f32238a3ee9377bc1edd610cd46e7f37ff426fbea9c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for RPIO-0.1.6-py3.2.egg
Algorithm Hash digest
SHA256 5d1d9ea721b9f1e88a24ee4391d0cf04088428805c4a5e0f92a155504e1f2fa2
MD5 647d7135650c8443353bfe85e794d337
BLAKE2b-256 379306efc0a185abe22026e0d84980028c79178456e1532be7f0b08dd882547c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for RPIO-0.1.6-py2.7.egg
Algorithm Hash digest
SHA256 74815a2143aec900a3456a4b5de6dc121dda8cb022be3b64ca93f118912452f0
MD5 df73de56f8cbc0fb72cd37e5e417cbf1
BLAKE2b-256 6e775f33784fc91ea6bfd15f12d2696116b03515f3b94431d16559fccd7498d0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for RPIO-0.1.6-py2.6.egg
Algorithm Hash digest
SHA256 3f80bf687877e54b1963e21e13fcf7cadd94e12e330f4036239a88e682c10351
MD5 b9074a3e1182be85c74a006651b94490
BLAKE2b-256 26029763a77d2605daa3218a35a84d1e3746aeb7b317f7b78ed14d62377008d8

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