Skip to content

Commit

Permalink
Merge pull request #8 from rll-rll/master
Browse files Browse the repository at this point in the history
Add color customization with Dualbandwidth support
  • Loading branch information
ddrown committed Sep 26, 2020
2 parents ef081f5 + 7a1a564 commit d2320c0
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 37 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ To install on the Turris Omnia:

```shell
opkg update
opkg install git
opkg install git luabitop
git clone git://github.com/ddrown/omnia-led-colors.git
cd omnia-led-colors
./install
Expand All @@ -13,6 +13,8 @@ cd omnia-led-colors

To update an existing install:

_Ensure that luabitop is installed_

```shell
cd omnia-led-colors
git pull
Expand Down
82 changes: 59 additions & 23 deletions omnia-led-colors
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
-- settings are now in /etc/config/omnia-led-colors

local uci = require "luci.model.uci".cursor()
require "bit"
require "nixio"

local data = {wan={}, ath9k={}, ath10k={}}
Expand Down Expand Up @@ -131,6 +132,9 @@ end
function dualbandwidth_led(ledname, arguments)
local interface, limit_in, limit_out = arguments.interface, arguments.limit_in, arguments.limit_out

--colors
local nominal, critical_in, critical_out = tonumber(arguments.nominal or '0x00FF00'), tonumber(arguments.critical_in or '0xFF0000'), tonumber(arguments.critical_out or '0x0000FF')

local wan_data = data.wan[interface]

if wan_data == nil then
Expand All @@ -146,25 +150,26 @@ function dualbandwidth_led(ledname, arguments)
local pct_in = wan_data.bps_in / limit_in
local pct_out = wan_data.bps_out / limit_out

local red, green, blue
if pct_out > 0.5 then
blue = math.min(math.floor(pct_out*255),255)
green = 0
red = 0
local colors_out = shift_color(nominal, critical_out, pct_out)
local red, green, blue = colors_out[1], colors_out[2], colors_out[3]
debug_print(wan_data.bps_in .."^|v".. wan_data.bps_out .. " (" .. math.floor(pct_in*100) .. "% ^|v " .. math.floor(pct_out*100) .. "% "..red.." "..green.." "..blue..")")
write_led(ledname, red, green, blue)
else
green = math.floor(math.max(1-pct_in,0)*255)
red = math.min(math.floor(pct_in*255),255)
blue = 0
local colors_in = shift_color(nominal, critical_in, pct_in)
local red, green, blue = colors_in[1], colors_in[2], colors_in[3]
debug_print(wan_data.bps_in .."^|v".. wan_data.bps_out .. " (" .. math.floor(pct_in*100) .. "% ^|v " .. math.floor(pct_out*100) .. "% "..red.." "..green.." "..blue..")")
write_led(ledname, red, green, blue)
end

debug_print(wan_data.bps_in .."^|v".. wan_data.bps_out .. " (" .. math.floor(pct_in*100) .. "% ^|v " .. math.floor(pct_out*100) .. "% "..red.." "..green.." "..blue..")")

write_led(ledname, red, green, blue)
end

function bandwidth_led(ledname,arguments)
local interface, direction, limit = arguments.interface, arguments.direction, arguments.limit

--colors
local nominal, critical = tonumber(arguments.nominal or '0x00FF00'), tonumber(arguments.critical or '0xFF0000')

local wan_data = data.wan[interface]

if wan_data == nil then
Expand All @@ -184,47 +189,78 @@ function bandwidth_led(ledname,arguments)
pct = wan_data.bps_out / limit
end

-- 0% = green, 50% = yellow, 100% = red
local green = math.floor(math.max(1-pct,0)*255)
local red = math.min(math.floor(pct*255),255)
local colors = shift_color(nominal, critical, pct)
local red, green, blue = colors[1], colors[2], colors[3]

debug_print(wan_data.bps_in .."^/v".. wan_data.bps_out .. " (" .. math.floor(pct*100) .. "% "..red.." "..green .." 0)")
debug_print(wan_data.bps_in .."^/v".. wan_data.bps_out .. " (" .. math.floor(pct*100) .. "% "..red.." "..green .." "..blue..")")

write_led(ledname, red, green, 0)
write_led(ledname, red, green, blue)
end

function ath9k_led(ledname,arguments)
local interface = arguments.interface

--colors
local nominal, critical = tonumber(arguments.nominal or '0x00FF00'), tonumber(arguments.critical or '0xFF0000')

local pct = gather_ath9k(interface)

if pct == nil then
debug_print("ath9k interface "..interface.." nil data")
return
end

