Lua - 如何根据灯泡的亮度级别计算它使用的瓦特数?
2021-3-7 22:42:39
收藏:0
阅读:182
评论:2
我收到了以下代码作为计算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瓦特)
可惜,尝试各种事物后,我一直失败得很厉害,并发现自己回到了类似于原始查找类型表格的东西上。肯定有更好/更清洁的方法?
如果有的话,请有人分享他们会做什么。
非常感谢...
点赞
用户2858170
首先,您应该检查您的数据。
根据数据表,飞利浦Hue White LWB010的最大功率为9W。
在大多数情况下,将数据削减到93%的6.7W对我来说似乎不太聪明。
我查看了另一个来源,根据总体呈现,我会更信任它的数字。
https://www.reddit.com/r/Hue/comments/8kvcsi/power_consumption_hue_white_a60_lwb010/
根据这些测量结果,您的93%约偏差2W,而且您在这里夹值时,您的100%功率偏差几乎为4W!
现在进入编码部分:
查看图形后,我认为指数函数将给出很好的近似值。
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
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?



通常情况下,应该使用 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)。