Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mostley/PixelPi
Browse files Browse the repository at this point in the history
Conflicts:
	site/server.py
  • Loading branch information
mostley committed Oct 8, 2012
2 parents 97210e0 + f3f93b0 commit 1b2ece1
Show file tree
Hide file tree
Showing 6 changed files with 451 additions and 81 deletions.
Empty file added site/ledlib/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions site/ledlib/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
WHITE = [255,255,255]
RED = [255,0,0]
GREEN = [0,255,0]
BLUE = [0,0,255]
TURQUE = [0,255,255]
YELLOW = [255,255,0]
BLACK = [0,0,0]
64 changes: 64 additions & 0 deletions site/ledlib/remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import socket, math
from vector import *

class Remote:

def __init__(self):
self.UDP_IP = '127.0.0.1'
self.UDP_PORT = 6803
self.verbose = False

def sendGrid(self, grid):
bytedata = self.toByteArray(grid)
self.sendBytes(bytedata)

def sendBytes(self, bytedata):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto( bytedata, (self.UDP_IP, self.UDP_PORT) )

def toByteArray(self, grid):
result = bytearray(BUFFER_SIZE)

i = 0
while i < BUFFER_SIZE:
y = i/3 % PIXEL_DIM_Y
x = int(math.floor(i/3 / PIXEL_DIM_Y))
if x % 2 == 0:
c = grid[x][y]
result[i] = c[0]
i = i + 1
result[i] = c[1]
i = i + 1
result[i] = c[2]
i = i + 1
else:
c = grid[x][(PIXEL_DIM_Y-1)-y]
result[i] = c[0]
i = i + 1
result[i] = c[1]
i = i + 1
result[i] = c[2]
i = i + 1

return result

def createGridBuffer(self, color):
result = []
for x in range(PIXEL_DIM_X):
result.append([])
for y in range(PIXEL_DIM_Y):
result[x].append(color)
return result

def setPixel(self, grid, v, color):
pos = v.toIntArr()
if pos[0] >= 0 and pos[0] < PIXEL_DIM_X and \
pos[1] >= 0 and pos[1] < PIXEL_DIM_Y:
grid[pos[0]][pos[1]] = color

def clearPixels(self, grid, color):
for x in range(len(grid)):
for y in range(len(grid[x])):
grid[x][y] = color


243 changes: 243 additions & 0 deletions site/ledlib/vector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
# -*- coding: utf8 -*-

from random import *
from math import *

class Vector:
def __init__(self, x=0, y=0):
self.x = 0
self.y = 0
if isinstance(x, tuple) or isinstance(x, list):
y = x[1]
x = x[0]
elif isinstance(x, Vector):
y = x.y
x = x.x

self.set(x,y)

@staticmethod
def random(size=1):
sizex = size
sizey = size
if isinstance(size, tuple) or isinstance(size, list):
sizex = size[0]
sizey = size[1]
elif isinstance(size, Vector):
sizex = size.x
sizey = size.y
return Vector(random() * sizex, random() * sizey)

@staticmethod
def randomUnitCircle():
d = random()*pi
return Vector(cos(d)*choice([1,-1]), sin(d)*choice([1,-1]))

@staticmethod
def distance(a, b):
return (a - b).getLength()

def set(self, x,y):
self.x = x
self.y = y

def constrain(self, x,y,width,height):
if self.x < x:
self.x = x
elif self.x > x + width:
self.x = x + width
if self.y < y:
self.y = y
elif self.y > y + height:
self.y = y + height

def toArr(self): return [self.x, self.y]
def toInt(self): return Vector(int(self.x), int(self.y))
def toIntArr(self): return self.toInt().toArr()

def getNormalized(self):
if len(self) != 0:
return self / self.getLength()
else: return Vector(0,0)

