Unref gpio.trig callbacks when type=INTR_DISABLE (#3072)

Fixes #2880
This commit is contained in:
tomsci 2020-04-27 13:29:08 +01:00 committed by GitHub
parent a8b46af905
commit f7b8cf018e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

@ -139,7 +139,7 @@ static int lgpio_read (lua_State *L)
static int lgpio_trig (lua_State *L)
{
int gpio = luaL_checkint (L, 1);
int intr_type = luaL_checkint (L, 2);
int intr_type = luaL_optint (L, 2, GPIO_INTR_DISABLE);
if (!lua_isnoneornil (L, 3))
luaL_checkanyfunction (L, 3);
@ -156,7 +156,15 @@ static int lgpio_trig (lua_State *L)
}
// Set/update interrupt callback
if (!lua_isnoneornil (L, 3))
// For compatibility with esp8266 API, passing in a non-zero intr_type and a
// nil callback preserves any existing callback that has been set.
if (intr_type == GPIO_INTR_DISABLE)
{
luaL_unref (L, LUA_REGISTRYINDEX, gpio_cb_refs[gpio]);
gpio_cb_refs[gpio] = LUA_NOREF;
}
else if (!lua_isnoneornil (L, 3))
{
luaL_unref (L, LUA_REGISTRYINDEX, gpio_cb_refs[gpio]);
gpio_cb_refs[gpio] = luaL_ref (L, LUA_REGISTRYINDEX);
@ -165,7 +173,7 @@ static int lgpio_trig (lua_State *L)
// Disable interrupt while reconfiguring
check_err (L, gpio_intr_disable (gpio));
if (gpio_cb_refs[gpio] == LUA_NOREF)
if (intr_type == GPIO_INTR_DISABLE)
{
check_err (L, gpio_set_intr_type (gpio, GPIO_INTR_DISABLE));
check_err (L, gpio_isr_handler_remove (gpio));

View File

@ -67,17 +67,18 @@ Read digital GPIO pin value.
Establish or clear a callback function to run on interrupt for a GPIO.
#### Syntax
`gpio.trig(pin, type [, callback])`
`gpio.trig(pin [, type [, callback]])`
#### Parameters
- `pin`, see [GPIO Overview](#gpio-overview)
- `type` trigger type, one of
- `gpio.INTR_DISABLE` or `nil` to disable interrupts on this pin (in which case `callback` is ignored and should be `nil` or omitted)
- `gpio.INTR_UP` for trigger on rising edge
- `gpio.INTR_DOWN` for trigger on falling edge
- `gpio.INTR_UP_DOWN` for trigger on both edges
- `gpio.INTR_LOW` for trigger on low level
- `gpio.INTR_HIGH` for trigger on high level
- `callback` optional function to be called when trigger fires, trigger is disabled when omitted. Parameters are:
- `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:
- `pin`
- `level`