The examples folder has single application (code_animKey_PixelMaps.py) that runs the full set of animations tied to key presses. That code irked me as hard to explain. I like clean modular code. So I split that program up into 4 files:
- code_NeotrellisExplorations.py is the main program. It does the setup and has the infinite loop. copy it to code.py to run on a Circuit Python board
- onboard_neopixel.py handles the single pixel Neopixel on many CircuitPython boards, like the Adafruit Feather M4 Express. It should work ok if run on a board that doesn't have such pixel (eg. Raspberry Pi Pico)
- neotrellis_animations.py encapsulates the animations that run across the 4x4 matrix
- neotrellis_keypad.py encapsulates the key handling
This log will go over the main file with subsequent project logs discussing the other modules.
The first bit of code_NeotrellisExplorations.py is the bit discussed earlier to disable the autoreload feature of CircuitPython. Then it imports the required system and local modules
import time
import board
import busio
from adafruit_neotrellis.neotrellis import NeoTrellis
# local modules
import onboard_neopixel
import neotrellis_animations
import neotrellis_keypad
we then make a call setup the onboard_neopixel
# setup single pixel strip if board has it, blink it once
onboard_neopixel.setup_onboard_neopixel()
Then initialize the neotrellis board:
# create the i2c object for the trellis
# note the use of busio.I2C() instead of board.I2C()
# apparently this is an issue for M4 (rPi Pico too?)
i2c_bus = busio.I2C(board.SCL, board.SDA)
trellis = NeoTrellis(i2c_bus)
Two simple calls setup the animations and keypad handling
# setup animation and keypad modules
neotrellis_animations.setup_animations(trellis)
neotrellis_keypad.setup_keypad(trellis)
The remainder of code.py is the infinite loop. Basically this updates the animation and then handles keypad activity with trellis.sync(). There is a bunch of print stuff so we can see that program continues to run when reading the REPL (serial link) in MU Editor
print("Setup Complete enter forever loop ", neotrellis_animations.current_animation)
i = 0
while True:
# tell animation to update
neotrellis_animations.current_animation.animate()
# call the sync function call any triggered callbacks
trellis.sync()
# the trellis can only be read every 17 milliseconds or so
# really? the neopixel _getItem() could be an issue for i2c/seesaw connected neopixels
time.sleep(0.02)
# print out something so debug console watcher knows program is running
# also might give keyboard interrupt (Ctrl C) a chance to pause CircuitPython
i +=1
if i%50 == 0:
print(i, end='.')
if i%10000 == 0:
print(i, "reset")
i=0
Using modules for the app makes the top level easier to read and understand. Hopefully.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.