-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc_switch.lua
More file actions
98 lines (83 loc) · 1.74 KB
/
Copy pathrc_switch.lua
File metadata and controls
98 lines (83 loc) · 1.74 KB
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
97
rc_switch = {}
local pin
local plen
local rep
local cmd_strings = {
{ on = "FFF0FFFF0101", off = "FFF0FFFF0110" },
{ on = "FFF0FFFF1001", off = "FFF0FFFF1010" },
{ on = "FFF0FFFF0001", off = "FFF0FFFF0010" },
{ on = "FFF0FF1F0001", off = "FFF0FF1F0010" },
{ on = "FFF0F1FF0001", off = "FFF0F1FF0010" },
}
--[[ "zero" bit:
_ _
| |___| |___
--]]
local append_zero = function(tab, pl)
local i
for i = 1,2 do
table.insert(tab, pl)
table.insert(tab, pl * 3)
end
end
--[[ "one" bit:
___ ___
| |_| |_
--]]
local append_one = function(tab, pl)
local i
for i = 1,2 do
table.insert(tab, pl * 3)
table.insert(tab, pl)
end
end
--[[ "F" bit:
_ ___
| |___| |_
--]]
local append_f = function(tab, pl)
table.insert(tab, pl)
table.insert(tab, pl * 3)
table.insert(tab, pl * 3)
table.insert(tab, pl)
end
--[[ sync:
_
| |__ ... one pulse HIGH, then LOW 31 * pulse_length
--]]
local append_sync = function(tab, pl)
table.insert(tab, pl)
table.insert(tab, pl * 31)
end
local transitions = function(cmd, pl)
local result = {}
local len = #cmd
local i, k
for i = 1,len do
local k = cmd:sub(i,i)
if k == '0' then
append_zero(result, pl)
elseif k == '1' then
append_one(result, pl)
elseif k == 'F' then
append_f(result, pl)
end
end
append_sync(result, pl)
return result
end
-- Params:
-- p: pin number
-- l: pulse length
-- r: send frame this number of times
rc_switch.init = function(p, l, r)
pin = p or 3
plen = l or 160
rep = r or 3
gpio.mode(pin, gpio.OUTPUT, gpio.PULLUP)
end
rc_switch.switch = function(button, position)
local s = transitions(cmd_strings[button][position], plen)
gpio.serout(pin, 1, s, rep)
end
return rc_switch