← Files AiDot Lighting (EU)ARCHIVED FILE
skills/create-light-effect/references/light-effect-api.md
7.06 KB · Nov 30, 2026 · 00:25 UTC
# Light Effect API Document
## Metadata
First Lua block comment contains JSON defining script info & params.
Must wrapped exactly by `--[=[` and `--]=]` (both with leading --)
### Schema
- applicationId: unique ID of effect
- frameDuration: int, frame time in ms
- In the metadata section, make sure that --[=[ and the closing --]=] are not placed on the same line as the Lua code inside. They must each be on their own separate line.
```lua
--[=[
{
"applicationId": "ad.example",
"minSdkVersion": 1,
"version": {"versionCode": 1, "versionName": "1.0.0"},
"frameDuration": 50,
"comment": ["effect comment"],
"sliderInt8": [{"paramKey": "intensity", "minValue": 0, "maxValue": 255, "defaultValue": 128, "comment":"intensity"}],
"colorPalette": [{"paramKey": "cpt", "defaultCpt": [[0,255,0,0],[128,0,255,0],[255,0,0,255]], "comment": "palette"}],
"color": [{"paramKey": "fgc", "defaultColor": [255,0,0], "comment": "fg color","type":"RGB"}],
"blob": [{"paramKey": "gif", "data": "/wAAAP8AAAD/", "comment": "blob data"}],
"usesFeatures": ["blob","audio","cct"]
}
--]=]
```
**Note:**
The applicationId, version, and frameDuration configuration fields are required.
The applicationId length cannot exceed 100 characters.
## Runtime Status Tools (when available through MCP)
- Call `requestEffectRunStatus` and `getEffectRunStatusResult` only when the user explicitly asks to inspect or analyze actual effect runtime performance. Do not call them proactively during normal effect creation, code generation, or execution.
- Use `requestEffectRunStatus` first, then use the deviceId and returned requestId with `getEffectRunStatusResult`. A pending result means the device response has not arrived yet.
- genDurations is firmware frame-production execution time and excludes waiting. Compare it with frameDuration to assess production cost.
- outIntervals is actual visible spacing between frames and includes production, synchronization, and waiting. Its minimum is normally at least frameDuration because the engine waits when production is faster.
- Recent samples are ordered oldest to newest. >=255 ms means the actual value is at least 255 ms, not exactly 255 ms. Device code -1 means no lighting effect is currently running.
- Explain measurements in plain language. You may suggest increasing frameDuration or optimizing the algorithm, but do not automatically modify code or refuse execution because performance is insufficient.
## Event Handler
- onInit(ctx) (Put one-time initialization here)
- onFrame(ctx) (normally no return; optionally return isSkipFrame, nextFrameDelay; Put per-frame logic that runs once each frame here)
- onRender(ctx, index) (index=logical pixel ID, 0-based; return color; Avoid heavy or repeated calculations here,it is called for every pixel)
- onParamChanged(ctx) (on param change, must re-read all Metadata params)
- onGetManifest(ctx) (optional, return {duration=ms})
## Library
Runs on **Lua 5.4** with only plain ASCII characters, no emoji or non-ASCII symbols.
Use **dot notation** (e.g. `ctx.getNumber("speed")`), not colon notation.
### Context (`ctx`)
- Fields: pixelCount, frameIndex, pts, sdkVersion
- getNumber(paramKey) → number (Metadata sliderInt8)
- getCpt(paramKey) (Metadata colorPalette, ref ColorPaletteTable)
- getColor(paramKey) → color (Metadata color)
- getColorRGB(paramKey) → r,g,b (Metadata color)
- getBlob(paramKey, offset, length, dstTbl) → dwOutLen, byteOutLen (Metadata blob; writes raw data into dstTbl table as uint32 per 4 bytes, little-endian: dst[i] = B3<<24 | B2<<16 | B1<<8 | B0; requires usesFeatures:"blob")
- getAudio() → Audio (requires usesFeatures:"audio")
- Time(duration_ms : MUST be integer) → float [0..1] (sawtooth)
### Color
- Color.RGB(r,g,b) → color (r,g,b in [0..255]) → type = "RGB"
- Color.HSV(h,s,v) → color (h,s,v in [0..1]) → type = "HSV"
- Color.CCT(cct,brightness) → color (cct: color temperature in Kelvin [0..32767]; brightness: [0..127]) → type = "CCT" and "usesFeatures": [...,"cct"]
**Note:** `Color.CCT` is for CCT (correlated color temperature) white light only. Unless the user explicitly requests a CCT/color-temperature light effect, always use `Color.HSV` or `Color.RGB` instead.
### ColorPaletteTable (ctx.getCpt(paramKey))
- getColor(z : MUST be integer) → color (gradient color for z=0..255)
- getRGB(z : MUST be integer) → r,g,b
- getRaw() → string (packed rows: zmin,rmin,gmin,bmin,zmax,rmax,gmax,bmax * rows)
### WaveForm
- WaveForm.Wave(v) → y [0..1] (sine, v [0..1])
- WaveForm.Triangle(v) → y [0..1] (linear up/down, v [0..1])
- WaveForm.Sin8(z) → y [0..255] (8-bit sine, z [0..255])
- WaveForm.Triangle8(z) → y [0..255] (8-bit triangle, z [0..255])
- WaveForm.Quadratic8(z) → y [0..255] (8-bit quadratic curve, z [0..255])
### Audio (ctx.getAudio())
- getVolumeSmth() → float (0..255)
- getMajorPeak() → float (Hz)
- getSamplePeak() → bool
## Device `desc`
This field from `listControllableDevicesAndGroups` provides optional additional information about a specific device.
### rect
If `desc` indicates that a device is a `wxh` rect (for example `"20x20"`), you can use it to map the pixel index to 2D coordinates in your script. Note, NEVER hardcode h and w.
```lua
local h = ctx.height or 1
local w = ctx.width or ctx.pixelCount
local x=index//h -- index from onRender
local y=index%h
```
### encoded2DCoords
If `desc` contains `encoded2DCoords:BASE64_STRING`, it means the device uses an irregular 2D coordinate layout.
The value is a base64-encoded sequence of coordinates `[x0,y0,x1,y1,...]` where each coordinate is in the range 0–255 (`0` = top).
You can decode this string in your script to get the (x,y) position for each pixel instead of relying on `ctx.width/height`.
```lua
local coords = {}
function loadCoords(ctx)
local blob = {}
-- **do NOT forget to add this param to Metadata**
-- {"blob": [{"paramKey": "encoded2DCoords", "data": "gAD5WMrnNecGWA==", "comment": "blob data from device desc"}]}
local dwLen, byteLen = ctx.getBlob("encoded2DCoords", 0, math.maxinteger, blob)
local idx = 1
for i = 1, dwLen do
local data = blob[i]
local x = data & 0xFF
local y = (data >> 8) & 0xFF
coords[idx] = { x = x, y = y }
idx = idx + 1
byteLen = byteLen - 2
if byteLen >= 2 then
x = (data >> 16) & 0xFF
y = (data >> 24) & 0xFF
coords[idx] = { x = x, y = y }
idx = idx + 1
byteLen = byteLen - 2
```
## lua code
Variable Definition Rules
Variables used in a function must be explicitly declared globally outside the function.
Do not assign values to undefined variables within a function.
❌ Incorrect example (creates global variables):
```lua
function onParamChanged(ctx)
baseR, baseG, baseB = ctx.getColorRGB("fgc") -- first assignment without declaration
end
✅ Correct example:
```lua
local baseR, baseG, baseB = 255, 255, 0
function onParamChanged(ctx)
baseR, baseG, baseB = ctx.getColorRGB("fgc")
end
SHA-256: 874749f939e2ec710b755bfc94a1a10cf0b5311abfb5697670100776c71eaa5c