some more changes

master
cyrille 2013-09-30 23:36:19 +02:00
parent 581bcd71e9
commit ea4d06c5eb
5 changed files with 57 additions and 184 deletions

View File

@ -1,197 +1,38 @@
# Client program "cellular"
from socket import *
import sys, time
from math import *
from random import randint
from copy import deepcopy
# Set the socket parameters
local_port = 5001
# UDP webserver port to bypass frameserver for testing
remote_port = 4422
# port to frameserver
remote_port = 4321
# TODO: autodetect interface address for remote application
outgoing_if = "127.0.0.1"
remote_host = "127.0.0.1"
# udp is the default for DGRAM
UDPSock = socket(AF_INET, SOCK_DGRAM)
# we MUST bind, otherwise python will choose a different port for each
# connection
UDPSock.bind((outgoing_if, local_port))
# we will not use connections so we can keep working even if the server
# goes down or refuses connection
#UDPSock.connect((remote_host, remote_port))
# this is our handshake, weak certification
hash = "s2l\n<8<18 " # 10 bytes
# plus 2 bytes below
alpha = chr(255)
z_buffer = chr(1) + "\n"
# frame of 2 dimensional array holding data [r,g,b,a]
wdata = []
# segment display, each segment holds 8 values of [r,g,b,a]
sdata = []
from s2llib import *
# setup window area, channels and segments
segwindows = 12
seg_windows = 12
segments = 8 # number of segments in a window (7 + period/dot/point)
width = 12
height = 8
segchannels = 4 # RGBA
# timing elements
# timer will hold the elapsed time in seconds
timer = 0
sleeptime = 100 # milliseconds
frequency = 1000 / sleeptime # frames per second
cell = syndelights_canvas("s2l\n<8<18 ", 1, width, height, seg_windows, "127.0.0.1", 5001, "127.0.0.1", 4321, 2)
# function resets map to zero
def set_wdata(r, g, b):
global wdata
wdata = []
cell.connect()
for x in xrange(width):
wdata.append([0] * height)
for y in xrange(height):
# create an SRGBA array at each location
# where S is the state, and RGBA is colour
wdata[x][y] = [0, r, g, b, 1]
# function resets segment display
def set_sdata(r, g, b):
global sdata
sdata = []
for s in xrange(segwindows):
sdata.append([0] * segments)
for seg in xrange(segments):
# create an SRGBA array at each location
# where S is the state, and RGBA is colour
sdata[s][seg] = [0, r, g, b, 1]
# convert [r,g,b,a] to chr string
def i2c(rgba):
# ignore rgba[0] because it is the state
return chr(int(rgba[1])) + chr(int(rgba[2])) + chr(int(rgba[3])) + chr(int(rgba[4]))
# determine if a cells neighbour is active (any will do)
def has_active_neighb(x, y):
# check the cell to the left
if x > 0 and wdata[x - 1][y][0] > 0:
return True
# check the cell above
if y > 0 and wdata[x][y - 1][0] > 0:
return True
# check the cell to the right
if x < (width - 1) and wdata[x + 1][y][0] > 0:
return True
# check the cell below
if y < (height - 1) and wdata[x][y + 1][0] > 0:
return True
# function sends wdata and sdata state to server
def send_update():
#zero out the data buffer
data = hash
data = data + z_buffer # 12 bytes
# write frame data
for y in xrange(height):
for x in xrange(width):
data = data + i2c( wdata[x][y] )
#print str(wdata[x][y]) + " ",
#print "\n"
data = data + "\n"
# write segment window displays
for s in xrange(segwindows):
for seg in xrange(segments):
data = data + i2c( sdata[s][seg] )
data = data + "\n"
# print data
# send the data packet to remote host
UDPSock.sendto(data,(remote_host,remote_port))
# reset/initialize map
cell.reset_windows(0, 0, 0, 0, 0)
cell.reset_segment_windows(0, 255, 255, 255, 255)
# main loop
while True:
# reset/initialize map
set_wdata(67,67,67)
set_sdata(255,255,255)
# set start seed
# get random location
rx = randint(0, width - 1)
ry = randint(0, height - 1)
wdata[rx][ry] = [1,1,0,0,1]
# print "origin(" + str(rx) + "," + str(ry) + ")"
# main logic loop
for t in range(23):
cell.setwin_xy_srgba_array(rx, ry, [0, randint(0,127), randint(0, 127), randint(0, 127), 255])
cell.draw()
# push update to clients
send_update()
# create CA buffer
data_buffer = deepcopy(wdata)
for y in xrange(height):
for x in xrange(width):
# if cell is active, increase its rating/value
if wdata[x][y][0] > 0:
# change the state of the cell
data_buffer[x][y][0] += 16
# assign stat to red colour
data_buffer[x][y][1] = data_buffer[x][y][0]
# check we don't go too high
# if the state is over 127, reset red
if data_buffer[x][y][0] > 127:
# assign max stat to red colour
data_buffer[x][y][1] = 0
# assign stat to green colour
data_buffer[x][y][2] = data_buffer[x][y][0] - 128
# if the state is over 128*2, reset green
if data_buffer[x][y][0] > 255:
# assign max stat to green colour
data_buffer[x][y][2] = 0
# assign stat to blue colour
data_buffer[x][y][3] = data_buffer[x][y][0] - 255
# if the state is over 128*3, reset blue
if data_buffer[x][y][0] > 383:
data_buffer[x][y][3] = data_buffer[x][y][0]%128
# if not active, check if adjacent cell is active
if wdata[x][y][0] == 0 and has_active_neighb(x, y):
# activate and colour
data_buffer[x][y][0] = 1
data_buffer[x][y][1] = 1
data_buffer[x][y][2] = 0
data_buffer[x][y][3] = 0
# set wdata to new buffer
wdata = data_buffer
# wait to paint process next frame
time.sleep(sleeptime/1000.0)
# end of iteration, restart
time.sleep(0.5)
# end of main animation loop
# close socket
UDPSock.close()
cell.disconnect()

