Close

Walkthru onboard_neopixel.py module

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 08:080 Comments

The module onboard_neopixel.py encapsulates functions that access the single pixel neopixel strip that is on some CircuitPython boards. Most of the Adafruit link include these.  If the current board has one of these, the board module will include a member NEOPIXEL.  We import the required modules, define some module globals, and then define the setup_onboard_neopixel() function.

This function checks for existence of the neopixel, and if supported, creates an instance of the usual NeoPixel class with a single pixel on the member variable "on_board_neopixel". It prints some status to the console and then invokes a local function to blink the pixel in the default BLINK_COLOR

import board
import neopixel
import time
import adafruit_led_animation.color as Color

BLINK_COLOR = (100, 50, 150)  # color to blink, allow change
DELAY = 0.25  # blink rate in seconds

on_board_neopixel = None
def setup_onboard_neopixel():
    global on_board_neopixel
    # Create the NeoPixel object for the onBoard pixel,
    # check if processor board supports it
    # note this will be regular NeoPixel strip of length 1, not a seesaw.NeoPixel like on neotrellis
    if 'NEOPIXEL' in dir(board):
        on_board_neopixel = neopixel.NeoPixel(board.NEOPIXEL, 1, pixel_order=neopixel.GRB)
        print("Board have onboard NEOPIXEL", on_board_neopixel)
    else:
        print("Board does NOT have onboard NEOPIXEL")
    # blink it once to show we here
    blinkOnBoardPixel()

 That blinkOnBoardPixel() function is a basic neopixel blink() using time.sleep(). Clients can specify the blink color, or use the default.

def blinkOnBoardPixel(color=BLINK_COLOR):
    global on_board_neopixel
    if on_board_neopixel:
        on_board_neopixel.fill(color)
        on_board_neopixel.show()
        time.sleep(DELAY)
        on_board_neopixel[0] = (0,0,0)
        time.sleep(DELAY)
        on_board_neopixel.show()
    else:
        print("No onBoardPixel to blink")

 Lastly we define toggleOnBoardPixel() which toggles between the blink color and black on successive calls.

__onboardStatus = False
def toggleOnBoardPixel(color=BLINK_COLOR):
    global __onboardStatus
    global on_board_neopixel
    if on_board_neopixel:
        if __onboardStatus:
            on_board_neopixel.fill((0, 0, 0))
            __onboardStatus = False
        else:
            on_board_neopixel.fill(color)
            __onboardStatus = True

 Thats a pretty simple module.  Function names are not the standard lower_case_with_underscore Python convention but I'm more used to CamelCase style.

The module should work with other processor boards and other projects. I'll have to buy/build some to find out.

Discussions