cash2ecash

cash2ecash: cash acceptor that issues digital cash (experimental)
Log | Files | Refs | README | LICENSE

commit bb833ff41650acf46939c40a68a46527b15ae1d1
parent f94ca5d7b9b6b308bd30bd23cf863def1ae2e62d
Author: Tellenbach Reto <tellr1@bfh.ch>
Date:   Thu,  9 Apr 2026 11:17:12 +0200

[ref] thinker_CoinAcceptor: gpiod in seperate file

Diffstat:
Mthinker/CoinAcceptor/src/CMakeLists.txt | 18++++++++++++------
Athinker/CoinAcceptor/src/gpiod_wrapper.c | 155+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Athinker/CoinAcceptor/src/gpiod_wrapper.h | 15+++++++++++++++
Mthinker/CoinAcceptor/src/main.c | 151------------------------------------------------------------------------------
4 files changed, 182 insertions(+), 157 deletions(-)

diff --git a/thinker/CoinAcceptor/src/CMakeLists.txt b/thinker/CoinAcceptor/src/CMakeLists.txt @@ -1,13 +1,19 @@ -#Project Config +# Project Config cmake_minimum_required(VERSION 3.5) project(CoinAcceptor LANGUAGES C ) -#Compiler Settings +# Librarys +## Defining Librarys +add_library(gpio gpiod_wrapper.c) +add_executable(CoinAcceptor main.c) + +## Linking Targets +target_link_libraries(gpio PRIVATE gpiod) +target_link_libraries(CoinAcceptor PRIVATE gpio) + +# Compiler Settings set(CMAKE_C_STANDARD 17) set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_EXTENSIONS ON) - -add_executable(CoinAcceptor main.c) -target_link_libraries(CoinAcceptor PRIVATE gpiod) - +add_compile_definitions(_GNU_SOURCE) target_compile_options(CoinAcceptor PRIVATE -Wall -Wextra) diff --git a/thinker/CoinAcceptor/src/gpiod_wrapper.c b/thinker/CoinAcceptor/src/gpiod_wrapper.c @@ -0,0 +1,154 @@ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include <gpiod.h> +#include <stdlib.h> +#include <stdio.h> +#include "gpiod_wrapper.h" + +struct gpiod_chip *gpiod_chip_open_by_name(const char *name) +{ + struct gpiod_chip *chip; + char *path; + int ret; + + ret = asprintf(&path, "/dev/%s", name); + if (ret < 0) + { + perror("asprint() memory-allocation failed"); + return NULL; + } + + + chip = gpiod_chip_open(path); + free(path); + + return chip; +} + +struct gpiod_line_settings* gpiod_make_settings(enum gpiod_line_direction direction,enum gpiod_line_bias bias, enum gpiod_line_drive drive, enum gpiod_line_drive active_low) +{ + int ret = 0; + struct gpiod_line_settings* settings = gpiod_line_settings_new(); + if (!settings) + { + perror("gpiod_line_settings_new() failed"); + return NULL; + } + + ret |= gpiod_line_settings_set_direction(settings, direction); + ret |= gpiod_line_settings_set_bias(settings, bias); + ret |= gpiod_line_settings_set_drive(settings, drive); + gpiod_line_settings_set_active_low(settings, active_low); + if (ret) + { + perror("gpiod_line_settings_set....() failed"); + return NULL; + } + + + return settings; +} + +struct gpiod_line_request* gpiod_make_line_request_by_name(const char* chipname, const char* linename, struct gpiod_line_settings* settings) +{ + struct gpiod_request_config* request_cfg = NULL; //for kernel settings, not used here + struct gpiod_line_request* request = NULL; + struct gpiod_line_config* line_cfg; + struct gpiod_chip* chip; + int ret = 0; + + + chip = gpiod_chip_open_by_name(chipname); + if (!chip) + { + perror("gpiod_chip_open_by_name() failed"); + goto free_settings; + } + + + const int line_offset = gpiod_chip_get_line_offset_from_name(chip, linename); + if(line_offset<0) + { + perror("gpiod_chip_get_line_offset_from_name() failed"); + goto close_chip; + } + + const unsigned int u_line_offset = (unsigned int)line_offset; + + line_cfg = gpiod_line_config_new(); + if (!line_cfg) + { + perror("gpiod_line_config_new() failed"); + goto close_chip; + } + + + ret = gpiod_line_config_add_line_settings(line_cfg, &u_line_offset, 1, settings); + if (ret) + { + perror("gpiod_line_config_add_line_settings() failed"); + goto free_line_cfg; + } + + + request = gpiod_chip_request_lines(chip, request_cfg, line_cfg); + +free_line_cfg: + gpiod_line_config_free(line_cfg); + +close_chip: + gpiod_chip_close(chip); + +free_settings: + gpiod_line_settings_free(settings); + + return request; //NULL on error +} + +struct gpiod_line_request* gpiod_make_line_request(const char* chip_path, const unsigned int line_offset, struct gpiod_line_settings* settings) +{ + struct gpiod_request_config* request_cfg = NULL; //for kernel settings, not used here + struct gpiod_line_request* request = NULL; + struct gpiod_line_config* line_cfg; + struct gpiod_chip* chip; + int ret = 0; + + chip = gpiod_chip_open(chip_path); + if (!chip) + { + perror("gpiod_chip_open_by_name() failed"); + goto free_settings; + } + + + line_cfg = gpiod_line_config_new(); + if (!line_cfg) + { + perror("gpiod_line_config_new() failed"); + goto close_chip; + } + + + ret = gpiod_line_config_add_line_settings(line_cfg, &line_offset, 1, settings); + if (ret) + { + perror("gpiod_line_config_add_line_settings() failed"); + goto free_line_cfg; + } + + + request = gpiod_chip_request_lines(chip, request_cfg, line_cfg); + +free_line_cfg: + gpiod_line_config_free(line_cfg); + +close_chip: + gpiod_chip_close(chip); + +free_settings: + gpiod_line_settings_free(settings); + + return request; //NULL on error +} +\ No newline at end of file diff --git a/thinker/CoinAcceptor/src/gpiod_wrapper.h b/thinker/CoinAcceptor/src/gpiod_wrapper.h @@ -0,0 +1,14 @@ +#ifndef GPIOD_WRAPPER_H +#define GPIOD_WRAPPER_H + +#include <gpiod.h> + +struct gpiod_chip *gpiod_chip_open_by_name(const char *name); + +struct gpiod_line_settings* gpiod_make_settings(enum gpiod_line_direction direction,enum gpiod_line_bias bias, enum gpiod_line_drive drive, enum gpiod_line_drive active_low); + +struct gpiod_line_request* gpiod_make_line_request_by_name(const char* chipname, const char* linename, struct gpiod_line_settings* settings); + +struct gpiod_line_request* gpiod_make_line_request(const char* chip_path, const unsigned int line_offset, struct gpiod_line_settings* settings); + +#endif +\ No newline at end of file diff --git a/thinker/CoinAcceptor/src/main.c b/thinker/CoinAcceptor/src/main.c @@ -20,157 +20,6 @@ enum command{ }; static char keyInput = down; -//GPIOD Wrapers -typedef struct { - struct gpiod_line_request* request; - unsigned int offset; -} gpiod_line; - -struct gpiod_chip *gpiod_chip_open_by_name(const char *name) -{ - struct gpiod_chip *chip; - char *path; - int ret; - - ret = asprintf(&path, "/dev/%s", name); - if (ret < 0) - { - perror("asprint() memory-allocation failed"); - return NULL; - } - - - chip = gpiod_chip_open(path); - free(path); - - return chip; -} - -struct gpiod_line_settings* gpiod_make_settings(enum gpiod_line_direction direction,enum gpiod_line_bias bias, enum gpiod_line_drive drive, enum gpiod_line_drive active_low) -{ - int ret = 0; - struct gpiod_line_settings* settings = gpiod_line_settings_new(); - if (!settings) - { - perror("gpiod_line_settings_new() failed"); - return NULL; - } - - ret |= gpiod_line_settings_set_direction(settings, direction); - ret |= gpiod_line_settings_set_bias(settings, bias); - ret |= gpiod_line_settings_set_drive(settings, drive); - gpiod_line_settings_set_active_low(settings, active_low); - if (ret) - { - perror("gpiod_line_settings_set....() failed"); - return NULL; - } - - - return settings; -} - -struct gpiod_line_request* gpiod_make_line_request_by_name(const char* chipname, const char* linename, struct gpiod_line_settings* settings) -{ - struct gpiod_request_config* request_cfg = NULL; //for kernel settings, not used here - struct gpiod_line_request* request = NULL; - struct gpiod_line_config* line_cfg; - struct gpiod_chip* chip; - int ret = 0; - - - chip = gpiod_chip_open_by_name(chipname); - if (!chip) - { - perror("gpiod_chip_open_by_name() failed"); - goto free_settings; - } - - - const int line_offset = gpiod_chip_get_line_offset_from_name(chip, linename); - if(line_offset<0) - { - perror("gpiod_chip_get_line_offset_from_name() failed"); - goto close_chip; - } - - const unsigned int u_line_offset = (unsigned int)line_offset; - - line_cfg = gpiod_line_config_new(); - if (!line_cfg) - { - perror("gpiod_line_config_new() failed"); - goto close_chip; - } - - - ret = gpiod_line_config_add_line_settings(line_cfg, &u_line_offset, 1, settings); - if (ret) - { - perror("gpiod_line_config_add_line_settings() failed"); - goto free_line_cfg; - } - - - request = gpiod_chip_request_lines(chip, request_cfg, line_cfg); - -free_line_cfg: - gpiod_line_config_free(line_cfg); - -close_chip: - gpiod_chip_close(chip); - -free_settings: - gpiod_line_settings_free(settings); - - return request; //NULL on error -} - -struct gpiod_line_request* gpiod_make_line_request(const char* chip_path, const unsigned int line_offset, struct gpiod_line_settings* settings) -{ - struct gpiod_request_config* request_cfg = NULL; //for kernel settings, not used here - struct gpiod_line_request* request = NULL; - struct gpiod_line_config* line_cfg; - struct gpiod_chip* chip; - int ret = 0; - - chip = gpiod_chip_open(chip_path); - if (!chip) - { - perror("gpiod_chip_open_by_name() failed"); - goto free_settings; - } - - - line_cfg = gpiod_line_config_new(); - if (!line_cfg) - { - perror("gpiod_line_config_new() failed"); - goto close_chip; - } - - - ret = gpiod_line_config_add_line_settings(line_cfg, &line_offset, 1, settings); - if (ret) - { - perror("gpiod_line_config_add_line_settings() failed"); - goto free_line_cfg; - } - - - request = gpiod_chip_request_lines(chip, request_cfg, line_cfg); - -free_line_cfg: - gpiod_line_config_free(line_cfg); - -close_chip: - gpiod_chip_close(chip); - -free_settings: - gpiod_line_settings_free(settings); - - return request; //NULL on error -} int main(void)