Uno R4 Led Matrix in shared library

I wonder if anyone can help, I have the following (minimal) code which is not updating the LED matrix on my Arduino R4. if compiled directly into the .ino file all goes well, but as a potential shared class it fails to update the Led Matrix.

C++ is not my first language so hopefully it is just something dumb that I am doing wrong, but any help greatly appreciated.

.ino File:

#include <Arduino_LED_Matrix.h>
#include "LedManager.h"

LedManager ledManager;

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

void loop() {
    ledManager.ProcessLedMatrix(millis());
}

LedManager.h

#ifndef __LedManager__H
#define __LedManager__H

#include <Arduino_LED_Matrix.h>

const int MaxLedRows = 8;
const int MaxLedColumns = 12;
const byte LedOn = 1;
const byte LedOff = 0;

class LedManager
{
private:
    bool _isOn = false;
    unsigned long _lastUpdate = 0;
	ArduinoLEDMatrix *_matrix;
	byte _ledFrame[MaxLedRows][MaxLedColumns];
public:
	LedManager();
	~LedManager();
	void ProcessLedMatrix(unsigned long currMillis);
};


#endif

LedManager.cpp

#include "LedManager.h"

LedManager::LedManager()
{
	_matrix = new ArduinoLEDMatrix();
	_matrix->begin();
}

LedManager::~LedManager()
{
	delete(_matrix);
}

void LedManager::ProcessLedMatrix(unsigned long currMillis)
{
    if (currMillis > _lastUpdate)
    {
        _isOn = !_isOn;
        _ledFrame[5][4] = _isOn ? LedOn : LedOff;
        _matrix->renderBitmap(_ledFrame, 8, 12);    
        _lastUpdate = currMillis + 1000;
        Serial.print("Is on: ");
        Serial.println(_isOn);
    }
}

All compiles without warning and errors it just doesn't update the LED matrix.

The constructor is called super early in the C++ program boot sequence. It is likely that the environment is not ready or that the main() that is called later on changes what you have done with that begin.

the rationale for having a begin() function is that you call it when the hardware is fully configured, from your setup.

I would suggest to modify your class to include a begin function and call that from the setup()

#include "LedManager.h"

LedManager::LedManager() {
	_matrix = new ArduinoLEDMatrix();
}

bool  LedManager::begin() {
  return _matrix->begin();
}


(of course declare bool begin(); in the .h too)

and the ino file would become

#include <Arduino_LED_Matrix.h>
#include "LedManager.h"

LedManager ledManager;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  ledManager.begin(); // <== add this
}

void loop() {
  ledManager.ProcessLedMatrix(millis());
}

PS: also I would not use new in the constructor, just have an ArduinoLEDMatrix instance variable. this way you don't need a destructor.

1 Like

Your advice was spot on, thank you very much

as a fun exercice, your class could be a subclass of ArduinoLEDMatrix :wink:

I might try that, once my current project is completed :slight_smile:

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