How do I pass a string out of a function?

First of all a big thank you to @delianides, Adafruit and others for the great Neomatrix/pixel libraries. I was able to get a 8x32 matrix working based on the demos/examples.

Forgive me if this post is in the wrong section, I’m a newbie to the forum. I’ve searched and can’t find anyone else who seems to have had this issue, and some of the examples in the Arduino forum don’t seem to apply to the Particle.function.

Now I want to be able to write to a function and pass the text to display on the matrix. I’ve hacked some code so that if there are less than 6 characters, it doesn’t scroll, but if the number of characters is too large to fit on the screen then the text scrolls.

The problem I’m having is when I write to the API when there is text on the screen, it super imposes the text on top of what’s there. I’ve tried writing " " to the msgText variable to clear it first, but has no effect. What’s good way to blank/reset this variable before the new text is retrieved?

Second question is, what is the “best” way to pass the string back out of the Particle.function? Writing to the msgText variable seems to work, but not sure if this is good practice to do it this way or if there is a better way to handle it. I’m new to Particle/Arduino coding.

#include "Particle.h"
#include "neomatrix.h"

// IMPORTANT: Set pixel PIN and TYPE
#define PIXEL_PIN D6
#define PIXEL_TYPE WS2812B

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 8, PIXEL_PIN,
  NEO_MATRIX_BOTTOM    + NEO_MATRIX_RIGHT +
  NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
  PIXEL_TYPE);
 
const uint16_t colors[] = {
  matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(255, 255, 0),matrix.Color(0, 0, 255), matrix.Color(255, 0, 255), matrix.Color(0, 255, 255), matrix.Color(255, 255, 255)};
 
void setup() {
  matrix.begin();
  matrix.setTextWrap(false);
  matrix.setBrightness(5);
  matrix.setTextColor(colors[0]);
  // create cloud function to pass text to display
  Particle.function("setMsg", getMsg);
}
 
int x    = matrix.width();
int pass = 0;
String msgText = "My default text message";  //set default text
int scrollMaxPosition = 0;

void loop() {

  // if the length of the text will fit on the screen, don't scroll
  
  if (msgText.length() < 6
  ) {  
    //center the text string
    x = (matrix.width() - msgText.length()*6)/2;
    matrix.setCursor(x, 0);
    matrix.print(F(msgText));
    matrix.show();
  }
  else
  {
    // if the length of the text is longer than the screen width, scroll
    matrix.fillScreen(0);
    matrix.setCursor(x, 0);
    matrix.print(F(msgText));
    //calculate how far to scroll to the left to get the text off the screen
    scrollMaxPosition = matrix.width() + msgText.length()*6;
    if(--x < -scrollMaxPosition
    ) {
      x = matrix.width();
 
      if(++pass >= 8) pass = 0;
      matrix.setTextColor(colors[pass]);
    }
    matrix.show();
    delay(70);
  }
}
  
int getMsg(String command)
    {
    //erase previous message
    msgText = " ";
    matrix.print(F(msgText));
    matrix.show();
    // set message equal to command string from API
    msgText = command;
     return 1;
    }

typically, you can just append your string with a space which will destroy the previously printed character as the scrolling leaves the screen:

int getMsg(String command)
{
//erase previous message
msgText = " ";
matrix.print(F(msgText));
matrix.show();
// set message equal to command string from API
msgText = command;
msgText += " "; // <<< here
return 1;
}

that said, it is recommended that you stay away from using the String class in favor of C strings. Search String class fragmentation on this website for more about why.

1 Like

In that case the library is obviously only transfering set pixels and not clearing the dark ones.
But for that purpose there usually exists another function or a flag to set both pixels.
If there isn't you may need to clear the whole strip and then set the lit pixels.

For Adafruit_GFX you just use setTextColor(uint16_t c, uint16_t bg) to make the library also set the background pixels.

Thanks, I’ve gone through and made the conversion to C strings, and have done a lot of reading re: why. Appreciate the education.

ScruffR, that is the solution! Thanks, making the bg color black resolved the issue.

2 Likes