distance_app: Measure continuously until 'Ctrl+c' is pressed.

This commit is contained in:
Thomas Klaehn 2019-07-29 10:20:02 +01:00 committed by Thomas Klaehn
parent 78b36fcbb2
commit 273e481d3d
3 changed files with 41 additions and 17 deletions

View File

@ -4,7 +4,7 @@
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/libgpio_ftdi"
"/usr/include/libgpio_sys"
],
"defines": [],
"compilerPath": "/usr/bin/clang",

View File

@ -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)

View File

@ -5,18 +5,19 @@
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <libgpio_sys/gpio_sys.h>
#include <libgpio_sys/gpio.h>
#include <libads1115/ads1115.h>
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;
}