initial commit
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
//
|
||||
// 1-bpp PNG example - drawing transparent images on an SSD1306 OLED
|
||||
// written by Larry Bank
|
||||
// May 31, 2021
|
||||
//
|
||||
|
||||
#include <PNGdec.h>
|
||||
#include <OneBitDisplay.h>
|
||||
// really 2-bpp to get a transparent color in the palette
|
||||
#include "octocat_1bpp.h"
|
||||
|
||||
#define USE_HW_I2C 1
|
||||
#define INVERT 0
|
||||
#define FLIP180 0
|
||||
#define OLED_ADDR -1
|
||||
OBDISP obd;
|
||||
PNG png;
|
||||
static uint8_t ucBackBuffer[1024];
|
||||
|
||||
//
|
||||
// Draw 1-bpp pixels onto the back buffer while respecting the transparent mask
|
||||
// This needs to be done in the sketch and not the library because it's very
|
||||
// specific to the memory layout of the SSD1306
|
||||
//
|
||||
void DrawPixelsMasked(int y, uint8_t *pPixels, uint8_t *pMask, int width)
|
||||
{
|
||||
int x, iBit;
|
||||
uint8_t *d, ucPix, ucAlpha, ucMask;
|
||||
// The display has the bytes oriented vertically while the bitmap has them packed horizontally
|
||||
d = &ucBackBuffer[(y >> 3) * 128 + 25]; // pointer to display memory (center it horizontally too)
|
||||
ucMask = 1 << (y & 7); // bit position of display memory (LSB on top)
|
||||
for (x=0; x<width; x+= 8) { // read the source image one byte at a time
|
||||
ucAlpha = *pMask++; // alpha mask is always 1-bit per pixel
|
||||
for (iBit=0; iBit<8; iBit++) {
|
||||
if (iBit == 0 || iBit == 4) // source image is 2-bpp, so work in groups of 4 per byte
|
||||
ucPix = *pPixels++;
|
||||
if (ucAlpha & 0x80) { // this pixel is opaque
|
||||
if ((ucPix & 0xc0) == 0x80) // white? a bit of a hack for this image; I should do a palette lookup here
|
||||
*d |= ucMask;
|
||||
else
|
||||
*d &= ~ucMask;
|
||||
} // pixel is opaque
|
||||
ucAlpha <<= 1;
|
||||
ucPix <<= 2;
|
||||
d++;
|
||||
} // for each bit/pixel
|
||||
} // for each group of 8 pixels
|
||||
} /* DrawPixelsMasked() */
|
||||
|
||||
int PNGDraw(PNGDRAW *pDraw)
|
||||
{
|
||||
uint8_t ucMask[32];
|
||||
|
||||
if (png.getAlphaMask(pDraw, ucMask, 255)) { // if any pixels are opaque, draw them
|
||||
DrawPixelsMasked(pDraw->y, pDraw->pPixels, ucMask, pDraw->iWidth);
|
||||
}
|
||||
return 1;
|
||||
} /* PNGDraw() */
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
obdI2CInit(&obd, OLED_128x64, OLED_ADDR, FLIP180, INVERT, USE_HW_I2C, -1, -1, -1, 400000L);
|
||||
obdSetBackBuffer(&obd, ucBackBuffer);
|
||||
obdFill(&obd, 0, 1);
|
||||
} /* setup() */
|
||||
|
||||
void loop() {
|
||||
int i, rc;
|
||||
char szTemp[256];
|
||||
|
||||
// create a 'gray' background pattern on the OLED
|
||||
for (i=0; i<1024; i+=2) {
|
||||
ucBackBuffer[i] = 0x55;
|
||||
ucBackBuffer[i+1] = 0xaa;
|
||||
}
|
||||
obdDumpBuffer(&obd, NULL); // display gray background
|
||||
delay(2000);
|
||||
rc = png.openRAM((uint8_t *)octocat_1bpp, sizeof(octocat_1bpp), PNGDraw);
|
||||
if (rc == PNG_SUCCESS) {
|
||||
sprintf(szTemp, "image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
|
||||
Serial.print(szTemp);
|
||||
rc = png.decode(NULL, 0); // no private structure and skip CRC checking
|
||||
obdDumpBuffer(&obd, NULL); // show the image
|
||||
png.close();
|
||||
} // png opened successfully
|
||||
delay(2000); // rinse, repeat
|
||||
} /* loop() */
|
||||
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// octocat_1bpp
|
||||
// Data size = 373 bytes
|
||||
//
|
||||
// PNG, Compression=Flate, Size: 77 x 64, 2-Bpp
|
||||
//
|
||||
// for non-Arduino builds...
|
||||
#ifndef PROGMEM
|
||||
#define PROGMEM
|
||||
#endif
|
||||
const uint8_t octocat_1bpp[] PROGMEM = {
|
||||
0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
|
||||
0x00,0x00,0x00,0x4d,0x00,0x00,0x00,0x40,0x02,0x03,0x00,0x00,0x00,0x22,0xf9,0x12,
|
||||
0xfd,0x00,0x00,0x00,0x09,0x50,0x4c,0x54,0x45,0x00,0xf0,0x7a,0x00,0x00,0x00,0xff,
|
||||
0xff,0xff,0x34,0xae,0x54,0xfd,0x00,0x00,0x00,0x01,0x74,0x52,0x4e,0x53,0x00,0x40,
|
||||
0xe6,0xd8,0x66,0x00,0x00,0x01,0x1a,0x49,0x44,0x41,0x54,0x38,0xcb,0xb5,0xd3,0x31,
|
||||
0x6e,0xc3,0x30,0x0c,0x00,0x40,0x3a,0x80,0x96,0xcc,0xf5,0x13,0xba,0xf4,0x15,0xe9,
|
||||
0x90,0xdd,0x05,0x24,0x01,0xd6,0xd4,0x25,0x05,0xea,0x57,0xf8,0x13,0xce,0xdc,0x45,
|
||||
0x41,0xc4,0x57,0x56,0xb4,0x25,0x87,0x94,0x9c,0x66,0x68,0xcb,0x45,0xc0,0x81,0x22,
|
||||
0x21,0x93,0x06,0xb8,0x1b,0x4f,0xe9,0xd4,0x1c,0x4d,0x37,0x1f,0x4a,0xa2,0xe1,0xc7,
|
||||
0x8a,0xaf,0xc6,0x98,0xa3,0xc0,0xc6,0xe4,0x78,0x84,0xf0,0x0f,0x78,0x6b,0x74,0xb8,
|
||||
0xa1,0x5a,0xb1,0x7b,0x80,0xed,0x8a,0xfa,0x0f,0xd1,0x21,0x45,0x90,0x88,0x4b,0x6c,
|
||||
0xe2,0xc4,0xd1,0x26,0xf4,0x1c,0xdd,0x36,0x5e,0xee,0x61,0x18,0x0a,0xa4,0x56,0xd3,
|
||||
0x10,0x24,0x52,0x2b,0x2f,0x32,0x95,0xc3,0x33,0x21,0x16,0x98,0xba,0xb3,0x4f,0xd7,
|
||||
0xd0,0xe5,0x4b,0x81,0xb0,0xbe,0x88,0x8d,0x03,0xda,0xfc,0x76,0x3e,0xcd,0x54,0x34,
|
||||
0x88,0xad,0x6b,0x4c,0xd5,0x27,0x7f,0x12,0x2f,0xc6,0xae,0xd3,0x3a,0xf0,0x36,0x60,
|
||||
0x97,0x25,0x11,0xb6,0x53,0x87,0x79,0x49,0x3a,0x89,0x63,0x8d,0xd0,0xbc,0xcf,0x4b,
|
||||
0xa2,0xe5,0x2f,0x63,0xe2,0x98,0xde,0x4a,0xb4,0x1f,0xa7,0x1a,0xe3,0x36,0x1c,0x2b,
|
||||
0x54,0x84,0xba,0x40,0x68,0xd5,0x06,0xc2,0xaf,0xb1,0xb7,0x15,0x3e,0x9f,0x7a,0x67,
|
||||
0x47,0x69,0x2f,0x9e,0x10,0x85,0xee,0x71,0xc1,0xab,0x48,0x4c,0xc8,0x53,0x77,0x71,
|
||||
0xb8,0xbe,0x77,0x13,0xe2,0x17,0xc7,0x60,0x23,0xfa,0x81,0xdf,0xdf,0xd3,0xd4,0x3e,
|
||||
0x69,0x76,0x57,0x71,0x3d,0xee,0xa1,0xa9,0xf1,0x1c,0x97,0x4e,0xd4,0x04,0xcc,0x31,
|
||||
0x82,0x2c,0x3a,0x87,0x78,0xd1,0x46,0xe2,0x52,0x55,0x56,0xcc,0x15,0xe0,0xc7,0xf8,
|
||||
0x06,0xa3,0x85,0xe6,0x10,0x4b,0x49,0x02,0x6a,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,
|
||||
0x44,0xae,0x42,0x60,0x82};
|
||||
Reference in New Issue
Block a user