So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By fsa317
#67153 Hi, I'm working on a project using the FastLED and LEDText libraries. I'm trying to display dynamic strings via the LED matrix. I think my issue however is more related with my manipulation of the strings in memory.

Here is the relevant code:
Code: Select all#include <FastLED.h>
#include <ESP8266WiFi.h>
#include <LEDMatrix.h>
#include <LEDText.h>
#include <FontRobotron.h>
#include <FontMatrise.h>
#include <WiFiClient.h>
#include <PubSubClient.h>


const char *ssid = "TruffleShuffle";
const char *password = "Mets1234";

WiFiClient espClient;
PubSubClient mqttclient(espClient);

// Change the next 6 defines to match your matrix type and size

#define LED_PIN        7  //GPIO13
#define COLOR_ORDER    GRB
#define CHIPSET        WS2812B

#define MATRIX_WIDTH   60
#define MATRIX_HEIGHT  7
#define MATRIX_TYPE    HORIZONTAL_ZIGZAG_MATRIX

#define MAXMSG 2

cLEDMatrix<MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_TYPE> leds;

cLEDText ScrollingMsg;

char TxtDemo[] = { "          INTRO MESSAGE       "};

String msgList[MAXMSG];

int currentMsgIdx=0;
int totalMsg=0;
int nextSlot = 0;

void setup()
{
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  dbg("Attempting wifi connection");
  delay(100);
  WiFi.begin ( ssid, password );
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds[0], leds.Size());
  FastLED.setBrightness(64);
  FastLED.clear(true);
  delay(100);
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 100 );
    FastLED.showColor(CRGB::Red);
    delay(100);
    FastLED.showColor(CRGB::Lime);
    delay(100);
    FastLED.showColor(CRGB::Blue);
    delay(100);
    FastLED.showColor(CRGB::White);
  }
  connectToMQTT();
 
  FastLED.show();

  msgList[0]= "          Welcome, test message          !";
  totalMsg=1;
  nextSlot=1;

  //ScrollingMsg.SetFont(RobotronFontData);
  ScrollingMsg.SetFont(MatriseFontData);
  ScrollingMsg.Init(&leds, leds.Width(), ScrollingMsg.FontHeight() + 1, 0, 0);
  ScrollingMsg.SetText((unsigned char *)TxtDemo, sizeof(TxtDemo) - 1);
  ScrollingMsg.SetTextColrOptions(COLR_RGB | COLR_SINGLE, 0xff, 0x00, 0xff);
}


void loop()
{
  if (ScrollingMsg.UpdateText() == -1){
    mqttclient.loop();
    delay(150);
    currentMsgIdx++;
    int max = (totalMsg > MAXMSG ? MAXMSG : totalMsg);
    if (currentMsgIdx >= max){
      currentMsgIdx = 0;
    }
    dbg("Displaying msgidx "+String(currentMsgIdx));
    int len = msgList[currentMsgIdx].length();
    char textPtr[len+1];
    char * msgStr;
    dbg("Length of msg: "+String(len));
    msgList[currentMsgIdx].toCharArray(textPtr,len+1);
    textPtr[len]='\0';
    dbg("length of textptr"+String(strlen(textPtr)));
    dbg("Setting text to ");
    dbg(String(textPtr));
   /* dbg("CHARS:");
    for (int i =0; i < sizeof(textPtr); i++){
      Serial.print(textPtr[i]);
    }
    dbg(":ENDCHARS");*/
    dbg("SIZE OF"+String(sizeof(textPtr)-1));

    //This shows some gibberish and I cant figure out why
    strcpy(msgStr,textPtr);
    ScrollingMsg.SetText((unsigned char *)msgStr, sizeof(msgStr)-1);

    //WORKS! but truncates the message based on the orginal size of TxtDemo
    //strcpy(TxtDemo,textPtr);
    //ScrollingMsg.SetText((unsigned char *)TxtDemo, sizeof(TxtDemo) - 1);
  }
  else
    FastLED.show();
  delay(10);
 
}


Near the bottom you'll the strcpy to msgStr, when using that code the LED shows some gibberish. However, when I use the code directly below (commented out) text shows up fine although it is truncated. Also if I replace:

Code: Select allchar * msg;
with
Code: Select allchar msg[43];
it works but of course I dont want it hardcoded. On the other hand
Code: Select allchar msg[len+1];
doesn't work.

I'm pretty lost as to what is going on. Any guidance is greatly appreciated.
User avatar
By fsa317
#67158 Yes it does. It even works with the minor code tweaks shown in the commented code. I'm trying to figure out how to make it work better with more dynamic strings.
User avatar
By martinayotte
#67160 With "char * msg", it is only a pointer, no memory is allocated. If it works with "char msg[43];", but you don't wish to have that hard coded, then, you should manage the memory allocation yourself.
Use "char * msg = malloc(len+1)", and at the end of the usage, delete it with "free(msg);"