From 9934f27cc16e8a2da43033a1f957761e00db4916 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 16 Feb 2014 11:57:21 +0100 Subject: The fundamentals of the new ctypes interface are working --- host/server.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 host/server.py (limited to 'host/server.py') diff --git a/host/server.py b/host/server.py new file mode 100755 index 0000000..c8e665a --- /dev/null +++ b/host/server.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +from ctypes import CDLL, POINTER, c_void_p, Structure, c_uint8, c_size_t, cast +import numpy as np +from matelight import sendframe + +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) + +bdf = CDLL('./libbdf.so') +bdf.read_bdf_file.restype = c_void_p +bdf.framebuffer_render_text.restype = POINTER(FRAMEBUFFER) + +unifont = bdf.read_bdf_file('unifont.bdf') + +def render_text(text): + assert unifont + fb = bdf.framebuffer_render_text(str(text), unifont) + fbd = fb.contents + buf = np.ctypeslib.as_array(cast(fbd.data, POINTER(c_uint8)), shape=(fbd.h, fbd.w, 4)) + # Set data pointer to NULL before freeing framebuffer struct to prevent free_framebuffer from also freeing the data + # buffer that is now used by numpy + fb.data = cast(c_void_p(), POINTER(COLOR)) + bdf.free_framebuffer(fb) + return buf + +if __name__ == '__main__': + sendframe(render_text('test')); + -- cgit