examples: Cleaned up C examples.

This commit cleans up multiple items with the UPM C example source.

    * Switch from usleep and sleep to upm_delay* methods
    * Include a mraa_init and check return value prior to using sensor
    * All example mains now return
    * Added include for mraa.h and upm_utilites.h to all examples
    * Reformatted/removed tabs
    * Updated author line for the examples I wrote

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck
2016-09-19 15:45:22 -07:00
parent 2ccdc3e673
commit 324af8fc92
35 changed files with 775 additions and 482 deletions

View File

@ -26,6 +26,8 @@
#include <signal.h>
#include "urm37.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,37 +39,43 @@ void sig_handler(int signo)
int main()
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a URM37 sensor on analog pin A0, reset pin on D2,
// trigger pin on D3 with an analog reference voltage of 5.0
urm37_context sensor = urm37_init(0, 2, 3, 5.0, 0, true);
if (!sensor)
if (mraa_init() != MRAA_SUCCESS)
{
printf("urm37_init() failed.\n");
return(1);
perror("Failed to initialize mraa\n");
return -1;
}
// Every half a second, sample the URM37 and output the measured
// distance in cm.
signal(SIGINT, sig_handler);
while (shouldRun)
//! [Interesting]
// Instantiate a URM37 sensor on analog pin A0, reset pin on D2,
// trigger pin on D3 with an analog reference voltage of 5.0
urm37_context sensor = urm37_init(0, 2, 3, 5.0, 0, true);
if (!sensor)
{
float distance;
urm37_get_distance(sensor, &distance, 0);
printf("Detected distance (cm): %f\n", distance);
usleep(500000);
printf("urm37_init() failed.\n");
return(1);
}
//! [Interesting]
// Every half a second, sample the URM37 and output the measured
// distance in cm.
printf("Exiting\n");
while (shouldRun)
{
float distance;
urm37_close(sensor);
urm37_get_distance(sensor, &distance, 0);
printf("Detected distance (cm): %f\n", distance);
upm_delay_ms(500);
}
return 0;
//! [Interesting]
printf("Exiting\n");
urm37_close(sensor);
return 0;
}