Skip to content

Commit

Permalink
Ardafruit GFX bitmap functions with example
Browse files Browse the repository at this point in the history
  • Loading branch information
techi602 committed Jul 8, 2016
1 parent d37e5c4 commit 39dad32
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 0 deletions.
87 changes: 87 additions & 0 deletions TFT_22_ILI9225.cpp
Expand Up @@ -554,3 +554,90 @@ uint16_t TFT_22_ILI9225::drawChar(uint16_t x, uint16_t y, uint16_t ch, uint16_t
return charWidth;
}

// Draw a 1-bit image (bitmap) at the specified (x,y) position from the
// provided bitmap buffer (must be PROGMEM memory) using the specified
// foreground color (unset bits are transparent).
void TFT_22_ILI9225::drawBitmap(int16_t x, int16_t y,
const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {

int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;

for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
if (i & 7) byte <<= 1;
else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
if (byte & 0x80) drawPixel(x + i, y + j, color);
}
}
}

// Draw a 1-bit image (bitmap) at the specified (x,y) position from the
// provided bitmap buffer (must be PROGMEM memory) using the specified
// foreground (for set bits) and background (for clear bits) colors.
void TFT_22_ILI9225::drawBitmap(int16_t x, int16_t y,
const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {

int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;

for (j = 0; j < h; j++) {
for (i = 0; i < w; i++ ) {
if (i & 7) byte <<= 1;
else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
if (byte & 0x80) drawPixel(x + i, y + j, color);
else drawPixel(x + i, y + j, bg);
}
}
}

// drawBitmap() variant for RAM-resident (not PROGMEM) bitmaps.
void TFT_22_ILI9225::drawBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {

int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;

for(j = 0; j < h; j++) {
for(i = 0; i < w; i++) {
if (i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
if (byte & 0x80) drawPixel(x + i, y + j, color);
}
}
}

// drawBitmap() variant w/background for RAM-resident (not PROGMEM) bitmaps.
void TFT_22_ILI9225::drawBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {

int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;

for (j = 0; j < h; j++) {
for (i = 0; i < w; i++ ) {
if (i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
if (byte & 0x80) drawPixel(x + i, y + j, color);
else drawPixel(x + i, y + j, bg);
}
}
}

//Draw XBitMap Files (*.xbm), exported from GIMP,
//Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
//C Array can be directly used with this function
void TFT_22_ILI9225::drawXBitmap(int16_t x, int16_t y,
const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {

int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;

for (j = 0; j < h; j++) {
for (i = 0; i < w; i++ ) {
if (i & 7) byte >>= 1;
else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
if (byte & 0x01) drawPixel(x + i, y + j, color);
}
}
}
6 changes: 6 additions & 0 deletions TFT_22_ILI9225.h
Expand Up @@ -270,6 +270,12 @@ class TFT_22_ILI9225 {
/// @param color 16-bit color, default=white
uint16_t drawChar(uint16_t x, uint16_t y, uint16_t ch, uint16_t color = COLOR_WHITE);

void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg);
void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg);
void drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);

private:

void _swap(uint16_t &a, uint16_t &b),
Expand Down

0 comments on commit 39dad32

Please sign in to comment.