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 "hka5.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,42 +39,48 @@ void sig_handler(int signo)
int main()
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a HKA5 sensor on uart 0. We don't use the set or
// reset pins, so we pass -1 for them.
hka5_context sensor = hka5_init(0, -1, -1);
if (!sensor)
if (mraa_init() != MRAA_SUCCESS)
{
printf("hka5_init() failed.\n");
return 1;
perror("Failed to initialize mraa\n");
return -1;
}
// update once every 2 seconds and output data
while (shouldRun)
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a HKA5 sensor on uart 0. We don't use the set or
// reset pins, so we pass -1 for them.
hka5_context sensor = hka5_init(0, -1, -1);
if (!sensor)
{
if (hka5_update(sensor) != UPM_SUCCESS)
printf("hka5_init() failed.\n");
return 1;
}
// update once every 2 seconds and output data
while (shouldRun)
{
if (hka5_update(sensor) != UPM_SUCCESS)
{
printf("hka5_update() failed, exiting.\n");
shouldRun = false;
printf("hka5_update() failed, exiting.\n");
shouldRun = false;
}
printf("PM 1 : %d ug/m3\n", hka5_get_pm1(sensor));
printf("PM 2.5: %d ug/m3\n", hka5_get_pm2_5(sensor));
printf("PM 10 : %d ug/m3\n", hka5_get_pm10(sensor));
printf("\n");
printf("PM 1 : %d ug/m3\n", hka5_get_pm1(sensor));
printf("PM 2.5: %d ug/m3\n", hka5_get_pm2_5(sensor));
printf("PM 10 : %d ug/m3\n", hka5_get_pm10(sensor));
printf("\n");
sleep(2);
upm_delay(2);
}
//! [Interesting]
//! [Interesting]
printf("Exiting\n");
printf("Exiting\n");
hka5_close(sensor);
hka5_close(sensor);
return 0;
return 0;
}