hzeller / rpi-rgb-led-matrix

Controlling up to three chains of 64x64, 32x32, 16x32 or similar RGB LED displays using Raspberry Pi GPIO
GNU General Public License v2.0
3.67k stars 1.17k forks source link

How to load font file ? What is the proper path to a font file ? Because NOTHING is working ! #828

Closed GeorgeFlorian closed 5 years ago

GeorgeFlorian commented 5 years ago

Hello !

I am trying to make a simple code that can output a line of text on the display. But the font isn't loading.

This is what I've used to load the desired font:

const char *fontFile = "fonts/10x20.bdf";
//const char *fontFile = "10x20.bdf";
static void TextLine(Canvas *canvas, int x, int y, const char* line) {
    canvas->Fill(0,0,0);
    rgb_matrix::Color mRed(225, 20, 34);
    rgb_matrix::Color blackBg(0, 0, 0);
    rgb_matrix::Font font;
    if(!font.LoadFont(fontFile)) {
        std::cout<<"Couldn't load font file\n";
    }
    rgb_matrix::DrawText(canvas, font, x, y, mRed, &blackBg, line);

    int k = 10;
    while (k != 0) {
        TextLine(canvas,0,0,"Test 1");
        k--;
        usleep(1 * 1000);
    }

    canvas->Clear();
    delete canvas;
}

This is the full code:

#include "led-matrix.h"
#include "graphics.h"

#include <iostream>
#include <unistd.h>
#include <math.h>
#include <stdio.h>
#include <signal.h>

using rgb_matrix::GPIO;
using rgb_matrix::RGBMatrix;
using rgb_matrix::Canvas;

volatile bool interrupt_received = false;
static void InterruptHandler(int signo) {
  interrupt_received = true;
}

const char *fontFile = "fonts/10x20.bdf";

static void TextLine(Canvas *canvas, int x, int y, const char* line) {
    canvas->Fill(0,0,0);
    rgb_matrix::Color mRed(225, 20, 34);
    rgb_matrix::Color blackBg(0, 0, 0);
    rgb_matrix::Font font;
    if(!font.LoadFont(fontFile)) {
        std::cout<<"Couldn't load font file\n";
    }
    rgb_matrix::DrawText(canvas, font, x, y, mRed, &blackBg, line);

}

int main(int argc, char *argv[]) {

    RGBMatrix::Options defaults;
    defaults.hardware_mapping = "regular";  // or e.g. "adafruit-hat"
    defaults.rows = 32;
    defaults.chain_length = 1;
    defaults.parallel = 1;
    defaults.show_refresh_rate = true;
    Canvas *canvas = rgb_matrix::CreateMatrixFromFlags(&argc, &argv, &defaults);
    if (canvas == NULL)
        return 1;
    // It is always good to set up a signal handler to cleanly exit when we
    // receive a CTRL-C for instance. The DrawOnCanvas() routine is looking
    // for that.
    signal(SIGTERM, InterruptHandler);
    signal(SIGINT, InterruptHandler);

    int k = 10;
    while (k != 0) {
        TextLine(canvas,0,0,"Test 1");
        k--;
        usleep(1 * 1000);
    }

    canvas->Clear();
    delete canvas;

    return 0;
}

How do I properly load a font file ? Where did I go wrong ?

GeorgeFlorian commented 5 years ago

Guess what ? const char *fontFile = "rpi-rgb-led-matrix/fonts/10x20.bdf"; worked ! The font appears to be loading, but the code doesn't do anything. The display is black.