Close

Refactored code_NeotrellisExplorations and code.py

A project log for NeoTrellis Explorations

Adafruit NeoTrellis is a 4x4 button+Neopixel array that can be chained on i2c bus. I am exploring its programming and uses.

Jerry IsdaleJerry Isdale 07/19/2022 at 07:520 Comments

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:

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