forked from InfernoEmbedded/esp-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton-led.lua
39 lines (30 loc) · 992 Bytes
/
button-led.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
buttonUser = 1
red = 3
green = 4
blue = 5
state = false
gpio.mode(red, gpio.OUTPUT)
gpio.mode(green, gpio.OUTPUT)
gpio.mode(blue, gpio.OUTPUT)
gpio.write(green, gpio.LOW)
gpio.write(blue, gpio.LOW)
-- Debounce code based on
-- https://gist.github.com/marcelstoer/59563e791effa4acb65f
function debounce (func)
local last = 0
local delay = 200 * 1000 -- 200ms * 1000 as tmr.now() has μs resolution
return function (...)
local now = tmr.now()
local delta = now - last
if delta < 0 then delta = delta + 2147483647 end; -- proposed because of delta rolling over, https://github.com/hackhitchin/esp8266-co-uk/issues/2
if delta < delay then return end;
last = now
return func(...)
end
end
local function buttonUserCallback(level)
state = not state
gpio.write(red, state and gpio.HIGH or gpio.LOW)
end
gpio.mode(buttonUser, gpio.INT, gpio.PULLUP)
gpio.trig(buttonUser, "down", debounce(buttonUserCallback))