From 78b36fcbb268a3e511763196373218ce7a8c44b0 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Thu, 25 Jul 2019 16:38:56 +0200 Subject: [PATCH] move ada1115 to separate lib. --- Makefile | 4 +- inc/ads1115.h | 23 ------------ src/ads1115.c | 100 -------------------------------------------------- src/main.c | 2 +- 4 files changed, 3 insertions(+), 126 deletions(-) delete mode 100644 inc/ads1115.h delete mode 100644 src/ads1115.c diff --git a/Makefile b/Makefile index 4fa2a55..51ce7aa 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CROSS_COMPILE ?= -TARGET_FILE ?= blink +TARGET_FILE ?= distance_app CC = $(CROSS_COMPILE)gcc CPP = $(CROSS_COMPILE)cpp @@ -20,7 +20,7 @@ BIN_INSTALL_DIR ?= $(PREFIX)/bin/$(TARGET_FILE) INCLUDES := inc -LIBS = gpio_sys i2c_bb +LIBS = gpio_sys i2c_bb ads1115 ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" "" INCLUDES += test/inc diff --git a/inc/ads1115.h b/inc/ads1115.h deleted file mode 100644 index 1c89d60..0000000 --- a/inc/ads1115.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef __ADS1115__ -#define __ADS1115__ - -#include -#include - -struct ads1115_dev { - struct i2c_bb *i2c_dev; - uint8_t i2c_slave_address; -}; - -struct ads1115_conversation_result { - uint16_t raw; - float voltage; -}; - -int ads1115_open(const struct ads1115_dev *ads1115); -int ads1115_close(const struct ads1115_dev *ads1115); -int ads1115_start_conversation(const struct ads1115_dev *ads1115); -int ads1115_read_conversation_result(const struct ads1115_dev *ads1115, - struct ads1115_conversation_result *result); - -#endif diff --git a/src/ads1115.c b/src/ads1115.c deleted file mode 100644 index 762be90..0000000 --- a/src/ads1115.c +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include - -#define BUF_SIZE 3 -#define VPS 6.144 / 32768.0 - -int ads1115_open(const struct ads1115_dev *ads1115) -{ - int res; - - assert(NULL != ads1115); - - syslog(LOG_DEBUG, "Trying to open i2c_bb.\n"); - res = i2c_open(ads1115->i2c_dev); - if(res != 0) { - syslog(LOG_ERR, "Unable to open i2c_bb.\n"); - return res; - } - syslog(LOG_DEBUG, "i2c dev successfully opened.\n"); - - return 0; -} - -int ads1115_close(const struct ads1115_dev *ads1115) -{ - assert(NULL != ads1115); - - return i2c_close(ads1115->i2c_dev); -} - -int ads1115_start_conversation(const struct ads1115_dev *ads1115) -{ - int res; - char buffer[BUF_SIZE]; - - assert(NULL != ads1115); - - syslog(LOG_DEBUG, "ads1115 start conversation.\n"); - - /* FIXME: prevent hard coded config. */ - buffer[0] = 0b00000001; // config register @ address 1 - buffer[1] = 0b11000001; // config value high byte - buffer[2] = 0b10000111; // config value low byte - - // begin conversation - res = i2c_write(ads1115->i2c_dev, ads1115->i2c_slave_address, buffer, BUF_SIZE); - if(res != BUF_SIZE) { - syslog(LOG_ERR, "Unable to write to ADS1115.\n"); - i2c_close(ads1115->i2c_dev); - return res; - } - syslog(LOG_DEBUG, "ads1115 conversation done.\n"); - - return 0; -} - -int ads1115_read_conversation_result(const struct ads1115_dev *ads1115, - struct ads1115_conversation_result *result) -{ - int res; - char buffer[BUF_SIZE]; - - assert(NULL != ads1115); - assert(NULL != result); - - // wait for conversation complete - do { - res = i2c_read(ads1115->i2c_dev, ads1115->i2c_slave_address, buffer, 2); - if(res != 2) { - syslog(LOG_ERR, "Unable to read from ADS1115.\n"); - return res; - } - } while((buffer[0] & 0x80) == 0); - - // read conversation result - buffer[0] = 0b00000000; /* conversation register at address 0b00000000 */ - res = i2c_write(ads1115->i2c_dev, ads1115->i2c_slave_address, buffer, 1); - if(res != 1) { - syslog(LOG_ERR, "Unable to write to ADS1115.\n"); - return res; - } - - // read 2 bytes - res = i2c_read(ads1115->i2c_dev, ads1115->i2c_slave_address, buffer, 2); - if(res != 2) { - syslog(LOG_ERR, "Unable to read from ADS1115.\n"); - return res; - } - result->raw = buffer[0] << 8 | buffer[1]; - result->voltage = result->raw * VPS; - - return 0; -} diff --git a/src/main.c b/src/main.c index c0b0570..b5c796e 100644 --- a/src/main.c +++ b/src/main.c @@ -7,7 +7,7 @@ #include #include -#include +#include static struct gpio_sys gpio_sda = { .pin = 4,