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

@ -7,8 +7,17 @@
#include <unistd.h>
#include "a110x.h"
#include "upm_utilities.h"
#include "mraa.h"
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
a110x_context dev = a110x_init(2);
bool abc = 0;
while(1){

View File

@ -26,6 +26,8 @@
#include <signal.h>
#include "bh1750.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -62,7 +70,7 @@ int main()
bh1750_get_lux(sensor, &lux);
printf("Detected Light Level (lux): %f\n", lux);
sleep(1);
upm_delay(1);
}
//! [Interesting]

View File

@ -7,8 +7,17 @@
#include <unistd.h>
#include "collision.h"
#include "upm_utilities.h"
#include "mraa.h"
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
collision_context dev = collision_init(2);
bool abc = 0;
while(1){

View File

@ -26,6 +26,8 @@
#include <signal.h>
#include "dfrec.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
{
if (mraa_init() != MRAA_SUCCESS)
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -63,7 +71,7 @@ int main()
dfrec_get_volts(sensor), dfrec_get_temperature(sensor));
printf("\n");
sleep(2);
upm_delay(2);
}
//! [Interesting]

View File

@ -26,6 +26,8 @@
#include <signal.h>
#include "dfrorp.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -73,7 +81,7 @@ int main()
printf("ORP = %f mV\n", dfrorp_get_orp(sensor));
sleep(1);
upm_delay(1);
}
//! [Interesting]

View File

@ -1,5 +1,5 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -26,6 +26,8 @@
#include <signal.h>
#include "dfrph.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -61,7 +69,7 @@ int main()
printf("Detected volts: %0.03f\n", volts);
printf("pH value: %0.03f\n", pH);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -27,6 +27,8 @@
#include <signal.h>
#include "ds18b20.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -38,6 +40,12 @@ void sig_handler(int signo)
int main(int argc, char **argv)
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -69,7 +77,7 @@ int main(int argc, char **argv)
}
printf("\n");
sleep(2);
upm_delay(2);
}
printf("Exiting...\n");

View File