View File

@ -8,7 +8,7 @@ from math import *
# Set the socket parameters
local_port = 5001
# direct html udp port
#remote_port = 4422
remote_port = 4422
# production port
remote_port = 4321
@ -40,11 +40,11 @@ segments = 8
segwidth = 12
segchannels = 3
sleeptime = 0.2
sleeptime = 0.1
t = 0
#timer will hold the elapsed time in seconds
frequency = 2*pi/200
frequency = 2*pi/20
while (1):
#zero out the data buffer

View File

@ -79,6 +79,9 @@ class syndelights_canvas:
def setwin_xy_srgba_array(self, x, y, srgba):
self.window_data[y][x] = dict(s=srgba[0], r=srgba[1], g=srgba[2], b=srgba[3], a=srgba[4])
def setsegwin_ws_srgba_array(self, w, s, srgba):
self.segmented_windows[w][s] = dict(s=srgba[0], r=srgba[1], g=srgba[2], b=srgba[3], a=srgba[4])
def setwin_xy_sval(self, x, y, col, val):
if col == 's':
self.window_data[y][x]['s'] = val

View File

@ -3,20 +3,49 @@ import time
width = 12
height = 8
seg_windows = 12
segments = 8
test = syndelights_canvas("s2l\n<8<18 ", 1, width, height, width, "127.0.0.1", 5001, "127.0.0.1", 4422, 3)
test = syndelights_canvas("s2l\n<8<18 ", 1, width, height, width, "127.0.0.1", 5001, "127.0.0.1", 4321, 3)
test = syndelights_canvas("s2l\n<8<18 ", 1, width, height, seg_windows, "127.0.0.1", 5001, "127.0.0.1", 4422, 3)
test = syndelights_canvas("s2l\n<8<18 ", 1, width, height, seg_windows, "127.0.0.1", 5001, "127.0.0.1", 4321, 3)
test.connect()
test.reset_windows(0, 0, 0, 0, 127)
test.reset_segment_windows(0, 62, 62, 62, 63)
test.reset_segment_windows(0, 255, 255, 255, 255)
x = 0
y = 0
w = 0
s = 0
c = []
for i in xrange(width * height):
# define colour
if i%3 == 0:
c = [0, 255, 0, 0, 255]
elif i%3 == 1:
c = [0, 0, 255, 0, 255]
elif i%3 == 2:
c = [0, 0, 0, 255, 255]
for y in xrange(height):
for x in xrange(width):
test.setwin_xy_srgba_array(x, y, [0, 255, 0, 0, 127])
test.draw()
time.sleep(0.5)
test.setwin_xy_srgba_array(x, y, [0, 0, 0, 0, 127])
# go to next floor
if x == width:
y = y + 1
x = 0
# go to next window
if s == segments:
w = w + 1
s = 0
test.setwin_xy_srgba_array(x, y, c)
test.setsegwin_ws_srgba_array(w, s, c)
test.draw()
time.sleep(0.5)
test.setwin_xy_srgba_array(x, y, [0, 0, 0, 0, 127])
test.setsegwin_ws_srgba_array(w, s, [0, 0, 0, 0, 127])
# increment windows, segments
x = x + 1
s = s + 1
test.disconnect()

View File

@ -86,7 +86,7 @@ var sendTime = function () {
server.on("message", function (msg, rinfo) {
if( verbose ) {
console.log("server got: " + msg + " from " +
console.log("server got (" + msg.length + ") bytes: " + msg + " from " +
rinfo.address + ":" + rinfo.port);
}