09.11.2020, 21:31
(09.11.2020, 08:56)admin Wrote: Use this event script for 4-bit object. Change R/G/B addresses as needed and modify step and delay to change the dimming speed and smoothness.Awesome! Thank you very much!!
Code:raddr = '32/1/1' -- red value address
gaddr = '32/1/2' -- green value address
baddr = '32/1/3' -- blue value address
step = 0.05 -- step side
delay = 0.3 -- delay between steps
value = event.getvalue()
up = bit.band(value, 0x08) == 0x08
stop = bit.band(value, 0x07) == 0
if stop then
return
end
-- get value and convert from 0..100 to 0..255
r = grp.getvalue(raddr) * 2.55
g = grp.getvalue(gaddr) * 2.55
b = grp.getvalue(baddr) * 2.55
function rgbtohsv(r, g, b)
local max, min, d, h, s, v
max = math.max(r, g, b)
min = math.min(r, g, b)
d = max - min
s = max == 0 and 0 or (d / max)
v = max / 255
if max == min then
h = 0
elseif max == r then
h = (g - b) + d * (g < b and 6 or 0)
h = h / (6 * d)
elseif max == g then
h = (b - r) + d * 2
h = h / (6 * d)
elseif max == b then
h = (r - g) + d * 4
h = h / (6 * d)
end
return h, s, v
end
function hsvtorgb(h, s, v)
local r, g, b, i, f, p, q, t, z
i = math.floor(h * 6)
f = h * 6 - i
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
z = i % 6
if z == 0 then
r = v
g = t
b = p
elseif z == 1 then
r = q
g = v
b = p
elseif z == 2 then
r = p
g = v
b = t
elseif z == 3 then
r = p
g = q
b = v
elseif z == 4 then
r = t
g = p
b = v
elseif z == 5 then
r = v
g = p
b = q
end
r = math.round(r * 255)
g = math.round(g * 255)
b = math.round(b * 255)
return r, g, b
end
function rgbstep(r, g, b, up, step)
local h, s, v = rgbtohsv(r, g, b)
local v2 = up and math.min(1, v + step) or math.max(0, v - step)
if v == v2 then
return
end
return hsvtorgb(h, s, v2)
end
while true do
r, g, b = rgbstep(r, g, b, up, step)
if r then
grp.write(raddr, r, dt.uint8)
grp.write(gaddr, g, dt.uint8)
grp.write(baddr, b, dt.uint8)
else
break
end
os.sleep(delay)
newvalue = grp.getvalue(event.dst)
if value ~= newvalue then
break
end
end