move ada1115 to separate lib.
This commit is contained in:
parent
48c63bb9df
commit
78b36fcbb2
4
Makefile
4
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
|
||||
|
@ -1,23 +0,0 @@
|
||||
#ifndef __ADS1115__
|
||||
#define __ADS1115__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <libi2c_bb/i2c_bb.h>
|
||||
|
||||
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
|
100
src/ads1115.c
100
src/ads1115.c
@ -1,100 +0,0 @@
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <syslog.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <ads1115.h>
|
||||
|
||||
#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;
|
||||
}
|
@ -7,7 +7,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include <libgpio_sys/gpio_sys.h>
|
||||
#include <ads1115.h>
|
||||
#include <libads1115/ads1115.h>
|
||||
|
||||
static struct gpio_sys gpio_sda = {
|
||||
.pin = 4,
|
||||
|
Loading…
Reference in New Issue
Block a user