SmallBASIC PiGPIO 2 logo
Last update: 08.11.2024

Graphic with Linux Framebuffer

Many SPI-displays provide a framebuffer device (i.e. /dev/fb0). With SmallBASIC GPIO 2’s framebuffer plugin graphics can be displayed on these displays. If you don’t have a graphical desktop running on your Raspberry Pi and you are using the terminal instead, the connected monitor is accessible as a framebuffer device, too.

Library

To use the framebuffer, import the library framebuffer

import framebuffer

Function Reference

Open Device

id = Open()
id = Open(device)
id = Open(device, UseMouse)
id = Open(device, UseMouse, HideText)

Open the framebuffer with device name device and return a device id id. Serveral framebuffers can be opened at the same time. id is used to select a framebuffer. UseMouse can be set to 0, if you don’t want to use the mouse. If HideText is 1, the current text dislay on the console will be hidden. Additionally the blinking text cursor will be disabled. After ending the program, the console will be restored.

Close Device

Close(id)

Close the framebuffer device with device id id.

Get Framebuffer Information

info = GetInfo(id)

Provide resolution, color bit-depth per pixel (bpp) and if double buffering is enabled.

Set Color

Color(id, fgcolor)
Color(id, fgcolor, bgcolor)

Set foreground color fgcolor and background color bgcolor. The color value depends on your framebuffer device.

For example: If you have a 32bit framebuffer with ARGB color order, then bit 0 to 7 contain the blue channel; bit 8 to 15 the green channel; bit 16 to 23 the red channel and bit 24 to 31 the alpha (transparency) channel. The standard framebuffer of a screen connected to HDMI is 32bit.

Example:

const BLUE   = 0x000000FF
const GREEN  = 0x0000FF00
const RED    = 0x00FF0000
const YELLOW = 0x00FFFF00
const ORANGE = 0x00FFAA00

Color(id, BLUE)

Clear Screen

Cls(id)
Cls(id, color)

Clear the screen using color color. If color is omitted, the current background color is used.

Draw a Pixel

Pset(id, x1, y1)
Pset(id, x1, y1, color)

Draw a pixel at point (x1, y1) with color color

Draw a Line

Line(id, x1, y1, x2, y2)
Line(id, x1, y1, x2, y2, color)

Draw a Rectangle

Rect(id, x1, y1, x2, y2)
Rect(id, x1, y1, x2, y2, color)
Rect(id, x1, y1, x2, y2, color, fill)

Draw a rectangle with the top left corner at point (x1, y1) and bottom right corner at (x2, y2) with line color color. If fill is 1 (true), a filled rectangle will be drawn.

Draw a Rectangle with Round Corners

Roundrect(id, x1, y1, x2, y2)
Roundrect(id, x1, y1, x2, y2, radius)
Roundrect(id, x1, y1, x2, y2, radius, color)
Roundrect(id, x1, y1, x2, y2, radius, color, fill)

Draw a rectangle with the top left corner at point (x1, y1) and bottom right corner at (x2, y2) with line color color and rounded corners. The radius in pixel of the corners if given by radius. If fill is 1 (true), a filled rectangle will be drawn.

Draw a Circle

Circle(id, x, y, radius)
Circle(id, x, y, radius, color)
Circle(id, x, y, radius, color, fill)

Draw a circle at position (x, y) with radius radius in pixel. color defines the line color. If fill is set to 1 (true), then the circle is filled with color. If no color is given, the current foreground color will be used.

Draw a Triangle

Triangle(id, x1, y1, x2, y2, x3, y3)
Triangle(id, x1, y1, x2, y2, x3, y3, color)
Triangle(id, x1, y1, x2, y2, x3, y3, color, fill)

Draw a triangle with the corner points (x1, y1), (x2, y2) and (x3, y3) with line color color. If fill is 1 (true), a filled triangle will be drawn.

Print(id, text)
Print(id, text, color)

Print text text with text color color. After printing the text, the text cursor advances by one text-line. The following special characters are supported:

Character Description
\a Set cursor position to upper left (0, 0)
\b Move cursor back by one position
\n Go to start of current line
\r Go to line below

Set Text Cursor Position

At(id)
At(id, x)
At(id, x, y)

Set the text cursor to the pixel (x, y).

Set Text Size

SetTextSize(id, size)

Set text size to size. size must be an multiple of 8.

Copy Array to Screen

SetArray(id, A)
SetArray(id, A, x)
SetArray(id, A, x, y)
SetArray(id, A, x, y, trans)

Copy the content of the 2D-array A to screen at position (x,y) using transparency mode trans. The following transparency modes are supported:

Mode Description
0 no transparency
1 Every element of A with value 0 will be transparent

Copy Screen to Array

A = GetArray(id)
A = GetArray(id, x)
A = GetArray(id, x, y)
A = GetArray(id, x, y, w)
A = GetArray(id, x, y, w, h)

Copy the screen context inside the rectangle with top-left corner at (x, y), a width of w and a height of h to the 2D-array A.

Double Buffering

SwapBuffer(id)

If the framebuffer device supports double buffering, drawing is always performed in a not visible back-buffer. When calling SwapBuffer, back-buffer and front-buffer will be swapped and the results of the drawing command will be visible.

Wait for Vertical Sync

WaitForVSync(id)

If the framebuffer device supports vertical sync, WaitForVSync will wait until the whole frame is displayed. This can be used to suppress artifacts, when swapping front- and back-buffer.

Get Mouse Data

A = GetMouse()

Return an 1D-array with current mouse data.

Example

OPTION BASE 1

import Framebuffer as FB

FILL = true
TRANSPARENT = true

dim a(100,100)

for xx = 1 to 100
    for yy = 1 to 100
        a[xx,yy] = floor(rnd*2^32)
    next
next

for xx = 10 to 40
    for yy = 10 to 40
        a[xx,yy] = 0
    next
next

id = FB.Open("/dev/fb0")

FB.Clear(id)
FB.Color(id, 0x0000FF00)
FB.SetPixel(id, 10,10)
FB.SetPixel(id, 20,10, 0x0000FFFF)
FB.setPixel(id, 30,10, 0x000000FF)

FB.Line(id, 50,10,100,60, 0x00FFFFFF)

FB.Rect(id, 110,10,160,60, 0x00AA8899, FILL)
FB.Rect(id, 110,10,160,60, 0x00AA4477)

FB.RoundRect(id, 170,10,220,60, 4, 0x00AA8899, FILL)
FB.RoundRect(id, 170,10,220,60, 4, 0x00AA4477)

FB.Circle(id, 255,35,20, 0x00AA8899, FILL)
FB.Circle(id, 255,35,20, 0x00AA4477)

FB.Triangle(id, 280,60, 305,10, 330,60, 0x00AA8899, FILL)
FB.Triangle(id, 280,60, 305,10, 330,60,0x00AA4477)

FB.At(id, 10, 70)
FB.Print(id, "test1")
FB.SetTextSize(id, 16)
FB.At(id, 10, 90)
FB.Print(id, "test2", 0x00118833)

FB.SetArray(id, a, 10, 120, TRANSPARENT)

'x,y,w,h
b = FB.GetArray(id, 10, 120, 50, 50)
FB.SetArray(id, b, 120, 120)

FB.WaitForVSync(id)
delay(1)
FB.SwapBuffer(id)

FB.Close(id)

[Back to main page]