From 822e42056045ecf2d8583b60cec6f85239c8438b Mon Sep 17 00:00:00 2001 From: cyrille Date: Thu, 19 Sep 2013 22:29:57 +0200 Subject: [PATCH 1/3] Added a client, not quite working yet --- clients/cellular.py | 142 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 clients/cellular.py diff --git a/clients/cellular.py b/clients/cellular.py new file mode 100644 index 0000000..043c3cb --- /dev/null +++ b/clients/cellular.py @@ -0,0 +1,142 @@ +# Client program "cellular" + +from socket import * +import sys +import time +from math import * +from random import randint + +# 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 " + +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 = [] + +# setup window area, channels and segments +segwindows = 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 = 5000 # milliseconds +frequency = 1000 / sleeptime # frames per second + +# function resets map to zero +def zero_wdata(): + for x in xrange(width): + wdata.append([0] * height) + for y in xrange(height): + # create an RGBA array at each location + wdata[x][y] = [0, 0, 0, 1] + +# function resets segment display +def zero_sdata(): + for s in xrange(segwindows): + sdata.append([0] * segments) + for seg in xrange(segments): + sdata[s][seg] = [0, 0, 1, 1] + +# convert [r,g,b,a] to chr string +def i2c(rgba): + return chr(int(255 * rgba[0])) + chr(int(255 * rgba[1])) + chr(int(255 * rgba[2])) + chr(int(255 * rgba[3])) + +# function sends wdata and sdata state to server +def send_update(): + #zero out the data buffer + data = hash + data = data + z_buffer + + # write frame data + for y in xrange(height): + for x in xrange(width): + data = data + i2c( wdata[x][y] ) + data = data + "\n" + + # write segment window displays + for s in xrange(segwindows): + for seg in xrange(segments): + # I don't think I understand what the 3 channels are... + for sc in xrange(segchannels): + 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)) + +# clear map +zero_wdata() +zero_sdata() + +# set start seed +# get random location +rx = randint(0, width - 1) +ry = randint(0, height - 1) +wdata[rx][ry] = [1,0,0,0.5] + +send_update() + +exit() + +# build loop here +time.sleep(sleeptime/1000) + +# old code below ... +# keep to compare for now + +t = 0 +if True: + #zero out the data buffer + data = hash + data += z_buffer + for i in range(0,width): + for j in range(0,height): + pixel = 0.5+0.5*sin(2*pi*(float(i+1)/width)+t*frequency)*sin(2*pi*(float(j+1)/height)+t*frequency) + data = data + chr(int(255*pixel)) + alpha + data = data + "\n" + for i in range(0,segwindows): + for j in range(0,segments): + for a in range(0,segchannels): + val = chr( 127 + int(128*sin(2*pi*(1+i)*(1+j)*(1+a)*t*frequency/200))) + data += val + val + val + data += "\n" + t+=1 + + print data + UDPSock.sendto(data,(remote_host,remote_port)) + +# close socket +UDPSock.close() From 2c643102cf7d18a8e6f29daf57228ade8f2c3d3b Mon Sep 17 00:00:00 2001 From: cyrille Date: Sun, 22 Sep 2013 00:23:13 +0200 Subject: [PATCH 2/3] Update size, channels, web server, working example cellular.py --- displayclienthtml/visionneuse.html | 118 ++++++++++++++++++----------- displayclienthtml/ws_udp.js | 8 +- frameserver/defines.h | 10 +-- frameserver/frameserver.cpp | 2 +- 4 files changed, 86 insertions(+), 52 deletions(-) diff --git a/displayclienthtml/visionneuse.html b/displayclienthtml/visionneuse.html index f16330d..d18e9ca 100644 --- a/displayclienthtml/visionneuse.html +++ b/displayclienthtml/visionneuse.html @@ -5,54 +5,83 @@ Web socket test @@ -73,6 +102,7 @@ headersize=12; context.fill(); } function lightWindow(x,y,r,g,b) { + //console.log(r + ',' + g + ',' + b); context.beginPath(); context.rect(488+40.3*x, 265+87.4*y, 23, 34); context.fillStyle = 'rgb('+r+','+g+','+b+')'; @@ -152,7 +182,5 @@ var ascii2segments = new Array ( // lightWindow(i,j,w) // switch on new window with brightness w -
Received from server:
-
diff --git a/displayclienthtml/ws_udp.js b/displayclienthtml/ws_udp.js index 66ad269..eb5991b 100644 --- a/displayclienthtml/ws_udp.js +++ b/displayclienthtml/ws_udp.js @@ -10,6 +10,9 @@ // byte windows 12*4 RGBA values '\n' repeated 8 times for the 8 rows // byte segments 8*4 RGBA values '\n' repeated 12 times for the top row +// set to true for debugging and seeing messages sent to server +var verbose = false; + var count = 0; var clients = {}; @@ -82,8 +85,11 @@ var sendTime = function () { }; server.on("message", function (msg, rinfo) { - console.log("server got: " + msg + " from " + + if( verbose ) { + console.log("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port); + } + for(i in clients) { // Send a message to the client with the message clients[i].sendUTF(msg); diff --git a/frameserver/defines.h b/frameserver/defines.h index e7911dc..804d0b5 100644 --- a/frameserver/defines.h +++ b/frameserver/defines.h @@ -8,17 +8,17 @@ #define NUMBUFS 1000 #define REMOTE_IP "127.0.0.1" -#define REMOTE_PORT 1234 +#define REMOTE_PORT 4422 -#define HASH "abcdefghij" +#define HASH "s2l\n<8<18 " // one byte number + 10 character hash plus newline #define HEADEROFFSET 12 -// 12 windows per floor, 7 floors, Value:Alpha +// 12 windows per floor, 8 floors, Value:Alpha #define WIDTH 12 -#define HEIGHT 7 -#define CHANNELS 2 +#define HEIGHT 8 +#define CHANNELS 4 #define WINDOWOFFSET (WIDTH*CHANNELS+1)*HEIGHT diff --git a/frameserver/frameserver.cpp b/frameserver/frameserver.cpp index f8b059e..f037597 100644 --- a/frameserver/frameserver.cpp +++ b/frameserver/frameserver.cpp @@ -4,7 +4,7 @@ #include #include - + #include "defines.h" #include "Server.h" From 7a5a42ccdad4a03f7271a00f5f8e362b2433b310 Mon Sep 17 00:00:00 2001 From: cyrille Date: Sun, 22 Sep 2013 00:26:32 +0200 Subject: [PATCH 3/3] cleaned up debugging print stat. --- clients/cellular.py | 149 +++++++++++++++++++++++++++++++------------- 1 file changed, 106 insertions(+), 43 deletions(-) diff --git a/clients/cellular.py b/clients/cellular.py index 043c3cb..6a38018 100644 --- a/clients/cellular.py +++ b/clients/cellular.py @@ -1,10 +1,10 @@ # Client program "cellular" from socket import * -import sys -import time +import sys, time from math import * from random import randint +from copy import deepcopy # Set the socket parameters local_port = 5001 @@ -30,8 +30,9 @@ UDPSock.bind((outgoing_if, local_port)) #UDPSock.connect((remote_host, remote_port)) # this is our handshake, weak certification -hash = "s2l\n<8<18 " +hash = "s2l\n<8<18 " # 10 bytes +# plus 2 bytes below alpha = chr(255) z_buffer = chr(1) + "\n" @@ -50,27 +51,34 @@ segchannels = 4 # RGBA # timing elements # timer will hold the elapsed time in seconds timer = 0 -sleeptime = 5000 # milliseconds +sleeptime = 100 # milliseconds frequency = 1000 / sleeptime # frames per second # function resets map to zero -def zero_wdata(): +def set_wdata(r, g, b): for x in xrange(width): wdata.append([0] * height) for y in xrange(height): - # create an RGBA array at each location - wdata[x][y] = [0, 0, 0, 1] + # 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 zero_sdata(): - for s in xrange(segwindows): +def set_sdata(r, g, b): + global sdata + sdata = [] + + for s in xrange(segwindows): sdata.append([0] * segments) for seg in xrange(segments): - sdata[s][seg] = [0, 0, 1, 1] + # 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): - return chr(int(255 * rgba[0])) + chr(int(255 * rgba[1])) + chr(int(255 * rgba[2])) + chr(int(255 * rgba[3])) + # 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])) # function sends wdata and sdata state to server def send_update(): @@ -82,61 +90,116 @@ def send_update(): 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): - # I don't think I understand what the 3 channels are... - for sc in xrange(segchannels): data = data + i2c( sdata[s][seg] ) data = data + "\n" - print data + # print data # send the data packet to remote host UDPSock.sendto(data,(remote_host,remote_port)) -# clear map -zero_wdata() -zero_sdata() +# initialize map +set_wdata(0,0,0) +set_sdata(0,0,255) # set start seed # get random location rx = randint(0, width - 1) ry = randint(0, height - 1) -wdata[rx][ry] = [1,0,0,0.5] +wdata[rx][ry] = [1,1,0,0,1] +print "origin(" + str(rx) + "," + str(ry) + ")" -send_update() +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 + +# main logic loop +for t in range(60): + + # push update to clients + send_update() -exit() + # create CA buffer + data_buffer = deepcopy(wdata) -# build loop here -time.sleep(sleeptime/1000) + 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] += 10 -# old code below ... -# keep to compare for now + # assign stat to red colour + data_buffer[x][y][1] = data_buffer[x][y][0] -t = 0 -if True: - #zero out the data buffer - data = hash - data += z_buffer - for i in range(0,width): - for j in range(0,height): - pixel = 0.5+0.5*sin(2*pi*(float(i+1)/width)+t*frequency)*sin(2*pi*(float(j+1)/height)+t*frequency) - data = data + chr(int(255*pixel)) + alpha - data = data + "\n" - for i in range(0,segwindows): - for j in range(0,segments): - for a in range(0,segchannels): - val = chr( 127 + int(128*sin(2*pi*(1+i)*(1+j)*(1+a)*t*frequency/200))) - data += val + val + val - data += "\n" - t+=1 + # check we don't go too high + if data_buffer[x][y][0] > 127: + data_buffer[x][y][1] = 127 + # assign stat to green colour + data_buffer[x][y][2] = data_buffer[x][y][0] - 127 - print data - UDPSock.sendto(data,(remote_host,remote_port)) + if data_buffer[x][y][0] > 255: + data_buffer[x][y][2] = 127 + # assign stat to blue colour + data_buffer[x][y][3] = data_buffer[x][y][0] - 255 + + if data_buffer[x][y][0] > 383: + data_buffer[x][y][3] = 127 + + set_sdata(0,0,0) + # E + sdata[0][0] = [0,255,0,0,1] + sdata[0][3] = [0,255,0,0,1] + sdata[0][4] = [0,255,0,0,1] + sdata[0][5] = [0,255,0,0,1] + sdata[0][6] = [0,255,0,0,1] + # N + sdata[1][2] = [0,255,0,0,1] + sdata[1][4] = [0,255,0,0,1] + sdata[1][5] = [0,255,0,0,1] + sdata[1][6] = [0,255,0,0,1] + # D + sdata[2][1] = [0,255,0,0,1] + sdata[2][2] = [0,255,0,0,1] + sdata[2][3] = [0,255,0,0,1] + sdata[2][4] = [0,255,0,0,1] + sdata[2][6] = [0,255,0,0,1] + + send_update() + exit() + + + # 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) # close socket UDPSock.close()