152 lines
7.3 KiB
Python
152 lines
7.3 KiB
Python
"""Build the demo project (demo/demo.HMI) entirely from code -- no Nextion files involved.
|
|||
|
|
|
||
|
|
python examples/make_demo.py # writes demo/demo.HMI, demo/decoded/, demo/render/
|
||
|
|
|
||
|
|
Needs Pillow (pictures, the bundled CC0 "Aileron" font, rendering).
|
||
|
|
"""
|
||
|
|
import io
|
||
|
|
import os
|
||
|
|
import shutil
|
||
|
|
import sys
|
||
|
|
|
||
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
||
|
|
ROOT = os.path.dirname(HERE)
|
||
|
|
sys.path.insert(0, os.path.join(ROOT, "src"))
|
||
|
|
|
||
|
|
from PIL import Image, ImageDraw # noqa: E402
|
||
|
|
|
||
|
|
from nxhmi import components as C, fonts, project # noqa: E402
|
||
|
|
from nxhmi.render import Renderer # noqa: E402
|
||
|
|
|
||
|
|
W, H = 480, 320
|
||
|
|
|
||
|
|
|
||
|
|
def png(img):
|
||
|
|
b = io.BytesIO()
|
||
|
|
img.save(b, "PNG")
|
||
|
|
return b.getvalue()
|
||
|
|
|
||
|
|
|
||
|
|
def background():
|
||
|
|
img = Image.new("RGB", (W, H))
|
||
|
|
d = ImageDraw.Draw(img)
|
||
|
|
for y in range(H):
|
||
|
|
t = y / (H - 1)
|
||
|
|
d.line([(0, y), (W - 1, y)], fill=(int(20 + 40 * t), int(40 + 80 * t), int(90 + 120 * t)))
|
||
|
|
for i in range(0, W, 40):
|
||
|
|
d.line([(i, 0), (i + 120, H)], fill=(60, 110, 170))
|
||
|
|
return png(img)
|
||
|
|
|
||
|
|
|
||
|
|
def icon(size=64):
|
||
|
|
img = Image.new("RGB", (size, size), (255, 255, 255))
|
||
|
|
d = ImageDraw.Draw(img)
|
||
|
|
d.ellipse([4, 4, size - 5, size - 5], fill=(255, 170, 0), outline=(120, 60, 0), width=3)
|
||
|
|
d.rectangle([size // 2 - 4, 12, size // 2 + 3, size - 13], fill=(255, 255, 255))
|
||
|
|
return png(img)
|
||
|
|
|
||
|
|
|
||
|
|
def skin(w, h, c1, c2):
|
||
|
|
img = Image.new("RGB", (w, h))
|
||
|
|
d = ImageDraw.Draw(img)
|
||
|
|
for x in range(w):
|
||
|
|
t = x / max(1, w - 1)
|
||
|
|
d.line([(x, 0), (x, h - 1)], fill=tuple(int(a + (b - a) * t) for a, b in zip(c1, c2)))
|
||
|
|
d.rectangle([0, 0, w - 1, h - 1], outline=(255, 255, 255))
|
||
|
|
return png(img)
|
||
|
|
|
||
|
|
|
||
|
|
def build():
|
||
|
|
h = project.new_project("NX4832T035_011", "home")
|
||
|
|
small = project.add_font(h, fonts.from_ttf(None, 16)) # font 0
|
||
|
|
big = project.add_font(h, fonts.from_ttf(None, 32)) # font 1
|
||
|
|
bg = project.add_picture(h, background())
|
||
|
|
ico = project.add_picture(h, icon())
|
||
|
|
sk1 = project.add_picture(h, skin(140, 40, (200, 40, 40), (60, 40, 200)))
|
||
|
|
sk2 = project.add_picture(h, skin(140, 40, (40, 160, 60), (220, 200, 40)))
|
||
|
|
bar = project.add_picture(h, skin(200, 24, (40, 40, 40), (110, 110, 110)))
|
||
|
|
barfg = project.add_picture(h, skin(200, 24, (255, 140, 0), (255, 240, 0)))
|
||
|
|
|
||
|
|
# ---- page 0: home
|
||
|
|
from nxhmi.page import Page
|
||
|
|
home = Page.parse(h.read("0.pa"))
|
||
|
|
home.components[0].set("sta", 2)
|
||
|
|
home.components[0].set("pic", bg)
|
||
|
|
home.components[0].set_event("codesloadend", ["tm0.en=1"])
|
||
|
|
C.add(home, "text", "title", x=10, y=6, w=460, h=40, txt="nxhmi demo", font=big, sta=0, picc=bg,
|
||
|
|
pco=0xFFFF, xcen=0)
|
||
|
|
C.add(home, "button", "bControls", x=330, y=60, w=140, h=40, txt="controls", font=small,
|
||
|
|
events={"codesup": ["page controls"]})
|
||
|
|
C.add(home, "button", "bGallery", x=330, y=110, w=140, h=40, txt="gallery", font=small,
|
||
|
|
events={"codesup": ["page gallery"]})
|
||
|
|
C.add(home, "number", "nCount", x=10, y=60, w=150, h=40, val=1234, font=big, sta=1, bco=0xFFFF)
|
||
|
|
C.add(home, "xfloat", "xTemp", x=170, y=60, w=150, h=40, val=2155, vvs0=0, vvs1=2, font=big, sta=1, bco=0xFFFF)
|
||
|
|
C.add(home, "progress_bar", "jLoad", x=10, y=110, w=310, h=24, val=65)
|
||
|
|
C.add(home, "slider", "hLevel", x=10, y=145, w=310, h=30, val=40, minval=0, maxval=100,
|
||
|
|
events={"codesslide": ["jLoad.val=hLevel.val"]})
|
||
|
|
C.add(home, "checkbox", "cOn", x=10, y=190, w=30, h=30, val=1)
|
||
|
|
C.add(home, "text", "tOn", x=45, y=190, w=100, h=30, txt="enabled", font=small, sta=0, picc=bg, pco=0xFFFF, xcen=0)
|
||
|
|
C.add(home, "radio", "rA", x=150, y=190, w=30, h=30, val=1)
|
||
|
|
C.add(home, "radio", "rB", x=190, y=190, w=30, h=30, val=0)
|
||
|
|
C.add(home, "gauge", "zSpeed", x=10, y=230, w=80, h=80, val=135, wid=3, sta=1, bco=0xFFFF)
|
||
|
|
C.add(home, "picture", "pIcon", x=100, y=240, w=64, h=64, pic=ico)
|
||
|
|
C.add(home, "text", "tStatus", x=180, y=270, w=290, h=40, txt="status: ok", font=small, sta=1,
|
||
|
|
bco=0x0000, pco=0x07E0, xcen=0, style=1, borderw=2, borderc=0x07E0)
|
||
|
|
C.add(home, "timer", "tm0", tim=1000, en=0, events={"codestimer": ["nCount.val++"]})
|
||
|
|
C.add(home, "variable", "vaMode", val=1)
|
||
|
|
h.write("0.pa", home.pack())
|
||
|
|
|
||
|
|
# ---- page 1: controls
|
||
|
|
ctl = project.add_page(h, "controls", sta=1, bco=0xEF7D)
|
||
|
|
C.add(ctl, "button", "bBack", x=10, y=6, w=80, h=34, txt="< back", font=small, events={"codesup": ["page home"]})
|
||
|
|
C.add(ctl, "text", "tHead", x=100, y=6, w=370, h=34, txt="controls", font=big, sta=1, bco=0xEF7D, xcen=2)
|
||
|
|
for i, (xc, yc, st, label) in enumerate([(0, 0, 0, "left/top flat"), (1, 1, 1, "center border"),
|
||
|
|
(2, 2, 2, "right 3D down"), (1, 1, 3, "3D up")]):
|
||
|
|
C.add(ctl, "text", "tAlign%d" % i, x=10 + (i % 2) * 235, y=50 + (i // 2) * 55, w=225, h=48,
|
||
|
|
txt=label, font=small, xcen=xc, ycen=yc, style=st, borderw=2, borderc=0x001F, sta=1, bco=0xFFFF)
|
||
|
|
C.add(ctl, "text", "tWrap", x=10, y=162, w=150, h=60, txt="word wrap inside a narrow box", font=small,
|
||
|
|
isbr=1, sta=1, bco=0xFFFF, style=1, borderw=1, borderc=0)
|
||
|
|
C.add(ctl, "dual_state_button", "btOff", x=170, y=162, w=140, h=28, txt="off", font=small, val=0)
|
||
|
|
C.add(ctl, "dual_state_button", "btOn", x=170, y=194, w=140, h=28, txt="on", font=small, val=1)
|
||
|
|
C.add(ctl, "scrolling_text", "gNews", x=320, y=162, w=150, h=28, txt="scrolling text", font=small, sta=1, bco=0xFFFF)
|
||
|
|
C.add(ctl, "number", "nHex", x=320, y=194, w=150, h=28, val=48879, format=2, font=small, sta=1, bco=0xFFFF)
|
||
|
|
C.add(ctl, "waveform", "sWave", x=10, y=230, w=300, h=80, ch=1)
|
||
|
|
C.add(ctl, "progress_bar", "jVert", x=320, y=230, w=30, h=80, val=70, dez=1)
|
||
|
|
C.add(ctl, "slider", "hVert", x=360, y=230, w=30, h=80, val=30, mode=1)
|
||
|
|
C.add(ctl, "hotspot", "mHot", x=400, y=230, w=70, h=80, events={"codesup": ["page home"]})
|
||
|
|
h.write("1.pa", ctl.pack())
|
||
|
|
|
||
|
|
# ---- page 2: gallery (pictures as backgrounds and skins)
|
||
|
|
gal = project.add_page(h, "gallery", sta=1, bco=0xFFFF)
|
||
|
|
C.add(gal, "button", "bBack", x=10, y=6, w=80, h=34, txt="< back", font=small, events={"codesup": ["page home"]})
|
||
|
|
C.add(gal, "crop", "qBg", x=100, y=6, w=370, h=34, picc=bg)
|
||
|
|
C.add(gal, "picture", "pIcon", x=10, y=50, w=64, h=64, pic=ico)
|
||
|
|
C.add(gal, "button", "bSkin", x=90, y=50, w=140, h=40, txt="skinned", font=small, sta=2, pic=sk1, pic2=sk2, pco=0xFFFF)
|
||
|
|
C.add(gal, "dual_state_button", "btSkin", x=240, y=50, w=140, h=40, txt="dual on", font=small, sta=2,
|
||
|
|
pic=sk1, pic2=sk2, pco=0xFFFF, val=1)
|
||
|
|
C.add(gal, "progress_bar", "jSkin", x=90, y=100, w=200, h=24, sta=1, bpic=bar, ppic=barfg, val=55)
|
||
|
|
C.add(gal, "text", "tCrop", x=10, y=140, w=460, h=60, txt="text on a cropped background", font=big,
|
||
|
|
sta=0, picc=bg, pco=0xFFFF)
|
||
|
|
C.add(gal, "text", "tImg", x=10, y=210, w=200, h=40, txt="image bg", font=small, sta=2, pic=sk2, pco=0)
|
||
|
|
h.write("2.pa", gal.pack())
|
||
|
|
return h
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
out = os.path.join(ROOT, "demo")
|
||
|
|
os.makedirs(out, exist_ok=True)
|
||
|
|
h = build()
|
||
|
|
path = os.path.join(out, "demo.HMI")
|
||
|
|
h.save(path)
|
||
|
|
dec = os.path.join(out, "decoded")
|
||
|
|
shutil.rmtree(dec, ignore_errors=True)
|
||
|
|
project.decode(path, dec)
|
||
|
|
r = Renderer(h)
|
||
|
|
os.makedirs(os.path.join(out, "render"), exist_ok=True)
|
||
|
|
for name in ("home", "controls", "gallery"):
|
||
|
|
r.render(name).save(os.path.join(out, "render", name + ".png"))
|
||
|
|
print("wrote", path, "+ decoded/ + render/")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|