Unverified Commit f7b8cf01 authored by tomsci's avatar tomsci Committed by GitHub
Browse files

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

Fixes #2880
parent a8b46af9
...@@ -139,7 +139,7 @@ static int lgpio_read (lua_State *L) ...@@ -139,7 +139,7 @@ static int lgpio_read (lua_State *L)
static int lgpio_trig (lua_State *L) static int lgpio_trig (lua_State *L)
{ {
int gpio = luaL_checkint (L, 1); 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)) if (!lua_isnoneornil (L, 3))
luaL_checkanyfunction (L, 3); luaL_checkanyfunction (L, 3);
...@@ -156,7 +156,15 @@ static int lgpio_trig (lua_State *L) ...@@ -156,7 +156,15 @@ static int lgpio_trig (lua_State *L)
} }
// Set/update interrupt callback // 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]); luaL_unref (L, LUA_REGISTRYINDEX, gpio_cb_refs[gpio]);
gpio_cb_refs[gpio] = luaL_ref (L, LUA_REGISTRYINDEX); gpio_cb_refs[gpio] = luaL_ref (L, LUA_REGISTRYINDEX);
...@@ -165,7 +173,7 @@ static int lgpio_trig (lua_State *L) ...@@ -165,7 +173,7 @@ static int lgpio_trig (lua_State *L)
// Disable interrupt while reconfiguring // Disable interrupt while reconfiguring
check_err (L, gpio_intr_disable (gpio)); 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_set_intr_type (gpio, GPIO_INTR_DISABLE));
check_err (L, gpio_isr_handler_remove (gpio)); check_err (L, gpio_isr_handler_remove (gpio));
......
...@@ -67,17 +67,18 @@ Read digital GPIO pin value. ...@@ -67,17 +67,18 @@ Read digital GPIO pin value.
Establish or clear a callback function to run on interrupt for a GPIO. Establish or clear a callback function to run on interrupt for a GPIO.
#### Syntax #### Syntax
`gpio.trig(pin, type [, callback])` `gpio.trig(pin [, type [, callback]])`
#### Parameters #### Parameters
- `pin`, see [GPIO Overview](#gpio-overview) - `pin`, see [GPIO Overview](#gpio-overview)
- `type` trigger type, one of - `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_UP` for trigger on rising edge
- `gpio.INTR_DOWN` for trigger on falling edge - `gpio.INTR_DOWN` for trigger on falling edge
- `gpio.INTR_UP_DOWN` for trigger on both edges - `gpio.INTR_UP_DOWN` for trigger on both edges
- `gpio.INTR_LOW` for trigger on low level - `gpio.INTR_LOW` for trigger on low level
- `gpio.INTR_HIGH` for trigger on high 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` - `pin`
- `level` - `level`
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment