From 273e481d3da28ba7af7eefa4e94a55c3d861483a Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Mon, 29 Jul 2019 10:20:02 +0100 Subject: [PATCH] distance_app: Measure continuously until 'Ctrl+c' is pressed. --- .vscode/c_cpp_properties.json | 2 +- Makefile | 10 ++++++-- src/main.c | 46 ++++++++++++++++++++++++----------- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index d11f98b..e0825c0 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -4,7 +4,7 @@ "name": "Linux", "includePath": [ "${workspaceFolder}/**", - "/usr/include/libgpio_ftdi" + "/usr/include/libgpio_sys" ], "defines": [], "compilerPath": "/usr/bin/clang", diff --git a/Makefile b/Makefile index 51ce7aa..e30dcbd 100644 --- a/Makefile +++ b/Makefile @@ -19,9 +19,13 @@ PREFIX ?= /usr BIN_INSTALL_DIR ?= $(PREFIX)/bin/$(TARGET_FILE) INCLUDES := inc +INCLUDES += $(PREFIX)/include/ +INCLUDES += $(EXTRA_INC) LIBS = gpio_sys i2c_bb ads1115 +LD_FLAGS += $(addprefix -L,$(EXTRA_LIB_DIR)) + ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" "" INCLUDES += test/inc C_FLAGS += --coverage @@ -32,7 +36,9 @@ C_FLAGS += -fpic C_FLAGS += -O0 -g -Wall -Wextra -Werror CPP_FLAGS += $(addprefix -I, $(INCLUDES)) -CHECK_FLAGS = --enable=all --template=gcc --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr $(C_SRCS) + +CHECK_FLAGS = $(addprefix -I,$(filter-out $(PREFIX)/include/,$(INCLUDES))) +CHECK_FLAGS += --enable=all --template=gcc --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr --force C_SRCS = $(wildcard $(SRC_DIR)/*.c) C_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst %.c,%.o,$(C_SRCS))) @@ -96,7 +102,7 @@ exec_unit_test: $(UNIT_TEST_TARGET) .PHONY: check check: $(C_SRCS) - $(CHECK) $(CPP_FLAGS) $(CHECK_FLAGS) $(C_SRCS) + $(CHECK) $(CHECK_FLAGS) $(C_SRCS) $(UNIT_TEST_TARGET): $(UNIT_TEST_OBJS) $(THIS_MAKEFILE) @mkdir -p $(BIN_DIR)/$(UNIT_TEST_SRC_DIR) diff --git a/src/main.c b/src/main.c index b5c796e..895216a 100644 --- a/src/main.c +++ b/src/main.c @@ -5,18 +5,19 @@ #include #include #include +#include -#include +#include #include static struct gpio_sys gpio_sda = { .pin = 4, - .direction = OUT, + .direction = 1, }; static struct gpio_sys gpio_scl = { .pin = 24, - .direction = OUT, + .direction = 1, }; static struct i2c_bb i2c_dev = { @@ -29,32 +30,49 @@ static struct ads1115_dev ads = { .i2c_slave_address = 0x48, }; +static bool run_condition = true; + +static void signal_handler(int sig) +{ + printf("Signal catched: %d\n Shutting down...\n", sig); + run_condition = false; +} int main(void) { int res; struct ads1115_conversation_result result; + struct sigaction sig_int_handler; + + sig_int_handler.sa_handler = signal_handler; + sigemptyset(&sig_int_handler.sa_mask); + sig_int_handler.sa_flags = 0; + + sigaction(SIGINT, &sig_int_handler, NULL); + res = ads1115_open(&ads); if(res != 0) { return res; } - res = ads1115_start_conversation(&ads); - if(res != 0) { - ads1115_close(&ads); - return res; - } + while(run_condition) { + res = ads1115_start_conversation(&ads); + if(res != 0) { + ads1115_close(&ads); + return res; + } - res = ads1115_read_conversation_result(&ads, &result); - if(res != 0) { - ads1115_close(&ads); - return res; + res = ads1115_read_conversation_result(&ads, &result); + if(res != 0) { + ads1115_close(&ads); + return res; + } + printf("Conversation: %4.3fV (0x%04x).\n", result.voltage, result.raw); + usleep(50000); } ads1115_close(&ads); - printf("Conversation: %4.3fV (0x%04x).\n", result.voltage, result.raw); - return res; }