From 18fd12e916c8a33007a8ccd305d280493e8cd27d Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 17 Feb 2014 00:57:47 +0100 Subject: ctypes pointer narf working now --- firmware/main.c | 4 ++-- host/font.c | 2 +- host/main.c | 4 ++++ host/matelight.py | 18 +++++++++++------- host/server.py | 53 ++++++++++++++++++++++++++++++++++++++++------------- 5 files changed, 58 insertions(+), 23 deletions(-) diff --git a/firmware/main.c b/firmware/main.c index e101296..945e0af 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -65,8 +65,8 @@ unsigned const char const CRATE_MAP[CRATE_COUNT] = { unsigned char framebuffer1[BUS_COUNT*BUS_SIZE]; unsigned char framebuffer2[BUS_COUNT*BUS_SIZE]; -unsigned char *framebuffer_input = framebuffer1; -unsigned char *framebuffer_output = framebuffer2; +volatile unsigned char *framebuffer_input = framebuffer1; +volatile unsigned char *framebuffer_output = framebuffer2; unsigned long framebuffer_read(void *data, unsigned long len); /* Kick off DMA transfer from RAM to SPI interfaces */ diff --git a/host/font.c b/host/font.c index 85882ad..ee9adb3 100644 --- a/host/font.c +++ b/host/font.c @@ -79,7 +79,7 @@ void free_glyphtable(glyphtable_t *glyph_table){ glyphtable_t *read_bdf(FILE *f){ glyphtable_t *glyph_table = malloc(sizeof(glyphtable_t)); if(!glyph_table){ - printf("Cannot allocate glyph table\n"); + fprintf(stderr, "Cannot allocate glyph table\n"); goto error; } memset(glyph_table, 0, sizeof(*glyph_table)); diff --git a/host/main.c b/host/main.c index c6086e1..fbe126a 100644 --- a/host/main.c +++ b/host/main.c @@ -20,6 +20,7 @@ void free_framebuffer(framebuffer_t *fb){ + printf("Freeing %lx and %lx\n", fb->data, fb); free(fb->data); free(fb); } @@ -78,6 +79,7 @@ framebuffer_t *framebuffer_render_text(char *s, glyphtable_t *glyph_table){ if(g->height > gbufheight) gbufheight = g->height; + gbufwidth += g->width; } /* For easier rendering on the terminal, round up to multiples of two */ @@ -301,6 +303,7 @@ framebuffer_t *framebuffer_render_text(char *s, glyphtable_t *glyph_table){ fb->w = gbufwidth; fb->h = gbufheight; fb->data = gbuf; + printf("Returning buffer with w %ld h %ld memory location %lx\n", gbufwidth, gbufheight, gbuf); return fb; error: free(gbuf); @@ -354,5 +357,6 @@ void console_render_buffer(framebuffer_t *fb){ } printf("\n"); } + printf("\033[0m"); } diff --git a/host/matelight.py b/host/matelight.py index f75ae80..55295f6 100644 --- a/host/matelight.py +++ b/host/matelight.py @@ -1,7 +1,7 @@ import usb import colorsys import numpy as np -import itertools +from itertools import product CRATE_WIDTH = 5 CRATE_HEIGHT = 4 @@ -20,16 +20,20 @@ def sendframe(framedata): The argument contains a h * w array of 3-tuples of (r, g, b)-data or 4-tuples of (r, g, b, a)-data where the a channel is ignored. """ - def chunks(l, n): - for i in xrange(0, len(l), n): - yield l[i:i+n] - - for cx, cy in itertools.product(range(DISPLAY_WIDTH), range(DISPLAY_HEIGHT)): + for cx, cy in product(range(CRATES_X), range(CRATES_Y)): data = [ v for x in range(CRATE_WIDTH) for y in range(CRATE_HEIGHT) for v in framedata[cy*CRATE_HEIGHT + y][cx*CRATE_WIDTH + x][:3] ] +# data = [0,0,0]*CRATE_WIDTH*CRATE_HEIGHT +# for x, y in product(range(CRATE_WIDTH), range(CRATE_HEIGHT)): +# r, g, b, _ = framedata[cy*CRATE_HEIGHT + y][cx*CRATE_WIDTH + x] +# data[x*3 + y*CRATE_WIDTH*3 + 0] = r +# data[x*3 + y*CRATE_WIDTH*3 + 1] = g +# data[x*3 + y*CRATE_WIDTH*3 + 2] = b +# print(r, g, b) + #data = [ framedata[DISPLAY_WIDTH*3*(cy*CRATE_HEIGHT + y) + 3*(cx*CRATE_WIDTH + x)+c] for x in range(CRATE_WIDTH) for y in range(CRATE_HEIGHT) for c in range(3) ] if len(data) != FRAME_SIZE: raise ValueError('Invalid frame data. Expected {} bytes, got {}.'.format(FRAME_SIZE, len(data))) # Send framebuffer data - dev.write(0x01, bytes([0, x, y])+bytes(data)) + dev.write(0x01, bytes([0, cx, cy])+bytes(data)) # Send latch command dev.write(0x01, b'\x01') diff --git a/host/server.py b/host/server.py index c8e665a..70afdf9 100755 --- a/host/server.py +++ b/host/server.py @@ -1,14 +1,16 @@ #!/usr/bin/env python -from ctypes import CDLL, POINTER, c_void_p, Structure, c_uint8, c_size_t, cast +from ctypes import CDLL, POINTER, c_void_p, Structure, c_uint8, c_size_t, cast, addressof import numpy as np +from itertools import product from matelight import sendframe +from terminal import printframe class COLOR(Structure): - _fields_ = ('r', c_uint8), ('g', c_uint8), ('b', c_uint8), ('a', c_uint8) + _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) + _fields_ = [('data', POINTER(COLOR)), ('w', c_size_t), ('h', c_size_t)] bdf = CDLL('./libbdf.so') bdf.read_bdf_file.restype = c_void_p @@ -17,16 +19,41 @@ 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 + assert unifont + fb = bdf.framebuffer_render_text(bytes(str(text), 'UTF-8'), unifont) + fbd = fb.contents + print('FB STRUCT: w {} h {} memory location {:x}'.format(fbd.w, fbd.h, addressof(fbd.data.contents))) + print("TYPE:", type(fbd.data)) + casted = cast(fbd.data, POINTER(c_uint8)) + print("CASTED TYPE:" , type(casted)) + buf = np.ctypeslib.as_array(cast(fbd.data, POINTER(c_uint8)), shape=(fbd.h, fbd.w, 4)) + print('RAW DATA:') + alen = fbd.h*fbd.w +# for i in range(alen): +# c = fbd.data[i] +# print('{:x}'.format(addressof(c)), c.r, c.g, c.b) + print('BUF', buf.shape, buf.dtype) +# for x, y, z in product(range(fbd.h), range(fbd.w), range(4)): +# print(buf[x][y][z]) + + # 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 + bdf.console_render_buffer(fb) + fbd.data = cast(c_void_p(), POINTER(COLOR)) + bdf.free_framebuffer(fb) + print('Rendered {} chars into a buffer of w {} h {} numpy buf {}'.format(len(text), fbd.w, fbd.h, buf.shape)) + return buf if __name__ == '__main__': - sendframe(render_text('test')); + fb = render_text('foobarbaz'); + print('buffer shape {} dtype {}'.format(fb.shape, fb.dtype)) +# import colorsys +# h, w, _ = fb.shape +# for x, y in product(range(w), range(h)): +# r,g,b = colorsys.hsv_to_rgb(x*0.03+y*0.05, 1, 1) +# fb[x][y] = (r,g,b,0) +# print(fb) + sendframe(fb) + #printframe(fb) + -- cgit