kupa/textrender.py

179 lines
4.4 KiB
Python
Raw Normal View History

2023-09-11 14:10:06 +00:00
import socket
import time
from matelightfont import *
FLASZKI_UDP_IP = "10.8.0.196"
FLASZKI_UDP_PORT = 65506
HEADER_DRGB = 2
HEADER_TIME_INFINITE = 255
HEADER_TIME_WAIT = 0
DISPLAY_RES_X = 12
DISPLAY_RES_Y = 10
current_text_offset=0
def get_char_pixel_length(ch):
l = {
'k': letter_k[0],
'u': letter_u[0],
'p': letter_p[0],
'a': letter_a[0],
' ': letter_space[0]
}.get(ch, 0)
return l + 1
def get_char_pixel_bitmap(ch):
l = {
'k': letter_k[1],
'u': letter_u[1],
'p': letter_p[1],
'a': letter_a[1],
' ': letter_space[1]
}.get(ch, ["0000","0000","0000","0000","0000"])
return l
def get_string_pixel_length(st):
l=0
for c in st:
l += get_char_pixel_length(c)
return l
def print_letter(ch):
white=(255,255,255)
for x in range(0,ch[0]):
for y in range(0,5):
if ch[1][y][x]=='1':
2023-09-11 14:51:31 +00:00
pset(x,y,255,255,255)
2023-09-11 14:10:06 +00:00
def print_letter_debug(ch):
print("Debug letter print")
def render_thread_debug(name):
print("Debug render thread")
render_buffer = bytearray(0)
def prepare_render_buffer(r,g,b):
global render_buffer
render_buffer = bytearray([HEADER_DRGB,HEADER_TIME_INFINITE])
for y in range(DISPLAY_RES_Y):
for x in range(DISPLAY_RES_X):
render_buffer.append(r)
render_buffer.append(g)
render_buffer.append(b)
2023-09-11 14:34:49 +00:00
def clear_render_buffer(r,g,b):
2023-09-11 14:10:06 +00:00
global render_buffer
for y in range(DISPLAY_RES_Y):
for x in range(DISPLAY_RES_X):
2023-09-11 14:34:49 +00:00
x3 = x*3
res3 = DISPLAY_RES_X*3
2023-09-11 15:39:29 +00:00
render_buffer[2+x3+y*res3]=r
render_buffer[3+x3+y*res3]=g
render_buffer[4+x3+y*res3]=b
2023-09-11 14:10:06 +00:00
2023-09-11 14:51:31 +00:00
def pset(x,y,r,g,b):
global render_buffer
x3 = x*3
res3 = DISPLAY_RES_X*3
render_buffer[2+x3+y*res3]=r
render_buffer[3+x3+y*res3]=g
render_buffer[4+x3+y*res3]=b
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def flush_render():
global sock
sock.sendto(render_buffer, (FLASZKI_UDP_IP, FLASZKI_UDP_PORT))
def close_sock():
global sock
sock.sendto(bytearray([HEADER_DRGB, HEADER_TIME_WAIT]), (FLASZKI_UDP_IP, FLASZKI_UDP_PORT))
2023-09-11 14:10:06 +00:00
def render_stub():
2023-09-11 15:39:29 +00:00
render_thread("dupa")
2023-09-11 14:10:06 +00:00
global render_buffer
prepare_render_buffer(255,0,0)
2023-09-11 15:39:29 +00:00
#clear_render_buffer(0,0,255)
2023-09-11 14:51:31 +00:00
print_letter(letter_k)
flush_render()
2023-09-11 14:10:06 +00:00
time.sleep(3)
2023-09-11 14:51:31 +00:00
close_sock()
2023-09-11 14:10:06 +00:00
def get_text_vline(st, i):
done=False
letter=0
offset=0
previous_offset = 0
if i >= get_string_pixel_length(st):
return['0','0','0','0','0']
while not done:
offset +=get_char_pixel_length(st[letter])
if i < offset-1:
done=True
vline = [get_char_pixel_bitmap(st[letter])[0][i-previous_offset],get_char_pixel_bitmap(st[letter])[1][i-previous_offset],get_char_pixel_bitmap(st[letter])[2][i-previous_offset],get_char_pixel_bitmap(st[letter])[3][i-previous_offset], get_char_pixel_bitmap(st[letter])[4][i-previous_offset]]
return vline
elif i == offset-1:
vline = ['0','0','0','0','0']
done=True
else:
letter += 1
previous_offset = offset
return vline
def prepare_text_block(st):
l=get_string_pixel_length(st)
#l+=8 # add full clear screen for text rotation
block=[]
i=0
for vline in range(0,l):
block.append(get_text_vline(st, i))
i+=1
return block
def print_text(block):
2023-09-11 15:39:29 +00:00
print(block)
for x in range(current_text_offset, current_text_offset+16):
for y in range(0, 5):
print(x,y)
2023-09-11 14:10:06 +00:00
if block[x][y] == '1':
2023-09-11 15:39:29 +00:00
pset(x-current_text_offset, y, 255,255,255)
2023-09-11 14:10:06 +00:00
def render_thread(name):
2023-09-11 15:39:29 +00:00
global render_buffer
2023-09-11 14:10:06 +00:00
global current_text_offset
2023-09-11 15:39:29 +00:00
prepare_render_buffer(255,0,0)
flush_render()
2023-09-11 14:10:06 +00:00
text=" kupa "
print("we start sraning")
block = prepare_text_block(text)
2023-09-11 15:39:29 +00:00
isKupa = True
2023-09-11 14:10:06 +00:00
while isKupa:
2023-09-11 15:39:29 +00:00
clear_render_buffer(255,0,0)
print_text(block)
#print_letter(letter_k)
flush_render()
2023-09-11 14:10:06 +00:00
time.sleep(0.125)
current_text_offset+=1
2023-09-11 15:39:29 +00:00
if (current_text_offset >= get_string_pixel_length(text)-16):
2023-09-11 14:10:06 +00:00
current_text_offset = 0