Skip to content

Instantly share code, notes, and snippets.

@frah
Created Oct 11, 2013
Embed
What would you like to do?
LimitlessLED control test with Lua on Nginx
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>LimitlessLED control</title>
<style type="text/css">
html {
-webkit-user-select: none;
-webkit-background-size: 10px 10px;
-moz-background-size: 10px 10px;
background-size: 10px 10px;
background-color: #333333;
background-image: -webkit-gradient(linear, 0 0, 100% 0, color-stop(.5, #3c3c3c), color-stop(.5, transparent), to(transparent));
background-image: -moz-linear-gradient(left, #3c3c3c 50%, transparent 50%, transparent);
background-image: -o-linear-gradient(left, #3c3c3c 50%, transparent 50%, transparent);
background-image: linear-gradient(left, #3c3c3c 50%, transparent 50%, transparent);
text-align: center;
color: #ddd;
}
input[type='range'] {
-webkit-appearance: none;
width: 90%;
height:30px;
}
#color-bar {
background: -webkit-linear-gradient(left, #0000ff 0%, #00ffff 17%, #00ff00 33%, #ffff00 50%, #ff0000 67%, #ff00ff 83%, #0000ff 100%);
}
#bright-bar {
background: -webkit-linear-gradient(left, #000 0%, #fff 100%);
}
input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none;
border: #000 1px solid;
background-color: #fff;
width: 15px;
height: 30px;
}
.btn {
background: #EEE;
border: 1px solid #DDD;
border-radius: 20px;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
color: #111;
width: 100px;
padding: 10px 0;
}
</style>
<script type="text/javascript">
function sendReq(com, opt) {
var arg = opt || '00';
var a = new XMLHttpRequest();
a.open('GET', '/led?cmd='+com+'&arg='+arg, true);
a.send('');
}
function powerOff() {
sendReq('41');
}
function powerOn() {
sendReq('42');
}
function changeColor(slider) {
sendReq('40', Number(slider.value).toString(16));
}
function toWhite() {
sendReq('C2');
}
function changeBright(slider) {
sendReq('4E', Number(slider.value).toString(16));
}
</script>
</head>
<body>
<h1>LimitlessLED control</h1>
<p>
<button class="btn" onclick="powerOn()">ON</button>&nbsp;
<button class="btn" onclick="powerOff()">OFF</button>
</p>
<p>
<input type="range" id="color-bar" max="255" min="0" onchange="changeColor(this)">
</p>
<p>
<button class="btn" onclick="toWhite()">White</button>
</p>
<p>
<input type="range" id="bright-bar" min="0" max="59" onchange="changeBright(this)">
</p>
</body>
</html>
target = '192.168.0.1'
local args = ngx.req.get_uri_args()
if args["cmd"] == nil then
io.input("led.html")
while true do
line = io.read()
if line == nil then break end
ngx.print(line)
end
io.close()
ngx.flush()
ngx.exit(ngx.HTTP_OK)
end
com = args["cmd"]
if args["arg"] == nil then
arg = '00'
else
arg = args["arg"]
end
local sock = ngx.socket.udp()
local ok, err = sock:setpeername(target, 8899)
if not ok then
ngx.say("failed to connect to LimitlessLED Terminal: ", err)
return
end
local command = string.char(tonumber(com, 16), tonumber(arg, 16), 0x55)
ok, err = sock:send(command)
sock:close()
if not ok then
ngx.say("send failed: ", err)
else
ngx.say("send successed: 0x"..com.." 0x"..arg.." 0x55")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment