Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python api for flipdots, hqstatus script #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ Gemfile.lock
.rvmrc
tmp/
Gemfile.lock
.project
.pydevproject
*.py[cod]

52 changes: 52 additions & 0 deletions archive/scripts/gen_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import socket, time
import Image, ImageFont, ImageDraw, sys
import fileinput

UDPHOST="2001:7f0:3003:cafe:222:f9ff:fe01:c65"
UDPPORT=2323

FPS = 3

IMG_SIZE = (40, 16)
FONT_SIZE = 8
FONT_OFFSET= (1, -1)

C_BLACK = (0, 0, 0)
C_WHITE = (255, 255, 255)

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):
image = Image.new("RGBA", IMG_SIZE, C_BLACK)
draw = ImageDraw.Draw(image)
draw.fontmode = "1" # No AA
font = ImageFont.load_default()
# font = ImageFont.truetype("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

while True:
text = sys.stdin.readline().decode('utf-8')
if text == "":
break
sock.sendto(array2packet(str2array(text.strip())), (UDPHOST, UDPPORT))
time.sleep(1.0/FPS)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
187 changes: 187 additions & 0 deletions scripts/FlipdotAPI/FlipdotMatrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import socket
from font import font8px

class FlipdotMatrix():
def __init__(self,
udpHostAndPort = ("2001:7f0:3003:cafe:222:f9ff:fe01:c65",2323),
imageSize=(40, 16)
):
self.__sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
self.udpHostAndPort=udpHostAndPort
self.flipdotImage = FlipdotImage.newBlackFlipdotImage(imageSize[0], imageSize[1])

def resetAll(self):
"""
flip every pixel at least once, end with cleared Matrix
"""
width = self.flipdotImage.width
height = self.flipdotImage.height
whiteImage = FlipdotImage.newWhiteFlipdotImage(width, height)
blackImage = FlipdotImage.newBlackFlipdotImage(width, height)
self.show(whiteImage)
self.show(blackImage)

def __showSerializedArrayOfPixels(self, imageArray):
udpPacket = FlipdotMatrix.__arrayToPacket(imageArray)
self.__sendUdpPackage(udpPacket)

def show(self, image):
"""
send FlipdotImage to display. fills up with black pixels
"""
self.__clearFlipdotImageWithoutUpdate()
self.flipdotImage.blitImageAtPosition(image)
self.__updateFlipdotMatrix()

def showBlit (self, image, xPos=0, yPos=0):
"""
send FlipdotImage to display, keeps old pixels around
"""
self.flipdotImage.blitImageAtPosition(image, xPos, yPos)
self.__updateFlipdotMatrix()

def __updateFlipdotMatrix(self):
serializedImageArray = self.flipdotImage.serializeImageArray()
self.__showSerializedArrayOfPixels(serializedImageArray)

def clear(self):
"""
clears display. fills with black pixels
"""
self.__clearFlipdotImageWithoutUpdate()
self.__updateFlipdotMatrix()

def __clearFlipdotImageWithoutUpdate(self):
width = self.flipdotImage.width
height = self.flipdotImage.height
self.flipdotImage = FlipdotImage.newBlackFlipdotImage(width, height)

def showText(self, text, linebreak = False, xPos=0, yPos = 0):
"""
print text to display
"""
self.__clearFlipdotImageWithoutUpdate()
self.flipdotImage.blitTextAtPosition(text, linebreak, xPos, yPos)
self.__updateFlipdotMatrix()

def showBlitText(self, text, linebreak=False, xPos=0, yPos=0):
"""
print text to display, keeps old pixels around
"""
self.flipdotImage.blitTextAtPosition(text, linebreak, xPos, yPos)
self.__updateFlipdotMatrix()

@staticmethod
def __arrayToPacket(imageArray):
return str(bytearray([FlipdotMatrix.__list2byte(imageArray[i*8:i*8+8]) for i in range(len(imageArray)/8)]))

@staticmethod
def __list2byte(ArrayOfBinaryInts):
byte = 0
for i in range(8):
byte += 2**(7-i) if ArrayOfBinaryInts[i] else 0
return byte


