Appearance
NavX UI SDK
How an app draws its screen with the NavX UI library and runs inside the NavX shell. The look and the rules are in ui-guidelines.md.
| Part | Where | Public later |
|---|---|---|
| UI host protocol | include/navx/app/ui_host.h | yes |
| UI library (fonts, theme, drawing, widgets, app side) | ui/ | yes |
| font tools, contrast check | ui/tools/ | yes |
| example app | examples/ui-hello.c | yes |
| the shell (the host) | navx-shell shell/ | no |
| standalone back end on the unit | navx-shell unit/ui_unit.c | no: it is built on navx-firmware's unit.c (how the firmware takes the page and the knob) |
Nothing in ui/ comes from BMW code or data. The fonts and icons are drawn by hand, and the colours come from our own mockups.
An app in 30 lines
c
#include "ui.h"
#include <navx/app/ui_host.h>
int main(int argc, char **argv) {
ui_app_want(NAVX_UI_WANT_SPLIT); /* or _FULL */
if (ui_app_start(argc, argv, NULL)) return 1; /* NULL: hosted only */
for (int dirty = 1;;) {
if (dirty) {
ui_app_bar("HELLO", 1); /* title, back item (2 = focused) */
ui_box(0, 0, ui_cw, ui_ch, ui_col[C_BG]);
ui_str(&px16b, 12, 8, "Hello", ui_col[C_TEXT]);
ui_app_show();
dirty = 0;
}
int e = ui_app_wait(5); /* 5 ticks = 50 ms */
if (e == UI_HOME) ui_app_exit(0);
if (e >= 0) dirty = 1; /* UI_PREV/NEXT/SELECT/REDRAW */
}
}The whole example, with a focus ring, is examples/ui-hello.c.
Build
js
import { build } from '@emdzej/navx-sdk';
import { checkNoMulDiv, uiBuild } from '@emdzej/navx-sdk/ui';
const ui = uiBuild('dist/gen/myapp'); // sources + includes; writes fonts.c there
const sources = ['myapp.c', ...ui.sources];
checkNoMulDiv(sources, ui.includes, 'dist/gen/myapp', 'myapp');
const { module } = build(sources, { name: 'myapp', cflags: ['-Os', '-Werror', ...ui.includes.map((i) => `-I${i}`)] });
// re-parse: parity + CRC, size a multiple of 4 (module.bytes), before writing it anywhereAn app that should also run on its own from the launcher adds the standalone back end: navx-shell/unit/ui_unit.c, with -I navx-firmware/launcher and -DUNIT_NAME="myapp". It passes &ui_unit to ui_app_start. navx-apps' tools/build.mjs does this with ui: true.
The app API (ui.h, app.c)
| Call | |
|---|---|
ui_app_start(argc, argv, standalone) | Hosted when started with -ui (NAVX_UI_PARAM); otherwise standalone takes the screen (NULL: fail). Sets the theme and the drawing target. 0 or -1. |
ui_app_bar(title, back) | The status bar: title (UTF-8, ≤ 31 bytes), back 0 none, 1 shown, 2 focused. |
ui_app_want(w) | NAVX_UI_WANT_SPLIT or _FULL. Call it before start for the first area, and later from a Full/Split item. |
ui_app_show() | The frame is drawn: the rows that changed go to the host (or the page). |
ui_app_wait(ticks) | Sleep (0: don't), then the next event: UI_PREV, UI_NEXT, UI_SELECT, UI_HOME, UI_REDRAW (area, focus or night changed), or -1. |
ui_app_exit(status) | Tell the host, unlink, exit. Use it, not exit(). |
ui_cw, ui_ch | The area's size now: 400 × 216 full, 248 × 216 split. Lay out from these. |
ui_app_focus | 0 while an overlay has the input: draw no focus. |
ui_app_hosted | 1 inside the shell. |
Immediate mode: on any change, draw the whole area again and call ui_app_show(). Only the rows that changed are sent.
Drawing (ui.h, gfx.c, widgets.c)
Coordinates belong to the target: (0, 0) is the area's top left.
- Shapes:
ui_box,ui_pset,ui_hline,ui_vline,ui_frame(1-px outline; r 1 cuts the corners),ui_line(t px thick),ui_quarter(a quarter circle). - Text, UTF-8, at the top of the font's cell:
ui_str,ui_str_r(right-aligned to x),ui_str_c(centred); each returns the x after the text.ui_strw(width),ui_icon(x, y, letter, colour).
- Fonts:
px13,px13b,px16,px16b,big,icons. - Colours:
ui_col[C_*],ui_theme(night),ui_night. - Numbers, without divides:
ui_dec,ui_dec1(v/10 as "v.d"),ui_dec2(two digits),ui_put(copy a string),ui_divmod. - Widgets:
ui_sel_row(the focus under a row)ui_kv(label, value, unit, rule)ui_tile(126 × 90)ui_status_bar(the host's)
- Surfaces (for hosts and back ends):
ui_surf_alloc,ui_surf_at,ui_surf_view,ui_target,ui_surf_copy,ui_flush.
Fonts and theme modules
The fonts and the colours can come from two data modules of ours:
ui_fonts(all the fonts, about 16 KB)ui_theme(the colours, day and night)
Both are built by node ui/tools/assets.mjs <dir>, or uiModules() in a script, from ui/fonts/ and theme.c. Both are checked: parity, CRC, size a multiple of 4.
At start (ui_init_gfx, ui_theme, ui_app_start), the library links each module if it's in memory, checks its magic, version and every offset against its size, and uses it. Otherwise it uses the program's built-in fallback. So one module, loaded by the firmware or put into flash, gives every app the same, updatable look. ui_assets() returns bit 1 for the fonts and bit 2 for the theme when they came from the modules.
Layouts (big-endian, version 1; append-only like the protocol):
ui_fonts:'nxFT', version, font count, size, then 24-byte entries (name, height, baseline, space, glyph count, glyph table offset, bits offset). Glyphs are 8 bytes each (struct pxglyph); the bits are one u16 per row. SeefontsPayloadinui/tools/font.mjs.ui_theme:'nxTH', version, count n, size, then n day colours and n night colours as 0x00RRGGBB, in theC_*order. A program takes the first min(n, its C_N) and its own for the rest.
The fallback size is uiBuild(gen, { fallback }):
'all'(the default): every font, about 14 KB.'small': Px13, Px13 bold and the icons; the bigger fonts show as Px13 untilui_fontsis in memory.
The shell keeps 'all'.
The host protocol (navx/app/ui_host.h)
The full contract is in the header's comment. In short:
- Start. The shell finds the app in the disc's
APPS.TXTwith a 4th fieldui(I-Bus monitor|/dvd/APPS/IBUSX||ui). The launcher ignores that field. The shell checks the file (parity, CRC, size a multiple of 4) and starts it with-uiand its own parameters. - Module. The app creates the data module
ui_<pid>: header (magicnxUI, version, hdr_size, size), then the pixels (max 400 × 216 RGB555). Then it sets state RUNNING. - Area. The shell links the module, checks it (
navx_ui_ok), writes x, y, w, h and adds 1 toarea_seq. The app redraws for the new size (UI_REDRAW). Split ↔ full is the same step. - Frames. The app copies the changed rows into the pixels, sets their bits in
rows[], then adds 1 toframe. The shell copies the rows with bits set and clears them. Ifframejumped by more than 1, or moved while it copied, the shell copies every row. - Input. The shell writes events into the 16-slot ring (
ev_head), and the app reads them (ev_tail). There's one writer per index, so no lock. Flags: FOCUS (the app has the input), NIGHT. - Back. The app owns its back item: the first stop of its ring. It shows it in the bar through
back. Pressing it goes up a level; at the top the app ends. - Home. MENU sends
UI_HOME, and the shell goes home. The app ends. If it hasn't ended within 1 s, it's killed. - Exit. The app sets state GONE, unlinks, and exits. The shell unlinks the module (that frees it) and reaps the process.
Versioning. Fields are append-only. A new version adds fields, never moves one, and says where the pixels start (hdr_size). Hosts check magic, version, hdr_size and size before they use anything.
MIPS I rules (the unit's R3000-class CPU)
- No runtime multiply or divide in drawing code. Use shifts, adds, tables, and
ui_divmod. clang also turns constant divides, stepped loops (y += 28from a variable start) and subtract loops intomult/div. Break those with__asm__ volatile("" : "+r"(v))on the loop variable.checkNoMulDivfails the build on any of it. - No ternary that picks between string constants or globals (
a ? "x" : "y",n ? tab1 : tab2): clang's MIPS backend crashes. Index astatic constarray instead. -Os -Werror. Module sizes must be a multiple of 4 (a bad module can take the kernel down). Re-parse every built module before you write it.- Function pointers live in data (
const struct ui_backendis fine).
Testing with shots
Build and drive the app on the emulated unit (@emdzej/navx-emu):
- Use a flash copy of your own (
NAVX_FLASH). - The launcher comes from
pstartup. - The apps go on a disc in
APPS/, withAPPS.TXT. - The knob is driven with I-Bus frames from the BMBT:
ibusFrame(0xf0, 0x3b, [0x49, 0x80 | steps])turns right;[0x48, 0x05]and then[0x48, 0x85]is a press. - Take shots with
m.video.snapshot().
See navx-shell/tools/shots.mjs (the shell with ibusx inside) and navx-apps/tools/shots.mjs (ibusx on its own). Check the shots against the guidelines, and node ui/tools/contrast.mjs after any colour change.