Repository navigation
Expand file tree
/
Copy pathrssiLEDs.lua
More file actions
103 lines (83 loc) · 3.01 KB
/
Copy pathrssiLEDs.lua
File metadata and controls
103 lines (83 loc) · 3.01 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
-- RSSI LED Ring script
-- Based on the throttle indicator on the LED bar by btastic_fpv
--
-- Ring 1 shows 1RSS, ring 2 shows 2RSS (ExpressLRS diversity).
-- If 2RSS is unavailable, ring 2 mirrors ring 1.
-- If a ring has no signal at all, its first LED pulses slowly.
---------------------------------------------------------------
-- User settings
---------------------------------------------------------------
-- Ring color (0-255 per channel). Change this line for other variants.
local COLOR = {255, 255, 255} -- white
-- local COLOR = {0, 120, 255} -- blue
-- local COLOR = {255, 40, 0} -- orange
local RSSI_MIN = -117 -- dBm mapped to an empty ring
local RSSI_MAX = -3 -- dBm mapped to a full ring
local PULSE_PERIOD = 200 -- link-lost pulse period in 10 ms ticks (200 = 2 s)
local PULSE_MIN = 8 -- dimmest level of the pulse (0-255)
local LED_GROUP1 = {10,11,12,13,14,15,16,17,18,19}
local LED_GROUP2 = {0,1,2,3,4,5,6,7,8,9}
---------------------------------------------------------------
-- Helpers
---------------------------------------------------------------
local function init()
end
-- Set one LED to COLOR scaled by level (0-255)
local function setLed(index, level)
local s = level / 255
setRGBLedColor(index,
math.floor(COLOR[1] * s + 0.5),
math.floor(COLOR[2] * s + 0.5),
math.floor(COLOR[3] * s + 0.5))
end
-- Smooth 0..1..0 pulse, returned as a brightness level (PULSE_MIN-255)
local function pulseLevel()
local phase = (getTime() % PULSE_PERIOD) / PULSE_PERIOD
local wave = (1 - math.cos(phase * 2 * math.pi)) / 2
return math.floor(PULSE_MIN + wave * (255 - PULSE_MIN))
end
-- Convert dBm to 0.0-1.0, or nil if there is no signal
local function rssiToFraction(dbm)
if dbm == nil or dbm == 0 then
return nil
end
return math.max(0, math.min(1, (dbm - RSSI_MIN) / (RSSI_MAX - RSSI_MIN)))
end
-- Fill a ring. fraction is 0.0-1.0, or nil for the link-lost pulse.
local function fillSlots(fraction, ledGroup)
local n = #ledGroup
-- No signal: pulse the first LED, keep the rest off
if fraction == nil then
local level = pulseLevel()
for i = 1, n do
setLed(ledGroup[i], (i == 1) and level or 0)
end
return
end
local lit = fraction * n
local full = math.floor(lit)
-- Squared for a more even-looking fade on the partial LED
local partial = math.floor(((lit - full) ^ 2) * 255)
for i = 1, n do
if i <= full then
setLed(ledGroup[i], 255)
elseif i == full + 1 then
setLed(ledGroup[i], partial)
else
setLed(ledGroup[i], 0)
end
end
end
---------------------------------------------------------------
-- Main
---------------------------------------------------------------
local function run()
local f1 = rssiToFraction(getValue("1RSS"))
local f2 = rssiToFraction(getValue("2RSS"))
fillSlots(f1, LED_GROUP1)
fillSlots(f2 or f1, LED_GROUP2) -- mirror ring 1 if 2RSS is unavailable
applyRGBLedColors()
end
local function background()
end
return { run=run, background=background, init=init }