Net

class net.DisplayServer(display)[source]

TCP-Server that listens on a specified port for TCP-Connections. Every request sent to the server has to be one of the special commands described later on or a string of 0s and 1s each specifying a dot to be turned on or off respectively. For instance, to display the letter ‘T’ on a 4x3 display of this form

1111
0110
0110

the following request must be sent to the server: 111101100110.

A simple command line client like nc can send this request to ‘server’ listening on port 10101:


$ echo 111101100110 | nc server 10101

If the request contains the string ‘SIZE’ (ignoring case), the server will respond with the dimensions of the display (width x height).

Lets start a server. Here we use a thread in order to be able to run the client afterwards. In practice the server will run on a different platform and can be started directly.

>>> import net
>>> import displayprovider
>>> import threading
>>> ds = net.DisplayServer(displayprovider.DisplayBase())
>>> th = threading.Thread(target=ds.start)
>>> th.setDaemon(True)
>>> th.start()
Starting display server for dimension 4 x 3 on 0.0.0.0 at port 10101

Now we can start a client and send some pixels to the server.

>>> cl = net.RemoteDisplay(host="0.0.0.0")
Remote display will send data to 0.0.0.0 on port 10101
>>> cl.px(1, 1, True)
>>> cl.px(2, 3, True)
>>> cl.show()
Listening on 0.0.0.0 at port 10101
....
....
....

The output lines after show() are coming from the server: once it has processed the request it prints its 4x3 display (unchanged here, since the client and server displays differ in size). Because show() waits for the server’s acknowledgement before returning, this output is guaranteed to have been printed already.

handle_request(payload)[source]
start(host='0.0.0.0', port=10101)[source]
class net.PixelflutServer(display)[source]

PixeflutServer that conforms to the Pixelflut protocol outlined at https://c3pixelflut.de/how.html

PX <x> <y> <rrggbb|aarrggbb> # set pixel at x,y with color rrggbb PX <x> <y> # get pixel color info for pixel at x,y SIZE # get size of the display OFFSET <x> <y> # set offset for following commands

get_pixel(x, y)[source]

Get the color of a specific pixel on the display. Will return the color of the pixel at the specified coordinates as a hex string.

handle_request(payload)[source]

Handle incoming requests and execute commands based on the payload. The payload is expected to be a string of ASCII-encoded commands separated by newlines. Each command is processed and appropriate actions are taken. Commands: - PX x y color: Set the pixel at (x, y) to the specified color. - PX x y: Get the color of the pixel at (x, y). - SIZE: Get the dimensions of the grid. - OFFSET x y: Set the offset for the grid coordinates. Args:

payload (bytes): The ASCII-encoded command string.

Returns:

str: The responses to the commands, joined by newlines.

set_pixel(x, y, color)[source]

Set the color of a specific pixel on the display. Parameters: x (int): The x-coordinate of the pixel. y (int): The y-coordinate of the pixel. color (str): The color to set the pixel to, represented as a hex string.

If the color is “000000”, the pixel will be turned off. Otherwise it will be turned on.

start(host='0.0.0.0', port=10101)[source]
class net.RemoteDisplay(host='0.0.0.0', port=10101, width=28, height=13)[source]

Remote class to connect with a running DisplayServer instance.

led(on_off)[source]

Turn LED of the display on (True) or off (False)

px(x, y, val)[source]

Set pixel at (x,y) to val (True/False)

show()[source]

Draw to console for debugging purposes.