distance_app: Measure continuously until 'Ctrl+c' is pressed.
This commit is contained in:
parent
78b36fcbb2
commit
b4f46eb2e5
40
src/main.c
40
src/main.c
@ -5,6 +5,7 @@
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include <libgpio_sys/gpio_sys.h>
|
||||
#include <libads1115/ads1115.h>
|
||||
@ -29,32 +30,49 @@ static struct ads1115_dev ads = {
|
||||
.i2c_slave_address = 0x48,
|
||||
};
|
||||
|
||||
static bool run_condition = true;
|
||||
|
||||
static void signal_handler(int sig)
|
||||
{
|
||||
printf("Signal catched: %d\n Shutting down...\n", sig);
|
||||
run_condition = false;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int res;
|
||||
struct ads1115_conversation_result result;
|
||||
|
||||
struct sigaction sig_int_handler;
|
||||
|
||||
sig_int_handler.sa_handler = signal_handler;
|
||||
sigemptyset(&sig_int_handler.sa_mask);
|
||||
sig_int_handler.sa_flags = 0;
|
||||
|
||||
sigaction(SIGINT, &sig_int_handler, NULL);
|
||||
|
||||
res = ads1115_open(&ads);
|
||||
if(res != 0) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = ads1115_start_conversation(&ads);
|
||||
if(res != 0) {
|
||||
ads1115_close(&ads);
|
||||
return res;
|
||||
}
|
||||
while(run_condition) {
|
||||
res = ads1115_start_conversation(&ads);
|
||||
if(res != 0) {
|
||||
ads1115_close(&ads);
|
||||
return res;
|
||||
}
|
||||
|
||||
res = ads1115_read_conversation_result(&ads, &result);
|
||||
if(res != 0) {
|
||||
ads1115_close(&ads);
|
||||
return res;
|
||||
res = ads1115_read_conversation_result(&ads, &result);
|
||||
if(res != 0) {
|
||||
ads1115_close(&ads);
|
||||
return res;
|
||||
}
|
||||
printf("Conversation: %4.3fV (0x%04x).\n", result.voltage, result.raw);
|
||||
usleep(50000);
|
||||
}
|
||||
|
||||
ads1115_close(&ads);
|
||||
|
||||
printf("Conversation: %4.3fV (0x%04x).\n", result.voltage, result.raw);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user