pixelmatix / SmartMatrix

SmartMatrix Library for Teensy 3, Teensy 4, and ESP32
http://docs.pixelmatix.com/SmartMatrix
611 stars 161 forks source link

Using a custom font misplaces the text with GFX enabled #152

Closed fcapano closed 2 years ago

fcapano commented 2 years ago

In the following code, when using the default font, the text is displayed correctly (1.png). If a custom font is selected (TomThumb in this case) the text is moved up and drawn off screen (2.png).

#define USE_ADAFRUIT_GFX_LAYERS

#include <MatrixHardware_ESP32_V0.h>
#include <SmartMatrix.h>
#include <Fonts/TomThumb.h>

//////////////////////////////////////////////////////////////////////

#define COLOR_DEPTH 24                                                      // Choose the color depth used for storing pixels in the layers: 24 or 48 (24 is good for most sketches - If the sketch uses type `rgb24` directly, COLOR_DEPTH must be 24)
const uint16_t kMatrixWidth     = 64;                                       // Set to the width of your display, must be a multiple of 8
const uint16_t kMatrixHeight    = 32;                                       // Set to the height of your display
const uint8_t  kRefreshDepth    = 24;                                       // Tradeoff of color quality vs refresh rate, max brightness, and RAM usage.  36 is typically good, drop down to 24 if you need to.  On Teensy, multiples of 3, up to 48: 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48.  On ESP32: 24, 36, 48
const uint8_t  kDmaBufferRows   = 4;                                        // known working: 2-4, use 2 to save RAM, more to keep from dropping frames and automatically lowering refresh rate.  (This isn't used on ESP32, leave as default)
const uint8_t  kPanelType       = SM_PANELTYPE_HUB75_32ROW_MOD16SCAN;       // Choose the configuration that matches your panels.  See more details in MatrixCommonHub75.h and the docs: https://github.com/pixelmatix/SmartMatrix/wiki
const uint32_t kMatrixOptions   = (SMARTMATRIX_OPTIONS_ESP32_INVERT_CLK);   // see docs for options: https://github.com/pixelmatix/SmartMatrix/wiki

//////////////////////////////////////////////////////////////////////

SMARTMATRIX_ALLOCATE_BUFFERS(matrix, kMatrixWidth, kMatrixHeight, kRefreshDepth, kDmaBufferRows, kPanelType, kMatrixOptions);

// There are two Adafruit_GFX compatible layers, a Mono layer and an RGB layer, and this example sketch works with either (choose one):
#if 0
const uint8_t kBackgroundLayerOptions = (SM_BACKGROUND_OPTIONS_NONE);
SMARTMATRIX_ALLOCATE_BACKGROUND_LAYER(backgroundLayer, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kBackgroundLayerOptions);
#else
const uint8_t kMonoLayerOptions = (SM_GFX_MONO_OPTIONS_NONE);
SMARTMATRIX_ALLOCATE_GFX_MONO_LAYER(backgroundLayer, kMatrixWidth, kMatrixHeight, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kMonoLayerOptions);
#endif

#define BLACK 0x0000
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

void testText()
{
    backgroundLayer.fillScreen(BLACK);
    backgroundLayer.setCursor(0, 0);
    backgroundLayer.setTextColor(WHITE);
    backgroundLayer.setTextSize(1);
    backgroundLayer.println("Hello World!");
    backgroundLayer.setTextColor(YELLOW);
    backgroundLayer.setTextSize(2);
    backgroundLayer.setTextColor(YELLOW);
    backgroundLayer.setTextSize(2);
    backgroundLayer.println(1234.56);
}

void setup()
{
    Serial.begin(115200);
    matrix.addLayer(&backgroundLayer);
    delay(1000);
    matrix.begin();
    matrix.setBrightness(22);

    // Enable/Disable this line
    backgroundLayer.setFont(&TomThumb);
}

void loop()
{
    testText();
    backgroundLayer.swapBuffers();
}

1 2

embedded-creations commented 2 years ago

This is how AdafruitGFX works. Search this for “classic font”:

https://learn.adafruit.com/adafruit-gfx-graphics-library/using-fonts