commit 2d40a35a482c9d2e0389caf38de1a344ae67e77e Author: Denis Benato Date: Thu Nov 2 12:02:29 2023 +0100 open and close threads diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..674305f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/build +/rogue_enemy +*.o +/depends \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..08f2910 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +# Almost all CMake files should start with this +# You should always specify a range with the newest +# and oldest tested versions of CMake. This will ensure +# you pick up the best policies. +cmake_minimum_required(VERSION 3.1...3.27) + +project( + ROGueENEMY + VERSION 1.0 + LANGUAGES CXX) + +set(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads REQUIRED) + +# Adding something we can run - Output name matches target name +add_executable(rogue_enemy main.c imu_output.c gamepad_output.c) + +target_link_libraries(rogue_enemy PRIVATE Threads::Threads) + +set_target_properties(rogue_enemy PROPERTIES LINKER_LANGUAGE C) \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c34b32e --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +CFLAGS=-g -std=c11 -pedantic # -Wall -Werror +LDFLAGS=-lpthread +CC=gcc +OBJECTS=main.o gamepad_output.o imu_output.o +TARGET=rogue_enemy + +all: $(TARGET) + +$(TARGET): $(OBJECTS) + $(CC) $(OBJECTS) -o $@ + +include depends + +depends: + $(CC) -MM $(OBJECTS:.o=.c) > depends + +clean: + rm ./$(TARGET) *.o diff --git a/gamepad_output.c b/gamepad_output.c new file mode 100644 index 0000000..39638b1 --- /dev/null +++ b/gamepad_output.c @@ -0,0 +1,21 @@ +#include "gamepad_output.h" + +#include +#include + +void *gamepad_thread_func(void *ptr) { + output_dev_t *out_dev = (output_dev_t*)ptr; + + for (;;) { + pthread_mutex_lock(&out_dev->ctrl_mutex); + + if (out_dev->crtl_flags & OUTPUT_DEV_CTRL_FLAG_EXIT) { + pthread_mutex_unlock(&out_dev->ctrl_mutex); + break; + } + + pthread_mutex_unlock(&out_dev->ctrl_mutex); + } + + return NULL; +} \ No newline at end of file diff --git a/gamepad_output.h b/gamepad_output.h new file mode 100644 index 0000000..3a961be --- /dev/null +++ b/gamepad_output.h @@ -0,0 +1,5 @@ +#pragma once + +#include "output_dev.h" + +void *gamepad_thread_func(void *ptr); \ No newline at end of file diff --git a/imu_output.c b/imu_output.c new file mode 100644 index 0000000..fc46725 --- /dev/null +++ b/imu_output.c @@ -0,0 +1,21 @@ +#include "imu_output.h" + +#include +#include + +void *imu_thread_func(void *ptr) { + output_dev_t *out_dev = (output_dev_t*)ptr; + + for (;;) { + pthread_mutex_lock(&out_dev->ctrl_mutex); + + if (out_dev->crtl_flags & OUTPUT_DEV_CTRL_FLAG_EXIT) { + pthread_mutex_unlock(&out_dev->ctrl_mutex); + break; + } + + pthread_mutex_unlock(&out_dev->ctrl_mutex); + } + + return NULL; +} \ No newline at end of file diff --git a/imu_output.h b/imu_output.h new file mode 100644 index 0000000..a5f55aa --- /dev/null +++ b/imu_output.h @@ -0,0 +1,5 @@ +#pragma once + +#include "output_dev.h" + +void *imu_thread_func(void *ptr); \ No newline at end of file diff --git a/input_dev.h b/input_dev.h new file mode 100644 index 0000000..2cbf659 --- /dev/null +++ b/input_dev.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +#define INPUT_DEV_CTRL_FLAG_EXIT 0x00000001U +#define INPUT_DEV_CTRL_FLAG_DATA 0x00000002U + +typedef struct input_dev { + pthread_mutex_t ctrl_mutex; + uint32_t crtl_flags; +} input_dev_t; \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..b76ed2c --- /dev/null +++ b/main.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "imu_output.h" +#include "gamepad_output.h" + +output_dev_t imu_dev = { + .fd = -1, + .ctrl_mutex = PTHREAD_MUTEX_INITIALIZER, + .crtl_flags = 0x00000000U, +}; + +output_dev_t gamepadd_dev = { + .fd = -1, + .ctrl_mutex = PTHREAD_MUTEX_INITIALIZER, + .crtl_flags = 0x00000000U, +}; + +void sig_handler(int signo) +{ + if (signo == SIGINT) { + pthread_mutex_lock(&imu_dev.ctrl_mutex); + imu_dev.crtl_flags |= OUTPUT_DEV_CTRL_FLAG_EXIT; + pthread_mutex_unlock(&imu_dev.ctrl_mutex); + + pthread_mutex_lock(&gamepadd_dev.ctrl_mutex); + gamepadd_dev.crtl_flags |= OUTPUT_DEV_CTRL_FLAG_EXIT; + pthread_mutex_unlock(&gamepadd_dev.ctrl_mutex); + + printf("received SIGINT\n"); + } +} + +int main(int argc, char ** argv) { + + __sighandler_t sigint_hndl = signal(SIGINT, sig_handler); + if (sigint_hndl == SIG_ERR) { + fprintf(stderr, "Error registering SIGINT handler"); + return -1; + } + + int ret = 0; + + pthread_t imu_thread, gamepad_thread; + + int imu_thread_creation = pthread_create(&imu_thread, NULL, imu_thread_func, (void*)(&imu_dev)); + if (imu_thread_creation != 0) { + fprintf(stderr, "Error creating IMU output thread: %d", imu_thread_creation); + ret = -1; + goto imu_thread_err; + } + + int gamepad_thread_creation = pthread_create(&gamepad_thread, NULL, gamepad_thread_func, (void*)(&gamepadd_dev)); + if (gamepad_thread_creation != 0) { + fprintf(stderr, "Error creating gamepad output thread: %d", gamepad_thread_creation); + ret = -1; + goto gamepad_thread_err; + } + + pthread_join(gamepad_thread, NULL); + +gamepad_thread_err: + pthread_join(imu_thread, NULL); + +imu_thread_err: + return ret; + + int fd; + char buf[256]; + + fd = open("/dev/input/event3", O_RDONLY); + if (fd == -1) { + perror("open_port: Unable to open /dev/ttyAMA0 - "); + return(-1); + } + + // Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that + //fcntl(fd, F_SETFL, 0); + + while(1){ + int n = read(fd, (void*)buf, 255); + if (n < 0) { + perror("Read failed - "); + return -1; + } else if (n == 0) { + printf("No data on port\n"); + } else { + buf[n] = '\0'; + printf("%i bytes read : %s", n, buf); + } + sleep(1); + printf("i'm still doing something"); + } + close(fd); + return 0; +} \ No newline at end of file diff --git a/output_dev.h b/output_dev.h new file mode 100644 index 0000000..80d8a3a --- /dev/null +++ b/output_dev.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +#define OUTPUT_DEV_CTRL_FLAG_EXIT 0x00000001U +#define OUTPUT_DEV_CTRL_FLAG_DATA 0x00000002U + +typedef struct output_dev { + int fd; + + pthread_mutex_t ctrl_mutex; + uint32_t crtl_flags; +} output_dev_t; \ No newline at end of file