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
Additional utility functions for color generation and MicroPython compatibility that enhance the CircuitPython API emulation.
Rainbow color generation utility for creating smooth color transitions, commonly used with NeoPixel LED strips and other RGB applications.
def colorwheel(color_value: int) -> int:
"""
Generate rainbow colors from input value.
Creates a smooth color wheel where:
- 0 and 255 are red
- 85 is green
- 170 is blue
- Values between create smooth rainbow transitions
Parameters:
- color_value: Integer from 0-255 representing position on color wheel
Returns:
32-bit RGB color value in format 0xRRGGBB
Note: Values outside 0-255 range return black (0x000000)
"""Decorator functions that provide MicroPython compatibility for code that needs to run on both CircuitPython and MicroPython platforms.
def const(x):
"""
Emulate MicroPython const decorator.
In MicroPython, this decorator optimizes constants at compile time.
In CPython/Blinka, this is a no-op that returns the value unchanged.
Parameters:
- x: Value to mark as constant
Returns:
The input value unchanged
"""
def native(f):
"""
Emulate MicroPython native decorator.
In MicroPython, this compiles functions to native code for performance.
In CPython/Blinka, this is a no-op that returns the function unchanged.
Parameters:
- f: Function to mark as native
Returns:
The input function unchanged
"""
def viper(f):
"""
MicroPython viper decorator - not supported.
Raises:
- SyntaxError: Always raised as viper code emitter is not supported
"""
def asm_thumb(f):
"""
MicroPython inline assembler decorator - not supported.
Raises:
- SyntaxError: Always raised as inline assembly is not supported
"""import rainbowio
# Generate rainbow colors for a color cycle
colors = []
for i in range(256):
color = rainbowio.colorwheel(i)
# Extract RGB components
red = (color >> 16) & 0xFF
green = (color >> 8) & 0xFF
blue = color & 0xFF
colors.append((red, green, blue))
# Use with NeoPixels
import neopixel_write
import board
# Create rainbow effect
pixels = bytearray(3 * 10) # 10 RGB pixels
for i in range(10):
color = rainbowio.colorwheel(i * 25) # Spread across color wheel
pixels[i*3] = (color >> 8) & 0xFF # Green
pixels[i*3+1] = (color >> 16) & 0xFF # Red
pixels[i*3+2] = color & 0xFF # Blue
neopixel_write.neopixel_write(board.D18, pixels)import micropython
# Define constants (optimized on MicroPython, no-op on CPython)
LED_PIN = micropython.const(18)
BLINK_DELAY = micropython.const(500)
# Native compilation hint (optimized on MicroPython, no-op on CPython)
@micropython.native
def fast_calculation(data):
result = 0
for value in data:
result += value * 2
return result
# Use in portable code
import board
import digitalio
import time
led = digitalio.DigitalInOut(getattr(board, f'D{LED_PIN}'))
led.direction = digitalio.Direction.OUTPUT
while True:
led.value = True
time.sleep(BLINK_DELAY / 1000)
led.value = False
time.sleep(BLINK_DELAY / 1000)import rainbowio
import time
def rainbow_cycle(strip_length, delay=0.1):
"""Create a rainbow cycle animation."""
for offset in range(256):
colors = []
for i in range(strip_length):
# Calculate color for this pixel
color_pos = (i * 256 // strip_length + offset) % 256
color = rainbowio.colorwheel(color_pos)
colors.append(color)
# Here you would send colors to your LED strip
yield colors
time.sleep(delay)
# Use the animation
for frame in rainbow_cycle(30, 0.05):
# Send frame to LED strip
print(f"Frame: {[hex(c) for c in frame[:5]]}...") # Show first 5 colors# Import color utilities
import rainbowio
color = rainbowio.colorwheel(128)
# Import MicroPython compatibility
import micropython
CONSTANT_VALUE = micropython.const(42)
# Or import specific functions
from rainbowio import colorwheel
from micropython import const, native
@native
def optimized_function():
THRESHOLD = const(100)
return THRESHOLD * 2viper and asm_thumb decorators raise SyntaxError as they're not supportedInstall with Tessl CLI
npx tessl i tessl/pypi-adafruit-blinka