Add 8-bit palette mode

This commit is contained in:
Jeff Epler 2025-02-18 16:12:47 -06:00
parent ba25264d3e
commit 32de12a0e8
3 changed files with 87 additions and 69 deletions

View file

@ -8,7 +8,7 @@ DVHSTX16 display(DVHSTX_PINOUT_METRO_RP2350, DVHSTX_RESOLUTION_320x240);
void setup() {
Serial.begin(115200);
// while(!Serial);
//while(!Serial);
if (!display.begin()) { // Blink LED if insufficient RAM
pinMode(LED_BUILTIN, OUTPUT);
for (;;) digitalWrite(LED_BUILTIN, (millis() / 500) & 1);
@ -16,13 +16,8 @@ void setup() {
Serial.println("display initialized");
}
int i, j=1;
void loop() {
// Draw random lines
display.drawLine(random(display.width()), random(display.height()), random(display.width()), random(display.height()), random(65536));
i++;
if (i % j == 0) {
j *= 10;
Serial.printf("%d\n", i);
}
sleep_ms(1);
}

View file

@ -0,0 +1,21 @@
// 16-bit Adafruit_GFX-compatible framebuffer for RP2350 HSTX
#include <Adafruit_dvhstx.h>
DVHSTX8 display(DVHSTX_PINOUT_METRO_RP2350, DVHSTX_RESOLUTION_640x360);
void setup() {
Serial.begin(115200);
//while(!Serial);
if (!display.begin()) { // Blink LED if insufficient RAM
pinMode(LED_BUILTIN, OUTPUT);
for (;;) digitalWrite(LED_BUILTIN, (millis() / 500) & 1);
}
Serial.println("display initialized");
}
void loop() {
// Draw random lines
display.drawLine(random(display.width()), random(display.height()), random(display.width()), random(display.height()), random(65536));
sleep_ms(1);
}

View file

@ -53,6 +53,7 @@ public:
bool result = hstx.init(dvhstx_width(res), dvhstx_height(res), pimoroni::DVHSTX::MODE_RGB565, double_buffered, pinout);
if (!result) return false;
buffer = hstx.get_back_buffer<uint16_t>();
fillScreen(0);
return true;
}
void end() { hstx.reset(); }
@ -65,71 +66,72 @@ public:
/**********************************************************************/
void swap(bool copy_framebuffer = false);
/**********************************************************************/
/*!
@brief Convert 24-bit RGB value to a framebuffer value
@param r The input red value, 0 to 255
@param g The input red value, 0 to 255
@param b The input red value, 0 to 255
@return The corresponding 16-bit pixel value
*/
/**********************************************************************/
uint16_t color565(uint8_t red, uint8_t green, uint8_t blue) {
return ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | (blue >> 3);
}
private:
DVHSTXPinout pinout;
DVHSTXResolution res;
mutable pimoroni::DVHSTX hstx;
bool double_buffered;
};
class DVHSTX8 : public GFXcanvas8 {
public:
/**************************************************************************/
/*!
@brief Instatiate a DVHSTX 8-bit canvas context for graphics
@param res Display resolution
@param double_buffered Whether to allocate two buffers
*/
/**************************************************************************/
DVHSTX8(DVHSTXPinout pinout, DVHSTXResolution res, bool double_buffered=false) : GFXcanvas8(dvhstx_width(res), dvhstx_height(res), false), pinout(pinout), res{res}, double_buffered{double_buffered} {}
~DVHSTX8() { end(); }
bool begin() {
bool result = hstx.init(dvhstx_width(res), dvhstx_height(res), pimoroni::DVHSTX::MODE_PALETTE, double_buffered, pinout);
if (!result) return false;
for(int i=0; i<255; i++ ) {
uint8_t r = (i >> 6) * 255 / 3;
uint8_t g = ((i >> 2) & 7) * 255 / 7;
uint8_t b = (i & 3) * 255 / 3;
setColor(i, r, g, b);
}
buffer = hstx.get_back_buffer<uint8_t>();
fillScreen(0);
return true;
}
void end() { hstx.reset(); }
void setColor(uint8_t idx, uint8_t red, uint8_t green, uint8_t blue) {
hstx.get_palette()[idx] = (red << 16) | (green << 8) | blue;
}
void setColor(uint8_t idx, uint32_t rgb) {
hstx.get_palette()[idx] = rgb;
}
/**********************************************************************/
/*!
@brief If double-buffered, wait for retrace and swap buffers. Otherwise, do nothing (returns immediately)
@param copy_framebuffer if true, copy the new screen to the new back buffer. Otherwise, the content is undefined.
*/
/**********************************************************************/
void swap(bool copy_framebuffer = false);
private:
DVHSTXPinout pinout;
DVHSTXResolution res;
mutable pimoroni::DVHSTX hstx;
bool double_buffered;
};
#if 0
class DVHSTX8 : GFXcanvas8 {
/**************************************************************************/
/*!
@brief Instatiate a DVHSTX 8-bit canvas context for graphics
@param res Display resolution
@param double_buffered Whether to allocate two buffers
*/
/**************************************************************************/
DVHSTX8(DVHSTXResolution res, bool double_buffered=false);
~DVHSTX8() { end(); }
bool begin() {
return hstx.init(width, height, MODE_PALETTE, double_buffered);
}
void end() { hstx.reset(); }
void drawPixel(int16_t x, int16_t y, uint16_t color);
void fillScreen(uint16_t color);
void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
bool getPixel(int16_t x, int16_t y) const;
/**********************************************************************/
/*!
@brief Get a pointer to the internal buffer memory (current back buffer)
@returns A pointer to the allocated buffer
*/
/**********************************************************************/
uint8_t *getBuffer(void) const { return hstx.get_back_buffer<uint8_t>(); }
/**********************************************************************/
/*!
@brief If double-buffered, wait for retrace and swap buffers. Otherwise, do nothing (returns immediately)
@param copy_framebuffer if true, copy the new screen to the new back buffer. Otherwise, the content is undefined.
*/
/**********************************************************************/
void swap(bool copy_framebuffer = false);
protected:
bool getRawPixel(int16_t x, int16_t y) const;
void drawFastRawVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
void drawFastRawHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
};
class DVHSTXTEXT1 : GFXcanvas16 {
DVHSTX16();
bool begin() {
return hstx.init(1280, 720, MODE_TEXT_MONO, true);
}
using TextColor = pimoroni::DVHSTX::TextColour;
void end() { hstx.reset(); }
};
class DVHSTXTEXT3 : GFXcanvas16 {
DVHSTX16();
bool begin() {
return hstx.init(1280, 720, MODE_TEXT_RGB111, true);
}
void end() { hstx.reset(); }
};
#endif