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 96e9bd0649
3 changed files with 14 additions and 2 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
@ -51,7 +53,7 @@ UNIT_TEST_TARGET = $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)/$(TARGET_FILE)
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
.PHONY: all install uninstall clean
.PHONY: all install uninstall clean distclean
all: $(STATIC_LIB) $(DYNAMIC_LIB)
install: all
@ -76,6 +78,9 @@ clean:
rm -f $(OBJ_DIR)/*.gcda $(OBJ_DIR)/*.gcno
rm -fr $(COVERAGE_DIR)
distclean:
rm -fr $(BIN_DIR) $(OBJ_DIR) $(LIB_DIR) $(COVERAGE_DIR)
.PHONY: coverage
coverage: _cov_genhtml
$(eval COVERAGE:=$(shell grep 'lines' .coverage.tmp | egrep -o '[0-9]+.[0-9]+%'))

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;