Files
slate32/test/settings_calibration.lua
T
evan ad396d3f01 feat: Lua app firmware for the E32R40T display module
Boots to a launcher that lists /apps/<name>/main.lua on the SD card and runs
the selected app in a vendored Lua 5.4 with gui, input, fs, sys and log
bindings. Settings persist as a Lua table in /settings.lua, covering touch
calibration and screen rotation, with a settings app to edit both. Rotation is
applied after mapping raw touch into the panel's rotation-0 frame, so turning
the UI never invalidates a calibration.
2026-07-31 21:28:11 -04:00

67 lines
2.3 KiB
Lua

-- Run: lua test/settings_calibration.lua
-- Stubs the firmware bindings so the settings app loads on a desktop Lua.
local saved
local rotation = 0
gui = {
color = function() return 0 end,
setRotation = function() end,
width = function() return 320 end,
height = function() return 480 end,
clear = function() end,
drawText = function() end,
drawRect = function() end,
drawLine = function() end,
}
sys = {
exit = function() end,
setCalibration = function(...) saved = {...} return true end,
getRotation = function() return rotation end,
setRotation = function(degrees) rotation = degrees return true end,
}
input = {getRawTouch = function() return nil end}
log = {info = function() end}
dofile("sdcard/apps/settings/main.lua")
-- A perfectly linear panel spanning raw 200..3800 over 320x480 must round-trip
-- to those same extremes from the two inset samples.
local inset, w, h = 30, 320, 480
local function raw(pixel, size) return 200 + (3800 - 200) * pixel / size end
local s1 = {x = raw(inset, w), y = raw(inset, h)}
local s2 = {x = raw(w - inset, w), y = raw(h - inset, h)}
local x0, y0, x1, y1 = computeCalibration(s1, s2, w, h, inset)
assert(math.abs(x0 - 200) <= 1, "x0 " .. x0)
assert(math.abs(y0 - 200) <= 1, "y0 " .. y0)
assert(math.abs(x1 - 3800) <= 1, "x1 " .. x1)
assert(math.abs(y1 - 3800) <= 1, "y1 " .. y1)
-- A flipped panel (raw decreasing with pixel) must yield a descending range.
local s3 = {x = 3800 - raw(inset, w) + 200, y = s1.y}
local s4 = {x = 3800 - raw(w - inset, w) + 200, y = s2.y}
local fx0, _, fx1 = computeCalibration(s3, s4, w, h, inset)
assert(fx0 > fx1, "flipped axis should descend")
-- Rotation cycles through the four quarter turns and wraps back to 0.
setup()
for _, expected in ipairs({90, 180, 270, 0}) do
on_touch(20, 85)
assert(sys.getRotation() == expected, "rotation " .. sys.getRotation())
end
setup()
on_touch(20, 45)
on_tick() -- release after the menu tap arms sampling
input.getRawTouch = function() return s1.x, s1.y end
on_tick()
input.getRawTouch = function() return nil end
on_tick()
input.getRawTouch = function() return s2.x, s2.y end
on_tick()
input.getRawTouch = function() return nil end
on_tick()
assert(saved, "calibration was not saved")
assert(math.abs(saved[1] - 200) <= 1, "saved x0 " .. saved[1])
print("ok")