@ -1,5 +1,5 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -26,6 +26,8 @@
#include <signal.h>
#include "emg.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -73,7 +81,7 @@ int main()
printf("Normalized output: %0.03f, raw emg sensor output: %0.03f v "
"adjusted output: %0.03f v\n", normalized, raw_volts, volts);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -1,5 +1,5 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -26,6 +26,8 @@
#include <signal.h>
#include "flex.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -73,7 +81,7 @@ int main()
printf("Normalized output: %0.03f, raw flex sensor output: %0.03f v "
"adjusted output: %0.03f v\n", normalized, raw_volts, volts);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -7,8 +7,17 @@
#include <unistd.h>
#include "gp2y0a.h"
#include "upm_utilities.h"
#include "mraa.h"
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
gp2y0a_context dev = gp2y0a_init(14, 5.0);
float val;
while(1){

View File

@ -7,8 +7,17 @@
#include <unistd.h>
#include "grovemoisture.h"
#include "upm_utilities.h"
#include "mraa.h"
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
grovemoisture_context dev = grovemoisture_init(14);
int val;
while(1){

View File

@ -1,5 +1,5 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -26,6 +26,8 @@
#include <signal.h>
#include "gsr.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -73,7 +81,7 @@ int main()
printf("Normalized output: %0.03f, raw gsr sensor output: %0.03f v "
"adjusted output: %0.03f v\n", normalized, raw_volts, volts);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -26,6 +26,8 @@
#include <signal.h>
#include "hka5.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -65,7 +73,7 @@ int main()
printf("PM 10 : %d ug/m3\n", hka5_get_pm10(sensor));
printf("\n");
sleep(2);
upm_delay(2);
}
//! [Interesting]

View File

@ -1,5 +1,5 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -26,6 +26,9 @@
#include <signal.h>
#include "joystick12.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +40,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -73,7 +82,7 @@ int main()
joystick12_get_value_y(sensor, &y);
printf("X: %5.02f Y: %5.02f\n", x, y);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -1,5 +1,5 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -26,6 +26,8 @@
#include <signal.h>
#include "ldt0028.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -82,7 +90,7 @@ int main()
"adjusted output: %0.03f v\n\n", normalized, raw_volts, volts);
}
usleep(50000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -7,8 +7,17 @@
#include <unistd.h>
#include "led.h"
void main(void)
#include "upm_utilities.h"
#include "mraa.h"
int main(void)
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
led_context dev = led_init(2);
while(1){
if(led_on(dev) != UPM_SUCCESS){
@ -21,4 +30,5 @@ void main(void)
upm_delay(1);
}
led_close(dev);
return 0;
}

View File

@ -1,5 +1,5 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -26,6 +26,8 @@
#include <signal.h>
#include "light.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -73,7 +81,7 @@ int main()
printf("Normalized output: %0.03f, raw light sensor output: %0.03f v "
"light output: %0.03f lux\n", normalized, raw_volts, lux);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -7,8 +7,17 @@
#include <unistd.h>
#include "loudness.h"
#include "upm_utilities.h"
#include "mraa.h"
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
loudness_context dev = loudness_init(14);
int val;
while(1){

View File

@ -5,8 +5,17 @@
#include <unistd.h>
#include "m24lr64e.h"
#include "upm_utilities.h"
#include "mraa.h"
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
m24lr64e_context dev = m24lr64e_init(0, M24LR64E_USER_MODE);
int addr = M24LR64E_EEPROM_I2C_LENGTH-1;
printf("address being accessed: %d\n", addr);

View File

@ -26,6 +26,8 @@
#include <signal.h>
#include "mma7361.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -72,7 +80,7 @@ int main()
printf("Volts x = %f y = %f z = %f\n\n",
x, y, z);
usleep(100000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -7,8 +7,17 @@
#include <unistd.h>
#include "moisture.h"
#include "upm_utilities.h"
#include "mraa.h"
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
moisture_context dev = moisture_init(14);
int val;
while(1){
@ -22,4 +31,3 @@ int main()
return 0;
}

View File

@ -7,8 +7,17 @@
#include <unistd.h>
#include "mpr121.h"
#include "upm_utilities.h"
#include "mraa.h"
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
mpr121_context dev = mpr121_init(MPR121_I2C_BUS, MPR121_DEFAULT_I2C_ADDR);
if(mpr121_config_an3944(dev) != UPM_SUCCESS){

View File

@ -7,7 +7,16 @@
#include <unistd.h>
#include "mq303a.h"
#include "upm_utilities.h"
#include "mraa.h"
int main() {
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
/* --------- MQ303A EXAMPLE -------- */
mq303a_context dev = mq303a_init(0, 15);
@ -23,4 +32,3 @@ int main() {
return 0;
}

View File

@ -1,5 +1,5 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -26,6 +26,8 @@
#include <signal.h>
#include "mqx.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -73,7 +81,7 @@ int main()
printf("Normalized output: %0.03f, raw mqx sensor output: %0.03f v "
"adjusted output: %0.03f v\n", normalized, raw_volts, volts);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -26,6 +26,8 @@
#include <signal.h>
#include "nmea_gps.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -39,6 +41,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]

View File

@ -26,6 +26,8 @@
#include <signal.h>
#include "nmea_gps.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -39,6 +41,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -60,7 +68,7 @@ int main()
while (shouldRun)
{
if (!nmea_gps_data_available(sensor, 0))
usleep(100);
upm_delay_ms(500);
else
{
if ((rv = nmea_gps_read(sensor, buffer, bufferLength)) >= 0)

View File

@ -1,5 +1,5 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -26,6 +26,8 @@
#include <signal.h>
#include "o2.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -62,7 +70,7 @@ int main()
printf("O2 raw volts: %0.03f v, o2: %0.03f %%\n",
raw_volts, o2_percent);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -7,8 +7,17 @@
#include <unistd.h>
#include "es08a.c"
#include "upm_utilities.h"
#include "mraa.h"
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
es08a_context dev = es08a_init(3, ES08A_MIN_PULSE_WIDTH, ES08A_MAX_PULSE_WIDTH);
if(dev == NULL){
printf("unable to initialize the servo context\n");

View File

@ -26,6 +26,8 @@
#include <signal.h>
#include "sht1x.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -64,7 +72,7 @@ int main()
printf("Humidity: %f RH\n", sht1x_get_humidity(sensor));
printf("\n");
sleep(2);
upm_delay(2);
}
//! [Interesting]

View File

@ -1,5 +1,5 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -26,6 +26,8 @@
#include <signal.h>
#include "slide.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -73,7 +81,7 @@ int main()
printf("Normalized output: %0.03f, raw slide sensor output: %0.03f v "
"adjusted output: %0.03f v\n", normalized, raw_volts, volts);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -7,8 +7,17 @@
#include <unistd.h>
#include "tsl2561.h"
#include "upm_utilities.h"
#include "mraa.h"
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
tsl2561_context dev = tsl2561_init(0, TSL2561_Address, GAIN_0X, INTEGRATION_TIME1_101MS);
float abc = 0;
if(tsl2561_get_lux(dev, &abc) != UPM_SUCCESS){

View File

@ -7,8 +7,17 @@
#include <unistd.h>
#include "ttp223.h"
#include "upm_utilities.h"
#include "mraa.h"
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
ttp223_context dev = ttp223_init(2);
bool abc = 0;
while(1){

View File

@ -26,6 +26,8 @@
#include <signal.h>
#include "urm37.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -62,7 +70,7 @@ int main()
urm37_get_temperature(sensor, &temperature);
printf("Temperature (C): %f\n\n", temperature);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -26,6 +26,8 @@
#include <signal.h>
#include "urm37.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -60,7 +68,7 @@ int main()
urm37_get_distance(sensor, &distance, 0);
printf("Detected distance (cm): %f\n", distance);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]

View File

@ -1,5 +1,5 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -26,6 +26,8 @@
#include <signal.h>
#include "vdiv.h"
#include "upm_utilities.h"
#include "mraa.h"
bool shouldRun = true;
@ -37,6 +39,12 @@ void sig_handler(int signo)
int main()
{
if (mraa_init() != MRAA_SUCCESS)
{
perror("Failed to initialize mraa\n");
return -1;
}
signal(SIGINT, sig_handler);
//! [Interesting]
@ -61,7 +69,7 @@ int main()
printf("Divide SW: %d ADC voltage: %0.03f Sensor voltage: %0.03f\n",
vdiv_get_divsw(sensor), raw_volts, computed_volts);
usleep(500000);
upm_delay_ms(500);
}
//! [Interesting]