From 324af8fc929d68780cc23bbab90924ef8b77e055 Mon Sep 17 00:00:00 2001 From: Noel Eck Date: Mon, 19 Sep 2016 15:45:22 -0700 Subject: [PATCH] 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 --- examples/c/a110x.c | 29 +++++++++----- examples/c/bh1750.c | 58 +++++++++++++++------------ examples/c/collision.c | 29 +++++++++----- examples/c/dfrec.c | 62 ++++++++++++++++------------- examples/c/dfrorp.c | 80 +++++++++++++++++++++----------------- examples/c/dfrph.c | 12 +++++- examples/c/ds18b20.c | 62 ++++++++++++++++------------- examples/c/emg.c | 12 +++++- examples/c/flex.c | 12 +++++- examples/c/gp2y0a.c | 31 +++++++++------ examples/c/grovemoisture.c | 31 +++++++++------ examples/c/gsr.c | 12 +++++- examples/c/hka5.c | 58 +++++++++++++++------------ examples/c/joystick12.c | 13 ++++++- examples/c/ldt0028.c | 12 +++++- examples/c/led.c | 36 ++++++++++------- examples/c/light.c | 12 +++++- examples/c/loudness.c | 31 +++++++++------ examples/c/m24lr64e.c | 45 ++++++++++++--------- examples/c/mma7361.c | 76 ++++++++++++++++++++---------------- examples/c/moisture.c | 32 +++++++++------ examples/c/mpr121.c | 39 ++++++++++++------- examples/c/mq303a.c | 34 +++++++++------- examples/c/mqx.c | 12 +++++- examples/c/nmea_gps.c | 58 +++++++++++++++------------ examples/c/nmea_gps_i2c.c | 64 +++++++++++++++++------------- examples/c/o2.c | 12 +++++- examples/c/servo.c | 45 ++++++++++++--------- examples/c/sht1x.c | 60 +++++++++++++++------------- examples/c/slide.c | 12 +++++- examples/c/tsl2561.c | 23 +++++++---- examples/c/ttp223.c | 29 +++++++++----- examples/c/urm37-uart.c | 58 +++++++++++++++------------ examples/c/urm37.c | 54 ++++++++++++++----------- examples/c/vdiv.c | 12 +++++- 35 files changed, 775 insertions(+), 482 deletions(-) diff --git a/examples/c/a110x.c b/examples/c/a110x.c index fdef3146..441b7271 100644 --- a/examples/c/a110x.c +++ b/examples/c/a110x.c @@ -7,17 +7,26 @@ #include #include "a110x.h" +#include "upm_utilities.h" +#include "mraa.h" + int main() { - a110x_context dev = a110x_init(2); - bool abc = 0; - while(1){ - if(a110x_magnet_detected(dev, &abc) != UPM_SUCCESS){ - printf("an error has occured\n"); - } - upm_delay(1); - printf("value retrieved: %d\n", abc); - } + if (mraa_init() != MRAA_SUCCESS) + { + perror("Failed to initialize mraa\n"); + return -1; + } - return 0; + a110x_context dev = a110x_init(2); + bool abc = 0; + while(1){ + if(a110x_magnet_detected(dev, &abc) != UPM_SUCCESS){ + printf("an error has occured\n"); + } + upm_delay(1); + printf("value retrieved: %d\n", abc); + } + + return 0; } diff --git a/examples/c/bh1750.c b/examples/c/bh1750.c index ce0b8a50..5af44f70 100644 --- a/examples/c/bh1750.c +++ b/examples/c/bh1750.c @@ -26,6 +26,8 @@ #include #include "bh1750.h" +#include "upm_utilities.h" +#include "mraa.h" bool shouldRun = true; @@ -37,39 +39,45 @@ void sig_handler(int signo) int main() { - signal(SIGINT, sig_handler); - -//! [Interesting] - - // Instantiate a BH1750 sensor on default I2C bus (0), using the - // default I2C address (0x23), and setting the mode to highest - // resolution, lowest power mode. - bh1750_context sensor = bh1750_init(BH1750_DEFAULT_I2C_BUS, - BH1750_DEFAULT_I2C_ADDR, - BH1750_OPMODE_H2_ONCE); - - if (!sensor) + if (mraa_init() != MRAA_SUCCESS) { - printf("bh1750_init() failed.\n"); - return 1; + perror("Failed to initialize mraa\n"); + return -1; } - // Every second, sample the BH1750 and output the measured lux value + signal(SIGINT, sig_handler); - while (shouldRun) + //! [Interesting] + + // Instantiate a BH1750 sensor on default I2C bus (0), using the + // default I2C address (0x23), and setting the mode to highest + // resolution, lowest power mode. + bh1750_context sensor = bh1750_init(BH1750_DEFAULT_I2C_BUS, + BH1750_DEFAULT_I2C_ADDR, + BH1750_OPMODE_H2_ONCE); + + if (!sensor) { - float lux; - - bh1750_get_lux(sensor, &lux); - printf("Detected Light Level (lux): %f\n", lux); - sleep(1); + printf("bh1750_init() failed.\n"); + return 1; } -//! [Interesting] + // Every second, sample the BH1750 and output the measured lux value - printf("Exiting\n"); + while (shouldRun) + { + float lux; - bh1750_close(sensor); + bh1750_get_lux(sensor, &lux); + printf("Detected Light Level (lux): %f\n", lux); + upm_delay(1); + } - return 0; + //! [Interesting] + + printf("Exiting\n"); + + bh1750_close(sensor); + + return 0; } diff --git a/examples/c/collision.c b/examples/c/collision.c index df3bce71..bbd8862f 100644 --- a/examples/c/collision.c +++ b/examples/c/collision.c @@ -7,16 +7,25 @@ #include #include "collision.h" +#include "upm_utilities.h" +#include "mraa.h" + int main() { - collision_context dev = collision_init(2); - bool abc = 0; - while(1){ - if(collision_is_colliding(dev, &abc) != UPM_SUCCESS){ - printf("an error has occured\n"); - } - upm_delay(1); - printf("value retrieved: %d\n", abc); - } - collision_close(dev); + if (mraa_init() != MRAA_SUCCESS) + { + perror("Failed to initialize mraa\n"); + return -1; + } + + collision_context dev = collision_init(2); + bool abc = 0; + while(1){ + if(collision_is_colliding(dev, &abc) != UPM_SUCCESS){ + printf("an error has occured\n"); + } + upm_delay(1); + printf("value retrieved: %d\n", abc); + } + collision_close(dev); } diff --git a/examples/c/dfrec.c b/examples/c/dfrec.c index 7177fb63..c2e8b855 100644 --- a/examples/c/dfrec.c +++ b/examples/c/dfrec.c @@ -26,6 +26,8 @@ #include #include "dfrec.h" +#include "upm_utilities.h" +#include "mraa.h" bool shouldRun = true; @@ -37,40 +39,46 @@ void sig_handler(int signo) int main() { - signal(SIGINT, sig_handler); - -//! [Interesting] - - // Instantiate a DFRobot EC sensor on analog pin A0, with a ds18b20 - // temperature sensor connected to UART 0, and a device index (for - // the ds1820b uart bus) of 0, and an analog reference voltage of - // 5.0. - dfrec_context sensor = dfrec_init(0, 0, 0, 5.0); - - if (!sensor) { - printf("dfrec_init() failed.\n"); - return(1); + if (mraa_init() != MRAA_SUCCESS) + perror("Failed to initialize mraa\n"); + return -1; } - // Every 2 seconds, update and print values - while (shouldRun) + signal(SIGINT, sig_handler); + + //! [Interesting] + + // Instantiate a DFRobot EC sensor on analog pin A0, with a ds18b20 + // temperature sensor connected to UART 0, and a device index (for + // the ds1820b uart bus) of 0, and an analog reference voltage of + // 5.0. + dfrec_context sensor = dfrec_init(0, 0, 0, 5.0); + + if (!sensor) { - dfrec_update(sensor); - - printf("EC = %f ms/cm\n", dfrec_get_ec(sensor)); - printf("Volts = %f, Temperature = %f C\n", - dfrec_get_volts(sensor), dfrec_get_temperature(sensor)); - printf("\n"); - - sleep(2); + printf("dfrec_init() failed.\n"); + return(1); } -//! [Interesting] + // Every 2 seconds, update and print values + while (shouldRun) + { + dfrec_update(sensor); - printf("Exiting...\n"); + printf("EC = %f ms/cm\n", dfrec_get_ec(sensor)); + printf("Volts = %f, Temperature = %f C\n", + dfrec_get_volts(sensor), dfrec_get_temperature(sensor)); + printf("\n"); - dfrec_close(sensor); + upm_delay(2); + } - return 0; + //! [Interesting] + + printf("Exiting...\n"); + + dfrec_close(sensor); + + return 0; } diff --git a/examples/c/dfrorp.c b/examples/c/dfrorp.c index 6a3e4734..46de2b74 100644 --- a/examples/c/dfrorp.c +++ b/examples/c/dfrorp.c @@ -26,6 +26,8 @@ #include #include "dfrorp.h" +#include "upm_utilities.h" +#include "mraa.h" bool shouldRun = true; @@ -37,50 +39,56 @@ void sig_handler(int signo) int main() { - signal(SIGINT, sig_handler); - -//! [Interesting] - - // Instantiate a DFRobot ORP sensor on analog pin A0 with an analog - // reference voltage of 5.0. - dfrorp_context sensor = dfrorp_init(0, 5.0); - - if (!sensor) + if (mraa_init() != MRAA_SUCCESS) { - printf("dfrorp_init() failed.\n"); - return(1); + perror("Failed to initialize mraa\n"); + return -1; } - // To calibrate: - // - // Disconnect the sensor probe (but leave the sensor interface board - // connected). Then run one of the examples while holding down the - // 'calibrate' button on the device. Read the ORP value reported - // (it should be fairly small). - // - // This value is what you should supply to - // dfrorp_set_orp_cal_offset(). Then reconnect the probe to the - // interface board and you should be ready to go. - // - // DO NOT press the calibrate button on the interface board while - // the probe is attached or you can permanently damage the probe. - dfrorp_set_calibration_offset(sensor, 0.97); + signal(SIGINT, sig_handler); - // Every second, update and print values - while (shouldRun) + //! [Interesting] + + // Instantiate a DFRobot ORP sensor on analog pin A0 with an analog + // reference voltage of 5.0. + dfrorp_context sensor = dfrorp_init(0, 5.0); + + if (!sensor) { - dfrorp_update(sensor); - - printf("ORP = %f mV\n", dfrorp_get_orp(sensor)); - - sleep(1); + printf("dfrorp_init() failed.\n"); + return(1); } -//! [Interesting] + // To calibrate: + // + // Disconnect the sensor probe (but leave the sensor interface board + // connected). Then run one of the examples while holding down the + // 'calibrate' button on the device. Read the ORP value reported + // (it should be fairly small). + // + // This value is what you should supply to + // dfrorp_set_orp_cal_offset(). Then reconnect the probe to the + // interface board and you should be ready to go. + // + // DO NOT press the calibrate button on the interface board while + // the probe is attached or you can permanently damage the probe. + dfrorp_set_calibration_offset(sensor, 0.97); - printf("Exiting...\n"); + // Every second, update and print values + while (shouldRun) + { + dfrorp_update(sensor); - dfrorp_close(sensor); + printf("ORP = %f mV\n", dfrorp_get_orp(sensor)); - return 0; + upm_delay(1); + } + + //! [Interesting] + + printf("Exiting...\n"); + + dfrorp_close(sensor); + + return 0; } diff --git a/examples/c/dfrph.c b/examples/c/dfrph.c index 6be3187c..4508c747 100644 --- a/examples/c/dfrph.c +++ b/examples/c/dfrph.c @@ -1,5 +1,5 @@ /* - * Author: Jon Trulson + * Author: Noel Eck * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -26,6 +26,8 @@ #include #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] diff --git a/examples/c/ds18b20.c b/examples/c/ds18b20.c index e858b901..6bfcd6f6 100644 --- a/examples/c/ds18b20.c +++ b/examples/c/ds18b20.c @@ -27,6 +27,8 @@ #include #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 + * Author: Noel Eck * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -26,6 +26,8 @@ #include #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] diff --git a/examples/c/flex.c b/examples/c/flex.c index 32b22a3b..97d70600 100644 --- a/examples/c/flex.c +++ b/examples/c/flex.c @@ -1,5 +1,5 @@ /* - * Author: Jon Trulson + * Author: Noel Eck * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -26,6 +26,8 @@ #include #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] diff --git a/examples/c/gp2y0a.c b/examples/c/gp2y0a.c index 7360fa5f..92e68c13 100644 --- a/examples/c/gp2y0a.c +++ b/examples/c/gp2y0a.c @@ -7,19 +7,28 @@ #include #include "gp2y0a.h" +#include "upm_utilities.h" +#include "mraa.h" + int main() { - gp2y0a_context dev = gp2y0a_init(14, 5.0); - float val; - while(1){ - if(gp2y0a_get_value(dev, 5.0, 20, &val) != UPM_SUCCESS){ - printf("Failed to get any values from the sensor\n"); - } - printf("Moisture Value: %f\n", val); - upm_delay(1); - } - gp2y0a_close(dev); + if (mraa_init() != MRAA_SUCCESS) + { + perror("Failed to initialize mraa\n"); + return -1; + } - return 0; + gp2y0a_context dev = gp2y0a_init(14, 5.0); + float val; + while(1){ + if(gp2y0a_get_value(dev, 5.0, 20, &val) != UPM_SUCCESS){ + printf("Failed to get any values from the sensor\n"); + } + printf("Moisture Value: %f\n", val); + upm_delay(1); + } + gp2y0a_close(dev); + + return 0; } diff --git a/examples/c/grovemoisture.c b/examples/c/grovemoisture.c index 52ae4d18..d787cecf 100644 --- a/examples/c/grovemoisture.c +++ b/examples/c/grovemoisture.c @@ -7,19 +7,28 @@ #include #include "grovemoisture.h" +#include "upm_utilities.h" +#include "mraa.h" + int main() { - grovemoisture_context dev = grovemoisture_init(14); - int val; - while(1){ - if(grovemoisture_get_moisture(dev, &val) != UPM_SUCCESS){ - printf("Failed to get any values from the sensor\n"); - } - printf("Moisture Value: %d\n", val); - upm_delay(1); - } - grovemoisture_close(dev); + if (mraa_init() != MRAA_SUCCESS) + { + perror("Failed to initialize mraa\n"); + return -1; + } - return 0; + grovemoisture_context dev = grovemoisture_init(14); + int val; + while(1){ + if(grovemoisture_get_moisture(dev, &val) != UPM_SUCCESS){ + printf("Failed to get any values from the sensor\n"); + } + printf("Moisture Value: %d\n", val); + upm_delay(1); + } + grovemoisture_close(dev); + + return 0; } diff --git a/examples/c/gsr.c b/examples/c/gsr.c index d0eea26a..047d74ed 100644 --- a/examples/c/gsr.c +++ b/examples/c/gsr.c @@ -1,5 +1,5 @@ /* - * Author: Jon Trulson + * Author: Noel Eck * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -26,6 +26,8 @@ #include #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] diff --git a/examples/c/hka5.c b/examples/c/hka5.c index 285ef7a7..34f61a37 100644 --- a/examples/c/hka5.c +++ b/examples/c/hka5.c @@ -26,6 +26,8 @@ #include #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; } diff --git a/examples/c/joystick12.c b/examples/c/joystick12.c index 27aa0285..6350dd3d 100644 --- a/examples/c/joystick12.c +++ b/examples/c/joystick12.c @@ -1,5 +1,5 @@ /* - * Author: Jon Trulson + * Author: Noel Eck * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -26,6 +26,9 @@ #include #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] diff --git a/examples/c/ldt0028.c b/examples/c/ldt0028.c index 6acec84e..c3c20d23 100644 --- a/examples/c/ldt0028.c +++ b/examples/c/ldt0028.c @@ -1,5 +1,5 @@ /* - * Author: Jon Trulson + * Author: Noel Eck * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -26,6 +26,8 @@ #include #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] diff --git a/examples/c/led.c b/examples/c/led.c index dbd4950b..b35ee0c9 100644 --- a/examples/c/led.c +++ b/examples/c/led.c @@ -7,18 +7,28 @@ #include #include "led.h" -void main(void) +#include "upm_utilities.h" +#include "mraa.h" + +int main(void) { - led_context dev = led_init(2); - while(1){ - if(led_on(dev) != UPM_SUCCESS){ - printf("problem turning the LED on\n"); - } - upm_delay(1); - if(led_off(dev) != UPM_SUCCESS){ - printf("problem turning the LED off\n"); - } - upm_delay(1); - } - led_close(dev); + 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){ + printf("problem turning the LED on\n"); + } + upm_delay(1); + if(led_off(dev) != UPM_SUCCESS){ + printf("problem turning the LED off\n"); + } + upm_delay(1); + } + led_close(dev); + return 0; } diff --git a/examples/c/light.c b/examples/c/light.c index 2828dbf4..1296649d 100644 --- a/examples/c/light.c +++ b/examples/c/light.c @@ -1,5 +1,5 @@ /* - * Author: Jon Trulson + * Author: Noel Eck * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -26,6 +26,8 @@ #include #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] diff --git a/examples/c/loudness.c b/examples/c/loudness.c index 7520b843..9e94a7b3 100644 --- a/examples/c/loudness.c +++ b/examples/c/loudness.c @@ -7,19 +7,28 @@ #include #include "loudness.h" +#include "upm_utilities.h" +#include "mraa.h" + int main() { - loudness_context dev = loudness_init(14); - int val; - while(1){ - if(loudness_get_value(dev, &val) != UPM_SUCCESS){ - printf("Failed to get any values from the sensor\n"); - } - printf("Loudness Value: %d\n", val); - upm_delay(1); - } - loudness_close(dev); + if (mraa_init() != MRAA_SUCCESS) + { + perror("Failed to initialize mraa\n"); + return -1; + } - return 0; + loudness_context dev = loudness_init(14); + int val; + while(1){ + if(loudness_get_value(dev, &val) != UPM_SUCCESS){ + printf("Failed to get any values from the sensor\n"); + } + printf("Loudness Value: %d\n", val); + upm_delay(1); + } + loudness_close(dev); + + return 0; } diff --git a/examples/c/m24lr64e.c b/examples/c/m24lr64e.c index 2c18dab6..f2087b30 100644 --- a/examples/c/m24lr64e.c +++ b/examples/c/m24lr64e.c @@ -5,25 +5,34 @@ #include #include "m24lr64e.h" +#include "upm_utilities.h" +#include "mraa.h" + int main() { - m24lr64e_context dev = m24lr64e_init(0, M24LR64E_USER_MODE); - int addr = M24LR64E_EEPROM_I2C_LENGTH-1; - printf("address being accessed: %d\n", addr); - uint8_t byte; - if(m24lr64e_read_byte(dev, addr, &byte) != UPM_SUCCESS) - printf("error while reading value\n"); - printf("value read from the device: %d\n", byte); - byte = ~byte; - printf("byte to be written: %d\n", byte); - if(m24lr64e_write_byte(dev, addr, byte) != UPM_SUCCESS) - printf("error while writing byte to the device\n"); - uint8_t var; - if(m24lr64e_read_byte(dev, addr, &var) != UPM_SUCCESS) - printf("error while reading value back\n"); - printf("new value at %d: %d\n", addr, var); - m24lr64e_close(dev); - printf("all done!!\n"); + if (mraa_init() != MRAA_SUCCESS) + { + perror("Failed to initialize mraa\n"); + return -1; + } - return 0; + m24lr64e_context dev = m24lr64e_init(0, M24LR64E_USER_MODE); + int addr = M24LR64E_EEPROM_I2C_LENGTH-1; + printf("address being accessed: %d\n", addr); + uint8_t byte; + if(m24lr64e_read_byte(dev, addr, &byte) != UPM_SUCCESS) + printf("error while reading value\n"); + printf("value read from the device: %d\n", byte); + byte = ~byte; + printf("byte to be written: %d\n", byte); + if(m24lr64e_write_byte(dev, addr, byte) != UPM_SUCCESS) + printf("error while writing byte to the device\n"); + uint8_t var; + if(m24lr64e_read_byte(dev, addr, &var) != UPM_SUCCESS) + printf("error while reading value back\n"); + printf("new value at %d: %d\n", addr, var); + m24lr64e_close(dev); + printf("all done!!\n"); + + return 0; } diff --git a/examples/c/mma7361.c b/examples/c/mma7361.c index c8c1ce5d..0d84f179 100644 --- a/examples/c/mma7361.c +++ b/examples/c/mma7361.c @@ -26,6 +26,8 @@ #include #include "mma7361.h" +#include "upm_utilities.h" +#include "mraa.h" bool shouldRun = true; @@ -37,49 +39,55 @@ void sig_handler(int signo) int main() { - signal(SIGINT, sig_handler); - -//! [Interesting] - - // Instantiate a MMA7361 sensor on analog pins A0 (X), A1 (Y) A2 - // (Z), selftest pin on D2, sleep pin on D3 nd an analog reference - // value of 5.0. The freefall pin and the range pin are unused - // (-1). - mma7361_context sensor = mma7361_init(0, 1, 2, 2, 3, -1, -1, 5.0); - - if (!sensor) + if (mraa_init() != MRAA_SUCCESS) { - printf("mma7361_init() failed.\n"); - return(1); + perror("Failed to initialize mraa\n"); + return -1; } - // 1.5g (true = 6g) - mma7361_set_range(sensor, false); + signal(SIGINT, sig_handler); - // Every 10th of a second, update and print values + //! [Interesting] - while (shouldRun) + // Instantiate a MMA7361 sensor on analog pins A0 (X), A1 (Y) A2 + // (Z), selftest pin on D2, sleep pin on D3 nd an analog reference + // value of 5.0. The freefall pin and the range pin are unused + // (-1). + mma7361_context sensor = mma7361_init(0, 1, 2, 2, 3, -1, -1, 5.0); + + if (!sensor) { - mma7361_update(sensor); - - float x, y, z; - - mma7361_get_acceleration(sensor, &x, &y, &z); - printf("Acceleration x = %f y = %f z = %f\n", - x, y, z); - - mma7361_get_volts(sensor, &x, &y, &z); - printf("Volts x = %f y = %f z = %f\n\n", - x, y, z); - - usleep(100000); + printf("mma7361_init() failed.\n"); + return(1); } -//! [Interesting] + // 1.5g (true = 6g) + mma7361_set_range(sensor, false); - printf("Exiting...\n"); + // Every 10th of a second, update and print values - mma7361_close(sensor); + while (shouldRun) + { + mma7361_update(sensor); - return 0; + float x, y, z; + + mma7361_get_acceleration(sensor, &x, &y, &z); + printf("Acceleration x = %f y = %f z = %f\n", + x, y, z); + + mma7361_get_volts(sensor, &x, &y, &z); + printf("Volts x = %f y = %f z = %f\n\n", + x, y, z); + + upm_delay_ms(500); + } + + //! [Interesting] + + printf("Exiting...\n"); + + mma7361_close(sensor); + + return 0; } diff --git a/examples/c/moisture.c b/examples/c/moisture.c index 04a4e096..6f17b57d 100644 --- a/examples/c/moisture.c +++ b/examples/c/moisture.c @@ -7,19 +7,27 @@ #include #include "moisture.h" +#include "upm_utilities.h" +#include "mraa.h" + int main() { - moisture_context dev = moisture_init(14); - int val; - while(1){ - if(moisture_get_moisture(dev, &val) != UPM_SUCCESS){ - printf("Failed to get any values from the sensor\n"); - } - printf("Moisture Value: %d\n", val); - upm_delay(1); - } - moisture_close(dev); + if (mraa_init() != MRAA_SUCCESS) + { + perror("Failed to initialize mraa\n"); + return -1; + } - return 0; + moisture_context dev = moisture_init(14); + int val; + while(1){ + if(moisture_get_moisture(dev, &val) != UPM_SUCCESS){ + printf("Failed to get any values from the sensor\n"); + } + printf("Moisture Value: %d\n", val); + upm_delay(1); + } + moisture_close(dev); + + return 0; } - diff --git a/examples/c/mpr121.c b/examples/c/mpr121.c index 06e98e65..129a2600 100644 --- a/examples/c/mpr121.c +++ b/examples/c/mpr121.c @@ -7,24 +7,33 @@ #include #include "mpr121.h" +#include "upm_utilities.h" +#include "mraa.h" + int main() { - mpr121_context dev = mpr121_init(MPR121_I2C_BUS, MPR121_DEFAULT_I2C_ADDR); + if (mraa_init() != MRAA_SUCCESS) + { + perror("Failed to initialize mraa\n"); + return -1; + } - if(mpr121_config_an3944(dev) != UPM_SUCCESS){ - printf("unable to configure device\n"); - } - uint32_t states; - while(1){ - if(mpr121_read_buttons(dev, &states, 0) != UPM_SUCCESS){ - printf("Error while reading button values\n"); - } - printf("retrieved button states: %d\n", states); - upm_delay(1); - } + mpr121_context dev = mpr121_init(MPR121_I2C_BUS, MPR121_DEFAULT_I2C_ADDR); - mpr121_close(dev); - printf("all done!!\n"); + if(mpr121_config_an3944(dev) != UPM_SUCCESS){ + printf("unable to configure device\n"); + } + uint32_t states; + while(1){ + if(mpr121_read_buttons(dev, &states, 0) != UPM_SUCCESS){ + printf("Error while reading button values\n"); + } + printf("retrieved button states: %d\n", states); + upm_delay(1); + } - return 0; + mpr121_close(dev); + printf("all done!!\n"); + + return 0; } diff --git a/examples/c/mq303a.c b/examples/c/mq303a.c index 86f17cb0..07bfca4d 100644 --- a/examples/c/mq303a.c +++ b/examples/c/mq303a.c @@ -7,20 +7,28 @@ #include #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); - printf("init done for mq303a\n"); - int value; - mq303a_heater_enable(dev, true); - upm_delay(12); - while(1){ - mq303a_get_value(dev, &value); - printf("returned value: %d\n", value); - upm_delay(1); - } - return 0; + /* --------- MQ303A EXAMPLE -------- */ + mq303a_context dev = mq303a_init(0, 15); + printf("init done for mq303a\n"); + int value; + mq303a_heater_enable(dev, true); + upm_delay(12); + while(1){ + mq303a_get_value(dev, &value); + printf("returned value: %d\n", value); + upm_delay(1); + } + + return 0; } - diff --git a/examples/c/mqx.c b/examples/c/mqx.c index 72d02974..3b3f1cdc 100644 --- a/examples/c/mqx.c +++ b/examples/c/mqx.c @@ -1,5 +1,5 @@ /* - * Author: Jon Trulson + * Author: Noel Eck * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -26,6 +26,8 @@ #include #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] diff --git a/examples/c/nmea_gps.c b/examples/c/nmea_gps.c index 4393f1d0..01e24d27 100644 --- a/examples/c/nmea_gps.c +++ b/examples/c/nmea_gps.c @@ -26,6 +26,8 @@ #include #include "nmea_gps.h" +#include "upm_utilities.h" +#include "mraa.h" bool shouldRun = true; @@ -39,42 +41,48 @@ void sig_handler(int signo) int main() { - signal(SIGINT, sig_handler); - -//! [Interesting] - - // Instantiate a NMEA_GPS sensor on uart 0 at 9600 baud with enable - // pin on D3. If you do not need an enable pin, you can specify -1. - nmea_gps_context sensor = nmea_gps_init(0, 9600, 3); - - if (!sensor) + if (mraa_init() != MRAA_SUCCESS) { - printf("nmea_gps_init() 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 && nmea_gps_data_available(sensor, 5000)) + //! [Interesting] + + // Instantiate a NMEA_GPS sensor on uart 0 at 9600 baud with enable + // pin on D3. If you do not need an enable pin, you can specify -1. + nmea_gps_context sensor = nmea_gps_init(0, 9600, 3); + + if (!sensor) { - if ((rv = nmea_gps_read(sensor, buffer, bufferLength)) >= 0) + printf("nmea_gps_init() failed.\n"); + return 1; + } + + char buffer[bufferLength]; + int rv = 0; + + // loop, dumping NMEA data out as fast as it comes in + while (shouldRun && nmea_gps_data_available(sensor, 5000)) + { + if ((rv = nmea_gps_read(sensor, buffer, bufferLength)) >= 0) { - int i; - for (i=0; i #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 + * Author: Noel Eck * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -26,6 +26,8 @@ #include #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] diff --git a/examples/c/servo.c b/examples/c/servo.c index b15805e4..afab1c6f 100644 --- a/examples/c/servo.c +++ b/examples/c/servo.c @@ -7,29 +7,38 @@ #include #include "es08a.c" +#include "upm_utilities.h" +#include "mraa.h" + int main() { - 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"); - } + if (mraa_init() != MRAA_SUCCESS) + { + perror("Failed to initialize mraa\n"); + return -1; + } - if(es08a_set_angle(dev, 90) != UPM_SUCCESS){ - printf("unable to set angle to 90 degrees\n"); - } - upm_delay(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"); + } - if(es08a_set_angle(dev, 180) != UPM_SUCCESS){ - printf("unable to set angle to 180 degrees\n"); - } - upm_delay(1); + if(es08a_set_angle(dev, 90) != UPM_SUCCESS){ + printf("unable to set angle to 90 degrees\n"); + } + upm_delay(1); - if(es08a_set_angle(dev, 90) != UPM_SUCCESS){ - printf("unable to set angle to 90 degrees\n"); - } - upm_delay(1); + if(es08a_set_angle(dev, 180) != UPM_SUCCESS){ + printf("unable to set angle to 180 degrees\n"); + } + upm_delay(1); - es08a_halt(dev); + if(es08a_set_angle(dev, 90) != UPM_SUCCESS){ + printf("unable to set angle to 90 degrees\n"); + } + upm_delay(1); - return 0; + es08a_halt(dev); + + return 0; } diff --git a/examples/c/sht1x.c b/examples/c/sht1x.c index ef7c6789..bf8ca5de 100644 --- a/examples/c/sht1x.c +++ b/examples/c/sht1x.c @@ -26,52 +26,60 @@ #include #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; } diff --git a/examples/c/slide.c b/examples/c/slide.c index 876b383e..83382fc3 100644 --- a/examples/c/slide.c +++ b/examples/c/slide.c @@ -1,5 +1,5 @@ /* - * Author: Jon Trulson + * Author: Noel Eck * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -26,6 +26,8 @@ #include #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] diff --git a/examples/c/tsl2561.c b/examples/c/tsl2561.c index 75c0086b..af4337d3 100644 --- a/examples/c/tsl2561.c +++ b/examples/c/tsl2561.c @@ -7,14 +7,23 @@ #include #include "tsl2561.h" +#include "upm_utilities.h" +#include "mraa.h" + int main() { - tsl2561_context dev = tsl2561_init(0, TSL2561_Address, GAIN_0X, INTEGRATION_TIME1_101MS); - float abc = 0; - if(tsl2561_get_lux(dev, &abc) != UPM_SUCCESS){ - printf("ERROR !! ERROR !! ERROR!!"); - } - printf("value retrieved: %f\n", abc); + if (mraa_init() != MRAA_SUCCESS) + { + perror("Failed to initialize mraa\n"); + return -1; + } - return 0; + tsl2561_context dev = tsl2561_init(0, TSL2561_Address, GAIN_0X, INTEGRATION_TIME1_101MS); + float abc = 0; + if(tsl2561_get_lux(dev, &abc) != UPM_SUCCESS){ + printf("ERROR !! ERROR !! ERROR!!"); + } + printf("value retrieved: %f\n", abc); + + return 0; } diff --git a/examples/c/ttp223.c b/examples/c/ttp223.c index b27b9feb..07b828ee 100644 --- a/examples/c/ttp223.c +++ b/examples/c/ttp223.c @@ -7,17 +7,26 @@ #include #include "ttp223.h" +#include "upm_utilities.h" +#include "mraa.h" + int main() { - ttp223_context dev = ttp223_init(2); - bool abc = 0; - while(1){ - if(ttp223_is_pressed(dev, &abc) != UPM_SUCCESS){ - printf("an error has occured\n"); - } - upm_delay(1); - printf("value retrieved: %d\n", abc); - } + if (mraa_init() != MRAA_SUCCESS) + { + perror("Failed to initialize mraa\n"); + return -1; + } - return 0; + ttp223_context dev = ttp223_init(2); + bool abc = 0; + while(1){ + if(ttp223_is_pressed(dev, &abc) != UPM_SUCCESS){ + printf("an error has occured\n"); + } + upm_delay(1); + printf("value retrieved: %d\n", abc); + } + + return 0; } diff --git a/examples/c/urm37-uart.c b/examples/c/urm37-uart.c index 7623ecb7..a1dbec6c 100644 --- a/examples/c/urm37-uart.c +++ b/examples/c/urm37-uart.c @@ -26,6 +26,8 @@ #include #include "urm37.h" +#include "upm_utilities.h" +#include "mraa.h" bool shouldRun = true; @@ -37,39 +39,45 @@ void sig_handler(int signo) int main() { - signal(SIGINT, sig_handler); - -//! [Interesting] - - // Instantiate a URM37 sensor on UART 0, with the reset pin on D2 - urm37_context sensor = urm37_init(0, 2, 0, 0, 0, false); - - if (!sensor) + if (mraa_init() != MRAA_SUCCESS) { - printf("urm37_init() failed.\n"); - return(1); + perror("Failed to initialize mraa\n"); + return -1; } - // Every half a second, sample the URM37 and output the measured - // distance in cm. + signal(SIGINT, sig_handler); - while (shouldRun) + //! [Interesting] + + // Instantiate a URM37 sensor on UART 0, with the reset pin on D2 + urm37_context sensor = urm37_init(0, 2, 0, 0, 0, false); + + if (!sensor) { - float distance, temperature; - - urm37_get_distance(sensor, &distance, 0); - printf("Detected distance (cm): %f\n", distance); - - urm37_get_temperature(sensor, &temperature); - printf("Temperature (C): %f\n\n", temperature); - usleep(500000); + printf("urm37_init() failed.\n"); + return(1); } -//! [Interesting] + // Every half a second, sample the URM37 and output the measured + // distance in cm. - printf("Exiting\n"); + while (shouldRun) + { + float distance, temperature; - urm37_close(sensor); + urm37_get_distance(sensor, &distance, 0); + printf("Detected distance (cm): %f\n", distance); - return 0; + urm37_get_temperature(sensor, &temperature); + printf("Temperature (C): %f\n\n", temperature); + upm_delay_ms(500); + } + + //! [Interesting] + + printf("Exiting\n"); + + urm37_close(sensor); + + return 0; } diff --git a/examples/c/urm37.c b/examples/c/urm37.c index 5dee3603..1a26619a 100644 --- a/examples/c/urm37.c +++ b/examples/c/urm37.c @@ -26,6 +26,8 @@ #include #include "urm37.h" +#include "upm_utilities.h" +#include "mraa.h" bool shouldRun = true; @@ -37,37 +39,43 @@ void sig_handler(int signo) int main() { - signal(SIGINT, sig_handler); - -//! [Interesting] - - // Instantiate a URM37 sensor on analog pin A0, reset pin on D2, - // trigger pin on D3 with an analog reference voltage of 5.0 - urm37_context sensor = urm37_init(0, 2, 3, 5.0, 0, true); - - if (!sensor) + if (mraa_init() != MRAA_SUCCESS) { - printf("urm37_init() failed.\n"); - return(1); + perror("Failed to initialize mraa\n"); + return -1; } - // Every half a second, sample the URM37 and output the measured - // distance in cm. + signal(SIGINT, sig_handler); - while (shouldRun) + //! [Interesting] + + // Instantiate a URM37 sensor on analog pin A0, reset pin on D2, + // trigger pin on D3 with an analog reference voltage of 5.0 + urm37_context sensor = urm37_init(0, 2, 3, 5.0, 0, true); + + if (!sensor) { - float distance; - - urm37_get_distance(sensor, &distance, 0); - printf("Detected distance (cm): %f\n", distance); - usleep(500000); + printf("urm37_init() failed.\n"); + return(1); } -//! [Interesting] + // Every half a second, sample the URM37 and output the measured + // distance in cm. - printf("Exiting\n"); + while (shouldRun) + { + float distance; - urm37_close(sensor); + urm37_get_distance(sensor, &distance, 0); + printf("Detected distance (cm): %f\n", distance); + upm_delay_ms(500); + } - return 0; + //! [Interesting] + + printf("Exiting\n"); + + urm37_close(sensor); + + return 0; } diff --git a/examples/c/vdiv.c b/examples/c/vdiv.c index 323faae7..e3bf02f6 100644 --- a/examples/c/vdiv.c +++ b/examples/c/vdiv.c @@ -1,5 +1,5 @@ /* - * Author: Jon Trulson + * Author: Noel Eck * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -26,6 +26,8 @@ #include #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]