Learn to make apps
An app is a folder with two small files, written in Lua in any text editor. Nothing to compile or install.
New to coding? Learn to code ↗ Build your first app from nothing, one step at a time, on a virtual unidex.An example
Here's a whole app. app.json says what it is and main.lua is the code. It counts
presses of B and remembers the count.
{
"id": "hello",
"name": "Hello",
"version": "1.0.0",
"author": "you",
"description": "Counts presses of B, and remembers the count.",
"category": "fun",
"api": 1
}
local count = tonumber(unidex.data.read("count") or "0")
function draw()
unidex.header("Hello")
unidex.centered(80, tostring(count), "large")
unidex.hints("", "add", "reset")
end
function onB()
count = count + 1
unidex.data.write("count", tostring(count))
return "partial"
end
function onBHold()
count = 0
unidex.data.write("count", "0")
return "partial"
end
For more, look at the apps in the store, or start from the template. The guide ↗ explains how an app runs, how to lay out a screen and how to keep data through sleep.
The API
Your code defines the functions the device calls: draw, onA, onB,
onBHold, onBack, tick, onEnter and onExit. To
draw and save, it calls unidex.*.
Each button function returns "partial", "full" or nothing, depending on whether the
screen needs a refresh.
| Area | What it can do |
|---|---|
| Drawing | header, hints, text, centered, wrap, rect, fill, line, qr, icon, bitmap, in five font sizes, black or white |
| Buttons | A, B, hold B and hold A, the same way the built-in apps use them |
| Its own files | unidex.data.read, write, append, list, remove, in the app's own folder |
| The device | time, battery, toast, wake_in, random, frame for animation (API 3), chime (API 5), and climate for the temperature and humidity (API 6) |
| WiFi | unidex.http.get over HTTPS, and unidex.wifi.scan for the networks nearby (API 2), with the "wifi" permission |
The API ↗: every call, in one short list.
Games
A game is played in Games, next to Flappy, Dino, Stack and Jetpack, with B as its one button. It's the same
kind of folder as an app, with "type": "game", "category": "games" and
"api": 4, and "frame_ms" to set how often it steps (100 ms if left out).
| Your code | What it does |
|---|---|
start(seed) | Starts a round. Call math.randomseed(seed), so the same seed plays the same round. |
step(tapped, held) | One frame. tapped: B was pressed since the last frame; held: B is down. Returns "over", "moved" or "still". |
draw() | Draws the whole play screen. |
score() | The score, a whole number. |
The Games app does the rest: the frames on the fast e-ink mode, the game-over screen, the best score, and
sleep. Games need firmware newer than v1.9. Until it's out, try one in the
App editor with ?v=next on its address, starting from "A game".
Making games ↗ has the details, and a template to start from.
Plugins
A plugin adds to one of the built-in apps instead of being an app of its own. It's the same kind of folder,
with "type": "plugin" and "extends".
For now, Badge is the app you can extend. Your badge() function draws a badge each time Badge
shows it, so it can show today's date, a countdown or a QR code. It appears in Badge after the picture
badges.
Writing plugins has the details and a template.
The sandbox
Each app is kept apart from the rest of the device:
- Its own memory (about 512 KB) and its own folder on the SD card, up to 512 KB of data.
- A time limit of about 2 seconds on each call, so a loop can't freeze the device.
- No way to reach the WiFi password, API keys, notes or other apps' files. It can only use the
internet with the
"wifi"permission, which people see before they install. - If an app breaks a limit or hits a bug, the device shows App error with the line it stopped on, and the rest of the device carries on.
Try it
- In your browser. Write it in the App editor and press Run: it opens on the emulated unidex straight away, with what your code
print()s and any errors beside it. - A folder you already have. Open Try unidex and choose the folder under Try your app. It installs the way the store does.
- On your unidex. Plug it in, press a button, and run
python3 tools/appinstall.py install path/to/your-appfrom a copy of the unidex repository. - Automatically.
node emu/test/app_smoke.mjs path/to/your-appopens it, presses every button, and tells you if it ever shows App error. The store runs the same check.