examples: make main loops finite to ensure proper cleanup
Signed-off-by: Alex Tereschenko <alext.mkrs@gmail.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
32340f6819
commit
c36e4add5a
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Author: Nandkishor Sonar
|
||||
* Contributors: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
@@ -23,30 +24,49 @@
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
//! [Interesting]
|
||||
#include <signal.h>
|
||||
|
||||
#include "mraa/aio.h"
|
||||
|
||||
int running = 0;
|
||||
|
||||
void
|
||||
sig_handler(int signo)
|
||||
{
|
||||
if (signo == SIGINT) {
|
||||
printf("closing down nicely\n");
|
||||
running = -1;
|
||||
}
|
||||
}
|
||||
|
||||
//! [Interesting]
|
||||
int
|
||||
main()
|
||||
{
|
||||
mraa_aio_context adc_a0;
|
||||
uint16_t adc_value = 0;
|
||||
float adc_value_float = 0.0;
|
||||
mraa_result_t r = MRAA_SUCCESS;
|
||||
|
||||
adc_a0 = mraa_aio_init(0);
|
||||
if (adc_a0 == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
while (running == 0) {
|
||||
adc_value = mraa_aio_read(adc_a0);
|
||||
adc_value_float = mraa_aio_read_float(adc_a0);
|
||||
fprintf(stdout, "ADC A0 read %X - %d\n", adc_value, adc_value);
|
||||
fprintf(stdout, "ADC A0 read float - %.5f\n", adc_value_float);
|
||||
fprintf(stdout, "ADC A0 read float - %.5f (Ctrl+C to exit)\n", adc_value_float);
|
||||
}
|
||||
|
||||
mraa_aio_close(adc_a0);
|
||||
r = mraa_aio_close(adc_a0);
|
||||
if (r != MRAA_SUCCESS) {
|
||||
mraa_result_print(r);
|
||||
}
|
||||
|
||||
return MRAA_SUCCESS;
|
||||
return r;
|
||||
}
|
||||
//! [Interesting]
|
||||
|
||||
Reference in New Issue
Block a user