commit f94ca5d7b9b6b308bd30bd23cf863def1ae2e62d
parent c89509bf58ce9973d9caee8fb1e2f73f42669853
Author: Tellenbach Reto <tellr1@bfh.ch>
Date: Wed, 8 Apr 2026 18:44:00 +0200
[new] thinker_CoinAcceptor: controll GPIO
Diffstat:
4 files changed, 244 insertions(+), 126 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,11 +1,14 @@
+# Build files
*.[oa]
-*~
-.*/
CMakeFiles/
-*.png
*.cmake
-build/**
+**/build/**
-# Temporary files
+# Temporary doc files
+*~
~$*.docx
-*.drawio.bkp
-\ No newline at end of file
+*.drawio.bkp
+
+# IDE files
+.vscode
+
diff --git a/thinker/CoinAcceptor/CA_inhibit.c b/thinker/CoinAcceptor/CA_inhibit.c
@@ -1,118 +0,0 @@
-/* Thinker CoinAcceptor */
-/* Use GPIO to enable coin insert*/
-//heavily inspired from gpiod source example of Gent Gibson toggle_line_value.c
-#include <gpiod.h>
-#include <stdio.h>
-#include <unistd.h>
-
-//CLI Controlls
-enum command{
- quit = 'q',
- up = 'w',
- down = 's'
-};
-static char keyInput;
-
-//GPIOD Wrapers
-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)
- 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)
- 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)
- return NULL;
-
- return settings;
-}
-
-struct gpiod_line_request* gpiod_make_line_request(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)
- goto free_settings;
-
- const int line_offset = gpiod_chip_get_line_offset_from_name(chip, linename);
- if(line_offset<0)
- goto close_chip;
- const unsigned int u_line_offset = (unsigned int)line_offset;
-
- line_cfg = gpiod_line_config_new();
- if (!line_cfg)
- goto close_chip;
-
- ret = gpiod_line_config_add_line_settings(line_cfg, &u_line_offset, 1, settings);
- if (ret)
- 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)
- {
- printf("CA_inhibit\n");
-
- struct gpiod_line_request* request_line16;
- struct gpiod_line_settings* settings;
-
- //Config
- settings = gpiod_make_settings(GPIOD_LINE_DIRECTION_OUTPUT,GPIOD_LINE_BIAS_PULL_UP,GPIOD_LINE_DRIVE_OPEN_DRAIN,1);
- if (!settings)
- return -1;
-
-
- request_line16 = gpiod_make_line_request("gpiochip0","GPIO16",settings);
- if (!request_line16)
- return -1;
-
-
- while(keyInput!=quit)
- {
- scanf("%c", &keyInput);// controlls
- if(gpiod_line_request_set_value(request_line16, 0, GPIOD_LINE_VALUE_ACTIVE))
- return -1;
- }
-
- gpiod_line_request_release(request_line16);
- return 0;
- }
-\ No newline at end of file
diff --git a/thinker/CoinAcceptor/src/CMakeLists.txt b/thinker/CoinAcceptor/src/CMakeLists.txt
@@ -0,0 +1,13 @@
+#Project Config
+cmake_minimum_required(VERSION 3.5)
+project(CoinAcceptor LANGUAGES C )
+
+#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)
+
+target_compile_options(CoinAcceptor PRIVATE -Wall -Wextra)
diff --git a/thinker/CoinAcceptor/src/main.c b/thinker/CoinAcceptor/src/main.c
@@ -0,0 +1,222 @@
+/* Thinker CoinAcceptor */
+/* Use GPIO to enable coin insert*/
+//heavily inspired from gpiod source example of Gent Gibson toggle_line_value.c
+#define _GNU_SOURCE
+
+#include <gpiod.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#define CHIPPATH "/dev/gpiochip0"
+
+const unsigned int GPIO16_OFFSET = 16;
+
+//CLI Controlls
+enum command{
+ quit = 'q',
+ up = 'w',
+ down = 's'
+};
+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)
+ {
+ printf("CA_inhibit\n");
+
+ struct gpiod_line_request* request_line16;
+ struct gpiod_line_settings* settings;
+ int ret;
+
+ //Config
+ settings = gpiod_make_settings(GPIOD_LINE_DIRECTION_OUTPUT,GPIOD_LINE_BIAS_PULL_UP,GPIOD_LINE_DRIVE_OPEN_DRAIN,1);
+ if (!settings)
+ {
+ perror("gpiod_make_settings() failed");
+ return -1;
+ }
+
+ request_line16 = gpiod_make_line_request(CHIPPATH,GPIO16_OFFSET,settings);
+ if (!request_line16)
+ {
+ perror("gpiod_make_line_request() failed");
+ return -1;
+ }
+
+ while(keyInput!=quit)
+ {
+ scanf(" %c", &keyInput);// controlls
+ if(keyInput == up)
+ {
+ printf("ACTIVE\n");
+ ret = gpiod_line_request_set_value(request_line16,GPIO16_OFFSET, GPIOD_LINE_VALUE_ACTIVE);
+ }
+
+ else if (keyInput == down)
+ {
+ printf("INACTIVE\n");
+ ret = gpiod_line_request_set_value(request_line16,GPIO16_OFFSET, GPIOD_LINE_VALUE_INACTIVE);
+ }
+ if(ret == -1)
+ {
+ perror("gpiod_line_request_set_value() failed");
+ return -1;
+ }
+ }
+
+ gpiod_line_request_release(request_line16);
+ return 0;
+ }