Skip to main content

A MicroPython library for driving NeoPixel animations with time functions and keyframes

Project description

np_animation logo

np_animation

PyPI Version License MicroPython

A MicroPython library for driving NeoPixel animations in a tight loop. It works like a mechanism, with time functions and keyframes, making it easy to create complex LED animations for robotics, wearables, and IoT projects.

This library provides a powerful animation framework that separates animation logic from LED control, allowing you to create smooth, time-based animations with minimal overhead. Perfect for robot lighting effects, indicator lights, and decorative patterns.

Table of Contents

Features

  • 🎨 Rich Animation Functions: Built-in effects like pulse, hue shift, knight rider, indicators, and brake lights
  • ⏱️ Time-Based Animation: Smooth animations using time functions and keyframes
  • 🎯 Function Matrix System: Map multiple animation functions to different LED positions
  • 🚗 Vehicle Lighting: Ready-made functions for indicators, brake lights, and headlights
  • 🌈 Color Utilities: HSL/RGB conversion and color manipulation helpers
  • 💡 LED Control: Direct control over individual LEDs or groups
  • 🔄 Keyframe Animation: Support for complex keyframe-based animation sequences
  • 🎮 Dynamic Control: Pass runtime parameters to control animation behavior

Installation

On LMS-ESP32

The module should be included in the latest MicroPython firmware from https://www.antonsmindstorms.com. If not, use ViperIDE or Thonny and create a new file called np_animation.py. Copy the contents from the same file in this repository inside.

On MicroPython device using micropip from PyPI

import micropip
await micropip.install("np_animation")

Note: micropip must be available on the target board and may require an internet connection from the device.

Manual Installation

Copy np_animation.py to your MicroPython device's filesystem.

Quick Start

Basic Animation with Function Matrix

from np_animation import NPAnimation, hue_shift, switch, grb
from time import sleep_ms

# Define animation function matrix
funcs = [
    [[0, 1, 2, 3, 4, 5], hue_shift(period=5000)],  # Rainbow effect on LEDs 0-5
    [[6, 7], switch(on=grb.WHITE, off=grb.OFF, name="headlights")],  # Switchable headlights
]

# Create animation instance (default pin 24, auto-detect LED count)
npa = NPAnimation(funcs)

# Animation loop
try:
    while True:
        npa.update_leds(headlights=True)  # Update with headlights on
        sleep_ms(50)
except KeyboardInterrupt:
    npa.leds_off()

Vehicle Lighting Example

from np_animation import NPAnimation, indicators, brake_lights, switch, grb

# Create a complete vehicle lighting system
funcs = [
    [[0, 1], indicators(name="left_indicators")],   # Left turn signals
    [[10, 11], indicators(name="right_indicators")], # Right turn signals
    [[2, 3], switch(on=grb.WHITE, name="headlights")], # Headlights
    [[8, 9], brake_lights()],  # Brake lights (respond to speed parameter)
]

npa = NPAnimation(funcs, pin=24)

# In your control loop
while True:
    speed = get_motor_speed()  # Your speed function
    npa.update_leds(
        left_indicators=turn_left,
        right_indicators=turn_right,
        headlights=lights_on,
        speed=speed
    )
    sleep_ms(50)

Color Conversion

from np_animation import hsl_to_rgb, rgb_to_hsl, to_grb

# Convert HSL to RGB
rgb = hsl_to_rgb(120, 100, 50)  # Green: (0, 255, 0)

# Convert RGB to HSL
hsl = rgb_to_hsl(255, 0, 0)  # Red: (0, 100, 50)

# Convert RGB to GRB bytes for NeoPixel
grb_bytes = to_grb((255, 0, 0))  # b'\x00\xff\x00'

Knight Rider Effect

from np_animation import NPAnimation, knight_rider, grb

funcs = [
    [[0, 1, 2, 3, 4, 5], knight_rider(period=2000, width=6, color=grb.RED)]
]

npa = NPAnimation(funcs)

while True:
    npa.update_leds()
    sleep_ms(50)

Keyframe Animation

from np_animation import NPAnimation, keyframes, grb

# Define keyframes: (time_ms, [led_colors])
EMERGENCY = [
    (0, [grb.RED]*3 + [grb.OFF]*3),
    (150, [grb.OFF]*6),
    (200, [grb.RED]*3 + [grb.OFF]*3),
    (350, [grb.OFF]*6),
    (500, [grb.OFF]*3 + [grb.BLUE]*3),
    (650, [grb.OFF]*6),
    (1000, [grb.OFF]*6)
]

funcs = [
    [[0, 1, 2, 3, 4, 5], keyframes(EMERGENCY)]
]

npa = NPAnimation(funcs)

