Files
hexapod-robot/2026-current/scripts/make_remote_hmi.py
T
mangavalkandClaude Opus 5.5 6b3b2629cd 2026 remote: display turned 180 degrees (it sits upside down in the remote)
make_remote_hmi.py now builds the project with Editor display direction 270
(landscape, flipped) before adding pictures and fonts, so they're stored for
that direction. Checked in Nextion Editor and on the real display.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 22:48:41 +01:00

284 lines
14 KiB
Python

"""Builds remote/remote.HMI, the remote's Nextion touchscreen project, from code.
The layout below is the single source of truth for the HMI: change it here and
re-run, rather than editing remote.HMI in Nextion Editor (an Editor edit is
lost on the next run). Component names and event code must match the
firmware - remote/NextionUI.cpp and remote/NEXTION.md.
python scripts\\make_remote_hmi.py (from 2026-current, PowerShell 5.1 fine)
Needs:
- the nxhmi package: the tools/nextion-hmi-tools submodule (git submodule update --init)
- Pillow, and the DejaVu Sans Mono fonts (free licence). They ship with
matplotlib; or pass --font-dir with DejaVuSansMono.ttf + DejaVuSansMono-Bold.ttf.
- the old remote HMI (../2019-teensy-nrf24/remote-nextion/remote.HMI), read
only: its splash picture is reused.
Then open remote.HMI in Nextion Editor, compile and upload (SETUP > UPDATE
DISPLAY puts the remote in USB passthrough for that).
"""
import argparse
import os
import sys
import tempfile
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE) # 2026-current
sys.path.insert(0, os.path.join(ROOT, "tools", "nextion-hmi-tools", "src"))
from nxhmi import mcp_server as nx # noqa: E402 (the same functions the nxhmi MCP server exposes)
OUT = os.path.join(ROOT, "remote", "remote.HMI")
OLD_HMI = os.path.join(ROOT, "..", "2019-teensy-nrf24", "remote-nextion", "remote.HMI")
W, H = 480, 320
BAR_H = 32 # top bar
TAB_Y, TAB_H = 272, 48
BIG, SMALL = 0, 1 # font ids: 24 px bold, 16 px
# Colours. The firmware repaints the stateful ones (remote/NextionUI.cpp):
# gray #c6c3c6 = off, green #00fc00 = on, red #f80000, orange #f88000.
PAGE_BG = "#182030"
BAR_BG = "#000000"
TEXT = "#ffffff"
LABEL = "#a0b0c8"
BTN = "#c6c3c6"
RED = "#f80000"
TRACK = "#404850"
TAB_ON = "#3060c0"
# Page order = page id; must match UiPage in remote/UiTypes.h.
PAGES = ["splash", "drive", "status", "servos", "cal", "setup", "upload"]
TABS = [("drive", "DRIVE"), ("status", "STATUS"), ("servos", "SERVOS"), ("cal", "CAL"), ("setup", "SETUP")]
LEG_NAMES = ["FR", "MR", "RR", "RL", "ML", "FL"] # robot leg order (robot/Config.h)
LEG_TITLES = ["FRONT RIGHT", "MID RIGHT", "REAR RIGHT", "REAR LEFT", "MID LEFT", "FRONT LEFT"]
def frame(cmd, value=None):
"""printh line for a custom '#' frame (remote/NextionUI.h)."""
body = [ord(cmd)] + ([value] if value is not None else [])
return "printh 23 %02X %s" % (len(body), " ".join("%02X" % b for b in body))
class Builder:
def __init__(self, path):
self.path = path
def add(self, page, kind, name, events=None, **att):
nx.t_add_component(self.path, page, kind, name, att, events or {})
def text(self, page, name, x, y, w, h, txt, font=SMALL, pco=TEXT, bco=PAGE_BG, xcen="left", ycen="center",
maxl=None, **kw):
self.add(page, "text", name, x=x, y=y, w=w, h=h, txt=txt, font=font, pco=pco, bco=bco,
xcen=xcen, ycen=ycen, txt_maxl=maxl or max(len(txt), 10), **kw)
def button(self, page, name, x, y, w, h, txt, up=None, font=BIG, bco=BTN, pco="#000000", maxl=None, **kw):
events = {"codesup": up} if up else {}
self.add(page, "button", name, events, x=x, y=y, w=w, h=h, txt=txt, font=font, bco=bco, bco2=bco,
pco=pco, pco2=pco, txt_maxl=maxl or max(len(txt), 10), **kw)
def slider(self, page, name, x, y, w, h, maxval, val, events=None, cursor=None):
self.add(page, "slider", name, events, x=x, y=y, w=w, h=h, bco=TRACK, bco1=TRACK, pco=TEXT,
wid=cursor or h, hig=h, maxval=maxval, minval=0, val=val)
def bar(self, page, name, x, y, w, h, val=0):
self.add(page, "progress_bar", name, x=x, y=y, w=w, h=h, bco=TRACK, pco="#00c000", val=val)
# ---- shared frame: top bar + tabs on every working page ----
def chrome(self, page):
self.text(page, "tLink", 0, 0, 112, BAR_H, "NO LINK", font=SMALL, pco=RED, bco=BAR_BG, xcen="center", maxl=12)
self.text(page, "tState", 112, 0, 256, BAR_H, "ROBOT: NO DATA", font=SMALL, pco=TEXT, bco=BAR_BG, maxl=30)
self.button(page, "bKill", 368, 0, 112, BAR_H, "DISARM", [frame("K")], font=BIG, bco=RED, pco=TEXT)
tw = W // len(TABS)
for i, (target, label) in enumerate(TABS):
on = target == page
self.button(page, "bTab%d" % i, i * tw, TAB_Y, tw, TAB_H, label, ["page %s" % target], font=SMALL,
bco=TAB_ON if on else "#283040", pco=TEXT, borderw=1)
def page(self, name, preinit=()):
"""Every page reports itself to the remote when it opens ('P' + page id)."""
if name != PAGES[0]:
nx.t_add_page(self.path, name, {"bco": PAGE_BG, "sta": "solid color"})
else:
nx.t_set_attributes(self.path, name, name, {"bco": PAGE_BG})
nx.t_set_event(self.path, name, name, "codesload", [frame("P", PAGES.index(name))] + list(preinit))
def splash_png(tmp):
"""The old remote's splash picture (a jumping spider), rendered from the old HMI."""
from nxhmi.render import Renderer
from nxhmi.container import HMIFile
img = Renderer(HMIFile.load(OLD_HMI)).render("splashsceen")
path = os.path.join(tmp, "splash.png")
img.save(path)
return path
def font_dir(arg):
if arg:
return arg
import matplotlib
return os.path.join(os.path.dirname(matplotlib.__file__), "mpl-data", "fonts", "ttf")
MODEL = "NX4832T035_011"
def new_project_flipped(path, program):
"""Like nxhmi's project.new_project, but landscape turned 180 degrees (Editor: Setting >
Display direction 270): the display sits upside down in the remote (owner, 2026-11-17).
Must come first: pictures and fonts added afterwards are stored for this direction."""
from nxhmi import models, project
from nxhmi.components import new_page
direction = models.direction_byte(MODEL, landscape=True, flipped=True)
w, h = models.page_size(MODEL, direction)
items = [("main.HMI", project.make_main(MODEL, [("pa", "0.pa")], "iso-8859-1", direction)),
("Program.s", program.encode("latin-1")),
("0.pa", new_page(PAGES[0], w=w, h=h).pack())]
project.build(items).save(path)
def build(path, fonts):
b = Builder(path)
program = ("//Runs once at power-on, before the first page.\r\n"
"bauds=115200 //the remote's NEXTION_BAUD; stored in the display\r\n"
"page 0\r\n")
new_project_flipped(path, program)
nx.t_add_font(path, 24, os.path.join(fonts, "DejaVuSansMono-Bold.ttf"), "iso-8859-1") # font 0
nx.t_add_font(path, 16, os.path.join(fonts, "DejaVuSansMono.ttf"), "iso-8859-1") # font 1
with tempfile.TemporaryDirectory() as tmp:
pic = nx.t_add_picture(path, splash_png(tmp))["picture_id"]
# ---------------------------------------------------------------- 0 splash
p = "splash"
b.page(p)
nx.t_set_attributes(path, p, p, {"sta": "image", "pic": pic})
b.text(p, "t0", 0, 0, 480, 40, "HEXAPOD REMOTE", font=BIG, pco=TEXT, bco="#000000", xcen="center", sta="crop image", picc=pic)
b.add(p, "timer", "tm0", {"codestimer": ["page drive"]}, tim=1500, en="enable")
for name in PAGES[1:]:
b.page(name, preinit=PREINIT.get(name, ()))
for name, _ in TABS:
b.chrome(name)
# ---------------------------------------------------------------- 1 drive
p = "drive"
y = 40
for i, (n, t, c) in enumerate([("bArm", "DISARMED", "A"), ("bStand", "SIT", "S"),
("bGait", "TRIPOD", "G"), ("bMode", "DIRECT", "M")]):
b.button(p, n, 4 + i * 119, y, 115, 48, t, [frame(c)], maxl=10)
y = 94
for i, (n, t, c) in enumerate([("bRs", "R: ROTATE", "X"), ("bRGyro", "REMOTE GYRO", "R"), ("bBGyro", "ROBOT GYRO", "B")]):
b.button(p, n, 4 + i * 159, y, 154, 44, t, [frame(c)], font=SMALL, maxl=12)
b.text(p, "tSpL", 4, 146, 80, 36, "SPEED", font=SMALL, pco=LABEL)
send_speed = ["printh 23 02 56", "prints hSpeed.val,1"] # 'V' + value byte
b.slider(p, "hSpeed", 86, 150, 300, 28, 255, 128, {"codesslide": send_speed, "codesup": send_speed}, cursor=20)
b.text(p, "tSpeed", 394, 146, 82, 36, "50%", font=BIG, xcen="right", maxl=6)
b.text(p, "tHtL", 4, 190, 80, 36, "HEIGHT", font=SMALL, pco=LABEL)
send_height = ["printh 23 02 48", "prints hHeight.val,1"] # 'H' + value byte
b.slider(p, "hHeight", 86, 194, 300, 28, 200, 100, {"codesslide": send_height, "codesup": send_height}, cursor=20)
b.text(p, "tHeight", 394, 190, 82, 36, "+0%", font=BIG, xcen="right", maxl=6)
b.text(p, "tMsg", 4, 234, 472, 34, "", font=SMALL, pco="#ffd040", maxl=40)
# ---------------------------------------------------------------- 2 status
p = "status"
for side, x, prefix in (("REMOTE (deg)", 4, "R"), ("ROBOT (deg)", 248, "B")):
b.text(p, "tHdr" + prefix, x, 36, 228, 22, side, font=SMALL, pco=LABEL)
b.text(p, "t%sRoll" % prefix, x, 58, 228, 22, "roll --", maxl=16)
b.slider(p, "h%sRoll" % prefix, x, 80, 228, 16, 90, 45, cursor=10)
b.text(p, "t%sPitch" % prefix, x, 100, 228, 22, "pitch --", maxl=16)
b.slider(p, "h%sPitch" % prefix, x, 122, 228, 16, 90, 45, cursor=10)
b.text(p, "tHdrS", 4, 144, 60, 22, "STICKS", pco=LABEL)
for i, axis in enumerate(("X", "Y", "TWIST")): # centred over the slider columns below
x = 32 + i * 150
if i == 0:
x, w = 67, 70 # same centre (102), clear of "STICKS"
else:
w = 140
b.text(p, "tAx%d" % i, x, 144, w, 22, axis, pco=LABEL, xcen="center")
for row, (label, y) in enumerate((("L", 168), ("R", 192))):
b.text(p, "tSt" + label, 4, y, 24, 20, label, pco=LABEL)
for i, axis in enumerate("XYT"):
b.slider(p, "h%s%s" % (label, axis), 32 + i * 150, y + 2, 140, 16, 200, 100, cursor=10)
b.text(p, "tBatt", 4, 220, 472, 24, "battery remote -- robot --", maxl=40)
b.text(p, "tRadio", 4, 244, 472, 24, "radio no link", maxl=44)
# ---------------------------------------------------------------- 3 servos
p = "servos"
# Top view of the robot, front up: right legs on the right.
slots = {5: (4, 36), 4: (4, 114), 3: (4, 192), 0: (304, 36), 1: (304, 114), 2: (304, 192)}
for leg, (x, y) in slots.items():
b.text(p, "tLeg%d" % leg, x, y, 172, 18, LEG_TITLES[leg], pco=LABEL, maxl=12)
for j in range(3):
i = leg * 3 + j
b.text(p, "tJ%d" % i, x, y + 19 + j * 19, 108, 18, ["coxa", "femur", "tibia"][j] + " --", maxl=12)
b.bar(p, "jJ%d" % i, x + 110, y + 22 + j * 19, 62, 13)
b.text(p, "tBody", 196, 40, 88, 200, "FRONT", font=SMALL, pco=TEXT, bco="#303848", xcen="center",
ycen="up", style="border", borderc="#8090a8", borderw=2)
b.text(p, "tSrv", 196, 246, 88, 22, "NO DATA", font=SMALL, pco="#808080", xcen="center", maxl=10)
# ---------------------------------------------------------------- 4 cal
p = "cal"
b.button(p, "bCalOn", 4, 38, 130, 44, "START", [frame("O")], maxl=6)
b.text(p, "tMsg", 140, 38, 336, 44, "Robot on a stand, disarmed: START", font=SMALL, pco="#ffd040", maxl=40, isbr="true")
for i in range(6):
b.button(p, "bL%d" % i, 4 + i * 79, 88, 75, 40, LEG_NAMES[i], [frame("L", i)], maxl=4)
for j, n in enumerate(["COXA", "FEMUR", "TIBIA"]):
b.button(p, "bJ%d" % j, 4 + j * 158, 134, 154, 40, n, [frame("J", j)], maxl=6)
for n, x, t, code in (("bM10", 4, "-10", 0), ("bM1", 78, "-1", 1), ("bP1", 332, "+1", 2), ("bP10", 406, "+10", 3)):
b.button(p, n, x, 180, 70, 44, t, [frame("T", code)], maxl=4)
b.text(p, "tTrim", 152, 180, 176, 44, "FR coxa", font=SMALL, xcen="center", maxl=20, bco="#303848")
b.button(p, "bZeroT", 4, 228, 110, 40, "ZERO", [frame("T", 4)], font=SMALL, maxl=6)
b.button(p, "bRevert", 118, 228, 140, 40, "REVERT", [frame("E")], font=SMALL, maxl=8)
b.button(p, "bSave", 262, 228, 214, 40, "SAVE TO ROBOT", [frame("W")], font=SMALL, maxl=14)
# ---------------------------------------------------------------- 5 setup
p = "setup"
b.text(p, "tHdr1", 4, 36, 472, 22, "STICKS AND HANDSET", pco=LABEL, maxl=20)
b.button(p, "bCal", 4, 60, 170, 44, "CAL STICKS", [frame("C")], font=SMALL, maxl=12)
b.button(p, "bCalX", 180, 60, 120, 44, "CANCEL", [frame("Q")], font=SMALL, maxl=8)
b.button(p, "bZero", 306, 60, 170, 44, "LEVEL REMOTE", [frame("Z")], font=SMALL, maxl=14)
b.text(p, "tHdr2", 4, 110, 472, 22, "DISPLAY", pco=LABEL, maxl=20)
b.button(p, "bUpd", 4, 134, 230, 44, "UPDATE DISPLAY", [frame("U")], font=SMALL, maxl=16)
b.text(p, "tDim", 244, 134, 70, 44, "LIGHT", pco=LABEL)
b.slider(p, "hDim", 314, 142, 162, 28, 100, 100, {"codesslide": ["dim=hDim.val"], "codesup": ["dims=hDim.val"]},
cursor=20)
b.text(p, "tVer", 4, 186, 472, 22, "remote fw --", pco=LABEL, maxl=50)
b.text(p, "tMsg", 4, 212, 472, 54, "", pco="#ffd040", maxl=40, isbr="true")
# ---------------------------------------------------------------- 6 upload
p = "upload"
b.text(p, "t0", 20, 40, 440, 50, "DISPLAY UPDATE MODE", font=BIG, xcen="center", maxl=20)
for i, line in enumerate(("Upload the .tft from Nextion Editor", "over the remote's USB port.", "",
"Power-cycle the remote when done.")):
b.text(p, "t%d" % (i + 1), 20, 110 + i * 26, 440, 24, line, xcen="center", maxl=40)
return path
# Page pre-initialize code after the 'P' frame.
PREINIT = {
# Indicator sliders: no touch, so a finger can't drag them.
"status": ["tsw hRRoll,0", "tsw hRPitch,0", "tsw hBRoll,0", "tsw hBPitch,0",
"tsw hLX,0", "tsw hLY,0", "tsw hLT,0", "tsw hRX,0", "tsw hRY,0", "tsw hRT,0"],
"setup": ["hDim.val=dim"],
}
def main():
ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])
ap.add_argument("--out", default=OUT)
ap.add_argument("--font-dir", help="folder with DejaVuSansMono.ttf and DejaVuSansMono-Bold.ttf")
a = ap.parse_args()
tmp = a.out + ".new"
if os.path.exists(tmp):
os.remove(tmp)
build(tmp, font_dir(a.font_dir))
problems = nx.t_verify(tmp)
if not problems["ok"]:
sys.exit("verify failed: %s" % problems["problems"])
os.replace(tmp, a.out)
print("wrote", a.out)
if __name__ == "__main__":
main()