Template:LIBGPIOD V2 C EXAMPLE: Difference between revisions

From Variscite Wiki
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
== libgpiod C Application ==
== libgpiod C++ Application ==


[{{#var:LIBGPIOD_URL}} libgpiod] provides bindings for C/C++ applications. C++ examples are available in the libgpiod [{{#var:LIBGPIOD_URL}}/tree/bindings/cxx/examples /tree/bindings/cxx/examples ] directory.
[{{#var:LIBGPIOD_URL}} libgpiod] provides bindings for C++, Python and Rust applications. C++ examples are available in the libgpiod [{{#var:LIBGPIOD_URL}}/tree/bindings/cxx/examples /tree/bindings/cxx/examples ] directory.


Below is a simple C application demonstrating how to use the bindings with GPIO4_IO21:
Below is a simple C application demonstrating how to use the bindings with GPIO0_IO05:


'''Makefile:'''
'''Makefile:'''
<pre>
<pre>
LDFLAGS = -lgpiod
LDFLAGS = -lgpiodcxx -lgpiod


# Nome do executável e arquivos fonte
TARGET = main
TARGET = main
SRCS = main.c
SRCS = main.cpp


all: $(TARGET)
all: $(TARGET)
Line 21: Line 22:
</pre>
</pre>


'''main.c'''
'''main.cpp'''
<pre>
<pre>
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-License-Identifier: GPL-2.0-or-later
Line 28: Line 29:
/* Minimal example of reading a single line. */
/* Minimal example of reading a single line. */


#include <errno.h>
#include <cstdlib>
#include <gpiod.h>
#include <filesystem>
#include <stdio.h>
#include <gpiod.hpp>
#include <stdlib.h>
#include <iostream>
#include <string.h>


/* Request a line as input. */
namespace {
static struct gpiod_line_request *request_input_line(const char *chip_path,
    unsigned int offset,
    const char *consumer)
{
struct gpiod_request_config *req_cfg = NULL;
struct gpiod_line_request *request = NULL;
struct gpiod_line_settings *settings;
struct gpiod_line_config *line_cfg;
struct gpiod_chip *chip;
int ret;


chip = gpiod_chip_open(chip_path);
/* Example configuration - customize to suit your situation */
if (!chip)
const ::std::filesystem::path chip_path("/dev/gpiochip0");
return NULL;
const ::gpiod::line::offset line_offset = 5;


settings = gpiod_line_settings_new();
} /* namespace */
if (!settings)
goto close_chip;


gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
int main()
 
{
line_cfg = gpiod_line_config_new();
auto request = ::gpiod::chip(chip_path)
if (!line_cfg)
      .prepare_request()
goto free_settings;
      .set_consumer("get-line-value")
 
      .add_line_settings(
ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1,
      line_offset,
  settings);
      ::gpiod::line_settings().set_direction(
if (ret)
      ::gpiod::line::direction::INPUT))
goto free_line_config;
      .do_request();
 
if (consumer) {
req_cfg = gpiod_request_config_new();
if (!req_cfg)
goto free_line_config;
 
gpiod_request_config_set_consumer(req_cfg, consumer);
}
 
request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
 
gpiod_request_config_free(req_cfg);
 
free_line_config:
gpiod_line_config_free(line_cfg);
 
free_settings:
gpiod_line_settings_free(settings);
 
close_chip:
gpiod_chip_close(chip);


return request;
::std::cout << line_offset << "="
}
    << (request.get_value(line_offset) ==
 
::gpiod::line::value::ACTIVE ?
static int print_value(unsigned int offset, enum gpiod_line_value value)
"Active" :
{
"Inactive")
if (value == GPIOD_LINE_VALUE_ACTIVE)
    << ::std::endl;
printf("%d=Active\n", offset);
else if (value == GPIOD_LINE_VALUE_INACTIVE) {
printf("%d=Inactive\n", offset);
} else {
fprintf(stderr, "error reading value: %s\n",
strerror(errno));
return EXIT_FAILURE;
}


return EXIT_SUCCESS;
return EXIT_SUCCESS;
}
int main(void)
{
/* Example configuration - customize to suit your situation. */
static const char *const chip_path = "/dev/gpiochip3";
static const unsigned int line_offset = 21;
struct gpiod_line_request *request;
enum gpiod_line_value value;
int ret;
request = request_input_line(chip_path, line_offset, "get-line-value");
if (!request) {
fprintf(stderr, "failed to request line: %s\n",
strerror(errno));
return EXIT_FAILURE;
}
value = gpiod_line_request_get_value(request, line_offset);
ret = print_value(line_offset, value);
/* not strictly required here, but if the app wasn't exiting... */
gpiod_line_request_release(request);
return ret;
}
}
</pre>
</pre>

Latest revision as of 19:52, 10 March 2025

libgpiod C++ Application

[ libgpiod] provides bindings for C++, Python and Rust applications. C++ examples are available in the libgpiod [/tree/bindings/cxx/examples /tree/bindings/cxx/examples ] directory.

Below is a simple C application demonstrating how to use the bindings with GPIO0_IO05:

Makefile:

LDFLAGS = -lgpiodcxx -lgpiod

# Nome do executável e arquivos fonte
TARGET = main
SRCS = main.cpp

all: $(TARGET)

$(TARGET): $(SRCS)
	$(CXX) $(SRCS) $(LDFLAGS) -o $(TARGET)

clean:
	rm -f $(TARGET)

main.cpp

// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>

/* Minimal example of reading a single line. */

#include <cstdlib>
#include <filesystem>
#include <gpiod.hpp>
#include <iostream>

namespace {

/* Example configuration - customize to suit your situation */
const ::std::filesystem::path chip_path("/dev/gpiochip0");
const ::gpiod::line::offset line_offset = 5;

} /* namespace */

int main()
{
	auto request = ::gpiod::chip(chip_path)
			       .prepare_request()
			       .set_consumer("get-line-value")
			       .add_line_settings(
				       line_offset,
				       ::gpiod::line_settings().set_direction(
					       ::gpiod::line::direction::INPUT))
			       .do_request();

	::std::cout << line_offset << "="
		    << (request.get_value(line_offset) ==
					::gpiod::line::value::ACTIVE ?
				"Active" :
				"Inactive")
		    << ::std::endl;

	return EXIT_SUCCESS;
}