From a2dedc633fa5c80c00d562735518b0157930b540 Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Tue, 22 Sep 2015 21:13:49 +0200 Subject: [PATCH] Added python2 wrapper for twinklclient.so --- .gitignore | 2 ++ animations/fullwhite.py | 32 ++++++++++------- animations/twinklclient.py | 74 ++++++++++++++++++++++++++++++++++++++ include/twinklsocket.h | 6 ++-- 4 files changed, 99 insertions(+), 15 deletions(-) create mode 100644 animations/twinklclient.py diff --git a/.gitignore b/.gitignore index 8efbdf3..7fe3e37 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ *.swp *.swo *.o +*.pyc +*.so bin/ diff --git a/animations/fullwhite.py b/animations/fullwhite.py index 1c1a967..628658f 100644 --- a/animations/fullwhite.py +++ b/animations/fullwhite.py @@ -2,12 +2,12 @@ from random import randint from time import sleep -from subprocess import Popen, PIPE +from twinklclient import TwinklSocket, TwinklMessage HEIGHT = 8 WIDTH = 6 -# As viewn from the inside +# As viewn from the inside BOX_MAP = [ [357, 18, 369, 186, 249, 228, 51], [279, 10, 57, 159, 300, 108, 204], @@ -20,27 +20,35 @@ BOX_MAP = [ ] -channels = {} +msg = TwinklMessage() def set_box(x,y,r,g,b): if x >= 0 and y >= 0 and x < WIDTH and y < HEIGHT: base_address = BOX_MAP[y][x] - channels[base_address] = r - channels[base_address + 1] = g - channels[base_address + 2] = b + msg[base_address] = r + msg[base_address + 1] = g + msg[base_address + 2] = b -def output_channels(): - for channel, value in channels.items(): - print "%d : %d" % (channel, value) - - print "" +socket = TwinklSocket("localhost", "1337") + +msg.set_priority(0); for x in range(0, WIDTH): for y in range(0, HEIGHT): set_box(x,y, 255, 255, 255) -output_channels() +socket.send(msg) + +sleep(5) + +msg.reset() +msg.set_priority(0) +socket.send(msg) + +msg.destroy() +socket.close() + diff --git a/animations/twinklclient.py b/animations/twinklclient.py new file mode 100644 index 0000000..59f61c5 --- /dev/null +++ b/animations/twinklclient.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python2 + +from ctypes import * + +_TWINKL_CLIENT = cdll.LoadLibrary("../bin/twinklclient.so") + +_TWINKL_CLIENT.twinklmsg_create.argtypes = [] +_TWINKL_CLIENT.twinklmsg_create.restype = c_void_p + +_TWINKL_CLIENT.twinklmsg_destroy.argtypes = [c_void_p] +_TWINKL_CLIENT.twinklmsg_destroy.restype = None + +_TWINKL_CLIENT.twinklmsg_reset.argtypes = [c_void_p] +_TWINKL_CLIENT.twinklmsg_reset.restype = None + +_TWINKL_CLIENT.twinklmsg_set_priority.argtypes = [c_void_p, c_ubyte] +_TWINKL_CLIENT.twinklmsg_set_priority.restype = None + +_TWINKL_CLIENT.twinklmsg_set_value.argtypes = [c_void_p, c_ushort, c_ubyte] +_TWINKL_CLIENT.twinklmsg_set_value.restype = None + +_TWINKL_CLIENT.twinklmsg_unset_value.argtypes = [c_void_p, c_ushort] +_TWINKL_CLIENT.twinklmsg_unset_value.restype = None + +_TWINKL_CLIENT.twinklsocket_close.argtypes = [c_int] +_TWINKL_CLIENT.twinklsocket_close.restype = None + +_TWINKL_CLIENT.twinklsocket_open.argtypes = [c_char_p, c_char_p] +_TWINKL_CLIENT.twinklsocket_open.restype = c_int + +_TWINKL_CLIENT.twinklsocket_send.argtypes = [c_int, c_void_p] +_TWINKL_CLIENT.twinklsocket_send.restype = None + + +class TwinklSocket(object): + def __init__(self, host, port): + self._socket = _TWINKL_CLIENT.twinklsocket_open(host, port) + + def close(self): + _TWINKL_CLIENT.twinklsocket_close(self._socket) + + def send(self, msg): + _TWINKL_CLIENT.twinklsocket_send(self._socket, msg._pointer) + + +class TwinklMessage(object): + def __init__(self): + self._pointer = _TWINKL_CLIENT.twinklmsg_create() + + def destroy(self): + _TWINKL_CLIENT.twinklmsg_destroy(self._pointer) + + def reset(self): + _TWINKL_CLIENT.twinklmsg_reset(self._pointer) + + def set_priority(self, priority): + _TWINKL_CLIENT.twinklmsg_set_priority(self._pointer, c_ubyte(priority)) + + def set_value(self, channel, value): + _TWINKL_CLIENT.twinklmsg_set_value(self._pointer, c_ushort(channel), c_ubyte(value)) + + def unset_value(self, channel): + _TWINKL_CLIENT.twinklmsg_set_value(self._pointer, c_ushort(channel)) + + + def __setitem__(self, channel, value): + if value == None: + self.unset_value(channel) + else: + self.set_value(channel, value) + + + + diff --git a/include/twinklsocket.h b/include/twinklsocket.h index 3dcfadc..831c5dc 100644 --- a/include/twinklsocket.h +++ b/include/twinklsocket.h @@ -3,10 +3,10 @@ #include "message.h" -extern int twinklsocket_open(const char *host, const char *port); +int twinklsocket_open(const char *host, const char *port); -extern void twinklsocket_send(int sockfd, const struct twinkl_message *message); +void twinklsocket_send(int sockfd, const struct twinkl_message *message); -extern void twinklsocket_close(int sockfd); +void twinklsocket_close(int sockfd); #endif