Skip to content

Commit f7b8cf0

Browse files
authored
Unref gpio.trig callbacks when type=INTR_DISABLE (#3072)
Fixes #2880
1 parent a8b46af commit f7b8cf0

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

components/modules/gpio.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static int lgpio_read (lua_State *L)
139139
static int lgpio_trig (lua_State *L)
140140
{
141141
int gpio = luaL_checkint (L, 1);
142-
int intr_type = luaL_checkint (L, 2);
142+
int intr_type = luaL_optint (L, 2, GPIO_INTR_DISABLE);
143143
if (!lua_isnoneornil (L, 3))
144144
luaL_checkanyfunction (L, 3);
145145

@@ -156,7 +156,15 @@ static int lgpio_trig (lua_State *L)
156156
}
157157

158158
// Set/update interrupt callback
159-
if (!lua_isnoneornil (L, 3))
159+
160+
// For compatibility with esp8266 API, passing in a non-zero intr_type and a
161+
// nil callback preserves any existing callback that has been set.
162+
if (intr_type == GPIO_INTR_DISABLE)
163+
{
164+
luaL_unref (L, LUA_REGISTRYINDEX, gpio_cb_refs[gpio]);
165+
gpio_cb_refs[gpio] = LUA_NOREF;
166+
}
167+
else if (!lua_isnoneornil (L, 3))
160168
{
161169
luaL_unref (L, LUA_REGISTRYINDEX, gpio_cb_refs[gpio]);
162170
gpio_cb_refs[gpio] = luaL_ref (L, LUA_REGISTRYINDEX);
@@ -165,7 +173,7 @@ static int lgpio_trig (lua_State *L)
165173
// Disable interrupt while reconfiguring
166174
check_err (L, gpio_intr_disable (gpio));
167175

168-
if (gpio_cb_refs[gpio] == LUA_NOREF)
176+
if (intr_type == GPIO_INTR_DISABLE)
169177
{
170178
check_err (L, gpio_set_intr_type (gpio, GPIO_INTR_DISABLE));
171179
check_err (L, gpio_isr_handler_remove (gpio));

docs/modules/gpio.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,18 @@ Read digital GPIO pin value.
6767
Establish or clear a callback function to run on interrupt for a GPIO.
6868

6969
#### Syntax
70-
`gpio.trig(pin, type [, callback])`
70+
`gpio.trig(pin [, type [, callback]])`
7171

7272
#### Parameters
7373
- `pin`, see [GPIO Overview](#gpio-overview)
7474
- `type` trigger type, one of
75+
- `gpio.INTR_DISABLE` or `nil` to disable interrupts on this pin (in which case `callback` is ignored and should be `nil` or omitted)
7576
- `gpio.INTR_UP` for trigger on rising edge
7677
- `gpio.INTR_DOWN` for trigger on falling edge
7778
- `gpio.INTR_UP_DOWN` for trigger on both edges
7879
- `gpio.INTR_LOW` for trigger on low level
7980
- `gpio.INTR_HIGH` for trigger on high level
80-
- `callback` optional function to be called when trigger fires, trigger is disabled when omitted. Parameters are:
81+
- `callback` optional function to be called when trigger fires. If `nil` or omitted (and `type` is not `gpio.INTR_DISABLE`) then any previously-set callback will continue to be used. Parameters are:
8182
- `pin`
8283
- `level`
8384

0 commit comments

Comments
 (0)