personal_site/assets/blog/micropython/example_micropython.py
Tom b1c92c3152 Squashed commit of the following:
commit 36cc58f5c418dbd55aecac0fe2b92977951e4401
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Mon Jul 29 12:04:44 2024 +0100

    revert

commit 74d40e498ac65c1789ceb5b07c9bad92e5356773
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Mon Jul 29 11:51:25 2024 +0100

    Update jekyll.yml

commit b9a1c11dbfdeeb4d26fdf54a70b47a7d16e71e3c
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Mon Jul 29 11:42:48 2024 +0100

    Update jekyll.yml

commit 94619844e19ceb61fa68c7d75a2508ff19f0bd08
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Mon Jul 29 11:33:15 2024 +0100

    Update _config.yml

commit e2a12fcf8e8e95b28be0f0c75c61745ee8afa7ac
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Mon Jul 29 11:18:01 2024 +0100

    change concurrency group

commit 3575764efaa6b8edb02d71ec6c1c84b9eefbb21d
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Mon Jul 29 11:12:27 2024 +0100

    Update Gemfile

commit 5f044a048a8c1b5f96e99e75eb91435ad3921e2a
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Mon Jul 29 11:09:57 2024 +0100

    setup a branch build

commit 9721978cb20bedd8a39abb89f436386585eab25f
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Mon Jul 29 10:49:20 2024 +0100

    add micropython draft
2024-07-29 12:04:57 +01:00

109 lines
2.1 KiB
Python

import gc
import struct
import time
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)
# The 'await' is necessary here to yield back to the JS event loop
# I tried to figure out how to hide this inside the JS implementation of sleep but
# couldn't make it work.
await time.sleep(0.2)