Trying to use the PxMatrix library

I’m trying to use this PxMatrix library to drive a P4 display from a Wemos D1 Mini. I’m able to drive it correctly with standard Arduino configuration and am in the process of moving it over to ESPHome. When I use the PxMatrix library above using the following configuration I get the following error

external_components:
  - source:
      url: https://github.com/Syndlex/esphome-pxmatrix-component.git
      type: git
    components: [ pxmatrix ]

display:
  platform: pxmatrix
  id: my_display
  brightness: 150
  lambda: |-
    it.line(0, 0, 10, 10);
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
Dependency Graph
|-- ESPAsyncTCP-esphome @ 1.2.3
|-- ESPAsyncWebServer-esphome @ 2.1.0
|   |-- ESPAsyncTCP-esphome @ 1.2.3
|   |-- Hash @ 1.0
|   |-- ESP8266WiFi @ 1.0
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- noise-c @ 0.1.4
|   |-- libsodium @ 1.10018.1
|-- PxMatrix LED MATRIX library @ 1.8.2
|-- Wire @ 1.0
|-- SPI @ 1.0
|-- Ticker @ 1.0
|-- Adafruit BusIO @ 1.9.9
|-- Adafruit GFX Library @ 1.10.12
Compiling /data/led-screen/.pioenvs/led-screen/src/esphome/components/pxmatrix/pxmatrix_display.cpp.o
src/esphome/components/pxmatrix/pxmatrix_display.cpp:1:10: fatal error: pxmatrix.h: No such file or directory

I’ve tried forking the project and changing the include in the pxmatrix_display.cpp file to be PxMatrix.h instead and it gets past this error but starts spewing a bunch of errors about variables not be defined that appear to be defined in pxmatrix_display.h.

I’m hoping I’m missing something really simple in my ESPHome configuration instead of me having to spend a ton of time reworking this library that someone else already did. I’ve already filed an issue on the GitHub for the project but haven’t heard anything from the maintainer. They provided an example for the dmamatrix library bur not one for pxmatrix.

This pxmatrix_display project is depending on external references: pxmatrix.h. This is a header file which is not included in the repo that you linked above and you need to get it from somewhere else: 2dom/PxMatrix · GitHub

It’s a pity that author of the repo hasn’t mentioned this in the readme.MD

It appears that it does get included

|-- PxMatrix LED MATRIX library @ 1.8.2

and in the display.py it includes

# https://github.com/2dom/PxMatrix/blob/ea8dc954f486c0f260efd98b51dd1b41312bb556/PxMatrix.h
cg.add_library("PxMatrix LED MATRIX library", "1.8.2")

I’ve been able to make it a bit further with this.

  • Change #include "pxmatrix.h" to #include "PxMatrix.h"
  • Add #include "pxmatrix_display.h"
  • Remove the HUB75_I2S_CFG stanza in setup since that’s only for dmamatrix.

I’m now down to an error that I don’t know how to handle since I haven’t touched c++ in a decade and it’s changed a bit…

src/main.cpp: In function 'void setup()':
src/main.cpp:270:54: error: invalid new-expression of abstract class type 'esphome::pxmatrix_display::PxmatrixDisplay'
  270 |   my_display = new pxmatrix_display::PxmatrixDisplay();
      |                                                      ^
In file included from src/esphome.h:35,
                 from src/main.cpp:3:
src/esphome/components/pxmatrix/pxmatrix_display.h:44:7: note:   because the following virtual functions are pure within 'esphome::pxmatrix_display::PxmatrixDisplay':
   44 | class PxmatrixDisplay : public PollingComponent, public display::DisplayBuffer {
      |       ^~~~~~~~~~~~~~~
In file included from src/esphome.h:17,
                 from src/main.cpp:3:
src/esphome/components/display/display_buffer.h:402:23: note:     'virtual esphome::display::DisplayType esphome::display::DisplayBuffer::get_display_type()'
  402 |   virtual DisplayType get_display_type() = 0;
      |                       ^~~~~~~~~~~~~~~~

The get_display_type() was added last year to the DisplayBuffer. Since this is a pure virtual function, all inheriting classes must override this.

I assume that this PxmatrixDisplay hasn’t been maintained for a while. You can fix it locally by adding the following to the pxmatrix_display.h file on line 77:

protected:
  ...
  int get_height_internal() override;
  display::DisplayType get_display_type() override; // <<-- add this line

And the following to pxmatrix_display.cpp file on line 102:

int PxmatrixDisplay::get_height_internal() { return this->height_; }

display::DisplayType PxmatrixDisplay::get_display_type() { return display::DisplayType::DISPLAY_TYPE_COLOR; } // <<-- add this line

This will fix the compilation error but I can’t guarantee that the code would function.

Perfect! That got it to compile (I’ll push this as a patch upstream once I get some documentation written for it as well). My last question is that it seems to let you define the CONF_PIN_A and such to override the default GPIO pins. How do you that in the ESPHome config?

I guess similar to how you would do it for any other component that supports pins like pin_a, pin_b, etc. See Configuration Types and for an example: H-bridge Fan

So something like:

display:
  platform: pxmatrix
  # ...
  pin_a: GPIO5
  pin_b: GPIO4