mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-26 21:51:13 +01:00
gpio
move to cxx bindings
This commit is contained in:
@@ -25,8 +25,12 @@ target_include_directories(server
|
||||
${CMAKE_CURRENT_LIST_DIR}/simulators
|
||||
PRIVATE
|
||||
)
|
||||
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
|
||||
set(GPIOD_LIB "gpiod")
|
||||
|
||||
if(UNIX AND NOT APPLE)
|
||||
set(LINUX TRUE)
|
||||
endif()
|
||||
if (LINUX)
|
||||
set(GPIOD_LIB "gpiodcxx")
|
||||
endif()
|
||||
target_link_libraries(server PRIVATE
|
||||
Qt::WebSockets
|
||||
|
||||
+29
-183
@@ -1,64 +1,8 @@
|
||||
#include "gpio.hpp"
|
||||
#include "spdlog/spdlog.h"
|
||||
#include <errno.h>
|
||||
#include <filesystem>
|
||||
#if __aarch64__
|
||||
#else
|
||||
struct gpiod_chip *gpiod_chip_open(const char *path)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
struct gpiod_line_settings *gpiod_line_settings_new(void)
|
||||
{
|
||||
}
|
||||
int gpiod_line_settings_set_direction(struct gpiod_line_settings *settings, enum gpiod_line_direction direction)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int gpiod_line_settings_set_output_value(struct gpiod_line_settings *settings, enum gpiod_line_value value)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
struct gpiod_line_config *gpiod_line_config_new(void)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
int gpiod_line_config_add_line_settings(struct gpiod_line_config *config, const unsigned int *offsets,
|
||||
size_t num_offsets, struct gpiod_line_settings *settings)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
struct gpiod_request_config *gpiod_request_config_new(void)
|
||||
{
|
||||
}
|
||||
void gpiod_request_config_set_consumer(struct gpiod_request_config *config, const char *consumer)
|
||||
{
|
||||
}
|
||||
struct gpiod_line_request *gpiod_chip_request_lines(struct gpiod_chip *chip, struct gpiod_request_config *req_cfg,
|
||||
struct gpiod_line_config *line_cfg)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
int gpiod_line_request_set_value(struct gpiod_line_request *request, unsigned int offset, enum gpiod_line_value value)
|
||||
{
|
||||
}
|
||||
enum gpiod_line_value gpiod_line_request_get_value(struct gpiod_line_request *request, unsigned int offset)
|
||||
{
|
||||
}
|
||||
void gpiod_line_request_release(struct gpiod_line_request *request)
|
||||
{
|
||||
}
|
||||
void gpiod_request_config_free(struct gpiod_request_config *config)
|
||||
{
|
||||
}
|
||||
void gpiod_line_config_free(struct gpiod_line_config *config)
|
||||
{
|
||||
}
|
||||
void gpiod_line_settings_free(struct gpiod_line_settings *settings)
|
||||
{
|
||||
}
|
||||
void gpiod_chip_close(struct gpiod_chip *chip)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
GPIO::GPIO(QObject *parent) : QObject(parent), m_offset(0)
|
||||
@@ -68,152 +12,54 @@ GPIO::GPIO(QObject *parent) : QObject(parent), m_offset(0)
|
||||
|
||||
GPIO::~GPIO()
|
||||
{
|
||||
gpiod_line_request_release(m_request);
|
||||
m_request->release();
|
||||
}
|
||||
|
||||
void GPIO::configure_pin(Direction dir, uint8_t gpio, bool state = false)
|
||||
void GPIO::configure_pin(Direction dir, uint8_t gpio)
|
||||
{
|
||||
std::filesystem::path chip_path("/dev/gpiochip0");
|
||||
m_offset = gpio;
|
||||
if (dir == Direction::Output)
|
||||
{
|
||||
m_request = request_output_line("/dev/gpiochip0", gpio, (enum gpiod_line_value)state, "unisim_cpp");
|
||||
if (!m_request)
|
||||
{
|
||||
SPDLOG_ERROR("failed to request line: {}", strerror(errno));
|
||||
}
|
||||
return;
|
||||
m_request = std::make_unique<gpiod::line_request>(
|
||||
::gpiod::chip(chip_path)
|
||||
.prepare_request()
|
||||
.set_consumer("unisim_cpp")
|
||||
.add_line_settings(
|
||||
m_offset,
|
||||
::gpiod::line_settings().set_direction(
|
||||
::gpiod::line::direction::OUTPUT))
|
||||
.do_request());
|
||||
}
|
||||
if (dir == Direction::Input)
|
||||
{
|
||||
m_request = request_input_line("/dev/gpiochip0", gpio, "unisim_cpp");
|
||||
if (!m_request)
|
||||
{
|
||||
fprintf(stderr, "failed to request line: %s\n", strerror(errno));
|
||||
}
|
||||
m_request = std::make_unique<gpiod::line_request>(
|
||||
::gpiod::chip(chip_path)
|
||||
.prepare_request()
|
||||
.set_consumer("get-line-value")
|
||||
.add_line_settings(
|
||||
m_offset,
|
||||
::gpiod::line_settings().set_direction(
|
||||
::gpiod::line::direction::INPUT))
|
||||
.do_request());
|
||||
}
|
||||
}
|
||||
void GPIO::set_value(bool value)
|
||||
{
|
||||
#if __aarch64__
|
||||
if (!m_request)
|
||||
return;
|
||||
gpiod_line_request_set_value(m_request, m_offset, (enum gpiod_line_value)value);
|
||||
#if defined(__aarch64__) || defined(__x86_64__)
|
||||
if (!m_request.get())
|
||||
return;
|
||||
m_request->set_value(m_offset, (gpiod::line::value)value);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool GPIO::value()
|
||||
{
|
||||
#if __aarch64__
|
||||
if (!m_request)
|
||||
#if defined(__aarch64__) || defined(__x86_64__)
|
||||
if (!m_request.get())
|
||||
return false;
|
||||
return (bool)gpiod_line_request_get_value(m_request, m_offset);
|
||||
return (bool)m_request->get_value(m_offset);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
struct gpiod_line_request *GPIO::request_output_line(const char *chip_path, unsigned int offset,
|
||||
enum gpiod_line_value value, 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);
|
||||
if (!chip)
|
||||
return NULL;
|
||||
|
||||
settings = gpiod_line_settings_new();
|
||||
if (!settings)
|
||||
goto close_chip;
|
||||
|
||||
gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_OUTPUT);
|
||||
gpiod_line_settings_set_output_value(settings, value);
|
||||
|
||||
line_cfg = gpiod_line_config_new();
|
||||
if (!line_cfg)
|
||||
goto free_settings;
|
||||
|
||||
ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1, settings);
|
||||
if (ret)
|
||||
goto free_line_config;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
struct gpiod_line_request *GPIO::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);
|
||||
if (!chip)
|
||||
return NULL;
|
||||
|
||||
settings = gpiod_line_settings_new();
|
||||
if (!settings)
|
||||
goto close_chip;
|
||||
|
||||
gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
|
||||
|
||||
line_cfg = gpiod_line_config_new();
|
||||
if (!line_cfg)
|
||||
goto free_settings;
|
||||
|
||||
ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1, settings);
|
||||
if (ret)
|
||||
goto free_line_config;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
+5
-42
@@ -2,43 +2,10 @@
|
||||
#define GPIO_HPP
|
||||
|
||||
#include <QObject>
|
||||
#ifdef __aarch64__
|
||||
#include <gpiod.h>
|
||||
#if defined(__aarch64__) || defined(__x86_64__)
|
||||
#include <gpiod.hpp>
|
||||
#else
|
||||
struct gpiod_line_request
|
||||
{
|
||||
};
|
||||
struct gpiod_request_config
|
||||
{
|
||||
};
|
||||
struct gpiod_line_settings
|
||||
{
|
||||
};
|
||||
struct gpiod_line_config
|
||||
{
|
||||
};
|
||||
struct gpiod_chip
|
||||
{
|
||||
};
|
||||
enum gpiod_line_value
|
||||
{
|
||||
GPIOD_LINE_VALUE_ERROR = -1,
|
||||
/**< Returned to indicate an error when reading the value. */
|
||||
GPIOD_LINE_VALUE_INACTIVE = 0,
|
||||
/**< Line is logically inactive. */
|
||||
GPIOD_LINE_VALUE_ACTIVE = 1,
|
||||
/**< Line is logically active. */
|
||||
};
|
||||
enum gpiod_line_direction
|
||||
{
|
||||
GPIOD_LINE_DIRECTION_AS_IS = 1,
|
||||
/**< Request the line(s), but don't change direction. */
|
||||
GPIOD_LINE_DIRECTION_INPUT,
|
||||
/**< Direction is input - for reading the value of an externally driven
|
||||
* GPIO line. */
|
||||
GPIOD_LINE_DIRECTION_OUTPUT,
|
||||
/**< Direction is output - for driving the GPIO line. */
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
class GPIO : public QObject
|
||||
@@ -52,16 +19,12 @@ class GPIO : public QObject
|
||||
};
|
||||
explicit GPIO(QObject *parent = nullptr);
|
||||
~GPIO();
|
||||
void configure_pin(Direction dir, uint8_t gpio, bool state = false);
|
||||
void configure_pin(Direction dir, uint8_t gpio);
|
||||
void set_value(bool val);
|
||||
bool value();
|
||||
|
||||
private:
|
||||
struct gpiod_line_request *request_output_line(const char *chip_path, unsigned int offset,
|
||||
enum gpiod_line_value value, const char *consumer);
|
||||
struct gpiod_line_request *request_input_line(const char *chip_path, unsigned int offset, const char *consumer);
|
||||
|
||||
struct gpiod_line_request *m_request;
|
||||
std::unique_ptr<gpiod::line_request> m_request;
|
||||
unsigned int m_offset;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user