ADS1115: skip negative values.

This commit is contained in:
Thomas Klaehn 2019-07-29 10:18:14 +01:00 committed by Thomas Klaehn
parent eadddf7334
commit f0c3514884
3 changed files with 16 additions and 5 deletions

View File

@ -23,6 +23,8 @@ LIB_INSTALL_DIR ?= $(PREFIX)/lib
INC_INSTALL_DIR ?= $(PREFIX)/include/lib$(TARGET_FILE)
INCLUDES := inc
INCLUDES += $(PREFIX)/include/
INCLUDES += $(EXTRA_INC)
ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" ""
INCLUDES += test/inc
@ -35,7 +37,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)))
@ -62,8 +66,8 @@ install: all
install -m 0644 inc/* $(INC_INSTALL_DIR)
uninstall:
rm -f $(LIB_INSTALL_DIR)/$(STATIC_LIB)
rm -f $(LIB_INSTALL_DIR)/$(DYNAMIC_LIB)
rm -f $(LIB_INSTALL_DIR)/$(STATIC_LIB_FILE)
rm -f $(LIB_INSTALL_DIR)/$(DYNAMIC_LIB_FILE)
rm -f $(addprefix $(INC_INSTALL_DIR)/,$(shell ls inc))
rm -rf $(INC_INSTALL_DIR)
@ -104,7 +108,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

@ -2,7 +2,7 @@
#define __ADS1115__
#include <stdint.h>
#include <libi2c_bb/i2c_bb.h>
#include <libi2c_bb/i2c.h>
struct ads1115_dev {
struct i2c_bb *i2c_dev;

View File

@ -11,6 +11,7 @@
#define BUF_SIZE 3
#define VPS 6.144 / 32768.0
// cppcheck-suppress unusedFunction
int ads1115_open(const struct ads1115_dev *ads1115)
{
int res;
@ -28,6 +29,7 @@ int ads1115_open(const struct ads1115_dev *ads1115)
return 0;
}
// cppcheck-suppress unusedFunction
int ads1115_close(const struct ads1115_dev *ads1115)
{
assert(NULL != ads1115);
@ -35,6 +37,7 @@ int ads1115_close(const struct ads1115_dev *ads1115)
return i2c_close(ads1115->i2c_dev);
}
// cppcheck-suppress unusedFunction
int ads1115_start_conversation(const struct ads1115_dev *ads1115)
{
int res;
@ -61,6 +64,7 @@ int ads1115_start_conversation(const struct ads1115_dev *ads1115)
return 0;
}
// cppcheck-suppress unusedFunction
int ads1115_read_conversation_result(const struct ads1115_dev *ads1115,
struct ads1115_conversation_result *result)
{
@ -94,6 +98,9 @@ int ads1115_read_conversation_result(const struct ads1115_dev *ads1115,
return res;
}
result->raw = buffer[0] << 8 | buffer[1];
if(result->raw & 0x8000) {
result->raw = 0;
}
result->voltage = result->raw * VPS;
return 0;