Easy customization of led matrix MAX7219 Wemos D1 mini pro

Hi guys,

I need some help to customize my led matrix display. I took an example of someone else and modified it a bit and it is working pretty good now. Data is shown up and the display works.

At the moment, the text is scrolling from the right to the left in an endless but always with data refreshed loop. I want to change that. I don' want to have so much movement on the screen.
My intention is, the text should just appear or scroll into the matrix display comming from the bottom and then STAY THERE for several seconds, for example 5 seconds. And after that period of time it should refresh.

What do I have to change or add into my code?
I still had a look at the wiki of the library but I was not able to achieve that goal :frowning:

#include <ESP8266WiFi.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include "EspMQTTClient.h"

#define MAX_DEVICES 4 // your device count

#define CLK_PIN   D5 // or SCK
#define DATA_PIN  D7 // or MOSI
#define CS_PIN    D6 // or SS // you can set it to any pin

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW // change according to your display type
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

const uint8_t MESG_SIZE = 255;
const uint8_t CHAR_SPACING = 1;
uint8_t SCROLL_DELAY = 75; // default scroll delay
uint8_t INTENSITY = 5; // default intensity

char curMessage[MESG_SIZE];
char newMessage[MESG_SIZE];
bool newMessageAvailable = false;

void scrollDataSink(uint8_t dev, MD_MAX72XX::transformType_t t, uint8_t col) {}

uint8_t scrollDataSource(uint8_t dev, MD_MAX72XX::transformType_t t)
{
  static enum { S_IDLE, S_NEXT_CHAR, S_SHOW_CHAR, S_SHOW_SPACE } state = S_IDLE;
  static char   *p;
  static uint16_t curLen, showLen;
  static uint8_t  cBuf[8];
  uint8_t colData = 0;

  switch (state)
  {
  case S_IDLE: 
    p = curMessage;
    if (newMessageAvailable) 
    {
      strcpy(curMessage, newMessage);
      newMessageAvailable = false;
    }
    state = S_NEXT_CHAR;
    break;

  case S_NEXT_CHAR: 
    if (*p == '\0')
      state = S_IDLE;
    else
    {
      showLen = mx.getChar(*p++, sizeof(cBuf) / sizeof(cBuf[0]), cBuf);
      curLen = 0;
      state = S_SHOW_CHAR;
    }
    break;

  case S_SHOW_CHAR: 
    colData = cBuf[curLen++];
    if (curLen < showLen)
      break;

    showLen = (*p != '\0' ? CHAR_SPACING : (MAX_DEVICES*COL_SIZE)/2);
    curLen = 0;
    state = S_SHOW_SPACE;

  case S_SHOW_SPACE:  
    curLen++;
    if (curLen == showLen)
      state = S_NEXT_CHAR;
    break;

  default:
    state = S_IDLE;
  }

  return(colData);
}

EspMQTTClient client(
  "ssid",
  "password",
  "192.168.178.44",  // MQTT Broker server ip
  "user",   // Can be omitted if not needed
  "password",   // Can be omitted if not needed
  "MAX7219_DISPLAY",     // Client name that uniquely identify your device
  1883              // The MQTT port, default to 1883. this line can be omitted
);

void scrollText(void)
{
  static uint32_t prevTime = 0;

  // Is it time to scroll the text?
  if (millis() - prevTime >= SCROLL_DELAY)
  {
   //mx.transform(MD_MAX72XX::TSL);  // scroll along - the callback will load all the data
   mx.transform(MD_MAX72XX::TSL);  // scroll along - the callback will load all the data
    prevTime = millis();      // starting point for next time
  }
}

void setup()
{
  Serial.begin(115200);
  mx.begin();
  mx.control(MD_MAX72XX::INTENSITY, INTENSITY);
  mx.setShiftDataInCallback(scrollDataSource);
  mx.setShiftDataOutCallback(scrollDataSink);
  curMessage[0] = newMessage[0] = '\0';
  sprintf(curMessage, "Smart Display");
}

void onConnectionEstablished()
{
// MQTT subscription topic for display text
  client.subscribe("strom/smartmeter/sensor/1/obis/1-0:16.7.0/255/value", [](const String & payload) {
    sprintf(curMessage, payload.c_str());
  });
  
// MQTT subscription topic for display intensity controll 
client.subscribe("leddisplay/intensity", [](const String & payload) {
    mx.control(MD_MAX72XX::INTENSITY, payload.toInt());
  });

// MQTT subscription topic for display scroll speed controll
  client.subscribe("leddisplay/scroll", [](const String & payload) {
    SCROLL_DELAY = payload.toInt();
  });
}

void loop()
{
  client.loop();
  scrollText();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.