|
| 1 | +:mod:`framebuf` --- Frame buffer manipulation |
| 2 | +============================================= |
| 3 | + |
| 4 | +.. module:: framebuf |
| 5 | + :synopsis: Frame buffer manipulation |
| 6 | + |
| 7 | +This module provides a general frame buffer which can be used to create |
| 8 | +bitmap images, which can then be sent to a display. |
| 9 | + |
| 10 | +class FrameBuffer |
| 11 | +----------------- |
| 12 | + |
| 13 | +The FrameBuffer class provides a pixel buffer which can be drawn upon with |
| 14 | +pixels, lines, rectangles, text and even other FrameBuffer's. It is useful |
| 15 | +when generating output for displays. |
| 16 | + |
| 17 | +For example:: |
| 18 | + |
| 19 | + import framebuf |
| 20 | + |
| 21 | + # FrameBuffer needs 2 bytes for every RGB565 pixel |
| 22 | + fbuf = FrameBuffer(bytearray(10 * 100 * 2), 10, 100, framebuf.RGB565) |
| 23 | + |
| 24 | + fbuf.fill(0) |
| 25 | + fbuf.text('MicroPython!', 0, 0, 0xffff) |
| 26 | + fbuf.hline(0, 10, 96, 0xffff) |
| 27 | + |
| 28 | +Constructors |
| 29 | +------------ |
| 30 | + |
| 31 | +.. class:: FrameBuffer(buffer, width, height, format, stride=width) |
| 32 | + |
| 33 | + Construct a FrameBuffer object. The parameters are: |
| 34 | + |
| 35 | + - `buffer` is an object with a buffer protocol which must be large |
| 36 | + enough to contain every pixel defined by the width, height and |
| 37 | + format of the FrameBuffer. |
| 38 | + - `width` is the width of the FrameBuffer in pixels |
| 39 | + - `height` is the height of the FrameBuffer in pixels |
| 40 | + - `format` specifies the type of pixel used in the FrameBuffer; |
| 41 | + valid values are ``framebuf.MVLSB``, ``framebuf.RGB565`` |
| 42 | + and ``framebuf.GS4_HMSB``. MVLSB is monochrome 8-bit color, |
| 43 | + RGB565 is RGB 16-bit color, and GS4_HMSB is grayscale 4-bit color. |
| 44 | + Where a color value c is passed to a method, c is a small integer |
| 45 | + with an encoding that is dependent on the format of the FrameBuffer. |
| 46 | + - `stride` is the number of pixels between each horizontal line |
| 47 | + of pixels in the FrameBuffer. This defaults to `width` but may |
| 48 | + need adjustments when implementing a FrameBuffer within another |
| 49 | + larger FrameBuffer or screen. The `buffer` size must accommodate |
| 50 | + an increased step size. |
| 51 | + |
| 52 | + One must specify valid `buffer`, `width`, `height`, `format` and |
| 53 | + optionally `stride`. Invalid `buffer` size or dimensions may lead to |
| 54 | + unexpected errors. |
| 55 | + |
| 56 | +Drawing primitive shapes |
| 57 | +------------------------ |
| 58 | + |
| 59 | +The following methods draw shapes onto the FrameBuffer. |
| 60 | + |
| 61 | +.. method:: FrameBuffer.fill(c) |
| 62 | + |
| 63 | + Fill the entire FrameBuffer with the specified color. |
| 64 | + |
| 65 | +.. method:: FrameBuffer.pixel(x, y[, c]) |
| 66 | + |
| 67 | + If `c` is not given, get the color value of the specified pixel. |
| 68 | + If `c` is given, set the specified pixel to the given color. |
| 69 | + |
| 70 | +.. method:: FrameBuffer.hline(x, y, w, c) |
| 71 | +.. method:: FrameBuffer.vline(x, y, h, c) |
| 72 | +.. method:: FrameBuffer.line(x1, y1, x2, y2, c) |
| 73 | + |
| 74 | + Draw a line from a set of coordinates using the given color and |
| 75 | + a thickness of 1 pixel. The `line` method draws the line up to |
| 76 | + a second set of coordinates whereas the `hline` and `vline` |
| 77 | + methods draw horizontal and vertical lines respectively up to |
| 78 | + a given length. |
| 79 | + |
| 80 | +.. method:: FrameBuffer.rect(x, y, w, h, c) |
| 81 | +.. method:: FrameBuffer.fill_rect(x, y, w, h, c) |
| 82 | + |
| 83 | + Draw a rectangle at the given location, size and color. The `rect` |
| 84 | + method draws only a 1 pixel outline whereas the `fill_rect` method |
| 85 | + draws both the outline and interior. |
| 86 | + |
| 87 | +Drawing text |
| 88 | +------------ |
| 89 | + |
| 90 | +.. method:: FrameBuffer.text(s, x, y[, c]) |
| 91 | + |
| 92 | + Write text to the FrameBuffer using the the coordinates as the upper-left |
| 93 | + corner of the text. The color of the text can be defined by the optional |
| 94 | + argument but is otherwise a default value of 1. All characters have |
| 95 | + dimensions of 8x8 pixels and there is currently no way to change the font. |
| 96 | + |
| 97 | + |
| 98 | +Other methods |
| 99 | +------------- |
| 100 | + |
| 101 | +.. method:: FrameBuffer.scroll(xstep, ystep) |
| 102 | + |
| 103 | + Shift the contents of the FrameBuffer by the given vector. This may |
| 104 | + leave a footprint of the previous colors in the FrameBuffer. |
| 105 | + |
| 106 | +.. method:: FrameBuffer.blit(fbuf, x, y[, key]) |
| 107 | + |
| 108 | + Draw another FrameBuffer on top of the current one at the given coordinates. |
| 109 | + If `key` is specified then it should be a color integer and the |
| 110 | + corresponding color will be considered transparent: all pixels with that |
| 111 | + color value will not be drawn. |
| 112 | + |
| 113 | + This method works between FrameBuffer's utilising different formats, but the |
| 114 | + resulting colors may be unexpected due to the mismatch in color formats. |
| 115 | + |
| 116 | +Constants |
| 117 | +--------- |
| 118 | + |
| 119 | +.. data:: framebuf.MVLSB |
| 120 | + |
| 121 | + Monochrome (1-bit) color format |
| 122 | + |
| 123 | +.. data:: framebuf.RGB565 |
| 124 | + |
| 125 | + Red Green Blue (16-bit, 5+6+5) color format |
| 126 | + |
| 127 | +.. data:: framebuf.GS4_HMSB |
| 128 | + |
| 129 | + Grayscale (4-bit) color format |
0 commit comments