|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# framebuffer.py |
| 4 | +# |
| 5 | +# Helper class for handling framebuffers for ev3dev |
| 6 | + |
| 7 | +# The MIT License (MIT) |
| 8 | +# |
| 9 | +# Copyright (c) 2016 David Lechner <[email protected]> |
| 10 | +# |
| 11 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | +# of this software and associated documentation files (the "Software"), to deal |
| 13 | +# in the Software without restriction, including without limitation the rights |
| 14 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | +# copies of the Software, and to permit persons to whom the Software is |
| 16 | +# furnished to do so, subject to the following conditions: |
| 17 | +# |
| 18 | +# The above copyright notice and this permission notice shall be included in all |
| 19 | +# copies or substantial portions of the Software. |
| 20 | +# |
| 21 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | +# SOFTWARE. |
| 28 | + |
| 29 | +from ctypes import Structure, c_char, c_ulong, c_uint16, c_uint32 |
| 30 | +from enum import Enum |
| 31 | +from fcntl import ioctl |
| 32 | +from collections import namedtuple |
| 33 | + |
| 34 | +class FrameBuffer(object): |
| 35 | + |
| 36 | + # ioctls |
| 37 | + _FBIOGET_VSCREENINFO = 0x4600 |
| 38 | + _FBIOGET_FSCREENINFO = 0x4602 |
| 39 | + _FBIOGET_CON2FBMAP = 0x460F |
| 40 | + |
| 41 | + class Type(Enum): |
| 42 | + PACKED_PIXELS = 0 # Packed Pixels |
| 43 | + PLANES = 1 # Non interleaved planes |
| 44 | + INTERLEAVED_PLANES = 2 # Interleaved planes |
| 45 | + TEXT = 3 # Text/attributes |
| 46 | + VGA_PLANES = 4 # EGA/VGA planes |
| 47 | + FOURCC = 5 # Type identified by a V4L2 FOURCC |
| 48 | + |
| 49 | + class Visual(Enum): |
| 50 | + MONO01 = 0 # Monochrome 1=Black 0=White |
| 51 | + MONO10 = 1 # Monochrome 1=White 0=Black |
| 52 | + TRUECOLOR = 2 # True color |
| 53 | + PSEUDOCOLOR = 3 # Pseudo color (like atari) |
| 54 | + DIRECTCOLOR = 4 # Direct color |
| 55 | + STATIC_PSEUDOCOLOR = 5 # Pseudo color readonly |
| 56 | + FOURCC = 6 # Visual identified by a V4L2 FOURCC |
| 57 | + |
| 58 | + class _FixedScreenInfo(Structure): |
| 59 | + _fields_ = [ |
| 60 | + ('id', c_char * 16), # identification string eg "TT Builtin" |
| 61 | + ('smem_start', c_ulong), # Start of frame buffer mem (physical address) |
| 62 | + ('smem_len', c_uint32), # Length of frame buffer mem |
| 63 | + ('type', c_uint32), # see FB_TYPE_* |
| 64 | + ('type_aux', c_uint32), # Interleave for interleaved Planes |
| 65 | + ('visual', c_uint32), # see FB_VISUAL_* |
| 66 | + ('xpanstep', c_uint16), # zero if no hardware panning |
| 67 | + ('ypanstep', c_uint16), # zero if no hardware panning |
| 68 | + ('ywrapstep', c_uint16), # zero if no hardware ywrap |
| 69 | + ('line_length', c_uint32), # length of a line in bytes |
| 70 | + ('mmio_start', c_ulong), # Start of Memory Mapped I/O (physical address) |
| 71 | + ('mmio_len', c_uint32), # Length of Memory Mapped I/O |
| 72 | + ('accel', c_uint32), # Indicate to driver which specific chip/card we have |
| 73 | + ('capabilities', c_uint16), # see FB_CAP_* |
| 74 | + ('reserved', c_uint16 * 2), # Reserved for future compatibility |
| 75 | + ] |
| 76 | + |
| 77 | + class _VariableScreenInfo(Structure): |
| 78 | + |
| 79 | + class _Bitfield(Structure): |
| 80 | + _fields_ = [ |
| 81 | + ('offset', c_uint32), # beginning of bitfield |
| 82 | + ('length', c_uint32), # length of bitfield |
| 83 | + ('msb_right', c_uint32), # != 0 : Most significant bit is right |
| 84 | + ] |
| 85 | + |
| 86 | + _fields_ = [ |
| 87 | + ('xres', c_uint32), # visible resolution |
| 88 | + ('yres', c_uint32), |
| 89 | + ('xres_virtual', c_uint32), # virtual resolution |
| 90 | + ('yres_virtual', c_uint32), |
| 91 | + ('xoffset', c_uint32), # offset from virtual to visible |
| 92 | + ('yoffset', c_uint32), # resolution |
| 93 | + ('bits_per_pixel', c_uint32), # guess what |
| 94 | + ('grayscale', c_uint32), # 0 = color, 1 = grayscale, >1 = FOURCC |
| 95 | + ('red', _Bitfield), # bitfield in fb mem if true color, |
| 96 | + ('green', _Bitfield), # else only length is significant |
| 97 | + ('blue', _Bitfield), |
| 98 | + ('transp', _Bitfield), # transparency |
| 99 | + ('nonstd', c_uint32), # != 0 Non standard pixel format |
| 100 | + ('activate', c_uint32), # see FB_ACTIVATE_* |
| 101 | + ('height', c_uint32), # height of picture in mm |
| 102 | + ('width', c_uint32), # width of picture in mm |
| 103 | + ('accel_flags', c_uint32), # (OBSOLETE) see fb_info.flags |
| 104 | + # Timing: All values, in pixclocks, except pixclock (of course) |
| 105 | + ('pixclock', c_uint32), # pixel clock in ps (pico seconds) |
| 106 | + ('left_margin', c_uint32), # time from sync to picture |
| 107 | + ('right_margin', c_uint32), # time from picture to sync |
| 108 | + ('upper_margin', c_uint32), # time from sync to picture |
| 109 | + ('lower_margin', c_uint32), |
| 110 | + ('hsync_len', c_uint32), # length of horizontal sync |
| 111 | + ('vsync_len', c_uint32), # length of vertical sync |
| 112 | + ('sync', c_uint32), # see FB_SYNC_* |
| 113 | + ('vmode', c_uint32), # see FB_VMODE_* |
| 114 | + ('rotate', c_uint32), # angle we rotate counter clockwise |
| 115 | + ('colorspace', c_uint32), # colorspace for FOURCC-based modes |
| 116 | + ('reserved', c_uint32 * 4), # Reserved for future compatibility |
| 117 | + ] |
| 118 | + |
| 119 | + class _Console2FrameBufferMap(Structure): |
| 120 | + _fields_ = [ |
| 121 | + ('console', c_uint32), |
| 122 | + ('framebuffer', c_uint32), |
| 123 | + ] |
| 124 | + |
| 125 | + def __init__(self, device='/dev/fb0'): |
| 126 | + self._fd = open(device, mode='r+b', buffering=0) |
| 127 | + self._fixed_info = self._FixedScreenInfo() |
| 128 | + ioctl(self._fd, self._FBIOGET_FSCREENINFO, self._fixed_info) |
| 129 | + self._variable_info = self._VariableScreenInfo() |
| 130 | + ioctl(self._fd, self._FBIOGET_VSCREENINFO, self._variable_info) |
| 131 | + |
| 132 | + def close(self): |
| 133 | + self._fd.close() |
| 134 | + |
| 135 | + def clear(self): |
| 136 | + self._fd.seek(0) |
| 137 | + self._fd.write(b'\0' * self._fixed_info.smem_len) |
| 138 | + |
| 139 | + def write_raw(self, data): |
| 140 | + self._fd.seek(0) |
| 141 | + self._fd.write(data) |
| 142 | + |
| 143 | + @staticmethod |
| 144 | + def get_fb_for_console(console): |
| 145 | + with open('/dev/fb0', mode='r+b') as fd: |
| 146 | + m = FrameBuffer._Console2FrameBufferMap() |
| 147 | + m.console = console |
| 148 | + ioctl(fd, FrameBuffer._FBIOGET_CON2FBMAP, m) |
| 149 | + return FrameBuffer('/dev/fb{}'.format(m.framebuffer)) |
| 150 | + |
| 151 | + @property |
| 152 | + def type(self): |
| 153 | + return self.Type(self._fixed_info.type) |
| 154 | + |
| 155 | + @property |
| 156 | + def visual(self): |
| 157 | + return self.Visual(self._fixed_info.visual) |
| 158 | + |
| 159 | + @property |
| 160 | + def line_length(self): |
| 161 | + return self._fixed_info.line_length |
| 162 | + |
| 163 | + @property |
| 164 | + def resolution(self): |
| 165 | + """Visible resolution""" |
| 166 | + Resolution = namedtuple('Resolution', 'x y') |
| 167 | + return Resolution(self._variable_info.xres, self._variable_info.yres) |
| 168 | + |
| 169 | + @property |
| 170 | + def bits_per_pixel(self): |
| 171 | + return self._variable_info.bits_per_pixel |
| 172 | + |
| 173 | + @property |
| 174 | + def grayscale(self): |
| 175 | + return self._variable_info.grayscale |
| 176 | + |
| 177 | + @property |
| 178 | + def size(self): |
| 179 | + """Size of picture in mm""" |
| 180 | + Size = namedtuple('Size', 'width height') |
| 181 | + return Size(self._variable_info.width, self._variable_info.height) |
0 commit comments