mirror of
https://github.com/eclipse/upm.git
synced 2025-07-03 02:11:15 +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:
@ -27,6 +27,8 @@
|
||||
#include <signal.h>
|
||||
|
||||
#include "ds18b20.h"
|
||||
#include "upm_utilities.h"
|
||||
#include "mraa.h"
|
||||
|
||||
bool shouldRun = true;
|
||||
|
||||
@ -38,44 +40,50 @@ void sig_handler(int signo)
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
//! [Interesting]
|
||||
|
||||
printf("Initializing...\n");
|
||||
|
||||
// Instantiate an DS18B20 instance using the uart 0
|
||||
ds18b20_context sensor = ds18b20_init(0);
|
||||
|
||||
if (!sensor)
|
||||
if (mraa_init() != MRAA_SUCCESS)
|
||||
{
|
||||
printf("ds18b20_init() failed.\n");
|
||||
return(1);
|
||||
perror("Failed to initialize mraa\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Found %d device(s)\n\n", ds18b20_devices_found(sensor));
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
// update and print available values every second
|
||||
while (shouldRun)
|
||||
//! [Interesting]
|
||||
|
||||
printf("Initializing...\n");
|
||||
|
||||
// Instantiate an DS18B20 instance using the uart 0
|
||||
ds18b20_context sensor = ds18b20_init(0);
|
||||
|
||||
if (!sensor)
|
||||
{
|
||||
// update our values for all sensors
|
||||
ds18b20_update(sensor, -1);
|
||||
printf("ds18b20_init() failed.\n");
|
||||
return(1);
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i=0; i<ds18b20_devices_found(sensor); i++)
|
||||
printf("Found %d device(s)\n\n", ds18b20_devices_found(sensor));
|
||||
|
||||
// update and print available values every second
|
||||
while (shouldRun)
|
||||
{
|
||||
// update our values for all sensors
|
||||
ds18b20_update(sensor, -1);
|
||||
|
||||
int i;
|
||||
for (i=0; i<ds18b20_devices_found(sensor); i++)
|
||||
{
|
||||
printf("Device %02d: Temperature: %f C\n",
|
||||
i, ds18b20_get_temperature(sensor, i));
|
||||
printf("Device %02d: Temperature: %f C\n",
|
||||
i, ds18b20_get_temperature(sensor, i));
|
||||
}
|
||||
printf("\n");
|
||||
printf("\n");
|
||||
|
||||
sleep(2);
|
||||
upm_delay(2);
|
||||
}
|
||||
|
||||
printf("Exiting...\n");
|
||||
printf("Exiting...\n");
|
||||
|
||||
ds18b20_close(sensor);
|
||||
//! [Interesting]
|
||||
ds18b20_close(sensor);
|
||||
//! [Interesting]
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user