diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 683b3771..5c8573ee 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -128,6 +128,7 @@ add_example (lm35) add_example (rotaryencoder) add_example (rpr220) add_example (md) +add_example (linefinder) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/linefinder.c b/examples/c/linefinder.c new file mode 100644 index 00000000..1a427a48 --- /dev/null +++ b/examples/c/linefinder.c @@ -0,0 +1,72 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include + +#include +#include "linefinder.h" + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + + +int main () +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // Instantiate a Line Finder sensor on digital pin D2 + linefinder_context sensor = linefinder_init(2); + + if (!sensor) + { + printf("linefinder_init() failed\n"); + return 1; + } + + // check every second for the presence of white or black detection + while (shouldRun) + { + if (linefinder_white_detected(sensor)) + printf("White detected.\n"); + else + printf("Black detected.\n"); + + upm_delay(1); + } + + printf("Exiting...\n"); + + linefinder_close(sensor); + +//! [Interesting] + return 0; +} diff --git a/src/linefinder/CMakeLists.txt b/src/linefinder/CMakeLists.txt index 43c567fb..96084829 100644 --- a/src/linefinder/CMakeLists.txt +++ b/src/linefinder/CMakeLists.txt @@ -1,5 +1,9 @@ upm_mixed_module_init (NAME linefinder DESCRIPTION "Infrared (IR) based line finder" + C_HDR linefinder.h + C_SRC linefinder.c CPP_HDR linefinder.hpp CPP_SRC linefinder.cxx + FTI_SRC linefinder_fti.c + CPP_WRAPS_C REQUIRES mraa) diff --git a/src/linefinder/linefinder.c b/src/linefinder/linefinder.c new file mode 100644 index 00000000..18e5f081 --- /dev/null +++ b/src/linefinder/linefinder.c @@ -0,0 +1,84 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include + +#include "linefinder.h" + +linefinder_context linefinder_init(int pin) +{ + linefinder_context dev = + (linefinder_context)malloc(sizeof(struct _linefinder_context)); + + if (!dev) + return NULL; + + dev->gpio = NULL; + + // make sure MRAA is initialized + int mraa_rv; + if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) + { + printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv); + linefinder_close(dev); + return NULL; + } + + // MRAA contexts... + if ( !(dev->gpio = mraa_gpio_init(pin)) ) + { + printf("%s: mraa_gpio_init() failed\n", + __FUNCTION__); + linefinder_close(dev); + return NULL; + } + + mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN); + + return dev; +} + +void linefinder_close(linefinder_context dev) +{ + assert(dev != NULL); + + if (dev->gpio) + mraa_gpio_close(dev->gpio); + + free(dev); +} + +bool linefinder_white_detected(const linefinder_context dev) +{ + assert(dev != NULL); + + return (!mraa_gpio_read(dev->gpio) ? true : false); +} + +bool linefinder_black_detected(const linefinder_context dev) +{ + assert(dev != NULL); + + return (mraa_gpio_read(dev->gpio) ? true : false); +} diff --git a/src/linefinder/linefinder.cxx b/src/linefinder/linefinder.cxx index 4b927bc4..9c93b92b 100644 --- a/src/linefinder/linefinder.cxx +++ b/src/linefinder/linefinder.cxx @@ -1,6 +1,6 @@ /* * Author: Jon Trulson - * Copyright (c) 2014 Intel Corporation. + * Copyright (c) 2014-2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -31,29 +31,25 @@ using namespace upm; using namespace std; -LineFinder::LineFinder(int pin) +LineFinder::LineFinder(int pin) : + m_linefinder(linefinder_init(pin)) { - if ( !(m_gpio = mraa_gpio_init(pin)) ) - { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_gpio_init() failed, invalid pin?"); - return; - } - - mraa_gpio_dir(m_gpio, MRAA_GPIO_IN); + if (!m_linefinder) + throw std::runtime_error(std::string(__FUNCTION__) + + ": linefinder_init() failed"); } LineFinder::~LineFinder() { - mraa_gpio_close(m_gpio); + linefinder_close(m_linefinder); } bool LineFinder::whiteDetected() { - return (!mraa_gpio_read(m_gpio) ? true : false); + return linefinder_white_detected(m_linefinder); } bool LineFinder::blackDetected() { - return (mraa_gpio_read(m_gpio) ? true : false); + return linefinder_black_detected(m_linefinder); } diff --git a/src/linefinder/linefinder.h b/src/linefinder/linefinder.h new file mode 100644 index 00000000..098671b9 --- /dev/null +++ b/src/linefinder/linefinder.h @@ -0,0 +1,77 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#pragma once + +#include +#include +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + /** + * Device context + */ + typedef struct _linefinder_context { + mraa_gpio_context gpio; + } *linefinder_context; + + /** + * Line Finder inititialization + * + * @param pin Digital pin to use + * @return device context + */ + linefinder_context linefinder_init(int pin); + + /** + * LineFinder close + * + * @param Device context + */ + void linefinder_close(linefinder_context dev); + + /** + * Determines whether white has been detected + * + * @param Device context + * @return True if white is detected + */ + bool linefinder_white_detected(const linefinder_context dev); + + /** + * Determines whether black has been detected + * + * @param Device context + * @return True if black is detected + */ + bool linefinder_black_detected(const linefinder_context dev); + + +#ifdef __cplusplus +} +#endif diff --git a/src/linefinder/linefinder.hpp b/src/linefinder/linefinder.hpp index 0bc36367..44ef1dda 100644 --- a/src/linefinder/linefinder.hpp +++ b/src/linefinder/linefinder.hpp @@ -1,6 +1,6 @@ /* * Author: Jon Trulson - * Copyright (c) 2014 Intel Corporation. + * Copyright (c) 2014-2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -23,63 +23,65 @@ */ #pragma once -#include -#include +#include namespace upm { - /** - * @brief Line Finder Sensor library - * @defgroup linefinder libupm-linefinder - * @ingroup seeed dfrobot gpio color robok - */ + /** + * @brief Line Finder Sensor library + * @defgroup linefinder libupm-linefinder + * @ingroup seeed dfrobot gpio color robok + */ - /** - * @library linefinder - * @sensor linefinder - * @comname Line Finder - * @altname Grove Line Finder, DFRobot Line Tracking sensor - * @type color - * @man seeed, dfrobot - * @con gpio - * @kit robok - * - * @brief API for the Line Finder Sensor - * - * UPM module for the Line Finder sensor. It outputs a - * digital signal indicating whether it is detecting black on a - * white background, or white on a black background. - * - * @image html linefinder.jpg - * @snippet linefinder.cxx Interesting - */ - class LineFinder { - public: /** - * Line Finder digital sensor constructor + * @library linefinder + * @sensor linefinder + * @comname Line Finder + * @altname Grove Line Finder, DFRobot Line Tracking sensor + * @type color + * @man seeed, dfrobot + * @con gpio + * @kit robok * - * @param pin Digital pin to use - */ - LineFinder(int pin); - /** - * LineFinder destructor - */ - ~LineFinder(); - /** - * Determines whether white has been detected + * @brief API for the Line Finder Sensor * - * @return True if white is detected - */ - bool whiteDetected(); - /** - * Determines whether black has been detected + * UPM module for the Line Finder sensor. It outputs a + * digital signal indicating whether it is detecting black on a + * white background, or white on a black background. * - * @return True if black is detected + * @image html linefinder.jpg + * @snippet linefinder.cxx Interesting */ - bool blackDetected(); + class LineFinder { + public: + /** + * Line Finder digital sensor constructor + * + * @param pin Digital pin to use + */ + LineFinder(int pin); - private: - mraa_gpio_context m_gpio; - }; + /** + * LineFinder destructor + */ + ~LineFinder(); + + /** + * Determines whether white has been detected + * + * @return True if white is detected + */ + bool whiteDetected(); + + /** + * Determines whether black has been detected + * + * @return True if black is detected + */ + bool blackDetected(); + + private: + linefinder_context m_linefinder; + }; } diff --git a/src/linefinder/linefinder_fti.c b/src/linefinder/linefinder_fti.c new file mode 100644 index 00000000..10878a19 --- /dev/null +++ b/src/linefinder/linefinder_fti.c @@ -0,0 +1,94 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "linefinder.h" +#include + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_linefinder_name[] = "LINEFINDER"; +const char upm_linefinder_description[] = "Line Finder"; +const upm_protocol_t upm_linefinder_protocol[] = {UPM_GPIO}; +const upm_sensor_t upm_linefinder_category[] = {UPM_BINARY}; + +// forward declarations +const void* upm_linefinder_get_ft(upm_sensor_t sensor_type); +void* upm_linefinder_init_name(); +void upm_linefinder_close(void *dev); +upm_result_t upm_linefinder_black_detected(void *dev, bool *value); + +const upm_sensor_descriptor_t upm_linefinder_get_descriptor() +{ + upm_sensor_descriptor_t usd; + usd.name = upm_linefinder_name; + usd.description = upm_linefinder_description; + usd.protocol_size = 1; + usd.protocol = upm_linefinder_protocol; + usd.category_size = 1; + usd.category = upm_linefinder_category; + return usd; +} + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = upm_linefinder_init_name, + .upm_sensor_close = upm_linefinder_close, +}; + +static const upm_binary_ft bft = +{ + .upm_binary_get_value = upm_linefinder_black_detected, +}; + +const void* upm_linefinder_get_ft(upm_sensor_t sensor_type) +{ + switch(sensor_type) + { + case UPM_SENSOR: + return &ft; + case UPM_BINARY: + return &bft; + default: + return NULL; + } +} + +void *upm_linefinder_init_name() +{ + return NULL; +} + +void upm_linefinder_close(void *dev) +{ + linefinder_close((linefinder_context)dev); +} + +upm_result_t upm_linefinder_black_detected(void *dev, bool *value) +{ + *value = linefinder_black_detected((linefinder_context)dev); + + return UPM_SUCCESS; +}