CircuitPython APIs for non-CircuitPython versions of Python such as CPython on Linux and MicroPython.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
GPIO pin control with direction configuration, pull resistors, and drive modes. Provides CircuitPython-compatible digital I/O operations with automatic resource management and context manager support.
The DigitalInOut class provides comprehensive GPIO pin control with CircuitPython-compatible behavior. Supports automatic cleanup through context managers and proper resource management.
class DigitalInOut(ContextManaged):
def __init__(self, pin):
"""
Initialize digital I/O pin.
Args:
pin: Board pin object (from board module)
"""
@property
def direction(self) -> Direction:
"""Get or set pin direction (INPUT or OUTPUT)"""
@direction.setter
def direction(self, value: Direction) -> None:
"""Set pin direction"""
@property
def value(self) -> bool:
"""
Get or set pin logic level.
Returns:
bool: True for HIGH (3.3V/5V), False for LOW (0V)
"""
@value.setter
def value(self, val: bool) -> None:
"""Set pin logic level"""
@property
def pull(self) -> Pull:
"""Get or set pull resistor configuration"""
@pull.setter
def pull(self, val: Pull) -> None:
"""Configure pull resistor (UP, DOWN, or None)"""
@property
def drive_mode(self) -> DriveMode:
"""Get or set output drive mode"""
@drive_mode.setter
def drive_mode(self, val: DriveMode) -> None:
"""Set output drive mode (PUSH_PULL or OPEN_DRAIN)"""
def switch_to_output(self, value: bool = False, drive_mode: DriveMode = DriveMode.PUSH_PULL) -> None:
"""
Switch pin to output mode with initial value and drive mode.
Args:
value: Initial output value (default False)
drive_mode: Output drive mode (default PUSH_PULL)
"""
def switch_to_input(self, pull: Pull = None) -> None:
"""
Switch pin to input mode with optional pull resistor.
Args:
pull: Pull resistor configuration (UP, DOWN, or None)
"""
def deinit(self) -> None:
"""Release pin resources"""Pin direction configuration constants for input and output modes.
class Direction:
INPUT: Direction # Configure pin as input
OUTPUT: Direction # Configure pin as outputPull resistor configuration constants for input pins.
class Pull:
UP: Pull # Enable internal pull-up resistor
DOWN: Pull # Enable internal pull-down resistorOutput drive mode constants for output pins.
class DriveMode:
PUSH_PULL: DriveMode # Standard push-pull output (default)
OPEN_DRAIN: DriveMode # Open-drain output (requires external pull-up)import board
import digitalio
import time
# Setup LED on pin D18
led = digitalio.DigitalInOut(board.D18)
led.direction = digitalio.Direction.OUTPUT
# Blink LED
for i in range(10):
led.value = True # Turn on
time.sleep(0.5)
led.value = False # Turn off
time.sleep(0.5)
# Cleanup
led.deinit()import board
import digitalio
# Setup button with pull-up resistor
button = digitalio.DigitalInOut(board.D2)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
# Read button state (False when pressed due to pull-up)
if not button.value:
print("Button pressed!")
# Using context manager for automatic cleanup
with digitalio.DigitalInOut(board.D2) as button:
button.switch_to_input(pull=digitalio.Pull.UP)
print(f"Button state: {button.value}")import board
import digitalio
# Setup open-drain output (for I2C-style communication)
signal = digitalio.DigitalInOut(board.D5)
signal.switch_to_output(value=True, drive_mode=digitalio.DriveMode.OPEN_DRAIN)
# Signal communication
signal.value = False # Pull line low
time.sleep(0.001)
signal.value = True # Release line (pulled high by external resistor)import board
import digitalio
# Setup multiple LEDs
leds = []
for pin in [board.D18, board.D19, board.D20, board.D21]:
led = digitalio.DigitalInOut(pin)
led.direction = digitalio.Direction.OUTPUT
leds.append(led)
# Create chase pattern
for i in range(10):
for j, led in enumerate(leds):
led.value = (i % len(leds)) == j
time.sleep(0.2)
# Cleanup all LEDs
for led in leds:
led.deinit()Install with Tessl CLI
npx tessl i tessl/pypi-adafruit-blinka