neopixel.py

LattePanda Alpha · File : neopixel.py · Language : Python · Use case : Animer un ruban WS2812 depuis Python

The 800 kHz WS2812 frame is generated by the MCU, the animation comes from Python: the addressable strip joins the PC's scripting world. Mood lighting drivable by camera or music.

Hardware required

A LattePanda Alpha (x86 Intel m3-7Y30, Windows 10 or Linux) with its ATmega32U4 co-processor, Arduino Leonardo compatible, on the 50-pin headers. 12 WS2812 + external 5 V supply ≥ 1 A + 330-470 Ω resistor on data.

Wiring

Strip data through 470 Ω to D5, VDD to the external supply, ground common with the header. A 1000 µF cap on the strip supply is advised.

How it works

The library sends the color list to the PinPong firmware, which bit-bangs the NRZ frame on the timer. The PC paces the animation, the MCU holds the 1.25 µs per bit. The ATmega32U4 is a genuine Leonardo: flash it as « Arduino Leonardo » in the IDE through its internal COM port. Host-side, DFRobot's lattepanda Python library talks to it over serial.

Going further

Control by spectral analysis of the mic (music → light), or weather as a color gradient.

neopixel.py

#!/usr/bin/env python3
# 12 WS2812 sur D5 — PinPong pilote la trame, le MCU la genere.
from pinpong.board import Board, Pin
from pinpong.libs.dfrobot_neopixel import Neopixel
import time

Board().begin()
ruban = Neopixel(Pin.D5, 12)

COULEURS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)]

print("Comete RGB (Ctrl-C pour sortir)")
try:
    tete = 0
    while True:
        ruban.rainbow(0)                  # fond discret
        for i in range(12):
            r, g, b = COULEURS[(tete + i) % 4]
            ruban.set_pixel(i, r // 4, g // 4, b // 4)
        ruban.set_pixel(tete, 255, 255, 255)   # tete blanche
        ruban.show()
        tete = (tete + 1) % 12
        time.sleep(0.08)
except KeyboardInterrupt:
    ruban.rainbow(0)
    print("\nRuban eteint")

← Back to the board