panel.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """The C panel core and simulator (from s1d13700-tca), through ctypes.
  2. The core is shared with the kernel driver, so it stays in C; build it with
  3. `make lib`. RADIO_PANEL_LIB overrides where the library is looked for.
  4. """
  5. import ctypes
  6. import errno
  7. import os
  8. API_VERSION = 1
  9. LIB_NAME = "libradiopanel.so"
  10. TX_CAP = 1025
  11. _here = os.path.dirname(os.path.abspath(__file__))
  12. _lib = None
  13. class PanelError(Exception):
  14. pass
  15. def _candidates():
  16. env = os.environ.get("RADIO_PANEL_LIB")
  17. if env:
  18. yield env
  19. yield os.path.join(_here, "..", "build", LIB_NAME)
  20. yield os.path.join(_here, LIB_NAME)
  21. def load_library():
  22. global _lib
  23. if _lib is not None:
  24. return _lib
  25. tried = []
  26. for path in _candidates():
  27. path = os.path.normpath(path)
  28. tried.append(path)
  29. if os.path.exists(path):
  30. lib = ctypes.CDLL(path)
  31. break
  32. else:
  33. raise PanelError("panel library not found (run `make lib`); tried: "
  34. + ", ".join(tried))
  35. vp = ctypes.c_void_p
  36. ci, cu, cul = ctypes.c_int, ctypes.c_uint, ctypes.c_ulong
  37. sigs = {
  38. "panel_api_version": (ci, []),
  39. "panel_new": (vp, [ci, cu, cu, cu, cu, cu, cu]),
  40. "panel_free": (None, [vp]),
  41. "panel_init": (ci, [vp]),
  42. "panel_update": (ci, [vp, vp, cu, cu, cu, cu, cu]),
  43. "panel_display": (ci, [vp, ci]),
  44. "panel_backlight": (ci, [vp, ci]),
  45. "panel_stats": (None, [vp, ctypes.POINTER(cul)]),
  46. "panel_stats_reset": (None, [vp]),
  47. "panel_bus_us": (ctypes.c_double, [vp, cu]),
  48. "panel_sim_render": (ci, [vp, ctypes.c_char_p]),
  49. "panel_sim_faults": (ci, [vp]),
  50. }
  51. for name, (res, args) in sigs.items():
  52. fn = getattr(lib, name)
  53. fn.restype = res
  54. fn.argtypes = args
  55. if lib.panel_api_version() != API_VERSION:
  56. raise PanelError("%s is out of date, rebuild it with `make lib`" % path)
  57. _lib = lib
  58. return lib
  59. def _check(ret, what):
  60. if ret < 0:
  61. raise OSError(-ret, "%s: %s" % (what, os.strerror(-ret)))
  62. return ret
  63. class Stats(object):
  64. FIELDS = ("writes", "data_bytes", "seeks", "dir_changes", "i2c_msgs", "i2c_bytes")
  65. def __init__(self, values, bus_us):
  66. for name, v in zip(self.FIELDS, values):
  67. setattr(self, name, v)
  68. self.bus_us = bus_us
  69. class Panel(object):
  70. """fd is an open, already checked /dev/i2c-N, or None for the simulator."""
  71. def __init__(self, width=240, height=160, bpp=2, tcr=162, fd=None,
  72. addr=0x74, tx_cap=TX_CAP):
  73. self._lib = load_library()
  74. self.width, self.height = width, height
  75. self.simulated = fd is None
  76. self._h = self._lib.panel_new(-1 if fd is None else fd, addr, width,
  77. height, bpp, tcr, tx_cap)
  78. if not self._h:
  79. raise PanelError("invalid panel configuration")
  80. def close(self):
  81. if self._h:
  82. self._lib.panel_free(self._h)
  83. self._h = None
  84. def init(self):
  85. _check(self._lib.panel_init(self._h), "panel init")
  86. def update(self, data, stride, x, y, w, h):
  87. """data: a writable buffer of XRGB8888 rows, such as a cairo surface's"""
  88. buf = (ctypes.c_char * len(data)).from_buffer(data)
  89. _check(self._lib.panel_update(self._h, ctypes.addressof(buf), stride,
  90. x, y, x + w, y + h), "panel update")
  91. def display(self, on):
  92. _check(self._lib.panel_display(self._h, int(bool(on))), "display")
  93. def backlight(self, on):
  94. _check(self._lib.panel_backlight(self._h, int(bool(on))), "backlight")
  95. def stats(self, scl_hz=400000):
  96. out = (ctypes.c_ulong * 6)()
  97. self._lib.panel_stats(self._h, out)
  98. return Stats(list(out), self._lib.panel_bus_us(self._h, scl_hz))
  99. def reset_stats(self):
  100. self._lib.panel_stats_reset(self._h)
  101. def sim_render(self):
  102. """Simulator: (shown, levels), levels being one byte per pixel"""
  103. levels = ctypes.create_string_buffer(self.width * self.height)
  104. ret = self._lib.panel_sim_render(self._h, levels)
  105. if ret == -errno.EINVAL:
  106. raise PanelError("not a simulated panel")
  107. return bool(ret), levels.raw
  108. def sim_faults(self):
  109. return self._lib.panel_sim_faults(self._h)