0

I'm using ESP8266 and 4 dot Matrix Tiles, to display clock ( updates using NTP and WiFi ). Everything is working well, but only when I'm constructing text using a const char* type.

But, since I wish to construct text as sprintf(temp, "12:00"); or char temp[20]="12:00\n; creates gibberish text.

MD_Parola dotMatrix = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

void updateTime()
{
  char clk[10];
  char dat[10];
  char temp[10];
  sprintf(temp, "12:00"); // <---- this ways failes
  const char *t = "12:00"; // <---- this way succeeds
  dotMatrix.displayText(t, PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
}

link to library

Appreciate explanation how to construct text not as const char*

4
  • You haven't written, how you supply temp to the dotMatrix.displayText() function. Are you just writing temp where now t is written? Have you tried providing a pointer to temp? I'm not sure here about the implicit handling of arrays as pointers, but you might try something like &temp or &temp[0] to get the pointer as parameter of the display function. That's what I would try in this case
    – chrisl
    Sep 10 '20 at 19:22
  • @chrisl - t replaces temp as you asked. &temp fails compilation and &temp[0] yields same result as temp
    – Guy . D
    Sep 10 '20 at 19:38
  • 1
    Mhh, the library documentation states, that the library doesn't use an internal buffer, but points to the buffer, that you provide a pointer to. I haven't looked into the library closer, but the problem might be, that the temp buffer goes out of scope after the function exits, which means, that the library uses then a pointer to an invalid location. Please try moving the declaration of the temp buffer to the global scope.
    – chrisl
    Sep 10 '20 at 19:43
  • @chrisl - I've read also what you;ve stated but I didn't understand that it should be external. it is correct! please post as an aunswer
    – Guy . D
    Sep 10 '20 at 20:07
1

The library documentation states:

The library does not allocate any memory for the text message

That suggests, that the buffer has to be valid beyond the execution of the displayText() function. But as you declare it inside of your updateTime() function, it the buffer gets out of scope, when exiting that function. The memory space can then be reused by the rest of the program. At that point the library will still have the pointer to the old location, though that pointer can now be considered invalid. Thus you are just reading, what the compiler decided to place in that memory location.

To solve that, you need to keep the buffer variable in scope. You can do this by moving the declaration to the global scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.