def __add__(self, other):
if isinstance(other, Vector):
return Vector(self.x + other.x, self.y + other.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector(self.x + other[0], self.y + other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector(self.x + other, self.y + other)
else:
return NotImplemented
def __sub__(self, other):
if isinstance(other, Vector):
return Vector(self.x - other.x, self.y - other.y)
if isinstance(other, tuple) or isinstance(other, list):
return Vector(self.x - other[0], self.y - other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector(self.x - other, self.y - other)
else:
return NotImplemented
def __rsub__(self, other):
if isinstance(other, Vector):
return Vector(other.x - self.x, other.y - self.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector(other[0] - self.x, other[1] - self.y)
elif isinstance(other, int) or isinstance(other, float):
return Vector(other - self.x, other - self.y)
else:
return NotImplemented
def __mul__(self, other):
if isinstance(other, Vector):
return Vector(self.x * other.x, self.y * other.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector(self.x * other[0], self.y * other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector(self.x * other, self.y * other)
else:
return NotImplemented
def __div__(self, other):
if isinstance(other, Vector):
return Vector(self.x / other.x, self.y / other.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector(self.x / other[0], self.y / other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector(self.x / other, self.y / other)
else:
return NotImplemented
def __rdiv__(self, other):
if isinstance(other, Vector):
return Vector(other.x / self.x, other.y / self.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector(other[0] / self.x, other[1] / self.y)
elif isinstance(other, int) or isinstance(other, float):
return Vector(other / self.x, other / self.y)
else:
return NotImplemented
def __pow__(self, other):
if isinstance(other, int) or isinstance(other, float):
return Vector(self.x ** other, self.y ** other)
else:
return NotImplemented

def __iadd__(self, other):
if isinstance(other, Vector):
self.x += other.x
self.y += other.y
return self
elif isinstance(other, tuple) or isinstance(other, list):
self.x += other[0]
self.y += other[1]
return self
elif isinstance(other, int) or isinstance(other, float):
self.x += other
self.y += other
return self
else:
return NotImplemented
def __isub__(self, other):
if isinstance(other, Vector):
self.x -= other.x
self.y -= other.y
return self
elif isinstance(other, tuple) or isinstance(other, list):
self.x -= other[0]
self.y -= other[1]
return self
elif isinstance(other, int) or isinstance(other, float):
self.x -= other
self.y -= other
return self
else:
return NotImplemented
def __imul__(self, other):
if isinstance(other, Vector):
self.x *= other.x
self.y *= other.y
return self
elif isinstance(other, tuple) or isinstance(other, list):
self.x *= other[0]
self.y *= other[1]
return self
elif isinstance(other, int) or isinstance(other, float):
self.x *= other
self.y *= other
return self
else:
return NotImplemented
def __idiv__(self, other):
if isinstance(other, Vector):
self.x /= other.x
self.y /= other.y
return self
elif isinstance(other, tuple) or isinstance(other, list):
self.x /= other[0]
self.y /= other[1]
return self
elif isinstance(other, int) or isinstance(other, float):
self.x /= other
self.y /= other
return self
else:
return NotImplemented
def __ipow__(self, other):
if isinstance(other, int) or isinstance(other, float):
self.x **= other
self.y **= other
return self
else:
return NotImplemented

def __eq__(self, other):
if isinstance(other, Vector):
return self.x == other.x and self.y == other.y
else:
return NotImplemented
def __ne__(self, other):
if isinstance(other, Vector):
return self.x != other.x or self.y != other.y
else:
return NotImplemented
def __gt__(self, other):
if isinstance(other, Vector):
return len(self) > len(other)
else:
return NotImplemented
def __ge__(self, other):
if isinstance(other, Vector):
return len(self) >= len(other)
else:
return NotImplemented
def __lt__(self, other):
if isinstance(other, Vector):
return len(self) < len(other)
else:
return NotImplemented
def __le__(self, other):
if isinstance(other, Vector):
return len(self) <= len(other)
else:
return NotImplemented

def __eq__(self, other):
if isinstance(other, Vector):
return self.x == other.x and self.y == other.y
else:
return NotImplemented

def __len__(self):
return int(sqrt(self.x**2 + self.y**2))
def getLength(self):
return sqrt(self.x**2 + self.y**2)

def __getitem__(self, key):
if key == "x" or key == "X" or key == 0 or key == "0":
return self.x
elif key == "y" or key == "Y" or key == 1 or key == "1":
return self.y

def __str__(self): return "[x: %(x)f, y: %(y)f]" % self
def __repr__(self): return "{'x': %(x)f, 'y': %(y)f}" % self

def __neg__(self): return Vector(-self.x, -self.y)

39 changes: 39 additions & 0 deletions site/modules/pong.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from ledlib.colors import *
from ledlib.remote import Remote
from vector import *

commands = ['pong_left_up', 'pong_left_down', 'pong_right_up', 'pong_right_down']
remote = None
grid = None

leftPaddle = Vector(0,4)
rightPaddle = Vector(11,4)
ball = Vector(6,4)

def init():
remote = Remote()
grid = remote.createGridBuffer(BLACK)
remote.sendGrid(grid)

def execute(data):
result = False

if data['command'] == 'pong_left_up':
pass
elif data['command'] == 'pong_left_down':
pass
elif data['command'] == 'pong_right_up':
pass
elif data['command'] == 'pong_right_down':
pass

remote.clearPixels(grid, BLACK)

remote.setPixel(grid, Vector(4,7), RED)
remote.setPixel(grid, leftPaddle, BLUE)
remote.setPixel(grid, rightPaddle, BLUE)
remote.setPixel(grid, ball, WHITE)

remote.sendGrid(grid)

return result
Loading

0 comments on commit 1b2ece1

Please sign in to comment.