open and close threads
This commit is contained in:
commit
2d40a35a48
10 changed files with 222 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
/build
|
||||||
|
/rogue_enemy
|
||||||
|
*.o
|
||||||
|
/depends
|
||||||
20
CMakeLists.txt
Normal file
20
CMakeLists.txt
Normal file
|
|
@ -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)
|
||||||
18
Makefile
Normal file
18
Makefile
Normal file
|
|
@ -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
|
||||||
21
gamepad_output.c
Normal file
21
gamepad_output.c
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include "gamepad_output.h"
|
||||||
|
|
||||||
|
#include <linux/uinput.h>
|
||||||
|
#include <linux/input.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
5
gamepad_output.h
Normal file
5
gamepad_output.h
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "output_dev.h"
|
||||||
|
|
||||||
|
void *gamepad_thread_func(void *ptr);
|
||||||
21
imu_output.c
Normal file
21
imu_output.c
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include "imu_output.h"
|
||||||
|
|
||||||
|
#include <linux/uinput.h>
|
||||||
|
#include <linux/input.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
5
imu_output.h
Normal file
5
imu_output.h
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "output_dev.h"
|
||||||
|
|
||||||
|
void *imu_thread_func(void *ptr);
|
||||||
12
input_dev.h
Normal file
12
input_dev.h
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
102
main.c
Normal file
102
main.c
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <termios.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
14
output_dev.h
Normal file
14
output_dev.h
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue