-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmspWarn
More file actions
107 lines (94 loc) · 2.87 KB
/
mspWarn
File metadata and controls
107 lines (94 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
protocol = assert(loadScript("protocols.lua"))()
radio = assert(loadScript("radios.lua"))().msp
assert(loadScript(protocol.mspTransport))()
assert(loadScript("MSP/common.lua"))()
-- =========================
-- CONFIG
-- =========================
local DEFAULT_TEXT = "https://www.youtube.com/@Akceptor"
local MSP_SET_OSD_CUSTOM_MSG = 237 -- dynamic text for analog OSD
local SET_TEXT_SWITCH = "se"
local SWITCH_THRESHOLD = 0
local TEXT_MAX_LENGTH = 32
local DISPLAY_LENGTH = 16
local SCROLL_INTERVAL = 100 -- 1 s assuming 100 Hz timer
-- =========================
-- STATE
-- =========================
local switchWasOn = false
local scrollIndex = 1
local lastScrollTime = 0
local configuredText = DEFAULT_TEXT
local textLength = #configuredText
-- =========================
-- UTILITIES
-- =========================
local function getScrollSegment()
if textLength == 0 then return "" end
local segment = string.sub(configuredText, scrollIndex, scrollIndex + DISPLAY_LENGTH - 1)
if #segment < DISPLAY_LENGTH then
segment = segment .. string.sub(configuredText, 1, DISPLAY_LENGTH - #segment)
end
return segment
end
-- Send MSP 237 (enable + text)
local function sendOsdMessage(text)
local payload = {1} -- first byte = enable flag
for i = 1, TEXT_MAX_LENGTH do
payload[#payload + 1] = string.byte(text, i) or 0
end
protocol.mspWrite(MSP_SET_OSD_CUSTOM_MSG, payload)
end
-- Optional: disable message (hide)
local function clearOsdMessage()
protocol.mspWrite(MSP_SET_OSD_CUSTOM_MSG, {0})
end
-- =========================
-- LOGIC
-- =========================
local function handleSwitch(telemetryReady)
if textLength == 0 then
switchWasOn = false
return
end
local switchValue = getValue(SET_TEXT_SWITCH)
local switchOn = switchValue and (switchValue > SWITCH_THRESHOLD)
local now = getTime()
if telemetryReady and switchOn then
if not switchWasOn then
scrollIndex = 1
lastScrollTime = now
sendOsdMessage(getScrollSegment())
elseif now - lastScrollTime >= SCROLL_INTERVAL then
scrollIndex = (scrollIndex % textLength) + 1
lastScrollTime = now
sendOsdMessage(getScrollSegment())
end
elseif switchWasOn and not switchOn then
clearOsdMessage() -- hide when switch released
end
switchWasOn = telemetryReady and switchOn
end
local function updateLogic()
local telemetryReady = getRSSI() > 0
if telemetryReady then
mspProcessTxQ()
else
switchWasOn = false
end
handleSwitch(telemetryReady)
end
local function run(event)
updateLogic()
return 0
end
local function background()
updateLogic()
return 0
end
local function init()
switchWasOn = false
scrollIndex = 1
lastScrollTime = 0
end
return { run = run, background = background, init = init }