Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
6822116d
Commit
6822116d
authored
Mar 19, 2016
by
Philip Gladstone
Browse files
Merge pull request #1136 from TerryE/dev-quick-gpio-bugfix
one line bugfix to GPIO.c -- Oops! (Finally!)
parents
3d508591
20465dd3
Changes
2
Show whitespace changes
Inline
Side-by-side
app/modules/gpio.c
View file @
6822116d
...
@@ -63,22 +63,38 @@ static void gpio_intr_callback_task (task_param_t param, uint8 priority)
...
@@ -63,22 +63,38 @@ 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"
};
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"
);
if
(
type
!=
GPIO_PIN_INTR_DISABLE
)
{
int
old_pin_ref
=
gpio_cb_ref
[
pin
];
int
cbtype
=
lua_type
(
L
,
3
);
int
type
=
opts_type
[
luaL_checkoption
(
L
,
2
,
"none"
,
opts
)];
luaL_argcheck
(
L
,
cbtype
==
LUA_TFUNCTION
||
cbtype
==
LUA_TLIGHTFUNCTION
,
3
,
"invalid callback type"
);
if
(
type
==
GPIO_PIN_INTR_DISABLE
)
{
lua_pushvalue
(
L
,
3
);
// copy argument (func) to the top of stack
// "none" clears the callback
gpio_cb_ref
[
pin
]
=
LUA_NOREF
;
}
else
if
(
lua_gettop
(
L
)
==
2
&&
old_pin_ref
!=
LUA_NOREF
)
{
// keep the old one if no callback
old_pin_ref
=
LUA_NOREF
;
}
else
if
(
lua_type
(
L
,
3
)
==
LUA_TFUNCTION
||
lua_type
(
L
,
3
)
==
LUA_TLIGHTFUNCTION
)
{
// set up the new callback if present
lua_pushvalue
(
L
,
3
);
gpio_cb_ref
[
pin
]
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
gpio_cb_ref
[
pin
]
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
}
else
{
// invalid combination, so clear down any old callback and throw an error
if
(
old_pin_ref
!=
LUA_NOREF
)
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
old_pin_ref
);
luaL_argcheck
(
L
,
0
,
3
,
"invalid callback type"
);
}
}
// unreference any overwritten callback
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
);
...
...
docs/en/modules/gpio.md
View file @
6822116d
...
@@ -98,19 +98,21 @@ gpio.serout(1,1,{8,18},8) -- serial 30% pwm 38k, lasts 8 cycles
...
@@ -98,19 +98,21 @@ 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 [,
callback_
function
]
])`
#### Parameters
#### Parameters
-
`pin`
**1~12**
, IO index, pin D0 does not support interrupt.
-
`pin`
**1-12**
, pin to trigger on, IO index. Note that pin 0 does not support interrupts.
-
`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
-
`function(level)`
callback function when triggered. The gpio level is the param. Use previous callback function if undefined here.
edges
*, *
low level
*, and *
high level
*
trigger modes respectivey. If the type is "none" or omitted
then the callback function is removed and the interrupt is disabled.
-
`callback_function(level)`
callback function when trigger occurs. The level of the specified pin
at the interrupt passed as the parameter to the callback. The previous callback function will be
used if the function is omitted.
#### Returns
#### Returns
`nil`
`nil`
...
@@ -118,20 +120,20 @@ This function is not available if GPIO_INTERRUPT_ENABLE was undefined at compile
...
@@ -118,20 +120,20 @@ This function is not available if GPIO_INTERRUPT_ENABLE was undefined at compile
#### Example
#### Example
```
lua
```
lua
-- use pin 1 as the input pulse width counter
do
pin
=
1
-- use pin 1 as the input pulse width counter
pulse1
=
0
local
pin
,
pulse1
,
du
,
now
,
trig
=
1
,
0
,
0
,
tmr
.
now
,
gpio
.
trig
du
=
0
gpio
.
mode
(
pin
,
gpio
.
INT
)
gpio
.
mode
(
pin
,
gpio
.
INT
)
local
function
pin1cb
(
level
)
function
pin1cb
(
level
)
local
pulse2
=
now
()
du
=
tmr
.
now
()
-
pulse1
print
(
level
,
pulse2
-
pulse1
)
print
(
du
)
pulse1
=
pulse2
pulse1
=
tmr
.
now
()
trig
(
pin
,
level
==
gpio
.
HIGH
and
"down"
or
"up"
)
if
level
==
gpio
.
HIGH
then
gpio
.
trig
(
pin
,
"down"
)
else
gpio
.
trig
(
pin
,
"up"
)
end
end
trig
(
pin
,
"down"
,
pin1cb
)
end
end
gpio
.
trig
(
pin
,
"down"
,
pin1cb
)
```
```
#### See also
#### See also
[
`gpio.mode()`
](
#gpiomode
)
[
`gpio.mode()`
](
#gpiomode
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment