Adafruit-GFX-Library icon indicating copy to clipboard operation
Adafruit-GFX-Library copied to clipboard

Add `getPixel` and `getRawPixel` Methods to GFXCanvas Classes and Enhance PROGMEM Toggling in Converter

Open KaliAssistant opened this issue 1 year ago • 0 comments

Pull Request: Add getPixel and getRawPixel Methods to GFXCanvas Classes and Enhance PROGMEM Toggling in Converter

Summary

This PR introduces new methods to the GFXcanvas1, GFXcanvas8, and GFXcanvas16 classes in Adafruit_GFX.cpp, enabling retrieval of pixel values with rotation adjustments. Additionally, it enhances the toggle_progmem_blocks function in converter.py to allow toggling all blocks at once.

Changes

1. Adafruit_GFX.cpp and Adafruit_GFX.h

  • New Methods:

    • getPixel(int16_t x, int16_t y): Returns the color value of a pixel at the specified (x, y) coordinates, adjusted for canvas rotation.
    • getRawPixel(int16_t x, int16_t y): Returns the color value of a pixel at the specified (x, y) coordinates without rotation adjustments. This method is intended for hardware drivers needing unrotated physical coordinates.
  • Header Updates:

    • Added getPixel as a public method and getRawPixel as a protected method in each canvas class.

2. converter.py

  • Enhanced toggle_progmem_blocks Functionality:
    • Added an ALL option to toggle all blocks in the converter at once, except for Block 00, which is always set to reside in PROGMEM.

Rationale

These additions provide more flexibility in accessing pixel data across different canvases with rotation handling. The enhanced toggle_progmem_blocks option in the font converter simplifies managing PROGMEM blocks, especially when working with multiple blocks.


Files Modified:

  • Adafruit_GFX.cpp
  • Adafruit_GFX.h
  • converter.py

Example Usage:

// For rotated pixel retrieval:
GFXcanvas1 canvas(128, 64);
bool color = canvas.getPixel(x, y); // With rotation adjustment

// For unrotated pixel retrieval:
bool rawColor = canvas.getRawPixel(x, y); // Physical coordinates only

Works on my ESP32S3 (16M flash, 2M PSRAM) fb.h

#include <Adafruit_GFX.h>

GFXcanvas1* charCanvas = nullptr;


void fb_char_canvas2frameBuffer(char* s, int32_t x, int32_t y, uint16_t color, uint16_t bg, uint8_t textSize, uint16_t charWidth, uint16_t charHeight)
{
    if (!charCanvas || charCanvas->width() != charWidth || charCanvas->height() != charHeight) {
        if (charCanvas) delete charCanvas;  // Delete old canvas if exists
        charCanvas = new GFXcanvas1(charWidth, charHeight);  // Create new canvas with the correct size
    }
    charCanvas->fillScreen(bg);
    charCanvas->setCursor(0, 0);
    charCanvas->setTextColor(color);
    charCanvas->setTextSize(textSize);
    charCanvas->printUTF8(s);
    //charCanvas->writeCodepoint(codepoint);
    for (int16_t i = 0; i < charHeight; i++) {
        for (int16_t j = 0; j < charWidth; j++) {
            if (charCanvas->getPixel(j, i)) {
                int bufIndex = (y + i) * TFT__WIDTH + (x + j);
                if (bufIndex < TFT__WIDTH * TFT__HEIHT) {
                    frameBuffer[bufIndex] = color;  // Set pixel to specified color
                }
            }
        }
    }
}

main.cpp

fb_char_canvas2frameBuffer("哈", 0, 0, 0xFFFF, 0x0000, 1, 16, 16);   // Display Chinese "哈"

PR

KaliAssistant avatar Nov 03 '24 03:11 KaliAssistant