1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
#include "gpiolib.h"
#include <driver/gpio.h>
static void lua_esp_check_err(lua_State *L, esp_err_t err)
{
if (err != ESP_OK)
luaL_error(L, "Error: %s", esp_err_to_name(err));
}
static int lgpio_reset_pin(lua_State *L)
{
gpio_num_t pin = luaL_checkinteger(L, 1);
lua_esp_check_err(L, gpio_reset_pin(pin));
return 0;
}
static int lgpio_set_level(lua_State *L)
{
gpio_num_t pin = luaL_checkinteger(L, 1);
int level = luaL_checkinteger(L, 2);
lua_esp_check_err(L, gpio_set_level(pin, level));
return 0;
}
static int lgpio_get_level(lua_State *L)
{
gpio_num_t pin = luaL_checkinteger(L, 1);
lua_pushinteger(L, gpio_get_level(pin));
return 1;
}
static int lgpio_set_direction(lua_State *L)
{
gpio_num_t pin = luaL_checkinteger(L, 1);
int mode = luaL_checkinteger(L, 2);
lua_esp_check_err(L, gpio_set_direction(pin, mode));
return 0;
}
static const struct luaL_Reg lgpio_funcs[] = {
{ "reset_pin", lgpio_reset_pin },
{ "set_direction", lgpio_set_direction },
{ "set_level", lgpio_set_level },
{ "get_level", lgpio_get_level },
{ NULL, NULL }
};
int luaopen_lgpio(lua_State *L)
{
luaL_newlib(L, lgpio_funcs);
return 1;
}
|