Files
xteink-apps/apps/BLETest/main.lua
T
evan 39afd7fac7 build: add a shared editor config and reformat apps to match
The apps were on four-space indent only because nothing pinned a style;
.editorconfig is what lua-language-server reads on save, so the tree now
matches the sibling repos. Manifests and the catalog regenerate from the
reformatted sources.
2026-08-04 13:20:40 -04:00

162 lines
4.3 KiB
Lua

-- DESCRIPTION: Test BLE scanning and connections
local screen = "devices"
local lastScreen = nil
local devices = {}
local selected = 1
local message = "Press Scan to find nearby devices"
local connectedDevice = nil
local function refreshMode()
local mode = screen == lastScreen and REFRESH_FAST or REFRESH_HALF
lastScreen = screen
return mode
end
local function deviceLabel(device)
return device.address .. " (" .. (device.name ~= "" and device.name or "N/A") .. ")"
end
local function drawDevices()
gui.clear()
gui.drawCenteredText(FONT_UI_12, 28, "BLE Devices", COLOR_BLACK, STYLE_BOLD)
local visible = math.max(1, math.floor((gui.height() - 130) / 38))
local first = math.max(1, math.min(selected - math.floor(visible / 2), #devices - visible + 1))
local last = math.min(#devices, first + visible - 1)
local y = 72
for index = first, last do
local prefix = index == selected and "> " or " "
gui.drawText(
FONT_UI_12,
36,
y,
prefix .. deviceLabel(devices[index]),
COLOR_BLACK,
index == selected and STYLE_BOLD or STYLE_NORMAL
)
y = y + 38
end
if #devices == 0 then
gui.drawCenteredText(FONT_UI_10, math.floor(gui.height() / 2), message)
elseif message ~= "" then
gui.drawCenteredText(FONT_UI_10, gui.height() - 82, message)
end
gui.drawButtonHints("Back", #devices > 0 and "Info" or "", "", #devices > 0 and "Refresh" or "Scan")
gui.refresh(refreshMode())
end
local function drawStatus(text)
gui.clear()
gui.drawCenteredText(FONT_UI_12, math.floor(gui.height() / 2), text, COLOR_BLACK, STYLE_BOLD)
gui.refresh(refreshMode())
end
local function drawInfo()
local device = devices[selected]
gui.clear()
gui.drawCenteredText(FONT_UI_12, 28, "Device Info", COLOR_BLACK, STYLE_BOLD)
gui.drawText(FONT_UI_12, 36, 88, "Address: " .. device.address)
gui.drawText(FONT_UI_12, 36, 126, "Name: " .. (device.name ~= "" and device.name or "N/A"))
gui.drawText(FONT_UI_12, 36, 164, "RSSI: " .. device.rssi .. " dBm")
gui.drawText(FONT_UI_12, 36, 202, "Index: " .. selected .. "/" .. #devices)
gui.drawButtonHints("Back", "Connect", "", "")
gui.refresh(refreshMode())
end
local function drawConnected()
gui.clear()
gui.drawCenteredText(FONT_UI_12, 44, "Connected", COLOR_BLACK, STYLE_BOLD)
gui.drawCenteredText(FONT_UI_12, 100, deviceLabel(connectedDevice))
gui.drawButtonHints("Back", "Disconnect", "", "")
gui.refresh(refreshMode())
end
local function render()
if screen == "devices" then
drawDevices()
elseif screen == "info" then
drawInfo()
elseif screen == "connected" then
drawConnected()
end
end
local function scan()
screen = "scanning"
drawStatus "Scanning..."
devices = {}
collectgarbage "collect"
local results, err = ble.scan(3000)
devices = results or {}
selected = 1
message = results and (#devices .. " devices found") or (err or "Scan failed")
screen = "devices"
render()
end
local function connect()
local device = devices[selected]
if not device then
return
end
screen = "connecting"
drawStatus "Connecting..."
if ble.connect(device.address) then
connectedDevice = device
screen = "connected"
else
message = "Could not connect to " .. device.address
screen = "devices"
end
render()
end
local function disconnect()
ble.disconnect()
connectedDevice = nil
message = "Disconnected"
screen = "devices"
render()
end
function init()
if not ble.init "BLETest" then
message = "BLE initialization failed"
end
render()
end
function on_button(name, event)
if event ~= "down" then
return
end
if screen == "devices" and #devices > 0 and (name == "page_back" or name == "page_forward") then
local step = name == "page_back" and -1 or 1
selected = (selected - 1 + step) % #devices + 1
render()
elseif name == "right" and screen == "devices" then
scan()
elseif name == "confirm" and screen == "devices" and #devices > 0 then
screen = "info"
render()
elseif name == "confirm" and screen == "info" then
connect()
elseif name == "back" and screen == "info" then
screen = "devices"
render()
elseif name == "confirm" and screen == "connected" then
disconnect()
elseif name == "back" and screen == "connected" then
disconnect()
elseif name == "back" then
ble.deinit()
sys.exit()
end
end