Compare commits

...

No commits in common. "5aaccc39e54a9c97ad1bd52f1e4ddff4833a9306" and "89ba05379f134b5ec37cc8db710568bf61b27d52" have entirely different histories.

6 changed files with 97 additions and 186 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ bin/
lib/
obj/
coverage/

View File

@ -1,17 +0,0 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/gpio_ftdi"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}

47
.vscode/launch.json vendored
View File

@ -1,47 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch distance",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/distance",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "(gdb) Launch unit test",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/test/unit/distance",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

115
.vscode/tasks.json vendored
View File

@ -1,115 +0,0 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "all",
"type":"shell",
"command": "make all -j8",
"problemMatcher": {
"base": "$gcc",
"owner": "gcc",
"fileLocation": [
"relative",
"${workspaceFolder}"
]
},
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "check",
"type":"shell",
"command": "make check -j8",
"problemMatcher": {
"base": "$gcc",
"owner": "gcc",
"fileLocation": [
"relative",
"${workspaceFolder}"
]
},
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "clean",
"type":"shell",
"command": "make clean -j8",
"problemMatcher": {
"base": "$gcc",
"owner": "gcc",
"fileLocation": [
"relative",
"${workspaceFolder}"
]
},
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "build_unit_test",
"type":"shell",
"command": "make build_unit_test -j8",
"problemMatcher": {
"base": "$gcc",
"owner": "gcc",
"fileLocation": [
"relative",
"${workspaceFolder}"
]
},
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "exec_unit_test",
"type":"shell",
"command": "make exec_unit_test -j1",
"problemMatcher": {
"base": "$gcc",
"owner": "gcc",
"fileLocation": [
"relative",
"${workspaceFolder}"
]
},
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "coverage",
"type":"shell",
"command": "make coverage -j1",
"problemMatcher": {
"base": "$gcc",
"owner": "gcc",
"fileLocation": [
"relative",
"${workspaceFolder}"
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
],
"presentation": {
"focus": true,
"reveal": "always",
"panel": "shared",
"clear": true,
}
}

View File

@ -1,6 +1,6 @@
CROSS_COMPILE ?=
TARGET_FILE ?= distance
TARGET_FILE ?= blink
CC = $(CROSS_COMPILE)gcc
CPP = $(CROSS_COMPILE)cpp
@ -19,13 +19,13 @@ PREFIX ?= /usr
BIN_INSTALL_DIR ?= $(PREFIX)/bin/$(TARGET_FILE)
INCLUDES := inc
INCLUDES += /usr/include/libftdi1
INCLUDES += /usr/include/gpio_ftdi
LIBS = gpio_sys i2c_bb
ifneq "$(findstring $(MAKECMDGOALS), build_unit_test exec_unit_test coverage)" ""
INCLUDES += test/inc
C_FLAGS += --coverage
L_FLAGS += --coverage
LD_FLAGS += --coverage
endif
C_FLAGS += -fpic
@ -46,7 +46,7 @@ UNIT_TEST_TARGET = $(BIN_DIR)/$(UNIT_TEST_SRC_DIR)/$(TARGET_FILE)
TARGET = $(BIN_DIR)/$(TARGET_FILE)
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
.PHONY: all install uninstall clean
.PHONY: all install uninstall clean distclean
all: $(TARGET)
install: all
@ -65,6 +65,9 @@ clean:
rm -f $(OBJ_DIR)/*.gcda $(OBJ_DIR)/*.gcno
rm -fr $(COVERAGE_DIR)
distclean:
rm -rf $(OBJ_DIR) $(BIN_DIR) $(COVERAGE_DIR)
.PHONY: coverage
coverage: _cov_genhtml
$(eval COVERAGE:=$(shell grep 'lines' .coverage.tmp | egrep -o '[0-9]+.[0-9]+%'))

View File

@ -6,9 +6,95 @@
#include <unistd.h>
#include <stdio.h>
#include <libgpio_sys/gpio_sys.h>
#include <libi2c_bb/i2c_bb.h>
#define ADS1115_ADDR 0x48
#define VPS 6.144 / 32768.0
static struct gpio_sys gpio_sda = {
.pin = 4,
.direction = OUT,
};
static struct gpio_sys gpio_scl = {
.pin = 24,
.direction = OUT,
};
static struct i2c_bb i2c_obj = {
.sda = &gpio_sda,
.scl = &gpio_scl,
};
int main(void)
{
printf("Hello world!\n");
int res;
uint16_t value;
float f_value;
char w_buffer[3], r_buffer[2];
return EXIT_SUCCESS;
openlog("distance_app", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
printf("Trying to open I2C device.\n");
syslog(LOG_DEBUG, "Trying to open i2c_bb.\n");
res = i2c_open(&i2c_obj);
if(res != EXIT_SUCCESS) {
syslog(LOG_ERR, "Unable to open i2c_bb.\n");
return EXIT_FAILURE;
}
syslog(LOG_DEBUG, "i2c_bb successfully opened.\n");
w_buffer[0] = 0b00000001; // config register @ address 1
w_buffer[1] = 0b11000001; // config value high byte
w_buffer[2] = 0b10000111; // config value low byte
// begin conversation
res = i2c_write(&i2c_obj, ADS1115_ADDR, w_buffer, 3);
if(res != 3) {
syslog(LOG_ERR, "Unable to write to ADS1115.\n");
i2c_close(&i2c_obj);
return EXIT_FAILURE;
}
// wait for conversation complete
do {
res = i2c_read(&i2c_obj, ADS1115_ADDR, w_buffer, 2);
if(res != 2) {
syslog(LOG_ERR, "Unable to read from ADS1115.\n");
i2c_close(&i2c_obj);
return EXIT_FAILURE;
}
} while((w_buffer[0] & 0x80) == 0);
// read conversation result
r_buffer[0] = 0b00000000; /* conversation register at address 0b00000000 */
res = i2c_write(&i2c_obj, ADS1115_ADDR, r_buffer, 1);
if(res != 1) {
syslog(LOG_ERR, "Unable to write to ADS1115.\n");
i2c_close(&i2c_obj);
return EXIT_FAILURE;
}
// read 2 bytes
res = i2c_read(&i2c_obj, ADS1115_ADDR, r_buffer, 2);
if(res != 2) {
syslog(LOG_ERR, "Unable to read from ADS1115.\n");
i2c_close(&i2c_obj);
return EXIT_FAILURE;
}
printf("Conversation done [0x%02x:%02x].\n", r_buffer[0], r_buffer[1]);
i2c_close(&i2c_obj);
value = r_buffer[0] << 8 | r_buffer[1];
if(value & 0x8000) {
value = 0;
}
f_value = value * VPS;
printf("Conversation result: %4.3fV (0x%04x).\n", f_value, value);
return res;
}