-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm_srv_blind.lua
96 lines (92 loc) · 4.65 KB
/
m_srv_blind.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
hc_server_responce=("No command "..pl.." found\n-options are blind_[1-5]_[close[_delay]|open[_delay]|status|toggle[_delay]|set_[0-1000]|laststate_|speedUp|")
if (string.find(pl,'blind_[1-5]_close') and type(m["blind_"..tonumber(string.sub(pl, 7,7)).."_pin"]) == "number") then
BLINDno=tonumber(string.sub(pl, 7,7))
delay=tonumber(string.sub(pl, 15))
hc_server_responce=("blind_"..BLINDno.."=closed")
MoveServo(BLINDno,m["blind_"..BLINDno.."_min"],delay) m["blind_"..BLINDno.."_state"]="closed"
elseif (string.find(pl,'blind_[1-5]_open') and type(m["blind_"..tonumber(string.sub(pl, 7,7)).."_pin"]) == "number") then
BLINDno=tonumber(string.sub(pl, 7,7))
delay=tonumber(string.sub(pl, 14))
hc_server_responce=("blind_"..BLINDno.."=open")
MoveServo(BLINDno,m["blind_"..BLINDno.."_max"],delay) m["blind_"..BLINDno.."_state"]="open"
elseif string.find(pl,'blind_[1-5]_status$') then
BLINDno=tonumber(string.sub(pl, 7,7))
hc_server_responce=("blind_"..BLINDno.."_position="..m["blind_"..BLINDno.."_position"])
elseif string.find(pl,'blind_[1-5]_set_') then
BLINDno=tonumber(string.sub(pl, 7,7))
hc_server_responce=("blind_"..BLINDno.."_position="..string.sub(pl, 13,15))
MoveServo(BLINDno,tonumber(string.sub(pl, 13,15)))
elseif (string.find(pl,'blind_[1-5]_toggle') and type(m["blind_"..tonumber(string.sub(pl, 7,7)).."_pin"]) == "number") then
BLINDno=tonumber(string.sub(pl, 7,7))
delay=tonumber(string.sub(pl, 16))
if m["blind_"..BLINDno.."_state"]=="closed" then
hc_server_responce=("blind_"..BLINDno.."=open")
MoveServo(BLINDno,m["blind_"..BLINDno.."_max"],delay)
m["blind_"..BLINDno.."_state"]="open"
else
hc_server_responce=("blind_"..BLINDno.."=closed")
MoveServo(BLINDno,m["blind_"..BLINDno.."_min"],delay)
m["blind_"..BLINDno.."_state"]="closed"
end
elseif string.find(pl,'blind_[1-5]_speedUp$') then
BLINDno=tonumber(string.sub(pl, 7,7)) hc_server_responce=("blind_"..BLINDno.."=not moving")
if _G["blind_tmr_"..BLINDno] ~= nil then
_G["blind_tmr_"..BLINDno]:interval(m["blind_"..BLINDno.."_speedUp"])
if m["blind_"..BLINDno.."_state"]=="closed" then
hc_server_responce=("blind_"..BLINDno.."_position="..m["blind_"..BLINDno.."_min"])
else
hc_server_responce=("blind_"..BLINDno.."_position="..m["blind_"..BLINDno.."_max"])
end
if m["blind_"..BLINDno.."_laststate"]=="true" and m["blind_"..BLINDno.."_lastvalue"] and m["blind_"..BLINDno.."_state"]=="open" then
m.blind_target=m["blind_"..BLINDno.."_lastvalue"]
hc_server_responce=("blind_"..BLINDno.."_position="..m["blind_"..BLINDno.."_lastvalue"])
end
end
elseif string.find(pl,'blind_[1-5]_stop$') then
BLINDno=tonumber(string.sub(pl, 7,7)) hc_server_responce=("blind_"..BLINDno.."_not moving")
if _G["blind_tmr_"..BLINDno] ~= nil then
_G["blind_tmr_"..BLINDno]:unregister()
_G["blind_tmr_"..BLINDno]=nil
pwm.close(m["blind_"..BLINDno.."_pin"])
hc_server_responce=("blind_"..BLINDno.."_position="..m["blind_"..BLINDno.."_position"])
end
end
if m.blind=="enabled" then
m.blind="started"
function MoveServo(BLINDno,target,delay)
m["blind_"..BLINDno.."_target"]=target
if delay then delay=delay*10 end
if (_G["blind_tmr_"..BLINDno] == nil) then
_G["blind_tmr_"..BLINDno] = tmr.create()
pwm.setup(m["blind_"..BLINDno.."_pin"], m["blind_"..BLINDno.."_freq"], m["blind_"..BLINDno.."_position"]) -- for walkera (56---240)
--pwm.setup(m["blind_"..BLINDno.."_pin"], m["blind_freq"], m["blind_"..BLINDno.."_position"]) -- for walkera (56---240)
end
_G["blind_tmr_"..BLINDno]:register(m["blind_"..BLINDno.."_speed"], tmr.ALARM_AUTO, function (t)
if m["blind_"..BLINDno.."_target"] == m["blind_"..BLINDno.."_position"] and m["blind_"..BLINDno.."_cont"] ~= "true" then
if delay then
delay=delay-1
if delay==1 then delay=nil end
else
t:unregister()
pwm.close(m["blind_"..BLINDno.."_pin"])
_G["blind_tmr_"..BLINDno]=nil
end
elseif m["blind_"..BLINDno.."_target"] > m["blind_"..BLINDno.."_position"] then
m["blind_"..BLINDno.."_position"]=m["blind_"..BLINDno.."_position"]+1
elseif m["blind_"..BLINDno.."_target"] < m["blind_"..BLINDno.."_position"] then
m["blind_"..BLINDno.."_position"]=m["blind_"..BLINDno.."_position"]-1
end
pwm.setduty(m["blind_"..BLINDno.."_pin"],m["blind_"..BLINDno.."_position"])
end)
_G["blind_tmr_"..BLINDno]:start()
end
for var,val in pairs(m) do
local _,_,blnd = string.find(var, "blind_(%d)_pin")
if blnd then
MoveServo(blnd,m["blind_"..blnd.."_position"],1)
end
end
end
--dofile("m_hc_server_blind.lua")
--node.compile("m_hc_server_blind.lua")
--print(node.heap())