DocsDevelop

Lua apps

What Lua apps on the device are, a minimal example, the manifest, how to push and debug, and runtime limits.

What it is#

An app is a Lua 5.4 program that runs inside the device kernel. It owns the app area of the screen, responds to the BOOT button, timers, and messages from the host, and can also use the device as a keyboard and mouse. The macro player is itself an app.

  • Apps live in device storage and hot reload when pushed. No device restart, no firmware build.
  • Only one app runs at a time. With no app running, the screen shows the home screen.
  • To write apps you only need the stick command line and a paired device. See The stick command line.

The full API reference is on GitHub: docs/lua.md. This page is an overview.

Minimal example#

A counter: a number in the middle of the screen that goes up by one each time you click BOOT.

Directory layout:

hello/
  manifest.json
  main.lua

manifest.json (计数器 means "Counter"):

JSON
{"id": "hello", "name": "计数器", "version": "0.1.0"}

main.lua:

Lua
local n = 0
local lbl = ui.label({text = "0", font = 48, align = "center"})

button.on(function(ev)
  if ev == "click" then
    n = n + 1
    lbl:set({text = tostring(n)})
    app.emit({count = n})
  end
end)

Push and start:

PowerShell
stick push hello

The command returns the start result, recent logs, and events emitted by the app. Run stick snap to take a screenshot and see the result.

Manifest#

Field Required Description
id Yes App ID: lowercase letters, digits, _, -, 1–24 characters. Must match the directory name
name No Display name
version No Version string
main No Entry file. Defaults to main.lua
perms No Array of permissions. Currently only "hid"

Runtime model#

  • All of an app's code runs serially in a single task: top level, timer callbacks, button callbacks, message callbacks. Callbacks never run concurrently, so you don't need locks.
  • After the top level finishes, the app keeps running and does its work in callbacks. There is no sleep; to wait, use timer.after.
  • If a callback throws, the app enters an error state: all timers stop, the UI stays as it was, and the screen shows App error. Restart the app to recover.
Limit Value
Top-level code 1000 ms
Each callback 200 ms
Memory 2 MB
Timers 32 at a time; period at least 10 ms
Single .lua file 256 KB

Sandbox: standard libraries such as string, table, math, utf8, and coroutine are available; io, os, debug, and package are not. require(name) only reads <name>.lua from the app's own directory.

Modules#

Module Purpose
sys Device ID, version, uptime, time and date, memory
log Logging; print is the same as log.i
timer One-shot and periodic timers
button Subscribe to the BOOT button: down up click double long
app Receive host messages, send events to the host, callback before stopping
kv A per-app key-value store that survives restarts
fs Files in the app's own data directory
sd The app's directory on the microSD card
json Encoding and decoding
hid Keyboard and mouse output; requires the hid permission
ui UI: boxes, labels, progress bars, arcs, lines, QR codes, images, animations
monitor Set the status shown on the right side of the monitor screen

UI essentials:

  • The app area is 320×148, with the origin at its top-left corner. The top 24 pixels are the system status bar, which apps cannot draw on.
  • There are only four font sizes: 20, 24, 32, 48.
  • Displaying arbitrary Chinese text requires the full font on the device. Without it, characters missing from the built-in font show as boxes.

Keyboard and mouse output#

Add "hid" to perms in the manifest. The app gets output access automatically when it starts, with no need to arm. When the app stops, the device clears its queue and releases everything.

The app name 按键 below means "Key":

JSON
{"id": "f13", "name": "按键", "perms": ["hid"]}
Lua
button.on(function(ev)
  if ev == "click" and hid.ready() then
    hid.tap("F13", {"ctrl"})
  end
end)
  • Actions return as soon as they are queued and are sent in order. Use hid.wait to pause between actions.
  • For frame-by-frame timing, submit a timeline with hid.play. Put large timeline files on the microSD card and play them with hid.play_file.
  • Pressing BOOT during output is an emergency stop: it clears the queue, releases everything, and pauses the app's output. The callback's second argument, stop, is true when the press was an emergency stop. Press BOOT once more to restore the app's output access.
  • Rejected calls throw an error: locked, not_mounted, queue_full, bad_action.

WarningAn app with the hid permission really sends keystrokes to the computer. While debugging, start with keys like F13–F24 that usually no program responds to.

Debugging#

Command Purpose
stick try main.lua Push as a temporary app, run for a few seconds, and return events, logs, and a screenshot. Change a line, try again: the fastest loop
stick try -e <code> Pass a snippet of code directly
stick push <dir> Install and start for real
stick eval <code> Run code inside the running app to inspect variables or call functions
stick events -f Follow events sent with app.emit
stick logs System log; app log entries are tagged app:<app ID>
stick snap Screenshot
stick ui Object tree of the app area: type, position, size, text
stick msg <JSON> Send a message to the app's app.on_message

Recommended loop: test an idea with stick try → put it in the app directory → stick push → check the result with stick snap → watch behavior with stick events -f and stick logs → inspect state with stick eval.

In Windows PowerShell 5.1, double quotes are dropped when code or JSON containing them is passed as an argument to an external program. eval and msg accept - to read from standard input, or "@file" to read from a file; for try, passing a .lua file is easiest.

Agents do the same with MCP tools of the same names: stick_push, stick_try, stick_eval, stick_snap.

Management#

Command Action
stick apps List apps and their run state
stick start <app ID> / stick stop Start / stop
stick auto <app ID> / stick auto --off Set / clear start on boot
stick rm-app <app ID> Delete an app; its data directory is kept

If an app set to start on boot errors within 10 seconds of starting 3 times in a row, start on boot is cleared.

Example apps#

App area of the clock app
clock: seven-segment clock (device screenshot, app area 320×148)
App area of the hello app
hello: counter and progress bar (device screenshot)

The repository's apps/ directory contains three apps:

App Contents
hello Counter: button input, persistence, events, messages
clock Clock: large seven-segment digits, date, seconds progress bar
macro Macro player

View the source of this page on GitHub