mirror of
https://github.com/TomHodson/tomhodson.github.com.git
synced 2025-06-26 10:01:18 +02:00
112 lines
2.5 KiB
Python
112 lines
2.5 KiB
Python
import asyncio
|
|
import gc
|
|
import struct
|
|
from array import array
|
|
|
|
import console
|
|
import display
|
|
import fonts
|
|
import framebuf
|
|
|
|
WHITE = 255
|
|
|
|
|
|
def palette_from_colors(*args):
|
|
pbuf = array("H", range(len(args)))
|
|
struct.pack_into(">HH", pbuf, 0, *args)
|
|
p = framebuf.FrameBuffer(
|
|
pbuf, len(args), 1, framebuf.RGB565
|
|
)
|
|
return p
|
|
|
|
|
|
def str_width(string, font, xpad=0):
|
|
return sum(font.get_ch(c)[2] + xpad for c in string)
|
|
|
|
|
|
def print_buf(
|
|
framebuffer,
|
|
string,
|
|
x,
|
|
y,
|
|
color,
|
|
font,
|
|
ha="left",
|
|
va="top",
|
|
bg=WHITE,
|
|
xpad=0,
|
|
):
|
|
total_width = str_width(string, font, xpad)
|
|
if ha == "center":
|
|
x -= total_width // 2
|
|
elif ha == "right":
|
|
x -= total_width
|
|
if va == "center":
|
|
y -= font.height() // 2
|
|
elif va == "bottom":
|
|
y -= font.height()
|
|
p = palette_from_colors(bg, color)
|
|
|
|
for c in string:
|
|
b, height, width = font.get_ch(c)
|
|
c_fbuf = framebuf.FrameBuffer(
|
|
array("B", b), width, height, framebuf.MONO_HLSB
|
|
)
|
|
framebuffer.blit(c_fbuf, x, y, bg, p)
|
|
x += width + xpad
|
|
return x, y
|
|
|
|
|
|
print(
|
|
"This is μPython saying hello from the on page console!"
|
|
)
|
|
console.log(
|
|
"This is μPython saying hello from the JS console!"
|
|
)
|
|
|
|
print(
|
|
f"Stack: alloc'd: {gc.mem_alloc()} free {gc.mem_free()} total {gc.mem_alloc() + gc.mem_free()}"
|
|
)
|
|
|
|
w, h = 240, 240
|
|
buf = bytearray(w * h)
|
|
fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS8)
|
|
|
|
i = 0
|
|
while True:
|
|
i += 1
|
|
fbuf.rect(0, 0, 240, 240, 255, True)
|
|
fbuf.pixel(120, 120, 128)
|
|
x, y = print_buf(
|
|
fbuf,
|
|
str(i % 150),
|
|
140,
|
|
145,
|
|
255,
|
|
font=fonts.gunship50,
|
|
ha="right",
|
|
va="bottom",
|
|
bg=0,
|
|
)
|
|
|
|
x, y = print_buf(
|
|
fbuf,
|
|
"mW",
|
|
x,
|
|
145,
|
|
255,
|
|
font=fonts.gunship30,
|
|
ha="left",
|
|
va="bottom",
|
|
bg=0,
|
|
)
|
|
|
|
display.draw(buf)
|
|
|
|
# Note: Because of the way the webassembly port works, this code is actually running like an asyncio thread
|
|
# This call to asyncio.sleep yeilds back to the JS event loop and gives the browser a chance to update the display.
|
|
# This is not needed on a real device.
|
|
# There is way to make it so that a bare time.sleep() will work but it requires emcripten's ASYNCIFY feature
|
|
# Which apparently kills performance. See https://github.com/tomhodson/micropython/commit/2fa6373d226b65f977486ecda32b8786cd1dceed
|
|
await asyncio.sleep(0.2)
|