2020-01-01 11:51:42 +00:00
|
|
|
:mod:`framebuf` --- frame buffer manipulation
|
2017-02-27 06:16:32 +00:00
|
|
|
=============================================
|
|
|
|
|
|
|
|
.. module:: framebuf
|
|
|
|
:synopsis: Frame buffer manipulation
|
|
|
|
|
|
|
|
This module provides a general frame buffer which can be used to create
|
|
|
|
bitmap images, which can then be sent to a display.
|
|
|
|
|
|
|
|
class FrameBuffer
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
The FrameBuffer class provides a pixel buffer which can be drawn upon with
|
|
|
|
pixels, lines, rectangles, text and even other FrameBuffer's. It is useful
|
|
|
|
when generating output for displays.
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
import framebuf
|
|
|
|
|
|
|
|
# FrameBuffer needs 2 bytes for every RGB565 pixel
|
2020-11-14 19:39:26 +00:00
|
|
|
fbuf = framebuf.FrameBuffer(bytearray(100 * 10 * 2), 100, 10, framebuf.RGB565)
|
2017-02-27 06:16:32 +00:00
|
|
|
|
|
|
|
fbuf.fill(0)
|
|
|
|
fbuf.text('MicroPython!', 0, 0, 0xffff)
|
2020-11-14 19:39:26 +00:00
|
|
|
fbuf.hline(0, 9, 96, 0xffff)
|
2017-02-27 06:16:32 +00:00
|
|
|
|
|
|
|
Constructors
|
|
|
|
------------
|
|
|
|
|
2020-01-11 06:44:17 +00:00
|
|
|
.. class:: FrameBuffer(buffer, width, height, format, stride=width, /)
|
2017-02-27 06:16:32 +00:00
|
|
|
|
|
|
|
Construct a FrameBuffer object. The parameters are:
|
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
- *buffer* is an object with a buffer protocol which must be large
|
2017-02-27 06:16:32 +00:00
|
|
|
enough to contain every pixel defined by the width, height and
|
|
|
|
format of the FrameBuffer.
|
2017-06-29 00:22:14 +01:00
|
|
|
- *width* is the width of the FrameBuffer in pixels
|
|
|
|
- *height* is the height of the FrameBuffer in pixels
|
|
|
|
- *format* specifies the type of pixel used in the FrameBuffer;
|
2017-09-12 16:48:39 +01:00
|
|
|
permissible values are listed under Constants below. These set the
|
|
|
|
number of bits used to encode a color value and the layout of these
|
|
|
|
bits in *buffer*.
|
2017-02-27 06:16:32 +00:00
|
|
|
Where a color value c is passed to a method, c is a small integer
|
|
|
|
with an encoding that is dependent on the format of the FrameBuffer.
|
2017-06-29 00:22:14 +01:00
|
|
|
- *stride* is the number of pixels between each horizontal line
|
|
|
|
of pixels in the FrameBuffer. This defaults to *width* but may
|
2017-02-27 06:16:32 +00:00
|
|
|
need adjustments when implementing a FrameBuffer within another
|
2017-06-29 00:22:14 +01:00
|
|
|
larger FrameBuffer or screen. The *buffer* size must accommodate
|
2017-02-27 06:16:32 +00:00
|
|
|
an increased step size.
|
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
One must specify valid *buffer*, *width*, *height*, *format* and
|
|
|
|
optionally *stride*. Invalid *buffer* size or dimensions may lead to
|
2017-02-27 06:16:32 +00:00
|
|
|
unexpected errors.
|
|
|
|
|
|
|
|
Drawing primitive shapes
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
The following methods draw shapes onto the FrameBuffer.
|
|
|
|
|
|
|
|
.. method:: FrameBuffer.fill(c)
|
|
|
|
|
|
|
|
Fill the entire FrameBuffer with the specified color.
|
|
|
|
|
|
|
|
.. method:: FrameBuffer.pixel(x, y[, c])
|
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
If *c* is not given, get the color value of the specified pixel.
|
|
|
|
If *c* is given, set the specified pixel to the given color.
|
2017-02-27 06:16:32 +00:00
|
|
|
|
|
|
|
.. method:: FrameBuffer.hline(x, y, w, c)
|
|
|
|
.. method:: FrameBuffer.vline(x, y, h, c)
|
|
|
|
.. method:: FrameBuffer.line(x1, y1, x2, y2, c)
|
|
|
|
|
|
|
|
Draw a line from a set of coordinates using the given color and
|
|
|
|
a thickness of 1 pixel. The `line` method draws the line up to
|
|
|
|
a second set of coordinates whereas the `hline` and `vline`
|
|
|
|
methods draw horizontal and vertical lines respectively up to
|
|
|
|
a given length.
|
|
|
|
|
|
|
|
.. method:: FrameBuffer.rect(x, y, w, h, c)
|
|
|
|
.. method:: FrameBuffer.fill_rect(x, y, w, h, c)
|
|
|
|
|
|
|
|
Draw a rectangle at the given location, size and color. The `rect`
|
|
|
|
method draws only a 1 pixel outline whereas the `fill_rect` method
|
|
|
|
draws both the outline and interior.
|
|
|
|
|
|
|
|
Drawing text
|
|
|
|
------------
|
|
|
|
|
|
|
|
.. method:: FrameBuffer.text(s, x, y[, c])
|
|
|
|
|
|
|
|
Write text to the FrameBuffer using the the coordinates as the upper-left
|
|
|
|
corner of the text. The color of the text can be defined by the optional
|
|
|
|
argument but is otherwise a default value of 1. All characters have
|
|
|
|
dimensions of 8x8 pixels and there is currently no way to change the font.
|
|
|
|
|
|
|
|
|
|
|
|
Other methods
|
|
|
|
-------------
|
|
|
|
|
|
|
|
.. method:: FrameBuffer.scroll(xstep, ystep)
|
|
|
|
|
|
|
|
Shift the contents of the FrameBuffer by the given vector. This may
|
|
|
|
leave a footprint of the previous colors in the FrameBuffer.
|
|
|
|
|
2021-08-18 20:05:25 +01:00
|
|
|
.. method:: FrameBuffer.blit(fbuf, x, y, key=-1, palette=None)
|
2017-02-27 06:16:32 +00:00
|
|
|
|
|
|
|
Draw another FrameBuffer on top of the current one at the given coordinates.
|
2017-06-29 00:22:14 +01:00
|
|
|
If *key* is specified then it should be a color integer and the
|
2017-02-27 06:16:32 +00:00
|
|
|
corresponding color will be considered transparent: all pixels with that
|
|
|
|
color value will not be drawn.
|
|
|
|
|
2021-08-18 20:05:25 +01:00
|
|
|
The *palette* argument enables blitting between FrameBuffers with differing
|
|
|
|
formats. Typical usage is to render a monochrome or grayscale glyph/icon to
|
|
|
|
a color display. The *palette* is a FrameBuffer instance whose format is
|
|
|
|
that of the current FrameBuffer. The *palette* height is one pixel and its
|
|
|
|
pixel width is the number of colors in the source FrameBuffer. The *palette*
|
|
|
|
for an N-bit source needs 2**N pixels; the *palette* for a monochrome source
|
|
|
|
would have 2 pixels representing background and foreground colors. The
|
|
|
|
application assigns a color to each pixel in the *palette*. The color of the
|
|
|
|
current pixel will be that of that *palette* pixel whose x position is the
|
|
|
|
color of the corresponding source pixel.
|
2017-02-27 06:16:32 +00:00
|
|
|
|
|
|
|
Constants
|
|
|
|
---------
|
|
|
|
|
2017-04-01 07:00:09 +01:00
|
|
|
.. data:: framebuf.MONO_VLSB
|
2017-02-27 06:16:32 +00:00
|
|
|
|
|
|
|
Monochrome (1-bit) color format
|
2017-04-01 07:00:09 +01:00
|
|
|
This defines a mapping where the bits in a byte are vertically mapped with
|
|
|
|
bit 0 being nearest the top of the screen. Consequently each byte occupies
|
|
|
|
8 vertical pixels. Subsequent bytes appear at successive horizontal
|
|
|
|
locations until the rightmost edge is reached. Further bytes are rendered
|
|
|
|
at locations starting at the leftmost edge, 8 pixels lower.
|
|
|
|
|
|
|
|
.. data:: framebuf.MONO_HLSB
|
|
|
|
|
|
|
|
Monochrome (1-bit) color format
|
|
|
|
This defines a mapping where the bits in a byte are horizontally mapped.
|
docs/library: Fix framebuf monochrome 1-bit modes, swapping HLSB/HMSB.
This fix can be demonstrated by the following:
b = bytearray(32)
f = framebuf.FrameBuffer(b, 32, 8, framebuf.MONO_HLSB)
f.pixel(0, 0, 1)
print('MONO_HLSB', hex(b[0]))
b = bytearray(32)
f = framebuf.FrameBuffer(b, 32, 8, framebuf.MONO_HMSB)
f.pixel(0, 0, 1)
print('MONO_HMSB', hex(b[0]))
Outcome:
MONO_HLSB 0x80
MONO_HMSB 0x1
2020-02-09 16:07:39 +00:00
|
|
|
Each byte occupies 8 horizontal pixels with bit 7 being the leftmost.
|
2017-04-01 07:00:09 +01:00
|
|
|
Subsequent bytes appear at successive horizontal locations until the
|
|
|
|
rightmost edge is reached. Further bytes are rendered on the next row, one
|
|
|
|
pixel lower.
|
|
|
|
|
|
|
|
.. data:: framebuf.MONO_HMSB
|
|
|
|
|
|
|
|
Monochrome (1-bit) color format
|
|
|
|
This defines a mapping where the bits in a byte are horizontally mapped.
|
docs/library: Fix framebuf monochrome 1-bit modes, swapping HLSB/HMSB.
This fix can be demonstrated by the following:
b = bytearray(32)
f = framebuf.FrameBuffer(b, 32, 8, framebuf.MONO_HLSB)
f.pixel(0, 0, 1)
print('MONO_HLSB', hex(b[0]))
b = bytearray(32)
f = framebuf.FrameBuffer(b, 32, 8, framebuf.MONO_HMSB)
f.pixel(0, 0, 1)
print('MONO_HMSB', hex(b[0]))
Outcome:
MONO_HLSB 0x80
MONO_HMSB 0x1
2020-02-09 16:07:39 +00:00
|
|
|
Each byte occupies 8 horizontal pixels with bit 0 being the leftmost.
|
2017-04-01 07:00:09 +01:00
|
|
|
Subsequent bytes appear at successive horizontal locations until the
|
|
|
|
rightmost edge is reached. Further bytes are rendered on the next row, one
|
|
|
|
pixel lower.
|
2017-02-27 06:16:32 +00:00
|
|
|
|
|
|
|
.. data:: framebuf.RGB565
|
|
|
|
|
|
|
|
Red Green Blue (16-bit, 5+6+5) color format
|
|
|
|
|
2017-07-15 14:53:47 +01:00
|
|
|
.. data:: framebuf.GS2_HMSB
|
|
|
|
|
|
|
|
Grayscale (2-bit) color format
|
|
|
|
|
2017-02-27 06:16:32 +00:00
|
|
|
.. data:: framebuf.GS4_HMSB
|
|
|
|
|
|
|
|
Grayscale (4-bit) color format
|
2017-12-14 06:36:13 +00:00
|
|
|
|
|
|
|
.. data:: framebuf.GS8
|
|
|
|
|
|
|
|
Grayscale (8-bit) color format
|