def __sendUdpPackage(self, udpPacket):
self.__sock.sendto(udpPacket, self.udpHostAndPort)



class FlipdotImage(object):
BLACKPIXEL = 0
WHITEPIXEL = 1

def __init__(self, pixel2DArray):
self.width = len(pixel2DArray[0])
self.height = len(pixel2DArray)
self.rowArrayOfLineArraysOfPixels = pixel2DArray

def blitImageAtPosition(self, flipdotImage, xPos=0, yPos=0):
for lineNr in range(self.height):
newImageLineNr = lineNr-yPos
if newImageLineNr >= 0 and flipdotImage.height > newImageLineNr:
self.__blitLineAtPosition(flipdotImage, lineNr, newImageLineNr, xPos, yPos)

def __blitLineAtPosition(self, flipdotImage, lineNr, newImageLineNr, xPos, yPos):
for rowNr in range(self.width):
newImageRowNr = rowNr - xPos
if newImageRowNr >= 0 and flipdotImage.width > newImageRowNr:
self.rowArrayOfLineArraysOfPixels[lineNr][rowNr] = flipdotImage.rowArrayOfLineArraysOfPixels[newImageLineNr][newImageRowNr]

def blitTextAtPosition(self, text, autoLineBreak = False, xPos = 0, yPos = 0, __indentXPos=None):
if __indentXPos==None:
__indentXPos = xPos

if len(text) <= 0:
return

letterImage = self.__getLetterImageForNextLetter(text)

if self.__isLineBreakRequired(text, autoLineBreak, letterImage, xPos):
xPos = __indentXPos
yPos = yPos + font8px["lineheight"]

self.blitImageAtPosition(letterImage, xPos, yPos)

nextLetterXPos = xPos + letterImage.width + font8px["whitespace"]
self.blitTextAtPosition(text[1:], autoLineBreak, nextLetterXPos, yPos, __indentXPos)

def __isLineBreakRequired(self, text, autoLineBreak, letterImage, xPos):
if text[:1] == "\n":
return True
elif autoLineBreak and self.__isEndOfLineReached(letterImage, xPos):
return True
else:
return False

def __isEndOfLineReached(self, letterImage, xPos):
return letterImage.width > self.width-xPos

def __getLetterImageForNextLetter(self, text):
nextLetter = text[:1].upper()
if not nextLetter in font8px:
nextLetter="?"
return FlipdotImage(font8px[nextLetter])

def serializeImageArray(self):
imageArray = []
for y in range(self.height):
for x in range(self.width):
imageArray.append(self.rowArrayOfLineArraysOfPixels[y][x])
return imageArray

def getLine(self, line):
return self.rowArrayOfLineArraysOfPixels[line]

@classmethod
def newBlackFlipdotImage(cls, width, height):
pixel2DArray = cls.generateColoredRowArrayOfLineArraysOfPixels(width, height, FlipdotImage.BLACKPIXEL)
return cls(pixel2DArray)

@classmethod
def newWhiteFlipdotImage(cls, width, height):
pixel2DArray = cls.generateColoredRowArrayOfLineArraysOfPixels(width, height, FlipdotImage.WHITEPIXEL)
return cls(pixel2DArray)

@staticmethod
def generateColoredRowArrayOfLineArraysOfPixels(width, height, color):
rowArrayOfLineArrayOfPixels = []
for y in range(height):
rowArrayOfLineArrayOfPixels.append(FlipdotImage.generateColoredLineArrayOfPixels(width, color))
return rowArrayOfLineArrayOfPixels

@staticmethod
def generateColoredLineArrayOfPixels(width, color):
lineArrayOfPixels = []
for x in range(width):
lineArrayOfPixels.append(color)
return lineArrayOfPixels

#main
if (__name__=="__main__"):
matrix = FlipdotMatrix()
matrix.resetAll()
matrix.showText("flip the dots!", True)


Empty file added scripts/FlipdotAPI/__init__.py
Empty file.
Loading