NeoPixel Emulator in Python with Pyglet
This emulator has two main ways to emulate. One is a simple NeoPixel strip with the basic setPixelColor and some simple effects, and the other is a matrix emulator. You can create large matrices that dynamically resize the window based on the size. There is also an almost one-to-one port of the Adafruit GFX library, which allows for the creation of many graphics primitives and even the drawing of monochrome bitmaps from an array. The only thing not implemented is tiling and printing of text.
- pyglet (e.g. from
pypi)
Here be original circuitpython modules.
emulator_backend: emulate thecircuitpythonneopixelAPI (seeneopixel_emulatorfor LED emulation)pixel: simple class to hold pixel informationneopixel_emulator: emulate theneopixelLED strip activation withpygletneopixel_effects: drive effects into LED strip,circuitpythondependent independent emulator of LED hardwareneopixel_gfx: special effects, independent of emulator/hardwareneopixel_viewer: demonstrationneopixel_neomatrix: demonstration
Since micropython and circuitpython neopixel classes have different methods for pixel access, modules for micropython have been created. NeoPixel class emulator in CPython with Pyglet for micropython neopixel class. New modules are:
upy_backend: emulate themicropythonneopixelAPI (seeneopixel_emulatorfor LED emulation)upy_effects: drive effects into LED strip,micropythondependent emulator of LED hardware replacesneopixel_effects
Examples are:
led_panel: demonstration class with effects derived from https://github.com/MikeEllis-personal/DMXfire.gitrjstest: demonstration drive led_panelupy_viewer: demonstration (examples)upy_matrix: demonstrationeffects: unused
Reused modules:
neopixel_emulator: used byupy_backend- clean up for new pyglet version.
- can change pixel size.
- set numpixels/row for linear strip.
neopixel_gfx: only code clean up (ruff) used by examplespixel: only code clean up (ruff)
The template for using the emulator or the actual hardware:
_real_hardware = False
try:
from neopixel import NeoPixel
import machine
_realhardware = True
except ModuleNotFoundError:
from upy_backend import NeoPixel
from upy_backend import machine
then use as normal. Remember for initializing the NeoPixel array the pin number is critical for real hardware but insignificant for the emulator. The emulator also has some optional additional parameters to defined the display on linux:
if _real_hardware:
pixels = NeoPixel(ledPin, numberPixels)
else:
pixels = NeoPixel(ledPin, numberPixels, init=True, window_w=1785, window_h=400,pixsize=10)
add at the end:
if not _real_hardware:
pixels.fclose() # ends pyglet session and closes display
See examples/upy_viewer.py and example/led_panel.py.
These examples assume:
- pyglet is available
- in the repository home directory
For MicroPython:
- run with the home directory mounted:
>mpremote mount . # connects to hardware, starts REPL with host directory as currentn dir
- download the required modules to device. Note that the upy_* modules are not necessary.
>mpremote mkdir :examples
>mpremote cp examples/*.py :examples # copy examples to device
>mpremote # start REPL
python -m examples.upy_viewer
python -m examples.upy_matrix
python -m examples.rjstest # uses led_panel, runs fire_test()
import examples.upy_viewer as view
view.run()
import examples.rjstest as rt # uses led_panel
rt.fire_test()
Interactive from REPL
import examples.upy_viewer as view
view.run(ledPin=1)
import examples.rjstest as rt
rt.fire_test(ledPin=1)