gpio¶
GPIO abstraction for Raspberry Pi pin control.
This module provides a thin, simplified abstraction layer over the
RPi.GPIO library for use across Oizom hardware drivers. It exposes
helpers for pin configuration, digital read/write, cleanup, and selection
of channels on the on-board I2C multiplexer.
A small in-process registry (_pin_modes) tracks the most recently
applied direction ("in" / "out") per BCM pin so that callers can
invoke read() or set() without an explicit prior setup()
call; the appropriate direction will be configured lazily on first use.
Example
Configure a pin and toggle its output level:
>>> from drivers.gpio import gpio
>>> gpio.setup(17, gpio.OUT)
>>> gpio.set(17, True)
>>> gpio.read(4)
True
Note
The module initializes
RPi.GPIOin BCM numbering mode (GPIO.setmode(GPIO.BCM)) at import time. Usesetmode()to switch toGPIO.BOARDnumbering if a different scheme is required, but be aware that all pin numbers throughout this module are assumed to be BCM.Internal pull-up / pull-down resistors are disabled by default for inputs. Pass
pullup=Trueorpullup=Falsetosetup()to explicitly engage the SoC’s internal pull resistor.Runtime warnings emitted by
RPi.GPIOare silenced at import time viaGPIO.setwarnings(False); re-enable them withsetwarnings()during debugging if needed.
See also
RPi.GPIOUnderlying low-level GPIO driver wrapped by this module.
Attributes¶
Functions¶
|
Release GPIO resources held by this process. |
|
Read the logic level of a GPIO pin (alias for |
|
Return the last-configured direction for a pin, if any. |
|
Drive a GPIO pin HIGH or LOW (alias for |
|
Read the current logic level of a GPIO pin. |
|
Select a channel on the Oizom I2C multiplexer. |
|
Drive a GPIO pin HIGH or LOW, auto-configuring it as output if needed. |
|
Select the pin numbering scheme used by |
|
Configure a GPIO pin as input or output. |
|
Enable or disable runtime warnings emitted by |
Module Contents¶
- gpio.cleanup(pin: int | None = None, assert_exists: bool = False) None¶
Release GPIO resources held by this process.
Wraps
RPi.GPIO.cleanup()and synchronizes the local_pin_modesregistry by either clearing it entirely (whenpin is None) or removing a single entry.- Parameters:
pin (
int | None) – BCM GPIO pin number to release, orNoneto release all pins configured through this process. Defaults toNone.assert_exists (
bool) – Reserved flag retained for API compatibility with prior versions; it is not consulted by the current implementation. Defaults toFalse.
- Returns:
This function performs hardware cleanup and returns nothing.
- Return type:
None
- Raises:
Exception – Any exception raised by
RPi.GPIOis caught and logged as a warning via the context logger; nothing is re-raised.
Example
Release a single pin or perform a global teardown at exit:
>>> cleanup(17) >>> cleanup()
Note
Calling
cleanup()with no arguments resets every pin previously configured in this process to a safe input state. It does not affect pins held by other processes. It is good practice to invoke this from anatexithandler or signal handler in long-running applications.See also
RPi.GPIO.cleanup()
- gpio.input(pin: int) bool¶
Read the logic level of a GPIO pin (alias for
read()).This thin wrapper exists so that callers familiar with the
RPi.GPIOAPI (GPIO.input) can use the same name when operating through this module.- Parameters:
pin (
int) – BCM GPIO pin number to sample.- Returns:
Truewhen the pin is at logic HIGH,Falsewhen it is at logic LOW. Propagates the return value ofread().- Return type:
- Raises:
Exception – Any exception raised by
RPi.GPIOis caught and logged insideread(); nothing is re-raised here.
Example
Drop-in replacement for
GPIO.input:>>> level = input(17)
Note
This function shadows the Python built-in
input(). Inside modules that need both, alias the import (for example,from drivers.gpio import gpioand usegpio.input).See also
read()RPi.GPIO.input()
- gpio.mode(pin: int) str | None¶
Return the last-configured direction for a pin, if any.
Looks up the pin in the module-private
_pin_modesregistry, which is populated bysetup()(called either directly by the user or implicitly byset()/read()).- Parameters:
pin (
int) – BCM GPIO pin number to query.- Returns:
IN("in") orOUT("out") if the pin has been configured through this module, otherwiseNone.- Return type:
str | None
- Raises:
None – This is a pure dictionary lookup and does not raise.
Example
Inspect whether a pin has been configured yet:
>>> mode(17) 'out' >>> mode(99)
Note
The registry is local to this module. Pins configured by other libraries acting on
RPi.GPIOdirectly will not appear here even though they may be live on the SoC.
- gpio.output(pin: int, value: bool) None¶
Drive a GPIO pin HIGH or LOW (alias for
set()).This thin wrapper exists so that callers familiar with the
RPi.GPIOAPI (GPIO.output) can use the same name when operating through this module.- Parameters:
- Returns:
This function delegates to
set()and returns nothing.- Return type:
None
- Raises:
Exception – Any exception raised by
RPi.GPIOis caught and logged insideset(); nothing is re-raised here.
Example
Drop-in replacement for
GPIO.output:>>> output(17, True)
Note
Auto-configures the pin as output via
setup()if it has not been seen before. Pin numbers must be BCM.See also
set()RPi.GPIO.output()
- gpio.read(pin: int) bool¶
Read the current logic level of a GPIO pin.
If the pin has not yet been configured as an input in the local
_pin_modesregistry,setup()is invoked first withmode=IN(and no pull resistor) before sampling.- Parameters:
pin (
int) – BCM GPIO pin number to sample.- Returns:
Truewhen the pin is at logic HIGH,Falsewhen it is at logic LOW. If sampling fails the function returnsNoneimplicitly after logging the error.- Return type:
- Raises:
Exception – Any exception raised by
RPi.GPIOis caught and logged via the context logger; nothing is re-raised.
Example
Sample a button connected to pin 4 with the internal pull-up engaged elsewhere:
>>> pressed = not read(4)
- gpio.select_I2C(pn: int = 24, ch: int = -1) None¶
Select a channel on the Oizom I2C multiplexer.
The Oizom hardware uses a 4-channel I2C mux whose two address-select lines are wired to BCM GPIO 24 and 25. This function maps either a sensor part-number (
pn) or an explicit channel index (ch) to the appropriate two-bit address and drives the lines throughset(). A 100 ms settling delay follows the switch so that downstreamsmbustransactions see a stable bus.Channel mapping (
GPIO24,GPIO25):Channel 0 -> (0, 0): part numbers 0, 18, 23, 24, 29, 53, 54
Channel 1 -> (1, 0): part numbers 1, 32, 33, 34, 35, 36, 38, 39
Channel 2 -> (0, 1): part numbers 2, 25, 26, 27, 28
Channel 3 -> (1, 1): part numbers 3, 4
- Parameters:
- Returns:
This function performs side effects on the GPIO bus and returns nothing.
- Return type:
None
- Raises:
Exception – Any exception raised by
RPi.GPIOor by the inner calls toset()is caught and logged via the context logger; nothing is re-raised.
Example
Switch the mux to channel 2 before reading sensor 25:
>>> select_I2C(25) >>> select_I2C(ch=2)
Note
The 100 ms
time.sleep(0.1)after switching is empirically tuned for the Oizom mux and matches what other drivers in this repository expect; do not shorten it without retesting the full sensor stack.If
pndoes not match any known mapping andchis left at its default, the address lines are not changed and the mux remains on its current channel; only the settling delay is incurred.GPIO 24 and 25 are reserved for this mux and should not be re-used by other drivers.
See also
set()RPi.GPIO.output()
- gpio.set(pin: int, value: bool) None¶
Drive a GPIO pin HIGH or LOW, auto-configuring it as output if needed.
If the pin has not yet been configured as an output in the local
_pin_modesregistry,setup()is invoked first withmode=OUTbefore the value is written.- Parameters:
- Returns:
This function performs a side effect on hardware and returns nothing.
- Return type:
None
- Raises:
Exception – Any exception raised by
RPi.GPIOis caught and logged via the context logger; nothing is re-raised.
Example
Drive pin 18 HIGH:
>>> set(18, True)
Note
This helper shadows the Python built-in
setinside the module namespace. Importers may want to alias it (for example,from drivers.gpio import gpioand then callgpio.set(...)) to avoid the name clash. The auto-configuration behaviour assumes BCM pin numbering.
- gpio.setmode(value: int) None¶
Select the pin numbering scheme used by
RPi.GPIO.The two supported schemes are:
GPIO.BCM– Broadcom SoC channel numbers (default for this module). Pin numbers match the chip-level GPIO names.GPIO.BOARD– physical header pin numbers (1-40 on a standard 40-pin Pi header).
- Parameters:
value (
int) – One of the numbering-mode constants exposed byRPi.GPIO–GPIO.BCMorGPIO.BOARD.- Returns:
This function configures the underlying library and returns nothing.
- Return type:
None
- Raises:
ValueError – Raised by
RPi.GPIOif the mode is invalid or conflicts with a previously-set mode in the same process.
Example
Switch the process to BOARD numbering before configuring pins:
>>> import RPi.GPIO as GPIO >>> setmode(GPIO.BOARD)
Note
All other helpers in this module assume BCM numbering. If you call this function with
GPIO.BOARDyou must use header pin numbers consistently for every subsequent call. Switching modes mid-program is not supported byRPi.GPIO.See also
RPi.GPIO.setmode()
- gpio.setup(pin: int, mode: str, pullup: bool | None = None, initial: bool = False) None¶
Configure a GPIO pin as input or output.
Delegates to
RPi.GPIO.setup()and records the chosen direction in the internal_pin_modesregistry so that later calls toread()orset()on the same pin can skip re-configuration.- Parameters:
pin (
int) – BCM GPIO pin number to configure.mode (
str) – Direction to apply. Must be one ofIN("in") for input orOUT("out") for output.pullup (
bool | None) – Optional pull resistor selection for inputs.Trueengages the internal pull-up resistor,Falseengages the internal pull-down resistor, andNone(default) leaves the pin floating. Ignored whenmodeis"out".initial (
bool) – Initial output level applied whenmodeis"out".Truedrives the pin HIGH,Falsedrives it LOW. Defaults toFalse(LOW).
- Returns:
This function configures hardware state and returns nothing.
- Return type:
None
- Raises:
ValueError – If
modeis neither"in"nor"out". The exception is logged via the context logger rather than propagated to the caller.
Example
Configure pin 17 as an output initially driven LOW, and pin 4 as an input with internal pull-up enabled:
>>> setup(17, OUT, initial=False) >>> setup(4, IN, pullup=True)
Note
Pin numbers are interpreted in BCM numbering. If
setmode()has been used to switch toGPIO.BOARDnumbering, callers must supply BOARD pin numbers instead.See also
RPi.GPIO.setup()
- gpio.setwarnings(value: bool) None¶
Enable or disable runtime warnings emitted by
RPi.GPIO.Forwards directly to
RPi.GPIO.setwarnings(). When warnings are enabled,RPi.GPIOprints a message if a pin is being configured that was already in use by another process or was not properly cleaned up previously.- Parameters:
value (
bool) –Trueto enable warnings,Falseto suppress them.- Returns:
This function configures the underlying library and returns nothing.
- Return type:
None
- Raises:
None – This is a thin pass-through and does not raise on its own.
Example
Re-enable warnings temporarily for debugging:
>>> setwarnings(True)
Note
At import time this module calls
GPIO.setwarnings(False)to keep production logs clean. Toggling this back toTrueis typically only useful during development.See also
RPi.GPIO.setwarnings()
- gpio._pin_modes¶
- gpio.basic_logger¶
- gpio.context_logger¶