while True:
    npa.update_leds()
    sleep_ms(50)

Projects & Tutorials

See np_animation in action! Check out these project tutorials and videos showcasing real-world applications.

📝 Blog Tutorials

Knight Rider KITT Scanner

Creating a Knight Rider NeoPixel Animation

Learn how to recreate the iconic KITT scanner effect from the Knight Rider TV series using the knight_rider() function. Includes detailed code explanation and implementation tips.

Astro Boy with Radar Sensor

Lighting Astro Boy with a Radar Sensor

Add interactive lighting to brick models using a radar proximity sensor. Astro Boy lights up only when someone waves at him, combining multiple animation effects with sensor input.

🎥 Video Tutorials

Knight Rider Animation Tutorial

Knight Rider Animation Video Tutorial

Watch the KITT scanner effect in action on a LEGO Technic car with step-by-step implementation guide.

Extreme Off-Roader with LED Effects

Extreme Off-Roader LED Effects

See the np_animation library in full action with complex lighting effects on an off-road vehicle build.

🔗 More Projects

Explore more NeoPixel projects and tutorials on the NeoPixel tag page at Anton's Mindstorms.

Documentation

Core Class

NPAnimation(light_funcs, pin=24, n_leds=0)

Main animation class that manages LED updates based on a function matrix.

  • light_funcs: List of [led_positions, animation_function] pairs
  • pin: GPIO pin for NeoPixel data line
  • n_leds: Number of LEDs (auto-detected from function matrix if 0)

Methods:

  • update_leds(time=None, **kwargs): Update all LEDs based on current time and parameters
  • leds_off(): Turn off all LEDs

Animation Functions

All animation functions return a function that takes (time, **kwargs) and returns color data.

hue_shift(period=1000, offset=0)
Continuously shifts through the color spectrum.

pulse(color=grb.WHITE, period=5000, offset=0, min_pct=0, max_pct=100)
Pulsing brightness effect with configurable color and range.

knight_rider(period=2000, width=6, color=grb.RED)
Classic scanning effect like KITT from Knight Rider.

indicators(on=grb.ORANGE, off=grb.OFF, interval=500, name="indicators")
Blinking indicator lights with on/off control.

brake_lights(drive=grb.DARK_RED, brake=grb.RED, reverse=grb.WHITE)
Vehicle brake lights that respond to speed parameter.

switch(on=grb.WHITE, off=grb.OFF, name="switch")
Simple on/off switch controlled by a boolean parameter.

delayed_switch(on=grb.RED, off=grb.OFF, delay=2000)
Switch that turns off after a delay.

keyframes(frames)
Play a list of (time, colors) keyframes in a loop.

keyframes_dict(frames_dict, name="animation")
Switch between multiple keyframe animations at runtime.

Color Utilities

hsl_to_rgb(h, s, l) - Convert HSL (0-359, 0-100, 0-100) to RGB (0-255)

rgb_to_hsl(r, g, b) - Convert RGB to HSL

to_grb(rgb) - Convert RGB tuple to GRB bytes

from_grb(grb) - Convert GRB bytes to RGB tuple

Color Constants

grb class - Pre-defined colors in GRB byte format:

  • ORANGE, BLACK/OFF/NONE, WHITE, RED, DARK_RED, BLUE, YELLOW, GREEN, CYAN, VIOLET, MAGENTA, GRAY

rgb class - Same colors in RGB tuple format

Supported Platforms

  • ESP32 with MicroPython
  • LEGO SPIKE Prime/Essential (with MicroPython firmware)
  • LEGO MINDSTORMS Robot Inventor
  • Raspberry Pi Pico/Pico W
  • Any MicroPython board with NeoPixel support

License

MIT License

Author

Anton Vanhoucke

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

np_animation-1.0.0.tar.gz (45.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

np_animation-1.0.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file np_animation-1.0.0.tar.gz.

File metadata

  • Download URL: np_animation-1.0.0.tar.gz
  • Upload date:
  • Size: 45.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for np_animation-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0312d98b580afe9bb61555770dd467605d6dc3c92a17d7275989bdee3a4feb9f
MD5 0d32ac2b527e0e152212e4d469c9bad4
BLAKE2b-256 1721f1cacc40bd13843eb6dec5e13ebe7a4036e8b250d11d7f006eca995a4193

See more details on using hashes here.

File details

Details for the file np_animation-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: np_animation-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for np_animation-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b78d997a82807dcaa4f33332d63ce9c3450aa13ca2ce4db3c9e83225f7deae57
MD5 94d583e1fa439a170d974e521cfd750d
BLAKE2b-256 d80d2c42dc24c741f5388b25712aca32a3ad3116a137621b09ce664389643881

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page