Adafruit-GFX-Library
Adafruit-GFX-Library copied to clipboard
Add `getPixel` and `getRawPixel` Methods to GFXCanvas Classes and Enhance PROGMEM Toggling in Converter
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
getPixelas a public method andgetRawPixelas a protected method in each canvas class.
- Added
2. converter.py
- Enhanced
toggle_progmem_blocksFunctionality:- Added an
ALLoption to toggle all blocks in the converter at once, except for Block 00, which is always set to reside inPROGMEM.
- Added an
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.cppAdafruit_GFX.hconverter.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 "哈"