-- 0% = green, 50% = yellow, 100% = red
local green = math.floor(math.max(1-pct,0)*255)
local red = math.min(math.floor(pct*255),255)
local colors = shift_color(nominal, critical, pct)
local red, green, blue = colors[1], colors[2], colors[3]

write_led(ledname, red, green, 0)
write_led(ledname, red, green, blue)
end

function ath10k_led(ledname,arguments)
local phy = arguments.phy

-- colors
local nominal, critical = tonumber(arguments.nominal or '0x00FF00'), tonumber(arguments.critical or '0xFF0000')

local pct = gather_ath10k(phy)

if pct == nil then
debug_print("ath10k phy "..phy.." nil data")
return
end

-- 0% = green, 50% = yellow, 100% = red
local green = math.floor(math.max(1-pct,0)*255)
local red = math.min(math.floor(pct*255),255)
local colors = shift_color(nominal, critical, pct)
local red, green, blue = colors[1], colors[2], colors[3]

write_led(ledname, red, green, blue)
end

function shift_color(nominal, critical, pct)
local n_red, n_green, n_blue, -- nominal colors
c_red, c_green, c_blue, -- critical colors
a_red, a_green, a_blue -- actual colors

-- bitmagic our way to individual colors
-- remember: 0xRRGGBB
n_red = bit.arshift(bit.band(nominal, tonumber('0xFF0000')), 16)
n_green = bit.arshift(bit.band(nominal, tonumber('0x00FF00')), 8)
n_blue = bit.band(nominal, tonumber('0x0000FF'))

c_red = bit.arshift(bit.band(critical, tonumber('0xFF0000')), 16)
c_green = bit.arshift(bit.band(critical, tonumber('0x00FF00')), 8)
c_blue = bit.band(critical, tonumber('0x0000FF'))

-- actual color is a range between critical and nominal based on the load
a_red = interpolate(n_red, c_red, pct)
a_green = interpolate(n_green, c_green, pct)
a_blue = interpolate(n_blue, c_blue, pct)

return {a_red, a_green, a_blue}
end

write_led(ledname, red, green, 0)
function interpolate(nominal, critical, pct)
pct = math.min(pct, 1)
return math.floor((critical * pct) + (nominal * (1 - pct)))
end

function setled(led)
Expand Down
77 changes: 64 additions & 13 deletions omnia-led-colors.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,75 @@
# option colorfunction 'dualbandwidth'
# option interface 'eth1'
# option limit_in 300000000
# option limit_out 20000000
# option limit_out 200000000
# option nominal "0x00FF00"
# option critical_in "0xFF0000"
# option critical_out "0x0000FF"

config led wan
option colorfunction 'bandwidth'
option interface 'eth1'
option direction 'in'
option limit 300000000
option colorfunction 'bandwidth'
option interface 'eth2'
option direction 'in'
option limit 300000000
option nominal "0x00FF00"
option critical "0xFF0000"

config led lan0
option colorfunction 'bandwidth'
option interface 'eth1'
option direction 'out'
option limit 20000000
option colorfunction 'bandwidth'
option interface 'lan0'
option direction 'out'
option limit 300000000
option nominal "0x00FF00"
option critical "0xFF0000"

config led lan1
option colorfunction 'bandwidth'
option interface 'lan1'
option direction 'out'
option limit 300000000
option nominal "0x00FF00"
option critical "0xFF0000"

config led lan2
option colorfunction 'bandwidth'
option interface 'lan2'
option direction 'out'
option limit 300000000
option nominal "0x00FF00"
option critical "0xFF0000"

config led lan3
option colorfunction 'bandwidth'
option interface 'lan3'
option direction 'out'
option limit 300000000
option nominal "0x00FF00"
option critical "0xFF0000"

config led lan4
option colorfunction 'bandwidth'
option interface 'lan4'
option direction 'out'
option limit 300000000
option nominal "0x00FF00"
option critical "0xFF0000"

config led lan5
option colorfunction 'bandwidth'
option interface 'lan5'
option direction 'out'
option limit 300000000
option nominal "0x00FF00"
option critical "0xFF0000"

config led pci3
option colorfunction 'ath9k'
option interface 'wlan1'
option colorfunction 'ath9k'
option interface 'wlan1'
option nominal "0x00FF00"
option critical "0xFF0000"

config led pci2
option colorfunction 'ath10k'
option phy 'phy0'
option colorfunction 'ath10k'
option phy 'phy0'
option nominal "0x00FF00"
option critical "0xFF0000"

0 comments on commit d2320c0

Please sign in to comment.