| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- """The C panel core and simulator (from s1d13700-tca), through ctypes.
- The core is shared with the kernel driver, so it stays in C; build it with
- `make lib`. RADIO_PANEL_LIB overrides where the library is looked for.
- """
- import ctypes
- import errno
- import os
- API_VERSION = 1
- LIB_NAME = "libradiopanel.so"
- TX_CAP = 1025
- _here = os.path.dirname(os.path.abspath(__file__))
- _lib = None
- class PanelError(Exception):
- pass
- def _candidates():
- env = os.environ.get("RADIO_PANEL_LIB")
- if env:
- yield env
- yield os.path.join(_here, "..", "build", LIB_NAME)
- yield os.path.join(_here, LIB_NAME)
- def load_library():
- global _lib
- if _lib is not None:
- return _lib
- tried = []
- for path in _candidates():
- path = os.path.normpath(path)
- tried.append(path)
- if os.path.exists(path):
- lib = ctypes.CDLL(path)
- break
- else:
- raise PanelError("panel library not found (run `make lib`); tried: "
- + ", ".join(tried))
- vp = ctypes.c_void_p
- ci, cu, cul = ctypes.c_int, ctypes.c_uint, ctypes.c_ulong
- sigs = {
- "panel_api_version": (ci, []),
- "panel_new": (vp, [ci, cu, cu, cu, cu, cu, cu]),
- "panel_free": (None, [vp]),
- "panel_init": (ci, [vp]),
- "panel_update": (ci, [vp, vp, cu, cu, cu, cu, cu]),
- "panel_display": (ci, [vp, ci]),
- "panel_backlight": (ci, [vp, ci]),
- "panel_stats": (None, [vp, ctypes.POINTER(cul)]),
- "panel_stats_reset": (None, [vp]),
- "panel_bus_us": (ctypes.c_double, [vp, cu]),
- "panel_sim_render": (ci, [vp, ctypes.c_char_p]),
- "panel_sim_faults": (ci, [vp]),
- }
- for name, (res, args) in sigs.items():
- fn = getattr(lib, name)
- fn.restype = res
- fn.argtypes = args
- if lib.panel_api_version() != API_VERSION:
- raise PanelError("%s is out of date, rebuild it with `make lib`" % path)
- _lib = lib
- return lib
- def _check(ret, what):
- if ret < 0:
- raise OSError(-ret, "%s: %s" % (what, os.strerror(-ret)))
- return ret
- class Stats(object):
- FIELDS = ("writes", "data_bytes", "seeks", "dir_changes", "i2c_msgs", "i2c_bytes")
- def __init__(self, values, bus_us):
- for name, v in zip(self.FIELDS, values):
- setattr(self, name, v)
- self.bus_us = bus_us
- class Panel(object):
- """fd is an open, already checked /dev/i2c-N, or None for the simulator."""
- def __init__(self, width=240, height=160, bpp=2, tcr=162, fd=None,
- addr=0x74, tx_cap=TX_CAP):
- self._lib = load_library()
- self.width, self.height = width, height
- self.simulated = fd is None
- self._h = self._lib.panel_new(-1 if fd is None else fd, addr, width,
- height, bpp, tcr, tx_cap)
- if not self._h:
- raise PanelError("invalid panel configuration")
- def close(self):
- if self._h:
- self._lib.panel_free(self._h)
- self._h = None
- def init(self):
- _check(self._lib.panel_init(self._h), "panel init")
- def update(self, data, stride, x, y, w, h):
- """data: a writable buffer of XRGB8888 rows, such as a cairo surface's"""
- buf = (ctypes.c_char * len(data)).from_buffer(data)
- _check(self._lib.panel_update(self._h, ctypes.addressof(buf), stride,
- x, y, x + w, y + h), "panel update")
- def display(self, on):
- _check(self._lib.panel_display(self._h, int(bool(on))), "display")
- def backlight(self, on):
- _check(self._lib.panel_backlight(self._h, int(bool(on))), "backlight")
- def stats(self, scl_hz=400000):
- out = (ctypes.c_ulong * 6)()
- self._lib.panel_stats(self._h, out)
- return Stats(list(out), self._lib.panel_bus_us(self._h, scl_hz))
- def reset_stats(self):
- self._lib.panel_stats_reset(self._h)
- def sim_render(self):
- """Simulator: (shown, levels), levels being one byte per pixel"""
- levels = ctypes.create_string_buffer(self.width * self.height)
- ret = self._lib.panel_sim_render(self._h, levels)
- if ret == -errno.EINVAL:
- raise PanelError("not a simulated panel")
- return bool(ret), levels.raw
- def sim_faults(self):
- return self._lib.panel_sim_faults(self._h)
|