Commit 5595ca35 authored by TerryE's avatar TerryE
Browse files

Small bugfix to GPIO.c -- Oops!

parent eff1b6fb
...@@ -62,22 +62,27 @@ static void gpio_intr_callback_task (task_param_t param, uint8 priority) ...@@ -62,22 +62,27 @@ static void gpio_intr_callback_task (task_param_t param, uint8 priority)
static int lgpio_trig( lua_State* L ) static int lgpio_trig( lua_State* L )
{ {
unsigned pin = luaL_checkinteger( L, 1 ); 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[] = { static const int opts_type[] = {
GPIO_PIN_INTR_DISABLE, GPIO_PIN_INTR_POSEDGE, GPIO_PIN_INTR_NEGEDGE, GPIO_PIN_INTR_DISABLE, GPIO_PIN_INTR_POSEDGE, GPIO_PIN_INTR_NEGEDGE,
GPIO_PIN_INTR_ANYEDGE, GPIO_PIN_INTR_LOLEVEL, GPIO_PIN_INTR_HILEVEL 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"); 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) { if (type != GPIO_PIN_INTR_DISABLE) {
int cbtype = lua_type(L, 3); 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"); "invalid callback type");
lua_pushvalue(L, 3); // copy argument (func) to the top of stack lua_pushvalue(L, 3); // copy argument (func) to the top of stack
gpio_cb_ref[pin] = luaL_ref(L, LUA_REGISTRYINDEX); 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", 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]); 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); platform_gpio_intr_init(pin, type);
......
...@@ -98,18 +98,18 @@ gpio.serout(1,1,{8,18},8) -- serial 30% pwm 38k, lasts 8 cycles ...@@ -98,18 +98,18 @@ gpio.serout(1,1,{8,18},8) -- serial 30% pwm 38k, lasts 8 cycles
## gpio.trig() ## gpio.trig()
Establish a callback function to run on interrupt for a pin. Establish or clear a callback function to run on interrupt for a pin.
There is currently no support for unregistering the callback.
This function is not available if GPIO_INTERRUPT_ENABLE was undefined at compile time. This function is not available if GPIO_INTERRUPT_ENABLE was undefined at compile time.
#### Syntax #### Syntax
`gpio.trig(pin, type [, function(level)])` `gpio.trig(pin, [type [, function(level)]})`
#### Parameters #### Parameters
- `pin` **1~12**, IO index, pin D0 does not support interrupt. - `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. - `function(level)` callback function when triggered. The gpio level is the param. Use previous callback function if undefined here.
#### Returns #### Returns
...@@ -131,6 +131,9 @@ function pin1cb(level) ...@@ -131,6 +131,9 @@ function pin1cb(level)
end end
gpio.trig(pin, "down", pin1cb) gpio.trig(pin, "down", pin1cb)
```
```Lua
gpio.trig(4, 'none') -- remove any existing trigger
``` ```
#### See also #### See also
[`gpio.mode()`](#gpiomode) [`gpio.mode()`](#gpiomode)
......
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