B4R Code Snippet GFX and Bitmaps

This code uses inline C to store 1-bit bitmaps in the program code (PROGMEM) and draws them with the rAdafruitGFX library.

You can use this tool to generate the bitmaps data: https://github.com/ehubin/Adafruit-GFX-Library/blob/master/Img2Code/Image2Code.jar

SS-2017-05-25_09.02.33.jpg


B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private ssd As AdafruitSSD1306
   Private bmpx, bmpy As Int 'ignore
   Private imageWidth, imageHeight As Int 'ignore
End Sub


Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   ssd.InitializeI2C(4, 0x3c)
   ssd.ClearDisplay
   For i = 0 To 3
     DrawBitmap(32 * i, 0, 32, 32)
   Next
End Sub

Private Sub DrawBitmap(x As Int, y As Int, Width As Int, Height As Int)
   bmpx = x
   bmpy = y
   imageWidth = Width
   imageHeight = Height
   RunNative("drawBitmap", ssd.GFX)
   ssd.Display
End Sub

#if C
#include <avr/pgmspace.h>

static const unsigned char PROGMEM bmp1[] =
{
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xe0,0x7,0xff,
0xff,0x80,0x0,0xff,
0xfe,0x0,0x0,0x7f,
0xfc,0xf,0xf0,0x3f,
0xf8,0x3f,0xfc,0x1f,
0xf8,0x7f,0xff,0xf,
0xf0,0xff,0xff,0x87,
0xe1,0xff,0xff,0x87,
0xe3,0xe7,0xff,0xc3,
0xc3,0xc3,0xc3,0xe3,
0xc7,0xc3,0x81,0xe3,
0xc7,0xc3,0xc3,0xe1,
0xc7,0xe7,0xff,0xe1,
0xc7,0xff,0xff,0xf1,
0xc7,0xff,0xff,0xf1,
0xc7,0xff,0xff,0xe1,
0xc7,0xdf,0xf9,0xe1,
0xc7,0x8f,0xf1,0xe3,
0xc3,0x87,0xe1,0xe3,
0xe3,0xc0,0x1,0xc3,
0xe1,0xe0,0x7,0xc7,
0xf0,0xf8,0xf,0x87,
0xf0,0x7f,0xff,0xf,
0xf8,0x3f,0xfc,0x1f,
0xfc,0xf,0xf8,0x3f,
0xfe,0x0,0x0,0x7f,
0xff,0x80,0x0,0xff,
0xff,0xe0,0x3,0xff,
0xff,0xfe,0x7f,0xff,
0xff,0xff,0xff,0xff
};

void drawBitmap(B4R::Object* o) {
   B4R::B4RAdafruitGFX* bgfx = (B4R::B4RAdafruitGFX*)o->data.PointerField;
   bgfx->gfx->drawBitmap(b4r_main::_bmpx, b4r_main::_bmpy, bmp1,b4r_main::_imagewidth
      , b4r_main::_imageheight, 1);
}
#End If

If you need to draw different bitmaps then duplicate the drawBitmap C method and change bmp1.
 

Peter Simpson

Expert
Licensed User
Longtime User
Cheer for this code @Erel.
I was using "LCDAssistant.exe" for converting monochromatic bitmaps files to data arrays. But after trying Image2Code.jar I've decided swapped over to it :)
 

Cableguy

Expert
Licensed User
Longtime User
Can this same approach be used with 256 Color bitmaps?
I have found a few online converters but can't get them to compile
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Top