|
| 1 | +// SPDX-FileCopyrightText: 2022 Phil B. for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +// Example for 5x5 NeoBFF - scrolls a message across the LED matrix. |
| 6 | +// Requires Adafruit_GFX, Adafruit_NeoPixel and Adafruit_NeoMatrix libraries. |
| 7 | + |
| 8 | +#include <Adafruit_GFX.h> // Graphics library |
| 9 | +#include <Adafruit_NeoPixel.h> // NeoPixel library |
| 10 | +#include <Adafruit_NeoMatrix.h> // Bridges GFX and NeoPixel |
| 11 | +#include <Fonts/TomThumb.h> // A tiny 3x5 font incl. w/GFX |
| 12 | + |
| 13 | +#define PIN A3 |
| 14 | + |
| 15 | +// NeoMatrix declaration for BFF with the power and |
| 16 | +// Neo pins at the top (same edge as QT Py USB port): |
| 17 | +Adafruit_NeoMatrix matrix(5, 5, PIN, |
| 18 | + NEO_MATRIX_TOP + NEO_MATRIX_RIGHT + |
| 19 | + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE, |
| 20 | + NEO_GRB + NEO_KHZ800); |
| 21 | + |
| 22 | +// Message to display, and a set of colors to cycle through. Because |
| 23 | +// the matrix is only 5 pixels tall, characters with descenders (e.g. |
| 24 | +// lowercase p or y) are best avoided. There are even smaller fonts |
| 25 | +// but these get progressively less legible. ALL CAPS helps! |
| 26 | +const char message[] = "HELLO BFF"; |
| 27 | +const uint16_t colors[] = { |
| 28 | + matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(0, 0, 255) }; |
| 29 | +uint16_t message_width; // Computed in setup() below |
| 30 | + |
| 31 | +void setup() { |
| 32 | + matrix.begin(); |
| 33 | + matrix.setBrightness(40); // Turn down brightness to about 15% |
| 34 | + matrix.setFont(&TomThumb); // Use custom font |
| 35 | + matrix.setTextWrap(false); // Allows text to scroll off edges |
| 36 | + matrix.setTextColor(colors[0]); // Start with first color in list |
| 37 | + // To determine when the message has fully scrolled off the left side, |
| 38 | + // get the bounding rectangle of the text. As we only need the width |
| 39 | + // value, a couple of throwaway variables are passed to the bounds |
| 40 | + // function for the other values: |
| 41 | + int16_t d1; |
| 42 | + uint16_t d2; |
| 43 | + matrix.getTextBounds(message, 0, 0, &d1, &d1, &message_width, &d2); |
| 44 | +} |
| 45 | + |
| 46 | +int x = matrix.width(); // Start with message off right edge |
| 47 | +int y = matrix.height(); // With custom fonts, y is the baseline, not top |
| 48 | +int pass = 0; // Counts through the colors[] array |
| 49 | + |
| 50 | +void loop() { |
| 51 | + matrix.fillScreen(0); // Erase message in old position. |
| 52 | + matrix.setCursor(x, y); // Set new cursor position, |
| 53 | + matrix.print(message); // draw the message |
| 54 | + matrix.show(); // and update the matrix. |
| 55 | + if(--x < -message_width) { // Move 1 pixel left. Then, if scrolled off left... |
| 56 | + x = matrix.width(); // reset position off right edge and |
| 57 | + if(++pass >= 3) pass = 0; // increment color in list, rolling over if needed. |
| 58 | + matrix.setTextColor(colors[pass]); |
| 59 | + } |
| 60 | + delay(100); // 1/10 sec pause |
| 61 | +} |
0 commit comments