Lua - 如何根据灯泡的亮度级别计算它使用的瓦特数?

我收到了以下代码作为计算hue灯泡根据其型号和亮度级别使用的瓦特数的示例。

if hue_model =="LWB010" and hue_state == true then
   if hue_level <= 0 then watt = 0.4 end
   if (hue_level > 0 and hue_level <= 6) then watt = 1.6 end
   if (hue_level > 6 and hue_level <= 13) then watt = 1.7 end
   if (hue_level > 13 and hue_level <= 20) then watt = 1.8 end
   if (hue_level > 20 and hue_level <= 26) then watt = 1.9 end
   if (hue_level > 26 and hue_level <= 33) then watt = 2.2 end
   if (hue_level > 33 and hue_level <= 40) then watt = 2.4 end
   if (hue_level > 40 and hue_level <= 46) then watt = 2.7 end
   if (hue_level > 46 and hue_level <= 53) then watt = 3.0 end
   if (hue_level > 53 and hue_level <= 60) then watt = 3.5 end
   if (hue_level > 60 and hue_level <= 66) then watt = 3.9 end
   if (hue_level > 66 and hue_level <= 73) then watt = 4.5 end
   if (hue_level > 73 and hue_level <= 80) then watt = 5.1 end
   if (hue_level > 73 and hue_level <= 86) then watt = 5.5 end
   if (hue_level > 86 and hue_level <= 93) then watt = 6.4 end
   if (hue_level > 93) then watt = 6.7 end
   print(watt)
    else
    print("灯光已关闭")
    end

由于Hue API返回的亮度范围似乎从0-100更改为0-255,因此需要进行更新。在这项工作的一部分,我想让它更高效(代码更少)并且更精确。

逻辑表明,当灯光全开时(255),它将使用6.7瓦特,如果它处于50%的状态(127),那么它将降至约(3.35瓦特)

可惜,尝试各种事物后,我一直失败得很厉害,并发现自己回到了类似于原始查找类型表格的东西上。肯定有更好/更清洁的方法?

如果有的话,请有人分享他们会做什么。

非常感谢...

点赞
用户12110258
用户12110258

通常情况下,应该使用 Lua math 操作来执行此任务。但是,为了使它更容易,我只是将现有的表格复制到了新的格式中。

local wattTable = {
        [ 1 ] = 1.6 ,
        [ 2 ] = 1.7 ,
        [ 3 ] = 1.8 ,
        [ 4 ] = 1.9 ,
        [ 5 ] = 2.2 ,
        [ 6 ] = 2.4 ,
        [ 7 ] = 2.7 ,
        [ 8 ] = 3.0 ,
        [ 9 ] = 3.5 ,
        [ 10 ] = 3.9 ,
        [ 11 ] = 4.5 ,
        [ 12 ] = 5.1 ,
        [ 13 ] = 5.5 ,
        [ 14 ] = 6.4 ,
}

以前,您使用了约为 6.5 的跳跃,这在新比例尺上大约相当于 16.5。我们可以使用 math.floor(hue/16.5) 来获取此表格的索引。

但是,小于 0 和大于 240 的值将超出表格的边界(边界为 1 到 14)。因此,我们可以进行一些检查:

local index = math.floor(hue / 16.5)
if index <= 0 then
    return 0.4
elseif index >= 15 then
    return 6.7

在那之后,我们只需要从表格中读取:

else
    return wattTable[index]
end

这是完成的代码:

local wattTable = {
        [ 1 ] = 1.6 ,
        [ 2 ] = 1.7 ,
        [ 3 ] = 1.8 ,
        [ 4 ] = 1.9 ,
        [ 5 ] = 2.2 ,
        [ 6 ] = 2.4 ,
        [ 7 ] = 2.7 ,
        [ 8 ] = 3.0 ,
        [ 9 ] = 3.5 ,
        [ 10 ] = 3.9 ,
        [ 11 ] = 4.5 ,
        [ 12 ] = 5.1 ,
        [ 13 ] = 5.5 ,
        [ 14 ] = 6.4 ,
}

function getWattage(hue)
    local index = math.floor(hue / 16.5)
    if index <= 0 then
        return 0.4
    elseif index >= 15 then
        return 6.7
    else
        return wattTable[index]
    end
end

这是使用它的示例:

print(getWattage(150))

总结:使用表格和一些 if 条件,我们创建了一个函数,该函数接受所有数字作为有效值(防弹),并允许我们将色调值的范围从 0-100(6.5)调整为 0-255(16.5)。

2021-03-08 00:36:03
用户2858170
用户2858170

首先,您应该检查您的数据。

根据数据表,飞利浦Hue White LWB010的最大功率为9W。

在大多数情况下,将数据削减到93%的6.7W对我来说似乎不太聪明。

我查看了另一个来源,根据总体呈现,我会更信任它的数字。

https://www.reddit.com/r/Hue/comments/8kvcsi/power_consumption_hue_white_a60_lwb010/

enter image description here

根据这些测量结果,您的93%约偏差2W,而且您在这里夹值时,您的100%功率偏差几乎为4W!

现在进入编码部分:

查看图形后,我认为指数函数将给出很好的近似值。

enter image description here

local wattsFromDim = {
  LWB010 = function (dim) return 0.43981651 * math.exp(0.012712893 * dim) end
}
-- how to use:
local watts = wattsFromDim.LWB010(128)

这是有意义的,因为人眼亮度感知是对数的。

因此,为了提供线性调光(您希望50%的亮度是100%的一半),输出功率必须按照对数的倒数,即指数函数,进行跟随。

2021-03-08 07:16:02