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

WIP: Refactor FlipdotAPI to use NumPy #20

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
33 changes: 13 additions & 20 deletions scripts/FlipdotAPI/FlipdotMatrix.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
import socket
import numpy as np
try:
from FlipdotAPI import config
from FlipdotAPI.font import font8px
Expand Down Expand Up @@ -45,6 +46,10 @@ def show(self, image):
self.flipdotImage.blitImageAtPosition(image)
self.__updateFlipdotMatrixes()

def showImage(self, image):
self.flipdotImage = image
self.__updateFlipdotMatrixes()

def showBlit (self, image, xPos=0, yPos=0):
"""
send FlipdotImage to display, keeps old pixels around
Expand All @@ -53,12 +58,9 @@ def showBlit (self, image, xPos=0, yPos=0):
self.__updateFlipdotMatrixes()

def __updateFlipdotMatrixes(self):
cuts = np.hsplit(self.flipdotImage.rowArrayOfLineArraysOfPixels, self.numberOfMatrixes)
for i in range(self.numberOfMatrixes):
MatrixSize = self.MatrixSize
xOffset = i*MatrixSize[0]
yOffset = 0
flipdotImage = FlipdotImage.newPartOfAnotherFlipdotImage(self.flipdotImage, newSize=MatrixSize, offset=(xOffset, yOffset))
serializedImageArray = flipdotImage.serializeImageArray(self.transposed)
serializedImageArray = cuts[i].ravel() if not self.transposed else cuts[i].T.ravel()
udpHostAndPort = self.udpHostsAndPorts[i]
self.__showSerializedArrayOfPixels(serializedImageArray, udpHostAndPort)

Expand Down Expand Up @@ -112,7 +114,7 @@ class FlipdotImage(object):
def __init__(self, pixel2DArray):
self.width = len(pixel2DArray[0])
self.height = len(pixel2DArray)
self.rowArrayOfLineArraysOfPixels = pixel2DArray
self.rowArrayOfLineArraysOfPixels = np.array(pixel2DArray)

def blitImageAtPosition(self, flipdotImage, xPos=0, yPos=0):
for lineNr in range(self.height):
Expand Down Expand Up @@ -164,21 +166,9 @@ def __getLetterImageForCharacter(self, char, uppercase = True):

def serializeImageArray(self, transposed = False):
if transposed:
return self.__serializeTransposedImageArray()

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

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

return self.rowArrayOfLineArraysOfPixels.ravel()

def getLine(self, line):
return self.rowArrayOfLineArraysOfPixels[line]
Expand Down Expand Up @@ -231,6 +221,9 @@ def generateColoredLineArrayOfPixels(width, color):
lineArrayOfPixels.append(color)
return lineArrayOfPixels

def __eq__(self, other):
return (self.rowArrayOfLineArraysOfPixels == other.rowArrayOfLineArraysOfPixels).all()

#main
if (__name__=="__main__"):
matrix = FlipdotMatrix()
Expand Down
11 changes: 8 additions & 3 deletions scripts/clock.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
from FlipdotAPI.FlipdotMatrix import FlipdotMatrix, FlipdotImage
from time import sleep
from time import sleep, time
import argparse
import datetime
import math
Expand Down Expand Up @@ -33,14 +33,19 @@ def loop(self):
oldImage = FlipdotImage(self.generateClockImage())
try:
while True:
t = time()
flipImage = FlipdotImage(self.generateClockImage())
if (self.flipdot_out and
flipImage.serializeImageArray() != oldImage.serializeImageArray()):
flipImage != oldImage):
self.matrix.show(flipImage)
oldImage = flipImage
if self.run_once:
break
sleep(self.update_interval)
sleep_time = self.update_interval - (time() - t)
if sleep_time > 0:
sleep(sleep_time)
else:
print("Too slow")
except KeyboardInterrupt:
return

Expand Down
1 change: 1 addition & 0 deletions scripts/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ with import <nixpkgs> {}; {
python3
python3Packages.flask
python3Packages.gevent
python3Packages.numpy
python3Packages.pygame
];
};
Expand Down
9 changes: 6 additions & 3 deletions scripts/pong.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def __init__(self, players):
self.socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
self.address = ("", 5555)
self.socket.bind(self.address)
self.lastCommand = time()
queues = (Queue(), Queue())
self.playerInputHandlers = (
(PlayerInputHandler(players[0],queues[0]), queues[0]),
Expand Down Expand Up @@ -181,6 +182,8 @@ def run(self):
self.playerIds[ri] = player_id
player = ri
self.playerInputHandlers[player][1].put(command)
if command != "NONE":
self.lastCommand = time()

class GameHandler:
def __init__(self, size, speed, udpHostsAndPorts = [], console_out = False, invert = False, panel_defaults = False, ball_speed = 6, player_speed = 3, scores_file = "scores"):
Expand Down Expand Up @@ -218,8 +221,8 @@ def loop(self):
self.ball.change_direction((1,0))
if self.invert:
pass
if self.flipdot_out:
flipImage = FlipdotImage(image.T)
if self.flipdot_out and (playing or (time() - self.inputHandler.lastCommand < 1)):
flipImage = FlipdotImage(np.flipud(image.T))
score_string = "%d - %d" % (self.players[0].score, self.players[1].score)
flipImage.blitTextAtPosition(score_string, xPos=self.get_center()[0]-2*(len(score_string)-1), yPos = 2*round(self.size[1]/12))
total_score_string = "%d - %d" % (self.players[0].total_score, self.players[1].total_score)
Expand All @@ -229,7 +232,7 @@ def loop(self):
flipImage.blitTextAtPosition("Press Left", xPos=self.players[0].upper_right()[0] + 2, yPos = self.size[1] - self.players[0].upper_right()[1])
if not self.players[1].active:
flipImage.blitTextAtPosition("Press Left", xPos=self.players[1].lower_left()[0] - 46, yPos = self.size[1] - self.players[1].upper_right()[1])
self.flipmatrix.show(flipImage)
self.flipmatrix.showImage(flipImage)
if self.console_out:
self.printImage(image)
wait = (1./self.speed) - (time() - t)
Expand Down
1 change: 1 addition & 0 deletions scripts/webapp/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ with import <nixpkgs> {}; {
buildInputs = [
python3
python3Packages.flask
python3Packages.numpy
python3Packages.pillow
];
};
Expand Down