diff options
Diffstat (limited to 'host/matelight.py')
-rw-r--r-- | host/matelight.py | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/host/matelight.py b/host/matelight.py index 91a158f..5b869e6 100644 --- a/host/matelight.py +++ b/host/matelight.py @@ -1,7 +1,8 @@ -import usb import colorsys import numpy as np from itertools import product +from ctypes import c_size_t, c_uint8, c_void_p, c_float, CDLL, Structure, POINTER +import time CRATE_WIDTH = 5 CRATE_HEIGHT = 4 @@ -19,7 +20,14 @@ GAMMA = 2.5 # Brightness of the LEDs in percent. 1.0 means 100%. BRIGHTNESS = 0.2 -dev = usb.core.find(idVendor=0x1cbe, idProduct=0x0003) +ml = CDLL('./libml.so') +ml.matelight_open.restype = c_void_p + +if ml.matelight_usb_init(): + raise OSError('Cannot initialize USB library') +matelights = ml.matelight_open() +if matelights is None: + raise ImportError('Cannot open any Mate Light devices') def sendframe(framedata): """ Send a frame to the display @@ -27,15 +35,25 @@ 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. """ - # Gamma correction - framedata = (((framedata/255) ** GAMMA) * BRIGHTNESS * 255).astype(np.uint8) - for cx, cy in product(range(CRATES_X), range(CRATES_Y)): - datar = framedata[cy*CRATE_HEIGHT:(cy+1)*CRATE_HEIGHT, cx*CRATE_WIDTH:(cx+1)*CRATE_WIDTH, :3] - data = datar.flat - if len(data) != CRATE_SIZE: - raise ValueError('Invalid frame data. Expected {} bytes, got {}.'.format(CRATE_SIZE, len(data))) - # Send framebuffer data - dev.write(0x01, bytes([0, cx, cy])+bytes(data)) - # Send latch command - dev.write(0x01, b'\x01') - + # just use the first Mate Light available + buf = framedata.ctypes.data_as(POINTER(c_uint8)) + ml.matelight_send_frame(matelights, buf, c_size_t(CRATES_X), c_size_t(CRATES_Y), c_float(BRIGHTNESS), True) + +if __name__ == '__main__': + #foo = np.array([[(0, 0, 0, 0)]*DISPLAY_WIDTH]*DISPLAY_HEIGHT) + #bar = np.array([[(255, 0, 255, 0)]*DISPLAY_WIDTH]*DISPLAY_HEIGHT) + x,y = 0,0 + while True: + x += 1 + if x == DISPLAY_WIDTH: + x = 0 + y += 1 + if y == DISPLAY_HEIGHT: + y = 0 + foo = np.array([[(64, 0, 0, 255)]*DISPLAY_WIDTH]*DISPLAY_HEIGHT, dtype=np.uint8) + foo[y,x,:] = (0,64,0,255) + #from terminal import printframe + sendframe(foo) + #printframe(foo) + #sendframe(bar) + time.sleep(0.1) |