Skip to content
Snippets Groups Projects
Commit 7eaacae1 authored by Šimon Fiala's avatar Šimon Fiala
Browse files

Ported to libgpiod 2.x API


- implemented own iteration when opening gpio-chips
- replaced old API functions with new

Signed-off-by: default avatarSimon Fiala <fiala@acrios.com>
parent ae2b7703
No related tags found
1 merge request!1Draft: Adding Lua 5.1 bindings as 'gpiod' Lua module
......@@ -8,7 +8,8 @@ lualib_LTLIBRARIES = gpiod.la
gpiod_la_SOURCES = gpiod.c
gpiod_la_CFLAGS = -Wall -Wextra -g -fPIC -pedantic -std=c11 $(LUA_INCLUDE)
gpiod_la_CFLAGS += -I$(top_srcdir)/include/
gpiod_la_LDFLAGS = -std=c11 -shared -module -lgpiod -version-info 1:0:0 $(LUA_LDFLAGS)
gpiod_la_LDFLAGS = -std=c11 -shared -module -version-info 1:0:0 $(LUA_LDFLAGS)
gpiod_la_LDFLAGS += -lgpiod -L$(top_builddir)/lib
gpiod_la_LIBADD = $(LUA_LIB)
SUBDIRS = .
......
......@@ -25,11 +25,14 @@
#include <lua.h>
#include <lauxlib.h>
// macros
#define GPIOD_METATABLE "gpiod"
#define GPIOD_CONSUMER_NAME "Lua"
#define MAX_GPIO_CHIPS (16)
#define MAX_CHIP_PATH_LEN (32)
#define GPIOD_DBG(verbose, ...) dbg_print(verbose, __func__, __VA_ARGS__)
// #define PULL_UP_DOWN_SUPPORTED // uncomment to enable pull-up and pull-down support
// typedefs
......@@ -78,7 +81,7 @@ static int gpiod_new(lua_State *L)
const char *mode;
struct gpiod_chip *chip_obj;
struct gpiod_line *line_obj;
struct gpiod_line_bulk bulk;
struct gpiod_line_bulk *bulk;
bool is_rising_edge = false;
bool is_falling_edge = false;
bool is_open_drain = false;
......@@ -336,7 +339,7 @@ static int gpiod_new(lua_State *L)
return 1;
}
gpiod_line_bulk_init(&bulk);
bulk = gpiod_line_bulk_new(32);
line_obj = gpiod_chip_get_line(chip_obj, line);
if (!line_obj) {
GPIOD_DBG(verbose, "Failed to get line %d", line);
......@@ -345,11 +348,11 @@ static int gpiod_new(lua_State *L)
return 1;
}
gpiod_line_bulk_add(&bulk, line_obj);
gpiod_line_bulk_add_line(bulk, line_obj);
if (!strcmp("out", mode)) {
rv = gpiod_line_request_bulk_output_flags(&bulk,
rv = gpiod_line_request_bulk_output_flags(bulk,
GPIOD_CONSUMER_NAME, flags, &def_val);
if (rv < 0) { // failed to open line
......@@ -361,7 +364,7 @@ static int gpiod_new(lua_State *L)
return 1;
}
} else if (!strcmp("in", mode)) {
rv = gpiod_line_request_bulk_input_flags(&bulk,
rv = gpiod_line_request_bulk_input_flags(bulk,
GPIOD_CONSUMER_NAME, flags);
if (rv < 0) { // failed to open line
GPIOD_DBG(verbose,
......@@ -374,13 +377,13 @@ static int gpiod_new(lua_State *L)
} else if (!strcmp("event", mode)) {
if (is_rising_edge)
rv = gpiod_line_request_bulk_rising_edge_events_flags(
&bulk, GPIOD_CONSUMER_NAME, flags);
bulk, GPIOD_CONSUMER_NAME, flags);
else if (is_falling_edge)
rv = gpiod_line_request_bulk_falling_edge_events_flags(
&bulk, GPIOD_CONSUMER_NAME, flags);
bulk, GPIOD_CONSUMER_NAME, flags);
else
rv = gpiod_line_request_bulk_both_edges_events_flags(
&bulk, GPIOD_CONSUMER_NAME, flags);
bulk, GPIOD_CONSUMER_NAME, flags);
if (rv < 0) { // failed to open line
GPIOD_DBG(verbose,
......@@ -628,6 +631,8 @@ static int gpiod_finalize(lua_State *L)
// entrypoint
int luaopen_gpiod(lua_State *L)
{
int i;
char path[MAX_CHIP_PATH_LEN];
// The user may pass in values here,
// but we'll ignore those values.
......@@ -649,25 +654,22 @@ int luaopen_gpiod(lua_State *L)
lua_pop(L, 1); // The table is saved in the Lua's registry.
// stack = []
struct gpiod_chip_iter *iter;
struct gpiod_chip *chip;
iter = gpiod_chip_iter_new();
gpiochips_opened = 0;
if (!iter)
return 0;
gpiod_foreach_chip_noclose(iter, chip) {
gpiochips[gpiochips_opened] = chip;
//Opening up to MAX_GPIO_CHIPS chips
for (i = 0; i < MAX_GPIO_CHIPS; i++) {
snprintf(path, MAX_CHIP_PATH_LEN, "/dev/gpiochip%d", i);
chip = gpiod_chip_open(path);
if (chip != NULL) {
gpiochips[i] = chip;
gpiochips_opened++;
if (gpiochips_opened >= MAX_GPIO_CHIPS) {
GPIOD_DBG(true,
"Limiting number of GPIOCHIPS!");
} else {
break;
}
}
gpiod_chip_iter_free(iter);
if (i == MAX_GPIO_CHIPS) {
GPIOD_DBG(true, "WARNING! Opening only first %d chips!", MAX_GPIO_CHIPS);
}
}
static struct luaL_Reg fns[] = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment