Last active
May 26, 2023 17:21
-
-
Save quietvoid/9728f4ffe939b7ba859bb9897ddbabbd to your computer and use it in GitHub Desktop.
PGenerator client to send patches manually
This file contains hidden or 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
import argparse | |
import socket | |
import math | |
def send_cmd(cmd): | |
client_socket.send(cmd.encode() + PGEN_CMD_END_BYTES) | |
res_bytes = client_socket.recv(1024) | |
end_idx = res_bytes.find(PGEN_CMD_END_BYTES) | |
if end_idx > 0: | |
return res_bytes[:end_idx].decode() | |
def stop_client(send_quit: bool = False): | |
if send_quit: | |
send_cmd("QUIT") | |
client_socket.close() | |
PGEN_HOST = "192.168.1.213" | |
PGEN_PORT = 85 | |
PGEN_CMD_END_BYTES = b"\x02\x0D" | |
# Set True to verify if responding, socket probably fails though | |
SEND_ALIVE = False | |
DISPLAY_RES = (3840, 2160) | |
PATCH_SIZE_PCT = 0.1 | |
display_w, display_h = DISPLAY_RES | |
display_area = float(display_w * display_h) | |
patch_area = float(PATCH_SIZE_PCT * display_area) | |
size_scale = math.sqrt(patch_area / display_area) | |
patch_size = ( | |
int(round(size_scale * display_w)), | |
int(round(size_scale * display_h)) | |
) | |
# Center | |
position = ( | |
int((display_w / 2) - int((patch_size[0] / 2))), | |
int((display_h / 2) - int((patch_size[1] / 2))) | |
) | |
parser = argparse.ArgumentParser(description="PGen pattern client") | |
parser.add_argument("-d", "--depth", nargs="?", type=int, default=10) | |
parser.add_argument("--rgb", nargs="*", type=int, default=[128, 128, 128]) | |
parser.add_argument("--bg-rgb", nargs="*", type=int, default=[0, 0, 0]) | |
parser.add_argument("--blank", action='store_true') | |
args = parser.parse_args() | |
draw_shape = "RECTANGLE10bit" if args.depth == 10 else "RECTANGLE" | |
# 10 bit RGB, 8 bit background | |
rgb = args.rgb | |
bg_rgb = args.bg_rgb | |
if args.blank: | |
rgb = [0, 0, 0] | |
bg_rgb = [0, 0, 0] | |
# Connect | |
client_socket = socket.socket() | |
client_socket.connect((PGEN_HOST, PGEN_PORT)) | |
if SEND_ALIVE: | |
res = send_cmd("IS_ALIVE") | |
if res != "ALIVE": | |
print("PGen did not respond with ALIVE") | |
stop_client() | |
exit(1) | |
# Send configured patch | |
r, g, b = rgb | |
bg_r, bg_g, bg_b = bg_rgb | |
w, h = patch_size | |
x, y = position | |
rgb_rect_cmd = f"RGB={draw_shape};{w},{h};0;{r},{g},{b};{bg_r},{bg_g},{bg_b};0,0,{x},{y};-1" | |
res = send_cmd(rgb_rect_cmd) | |
if res != "OK": | |
print("Patch request did not return OK") | |
stop_client() | |
exit(1) | |
stop_client(send_quit=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment