Adding arduino and CP code for 5x5 BFF

Adding example code from Jeff and Phil B. for the 5x5 NeoPixel BFF
This commit is contained in:
BlitzCityDIY 2022-11-28 07:45:06 -05:00
parent 527a999bf9
commit ff1088888d
6 changed files with 145 additions and 1 deletions

View file

@ -0,0 +1,61 @@
// SPDX-FileCopyrightText: 2022 Phil B. for Adafruit Industries
//
// SPDX-License-Identifier: MIT
// Example for 5x5 NeoBFF - scrolls a message across the LED matrix.
// Requires Adafruit_GFX, Adafruit_NeoPixel and Adafruit_NeoMatrix libraries.
#include <Adafruit_GFX.h> // Graphics library
#include <Adafruit_NeoPixel.h> // NeoPixel library
#include <Adafruit_NeoMatrix.h> // Bridges GFX and NeoPixel
#include <Fonts/TomThumb.h> // A tiny 3x5 font incl. w/GFX
#define PIN A3
// NeoMatrix declaration for BFF with the power and
// Neo pins at the top (same edge as QT Py USB port):
Adafruit_NeoMatrix matrix(5, 5, PIN,
NEO_MATRIX_TOP + NEO_MATRIX_RIGHT +
NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,
NEO_GRB + NEO_KHZ800);
// Message to display, and a set of colors to cycle through. Because
// the matrix is only 5 pixels tall, characters with descenders (e.g.
// lowercase p or y) are best avoided. There are even smaller fonts
// but these get progressively less legible. ALL CAPS helps!
const char message[] = "HELLO BFF";
const uint16_t colors[] = {
matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(0, 0, 255) };
uint16_t message_width; // Computed in setup() below
void setup() {
matrix.begin();
matrix.setBrightness(40); // Turn down brightness to about 15%
matrix.setFont(&TomThumb); // Use custom font
matrix.setTextWrap(false); // Allows text to scroll off edges
matrix.setTextColor(colors[0]); // Start with first color in list
// To determine when the message has fully scrolled off the left side,
// get the bounding rectangle of the text. As we only need the width
// value, a couple of throwaway variables are passed to the bounds
// function for the other values:
int16_t d1;
uint16_t d2;
matrix.getTextBounds(message, 0, 0, &d1, &d1, &message_width, &d2);
}
int x = matrix.width(); // Start with message off right edge
int y = matrix.height(); // With custom fonts, y is the baseline, not top
int pass = 0; // Counts through the colors[] array
void loop() {
matrix.fillScreen(0); // Erase message in old position.
matrix.setCursor(x, y); // Set new cursor position,
matrix.print(message); // draw the message
matrix.show(); // and update the matrix.
if(--x < -message_width) { // Move 1 pixel left. Then, if scrolled off left...
x = matrix.width(); // reset position off right edge and
if(++pass >= 3) pass = 0; // increment color in list, rolling over if needed.
matrix.setTextColor(colors[pass]);
}
delay(100); // 1/10 sec pause
}

View file

@ -13,7 +13,7 @@ font = bitmap_font.load_font("tom-thumb.pcf", Bitmap)
label = Label(text="Hello World!! Adafruit QT Py RP2040 + NeoPixel BFF ", font=font)
bitmap = label.bitmap
pixels = neopixel.NeoPixel(board.A2, 5*5, brightness=.07, auto_write=False)
pixels = neopixel.NeoPixel(board.A3, 5*5, brightness=.07, auto_write=False)
pixels.fill(0)
pixels.show()
colors = [0, 0]

View file

@ -0,0 +1,83 @@
# SPDX-FileCopyrightText: Copyright (c) 2022 Jeff Epler for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
import time
import board
import neopixel
import fontio
from adafruit_display_text.bitmap_label import Label
from adafruit_bitmap_font import bitmap_font
from displayio import Bitmap
from rainbowio import colorwheel
tom_thumb = bitmap_font.load_font("tom-thumb.pcf", Bitmap)
_glyph_keys = ['bitmap', 'tile_index', 'width', 'height', 'dx', 'dy', 'shift_x', 'shift_y']
def patch_glyph(base, **kw):
d = {}
for k in _glyph_keys:
d[k] = kw.get(k, getattr(base, k))
return fontio.Glyph(**d)
class PatchedFont:
def __init__(self, base_font, patches):
self.base_font = base_font
self.patches = patches
def get_glyph(self, glyph):
g = self.base_font.get_glyph(glyph)
patch = self.patches.get(glyph)
if patch is not None:
print("patching", repr(chr(glyph)), g)
g = patch_glyph(g, **patch)
print("patched", g)
return g
def get_bounding_box(self):
return self.base_font.get_bounding_box()
font = PatchedFont(tom_thumb,
{
32: {'shift_x': 1, 'dx': 0},
105: {'dx': 0, 'shift_x': 2},
33: {'dx': 0, 'shift_x': 2},
})
label = Label(text=" adafruit! ", font=font)
bitmap = label.bitmap
heart_bitmap = [
0,1,1,0,0,
1,1,1,1,0,
0,1,1,1,1,
1,1,1,1,0,
0,1,1,0,0,
]
pixels = neopixel.NeoPixel(board.A3, 5*5, brightness=.06, auto_write=False)
while True:
for hue in range(0, 255, 3):
color = colorwheel(hue)
pixels[:] = [pixel * color for pixel in heart_bitmap]
pixels.show()
time.sleep(.01)
hue = 0
for i in range(bitmap.width):
# Use a rainbow of colors, shifting each column of pixels
hue = hue + 7
if hue >= 256:
hue = hue - 256
color = colorwheel(hue)
# Scoot the old text left by 1 pixel
pixels[:20] = pixels[5:]
# Draw in the next line of text
for y in range(5):
# Select black or color depending on the bitmap pixel
pixels[20+y] = color * bitmap[i,y]
pixels.show()
time.sleep(.1)