mirror of
https://github.com/eclipse/upm.git
synced 2025-07-03 10:21:19 +03:00
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:
@ -26,52 +26,60 @@
|
||||
#include <signal.h>
|
||||
|
||||
#include "sht1x.h"
|
||||
#include "upm_utilities.h"
|
||||
#include "mraa.h"
|
||||
|
||||
bool shouldRun = true;
|
||||
|
||||
void sig_handler(int signo)
|
||||
{
|
||||
if (signo == SIGINT)
|
||||
shouldRun = false;
|
||||
if (signo == SIGINT)
|
||||
shouldRun = false;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
//! [Interesting]
|
||||
|
||||
// Instantiate a SHT1X sensor using D2 as the clock, and D3 as the
|
||||
// data pin.
|
||||
sht1x_context sensor = sht1x_init(2, 3);
|
||||
|
||||
if (!sensor)
|
||||
if (mraa_init() != MRAA_SUCCESS)
|
||||
{
|
||||
printf("sht1x_init() failed.\n");
|
||||
return 1;
|
||||
perror("Failed to initialize mraa\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Every 2 seconds, update and print values
|
||||
while (shouldRun)
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
//! [Interesting]
|
||||
|
||||
// Instantiate a SHT1X sensor using D2 as the clock, and D3 as the
|
||||
// data pin.
|
||||
sht1x_context sensor = sht1x_init(2, 3);
|
||||
|
||||
if (!sensor)
|
||||
{
|
||||
if (sht1x_update(sensor))
|
||||
printf("sht1x_init() failed.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Every 2 seconds, update and print values
|
||||
while (shouldRun)
|
||||
{
|
||||
if (sht1x_update(sensor))
|
||||
{
|
||||
printf("sht1x_update() failed, exiting.\n");
|
||||
break;
|
||||
printf("sht1x_update() failed, exiting.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
printf("Temperature: %f C\n", sht1x_get_temperature(sensor));
|
||||
printf("Humidity: %f RH\n", sht1x_get_humidity(sensor));
|
||||
printf("\n");
|
||||
printf("Temperature: %f C\n", sht1x_get_temperature(sensor));
|
||||
printf("Humidity: %f RH\n", sht1x_get_humidity(sensor));
|
||||
printf("\n");
|
||||
|
||||
sleep(2);
|
||||
upm_delay(2);
|
||||
}
|
||||
|
||||
//! [Interesting]
|
||||
//! [Interesting]
|
||||
|
||||
printf("Exiting\n");
|
||||
printf("Exiting\n");
|
||||
|
||||
sht1x_close(sensor);
|
||||
sht1x_close(sensor);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user