raumgirl/plugins/Flipdot/plugin.py

76 lines
2.3 KiB
Python

###
# Copyright (c) 2011, henne
# All rights reserved.
#
#
###
from threading import Event, Thread
import subprocess
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.ircmsgs as ircmsgs
import socket, time
import Image, ImageFont, ImageDraw, sys
import fileinput
class Flipdot(callbacks.Plugin):
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
def list2byte(l):
byte = 0
i = 0
for i in range(8):
byte += 2**(7-i) if l[i] else 0
return byte
def array2packet(a):
return str(bytearray([list2byte(a[i*8:i*8+8]) for i in range(len(a)/8)]))
def str2array(s):
IMG_SIZE = (80,16)
FONT_SIZE = 11
FONT_OFFSET= (1, 3)
C_BLACK = (0, 0, 0)
C_WHITE = (255, 255, 255)
image = Image.new("RGBA", IMG_SIZE, C_BLACK)
draw = ImageDraw.Draw(image)
draw.fontmode = "1" # No AA
#font = ImageFont.load_default()
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSans.ttf", FONT_SIZE)
draw.text(FONT_OFFSET, s, font=font, fill=C_WHITE)
imgmap = []
for pixel in image.getdata():
r, g, b, a = pixel
if r == 255:
imgmap.append(1)
else:
imgmap.append(0)
return imgmap
def __init__(self, irc):
self.__parent = super(Flipdot, self)
self.__parent.__init__(irc)
# def __del__(self, irc):
# ampel.connection.close()
def start(self, irc, msg, args):
irc.reply("Lampel kann nun gesteuert werden (noch nicht.....)")
# ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
# ampel.connection.write("io get port 2\n")
# buf = ampel.connection.read_until("\n", 1)
# self.state = buf[-2]
# irc.reply(self.state)
start = wrap(start)
def hilfe(self, irc, msg, args):
irc.reply("hilfe: Hilft dir...")
hilfe = wrap(hilfe)
def send(self, irc, msg, args):
sock.sendto(array2packet(str2array(msg.strip())), (self.registryValue('host'), self.registryValue('port')))
send = wrap(send)
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: