Browse Source

Added python2 wrapper for twinklclient.so

pull/4/head
Sebastian 7 years ago
parent
commit
a2dedc633f
  1. 2
      .gitignore
  2. 32
      animations/fullwhite.py
  3. 74
      animations/twinklclient.py
  4. 6
      include/twinklsocket.h

2
.gitignore vendored

@ -1,6 +1,8 @@
*.swp
*.swo
*.o
*.pyc
*.so
bin/

32
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()

74
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)

6
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

Loading…
Cancel
Save