Created
March 14, 2011 14:22
-
-
Save dodo/869195 to your computer and use it in GitHub Desktop.
ledwall content script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import random, shuffle | |
from time import sleep | |
from math import floor, ceil, sqrt, pow | |
import socket | |
# pentawall HD 16p (HD=hexadecimal) | |
PORT = 1338 | |
HOST = "172.22.100.252"#"172.22.99.6" | |
WIDTH = 16 | |
HEIGHT = 15 | |
#------------------------------------------------------------------------------# | |
def black(): | |
send(*(5*[0])) | |
def random_ping(): | |
old = [] | |
while True: | |
x = 1 + floor(random()*WIDTH) | |
y = 1 + floor(random()*HEIGHT) | |
r = floor(random()*256) + 123 | |
g = floor(random()*256) + 123 | |
b = floor(random()*256) + 123 | |
old.append([x, y, r, b, g]) | |
if len(old) > 222: | |
old.pop(0) | |
for p in old: | |
for i in range(2,5): | |
p[i] = round(p[i] * 0.5) | |
send(*p) | |
sleep(0.1)#sleep(0.000575) | |
def snake(): | |
body = [[0, 0, 255, 0, 0]]*7 | |
while True: | |
head = list(body[-1]) | |
uniq = False | |
while not uniq: | |
head[int(random() < 0.5)] += (-1 if random() < 0.5 else 1) * (random() < 0.5) | |
uniq = True | |
for p in body: | |
if head[0] == p[0] and head[1] == p[1]: | |
uniq = False | |
break | |
for i in range(2, 5): | |
head[i] = floor(random()*56) + 200 | |
if head[0] < 1: head[0] = WIDTH | |
if head[1] < 1: head[1] = HEIGHT | |
if head[0] > WIDTH : head[0] = 1 | |
if head[1] > HEIGHT: head[1] = 1 | |
body.append(head) | |
tail = body.pop(0) | |
send(tail[0], tail[1], 0, 0, 0) | |
send(*head) | |
sleep(0.05) | |
def blubb(): | |
size = 0.05 | |
center = (ceil(WIDTH/2.0), ceil(HEIGHT/2.0)) | |
color = [255, 0, 0] | |
old_color = [0, 0, 0] | |
while True: | |
center = (ceil(random()*WIDTH-random()*WIDTH/2.0), | |
ceil(random()*HEIGHT-random()*HEIGHT/2.0)) | |
X = list(range(1, WIDTH + 1)) | |
Y = list(range(1, HEIGHT + 1)) | |
shuffle(X) | |
shuffle(Y) | |
for y in Y: | |
for x in X: | |
_x = x - center[0] | |
_y = y - center[1] | |
l = abs(sqrt(_x*_x + _y*_y) - size) * 0.4 | |
l = round( l * 256 ) | |
if l < 0: l = 0 | |
if l > 255: l = 255 | |
l = (255 - l) / 255.0 | |
if l > 0.1: | |
send(x, y, | |
floor(color[0]*l),# + old_color[0]*(1-l)), | |
floor(color[1]*l),# + old_color[1]*(1-l)), | |
floor(color[2]*l))# + old_color[2]*(1-l))) | |
sleep(0.0008) | |
size += 0.3 | |
if size > (WIDTH+HEIGHT)*0.45: | |
#center = (ceil(random()*WIDTH-WIDTH/2.0), ceil(random()*HEIGHT-HEIGHT/2.0)) | |
old_color = list(color) | |
size = 0.05 | |
for i in range(3): | |
color[i] = floor(random()*256) | |
def meta(): | |
i = 0 | |
balls = [ [random()*WIDTH, random()*HEIGHT, random(), random(), random(), random()] for i in range(9) ] | |
while True: | |
X = list(range(1, WIDTH + 1)) | |
Y = list(range(1, HEIGHT + 1)) | |
#shuffle(X) | |
#shuffle(Y) | |
arr = [] | |
for y in Y: | |
for x in X: | |
f, r, g, b = 0, 0, 0, 0 | |
for ball in balls: | |
force = sqrt(pow(ball[0]-x,2)+pow(ball[1]-y,2))+0.000000001 | |
r += ball[2]/force * 0.9 - 0.01 | |
g += ball[3]/force * 0.9 - 0.01 | |
b += ball[4]/force * 0.9 - 0.01 | |
f += ball[5]/force * 0.6 - 0.01 | |
data = (r + g + b + f) / 4.0 - 0.01 | |
#if data > 0.5:# and data < 0.55: | |
r = min(floor(r*256*f*1.8), 255) | |
g = min(floor(g*256*f*1.8), 255) | |
b = min(floor(b*256*f*1.8), 255) | |
if r < 0: r = 0 | |
if g < 0: g = 0 | |
if b < 0: b = 0 | |
#if data < 0.5 or data > 0.7: | |
# r, g, b = 0, 0, 0 | |
#send(x,y,r,g,b) | |
arr.extend([r,g,b]) | |
#else: | |
# send(x,y,0,0,0) | |
sendarr(arr) | |
sleep(0.001) | |
#send(*(5*[0])) # clean screen | |
for ball in balls: | |
ball[0] += random()*0.1 - random()*0.1 | |
ball[1] += random()*0.1 - random()*0.1 | |
if ball[0] > WIDTH*1.1: ball[0] = -WIDTH*0.1 | |
if ball[0] < -WIDTH*0.1: ball[0] = WIDTH*1.1 | |
if ball[1] > HEIGHT*1.1: ball[1] = -HEIGHT*0.1 | |
if ball[1] < -HEIGHT*0.1: ball[1] = HEIGHT*1.1 | |
i += 1 | |
if i%3 == 0: | |
for ball in balls: | |
ball[5] += random()*0.4 - random()*0.4 | |
elif i%15 == 0: | |
i = 0 | |
for ball in balls: | |
for n in range(3): | |
ball[n+2] += random()*0.1 - random()*0.1 | |
if ball[n+2] > 1: ball[n+2] = 1 | |
if ball[n+2] < 0: ball[n+2] = 0 | |
def schach(): | |
a, b = [255,0,0], [0,0,255] | |
while True: | |
X = list(range(1, WIDTH + 1)) | |
Y = list(range(1, HEIGHT + 1)) | |
#shuffle(X) | |
#shuffle(Y) | |
arr = [] | |
#a, b = [255,0,0], [0,0,255] | |
for y in Y: | |
for x in X: | |
arr.extend(a) | |
a, b = b, a | |
sendarr(arr) | |
a, b = b, a | |
sleep(0.009) | |
# black, random_ping, snake, blubb, meta, schach | |
draw = blubb | |
#------------------------------------------------------------------------------# | |
con = None | |
def send(*args): | |
global con | |
con.send(bytes("02{:02x}{:02x}{:02x}{:02x}{:02x}\r\n".format(*args), "ascii")) | |
#con.recv(256) | |
def sendarr(arr): | |
global con | |
con.send(bytes("03"+"".join(map(lambda a:"{:02x}".format(a),arr))+"\r\n", "ascii")) | |
#con.recv(256) | |
#print(con.recv(256)) | |
def main(): | |
global con | |
print("* connecting", HOST, PORT, "...") | |
con = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
try: | |
con.connect((HOST, PORT)) | |
print("* connected") | |
black() # clean screen | |
draw() | |
except KeyboardInterrupt: | |
print(" → user abort") | |
finally: | |
con.close() | |
print("* connection closed") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment