1

I'm working on a project with a Adafruit 8x16 LED Matrix FeatherWing, which I want to use to display scrolling text. Looking at the example code provided via the Arduino library, I've managed to come up with the following:

void updateDisplay()
{
    for (int8_t x = 16; x >= -36; x--)
    {
        matrix.clear();
        matrix.setCursor(x, 0);
        matrix.print("World");
        // matrix.print(displayText);
        matrix.writeDisplay();
        delay(100);
    }
}

Nothing too different from the sample code, except I've set x to 16 so the message starts from off the display on the left.

If I understand this correctly, the -36 value is how many pixels is required to scroll the text off the other side of the display. That is to say, the buffer for "World" is 36 pixels wide. If this is the case, how can I work out the size of the buffer for an arbitrary string?

I'm guessing there's a method which I'm able to write (or exists) which can calculate it, or maybe the library already knows this info, in which case I need a way to access it.

You can see I have a displayText variable which I'm going to be updating with a duration, anything from "0 seconds" to "365 days" (and beyond), so I'm going to need to calculate this width each time the displayText value is updated - unless there's an easier way to solve this problem?

I welcome your advice, thank you.

Daniel Hollands
  • 6,281
  • 4
  • 24
  • 42

2 Answers2

1

You can find the length of a string using LengthA=StringA.length();

https://www.arduino.cc/en/Tutorial/StringLength

I assume the font is fixed width, so multiply the font width by the string length and add a value to account for any spaces before or after the string if they are required.

If you don't have or don't send a clear command between strings, you would normally add enough spaces to clear the display at the start the next string.

I would imagine the spaces between characters are included in the font, if not you would need to add them.

Trevor

Trevor
  • 11
  • 2
0

Very large thanks to Adam Bachman for helping me find the answer to this.

The following is his code, and demonstrates use of the getTextBounds() method for calculating the size of the buffer (and the weird way in which it's able to return multiple parameters to you):

#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>

/************************ LED Matrix *******************************/
Adafruit_8x16minimatrix matrix = Adafruit_8x16minimatrix();

void setup() {
Serial.begin(115200);
while(! Serial);
}

void loop() {
// void Adafruit_GFX::getTextBounds(const char *str, int16_t x, int16_t y,
//     int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
int16_t x1, y1;
uint16_t w, h;

matrix.getTextBounds((const char*)"my string is here", 0, 0, &x1, &y1, &w, &h);

Serial.print("x1="); Serial.print(x1);
Serial.print(", y1="); Serial.print(y1);
Serial.print(", w="); Serial.print(w);
Serial.print(", h="); Serial.print(h);
Serial.println(" ");
delay(1000);
}

The output of which looks like this:

x1=0, y1=0, w=6, h=136
x1=0, y1=0, w=6, h=136
x1=0, y1=0, w=6, h=136
x1=0, y1=0, w=6, h=136
x1=0, y1=0, w=6, h=136
x1=0, y1=0, w=6, h=136

In this instance, because he's using the default rotation, it's the h value which we need, but in my instance, because I'd set matrix.setRotation(1);, I'd use the w instead.

Daniel Hollands
  • 6,281
  • 4
  • 24
  • 42