piripherals.util module

utility functions and classes

class piripherals.util.IRQHandler(pin, callback, edge=0, pullup=1)[source]

Bases: object

Abstraction of an IRQ handler.

An edge on the IRQ pin sets a flag. The callback is invoked on a separate thread when the flag was set. The flag is reset when the pin is high again. The callback may be invoked repeatedly if the pin does not reset. So you have to reset the IRQ inside the callback, such that when the callback returns, the IRQ line is high again.

This uses RPi.GPIO internally.

Parameters:
  • pin (int) – BCM pin number attached to IRQ line. 0 disables use of GPIO pins, call interrupt explicitly
  • callback – function invoked on IRQ.
  • edge (int) – fire interrupt on falling=0 or rising=1 edge. 1 inverts the logic, so IRQ is considered reset when low.
  • pullup (int) – activate internal pullup 1=pullup, 0=nothing, -1=pulldown.
interrupt(*a, **kw)[source]

fire interrupt

All arguments are ignored.

class piripherals.util.Poller(callback, delay=0.01)[source]

Bases: object

Polling loop as replacement for IRQHandler.

Use it if using the IRQ line is not possible or desired.

Parameters:
  • callcack – function that is called continously. The actuall polling happens in this callback.
  • delay (float) – delay in seconds between invocations of callback
piripherals.util.fork(func)[source]

run func asynchronously in a DaemonThread

piripherals.util.not_raising(func)[source]

Wraps a function and swallows exceptions.

Parameters:func – function to wrap
Returns:wrapped function, that does not raise Exceptions, Exceptions are printed to console
piripherals.util.on_change(file, callback, delay=1, forking=1)[source]
piripherals.util.noop(*x)