mirror of
https://github.com/eclipse/upm.git
synced 2025-07-12 06:41:12 +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,6 +26,8 @@
|
||||
#include <signal.h>
|
||||
|
||||
#include "nmea_gps.h"
|
||||
#include "upm_utilities.h"
|
||||
#include "mraa.h"
|
||||
|
||||
bool shouldRun = true;
|
||||
|
||||
@ -33,50 +35,56 @@ const size_t bufferLength = 128;
|
||||
|
||||
void sig_handler(int signo)
|
||||
{
|
||||
if (signo == SIGINT)
|
||||
shouldRun = false;
|
||||
if (signo == SIGINT)
|
||||
shouldRun = false;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
//! [Interesting]
|
||||
|
||||
// Instantiate a NMEA_GPS UBLOX based i2c sensor on i2c bus 0 at
|
||||
// address 0x42
|
||||
nmea_gps_context sensor = nmea_gps_init_ublox_i2c(0, 0x42);
|
||||
|
||||
if (!sensor)
|
||||
if (mraa_init() != MRAA_SUCCESS)
|
||||
{
|
||||
printf("nmea_gps_init_ublox_i2c() failed.\n");
|
||||
return 1;
|
||||
perror("Failed to initialize mraa\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char buffer[bufferLength];
|
||||
int rv = 0;
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
// loop, dumping NMEA data out as fast as it comes in
|
||||
while (shouldRun)
|
||||
//! [Interesting]
|
||||
|
||||
// Instantiate a NMEA_GPS UBLOX based i2c sensor on i2c bus 0 at
|
||||
// address 0x42
|
||||
nmea_gps_context sensor = nmea_gps_init_ublox_i2c(0, 0x42);
|
||||
|
||||
if (!sensor)
|
||||
{
|
||||
if (!nmea_gps_data_available(sensor, 0))
|
||||
usleep(100);
|
||||
else
|
||||
printf("nmea_gps_init_ublox_i2c() failed.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char buffer[bufferLength];
|
||||
int rv = 0;
|
||||
|
||||
// loop, dumping NMEA data out as fast as it comes in
|
||||
while (shouldRun)
|
||||
{
|
||||
if (!nmea_gps_data_available(sensor, 0))
|
||||
upm_delay_ms(500);
|
||||
else
|
||||
{
|
||||
if ((rv = nmea_gps_read(sensor, buffer, bufferLength)) >= 0)
|
||||
if ((rv = nmea_gps_read(sensor, buffer, bufferLength)) >= 0)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<rv; i++)
|
||||
printf("%c", buffer[i]);
|
||||
int i;
|
||||
for (i=0; i<rv; i++)
|
||||
printf("%c", buffer[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//! [Interesting]
|
||||
//! [Interesting]
|
||||
|
||||
printf("Exiting\n");
|
||||
printf("Exiting\n");
|
||||
|
||||
nmea_gps_close(sensor);
|
||||
nmea_gps_close(sensor);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user