Display custom text on MAX7219

Hi there,

I want to display custom text on my MAX7219 via ESPHome. So I created a input_text helper, which I want to update and the updates text should then be displayed on the matrix.

This is my ESPHome device config:

spi:
  clk_pin: D5
  mosi_pin: D7

display:
  - platform: max7219digit
    cs_pin: D4
    num_chips: 4
    intensity: 15
    lambda: |-
      it.printf(0, 0, id(digit_font), "%s", id(matrix_text).state);
      
font:
  - file: "common/pixelmix.ttf"
    id: digit_font
    size: 9

time:
  - platform: homeassistant
    id: hass_time
    
text_sensor:
  - platform: homeassistant
    name: "Matrix Text"
    id: matrix_text
    entity_id: input_text.matrix_text

Can this work? I get this error on compile:

src/main.cpp: In lambda function:
src/main.cpp:262:59: error: cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'
       it.printf(0, 0, digit_font, "%s", matrix_text->state);
                                                           ^
src/main.cpp:262:59: warning: format '%s' expects argument of type 'char*', but argument 6 has type 'std::string {aka std::basic_string<char>}' [-Wformat=]

Can somebody tell my what I need to change in order to get this working?

Thanks!

Hi Zoker!
Seems here the answer - https://esphome.io/components/text_sensor/mqtt_subscribe.html

it.printf(0, 0, id(font), "The data is: %s", id(mysensor).state.c_str());
1 Like

Yes thanks, now it compiles and does not throw any issue anymore. But the matrix just stays empty. Do you have any idea why?

The entity does have this value:

Looks like alignment issue?!. Try to play with font size and TextAlign::BOTTOM_LEFT
I will try the same on my test matrix panel.

UPDATE:
Checked. Works correctly in my case. I’m using codi.ttf font - https://www.dafont.com/codi.font

Your yaml does not include the API.
Did you forget that or is it excluded from your copy paste here?

I actually forgot to add the ESPHome device to HA (I didnt notice before, because the time component seemed to work without adding it).

Yes I forgot to copy it, but it was/is there :slight_smile:

:smile: I caught it myself.
Another good check is:

if (id(matrix_text).has_state()) {
   it.printf(0, 0, id(digit_font), "%s", id(matrix_text).state.c_str());
} else {
   it.printf(24, 0, id(digit_font), "Nothing to show");
}

Can you give some instructions for this? The letters are on the half top left.

Here its written AAAAAA. I have tried everything BOTTOM, CENTER, BASELINE, BOTTOM_RIGHT, but when i put any of this the letters disappear. Any thoughts?
Thanks

This is a bug in the current version. A fix for it is already on the way:

In the mean time you can solve this by including the TextAlign parameter:

it.printf(0, 0, id(digit_font), TextAlign::TOP_LEFT, "%s", id(matrix_text).state.c_str());

Hope this help :slight_smile:

2 Likes

Thanks, i will try it.
Works great. Thanks

Cool! How I missed that! This really help with my issue - MAX7219 text alignment confused me
But I still confused which alignment does font has by default. :roll_eyes:

That’s why I love forums. Everyone helps everyone :smiley:
I think all it should use TOP_LEFT, but maybe there are some more bugs here.

@machine2669 BTW: The text disappears on different text alignment, because the position must be adjusted as well. So if you for example set TOP_RIGHT, the text will be gone, since it starts on pixel 0 and from there the text grows to the left (so into a area, where is not pixels). I hope this is in any way understandable :sweat_smile: