diff --git a/app/modules/gpio.c b/app/modules/gpio.c index 9991c908..e67838a3 100644 --- a/app/modules/gpio.c +++ b/app/modules/gpio.c @@ -62,22 +62,27 @@ static void gpio_intr_callback_task (task_param_t param, uint8 priority) static int lgpio_trig( lua_State* L ) { unsigned pin = luaL_checkinteger( L, 1 ); - static const char * const opts[] = {"", "up", "down", "both", "low", "high"}; + int old_pin_ref = gpio_cb_ref[pin]; + static const char * const opts[] = {"none", "up", "down", "both", "low", "high", NULL}; static const int opts_type[] = { GPIO_PIN_INTR_DISABLE, GPIO_PIN_INTR_POSEDGE, GPIO_PIN_INTR_NEGEDGE, GPIO_PIN_INTR_ANYEDGE, GPIO_PIN_INTR_LOLEVEL, GPIO_PIN_INTR_HILEVEL }; - int type = opts_type[luaL_checkoption(L, 2, "", opts)]; - luaL_argcheck(L, platform_gpio_exists(pin) && pin>0, 1, "Invalid interrupt pin"); + int type = opts_type[luaL_checkoption(L, 2, "none", opts)]; if (type != GPIO_PIN_INTR_DISABLE) { int cbtype = lua_type(L, 3); - luaL_argcheck(L, cbtype == LUA_TFUNCTION || cbtype == LUA_TLIGHTFUNCTION, 3, + luaL_argcheck(L, cbtype == LUA_TFUNCTION || cbtype == LUA_TLIGHTFUNCTION, 3, "invalid callback type"); lua_pushvalue(L, 3); // copy argument (func) to the top of stack gpio_cb_ref[pin] = luaL_ref(L, LUA_REGISTRYINDEX); + } else { + gpio_cb_ref[pin] = LUA_NOREF; } + + if(old_pin_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, old_pin_ref); + NODE_DBG("Pin data: %d %d %08x, %d %d %d, %08x\n", pin, type, pin_mux[pin], pin_num[pin], pin_func[pin], pin_int_type[pin], gpio_cb_ref[pin]); platform_gpio_intr_init(pin, type); diff --git a/docs/en/modules/gpio.md b/docs/en/modules/gpio.md index d76e05c4..d22af361 100644 --- a/docs/en/modules/gpio.md +++ b/docs/en/modules/gpio.md @@ -98,18 +98,18 @@ gpio.serout(1,1,{8,18},8) -- serial 30% pwm 38k, lasts 8 cycles ## gpio.trig() -Establish a callback function to run on interrupt for a pin. - -There is currently no support for unregistering the callback. +Establish or clear a callback function to run on interrupt for a pin. This function is not available if GPIO_INTERRUPT_ENABLE was undefined at compile time. #### Syntax -`gpio.trig(pin, type [, function(level)])` +`gpio.trig(pin, [type [, function(level)]})` #### Parameters - `pin` **1~12**, IO index, pin D0 does not support interrupt. -- `type` "up", "down", "both", "low", "high", which represent rising edge, falling edge, both edge, low level, high level trig mode correspondingly. +- `type` "up", "down", "both", "low", "high", which represent rising edge, falling edge, both edge, +low level, high level trig mode correspondingly. If the type is "none" or omitted then the +callback function is removed and the interrupt disabled. - `function(level)` callback function when triggered. The gpio level is the param. Use previous callback function if undefined here. #### Returns @@ -131,6 +131,9 @@ function pin1cb(level) end gpio.trig(pin, "down", pin1cb) +``` +```Lua +gpio.trig(4, 'none') -- remove any existing trigger ``` #### See also [`gpio.mode()`](#gpiomode)