aboutsummaryrefslogtreecommitdiff
path: root/host/bdf.py
diff options
context:
space:
mode:
authorjaseg <code@jaseg.net>2016-01-01 15:55:52 +0100
committerjaseg <code@jaseg.net>2016-01-02 01:28:58 +0100
commit330e1eb20ec751256ea1cee7a6b100e1cb2d3a72 (patch)
tree60b87fadebd58788b059b9eadc9e2ac3a6a9c746 /host/bdf.py
parent245fbf3d6b5bb5d9cd75882a2fda3f5604335f25 (diff)
downloadmatelight-330e1eb20ec751256ea1cee7a6b100e1cb2d3a72.tar.gz
matelight-330e1eb20ec751256ea1cee7a6b100e1cb2d3a72.tar.bz2
matelight-330e1eb20ec751256ea1cee7a6b100e1cb2d3a72.zip
Host: Add console CRAP client
Diffstat (limited to 'host/bdf.py')
-rw-r--r--host/bdf.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/host/bdf.py b/host/bdf.py
new file mode 100644
index 0000000..b386426
--- /dev/null
+++ b/host/bdf.py
@@ -0,0 +1,56 @@
+
+import config
+import threading
+import numpy
+from ctypes import *
+
+class COLOR(Structure):
+ _fields_ = [('r', c_uint8), ('g', c_uint8), ('b', c_uint8), ('a', c_uint8)]
+
+class FRAMEBUFFER(Structure):
+ _fields_ = [('data', POINTER(COLOR)), ('w', c_size_t), ('h', c_size_t)]
+
+lib = CDLL('./libbdf.so')
+lib.read_bdf_file.restype = c_void_p
+lib.framebuffer_render_text.restype = POINTER(FRAMEBUFFER)
+lib.framebuffer_render_text.argtypes= [c_char_p, c_void_p, c_void_p, c_size_t, c_size_t, c_size_t]
+
+dbuf = numpy.zeros(config.frame_size*4, dtype=numpy.uint8)
+printlock = threading.Lock()
+def printframe(fb):
+ with printlock:
+ print('\0337\033[H')
+ rgba = len(fb) == config.frame_size*4
+ ip = numpy.frombuffer(fb, dtype=numpy.uint8)
+ numpy.copyto(dbuf[0::4], ip[0::3+rgba])
+ numpy.copyto(dbuf[1::4], ip[1::3+rgba])
+ numpy.copyto(dbuf[2::4], ip[2::3+rgba])
+ lib.console_render_buffer(dbuf.ctypes.data_as(POINTER(c_uint8)), config.display_width, config.display_height)
+
+
+class Font:
+ def __init__(self, fontfile='unifont.bdf'):
+ self.font = lib.read_bdf_file(fontfile)
+ assert self.font
+ # hack to prevent unlocalized memory leak arising from ctypes/numpy/cpython interaction
+ self.cbuf = create_string_buffer(config.frame_size*sizeof(COLOR))
+ self.cbuflock = threading.Lock()
+
+ def compute_text_bounds(text):
+ textbytes = text.encode()
+ textw, texth = c_size_t(0), c_size_t(0)
+ res = lib.framebuffer_get_text_bounds(textbytes, unifont, byref(textw), byref(texth))
+ if res:
+ raise ValueError('Invalid text')
+ return textw.value, texth.value
+
+ def render_text(text, offset):
+ with cbuflock:
+ textbytes = bytes(str(text), 'UTF-8')
+ res = lib.framebuffer_render_text(textbytes, self.font, self.cbuf,
+ config.display_width, config.display_height, offset)
+ if res:
+ raise ValueError('Invalid text')
+ return self.cbuf
+
+