framebuf.rst 5.6 KB

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