From 22c7310428d36f28322e7ce976b37d94f384dc28 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Fri, 23 Sep 2016 12:19:27 -0600 Subject: [PATCH] yg1006: add C driver and example, C++ wraps C driver Signed-off-by: Jon Trulson --- examples/c/CMakeLists.txt | 1 + examples/c/yg1006.c | 70 +++++++++++++++++++++++++++++++++ src/yg1006/CMakeLists.txt | 14 ++++--- src/yg1006/yg1006.c | 77 ++++++++++++++++++++++++++++++++++++ src/yg1006/yg1006.cxx | 20 ++++------ src/yg1006/yg1006.h | 74 +++++++++++++++++++++++++++++++++++ src/yg1006/yg1006.hpp | 15 ++++--- src/yg1006/yg1006_fti.c | 82 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 331 insertions(+), 22 deletions(-) create mode 100644 examples/c/yg1006.c create mode 100644 src/yg1006/yg1006.c create mode 100644 src/yg1006/yg1006.h create mode 100644 src/yg1006/yg1006_fti.c diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index e7b7bd73..63ed87db 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -120,6 +120,7 @@ add_example (ds18b20) add_example (dfrec) add_example (sht1x) add_example (water) +add_example (yg1006) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/yg1006.c b/examples/c/yg1006.c new file mode 100644 index 00000000..3d361e1c --- /dev/null +++ b/examples/c/yg1006.c @@ -0,0 +1,70 @@ +/* + * 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 "yg1006.h" +#include "upm_utilities.h" + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + + +int main () +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // Instantiate a YG1006 sensor on digital pin D2 + yg1006_context sensor = yg1006_init(2); + + if (!sensor) + { + printf("yg1006_init() failed.\n"); + return(1); + } + + while (shouldRun) + { + if (yg1006_flame_detected(sensor)) + printf("Flame detected.\n"); + else + printf("No flame detected.\n"); + + upm_delay(1); + } +//! [Interesting] + + printf("Exiting...\n"); + + yg1006_close(sensor); + + return 0; +} diff --git a/src/yg1006/CMakeLists.txt b/src/yg1006/CMakeLists.txt index 01bdc2a3..1a6ea4ef 100644 --- a/src/yg1006/CMakeLists.txt +++ b/src/yg1006/CMakeLists.txt @@ -1,5 +1,9 @@ -set (libname "yg1006") -set (libdescription "upm yg1006 flame sensor module") -set (module_src ${libname}.cxx) -set (module_hpp ${libname}.hpp) -upm_module_init() +upm_mixed_module_init (NAME yg1006 + DESCRIPTION "UPM flame sensor" + C_HDR yg1006.h + C_SRC yg1006.c + CPP_HDR yg1006.hpp + CPP_SRC yg1006.cxx + FTI_SRC yg1006_fti.c + CPP_WRAPS_C + REQUIRES mraa) diff --git a/src/yg1006/yg1006.c b/src/yg1006/yg1006.c new file mode 100644 index 00000000..4688d97d --- /dev/null +++ b/src/yg1006/yg1006.c @@ -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. + */ + +#include +#include "yg1006.h" + +yg1006_context yg1006_init(unsigned int pin) +{ + yg1006_context dev = + (yg1006_context)malloc(sizeof(struct _yg1006_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); + yg1006_close(dev); + return NULL; + } + + // initialize the MRAA context + + if (!(dev->gpio = mraa_gpio_init(pin))) + { + printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__); + yg1006_close(dev); + return NULL; + } + + mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN); + + return dev; +} + +void yg1006_close(yg1006_context dev) +{ + assert(dev != NULL); + + if (dev->gpio) + mraa_gpio_close(dev->gpio); + + free(dev); +} + +bool yg1006_flame_detected(const yg1006_context dev) +{ + assert(dev != NULL); + + // gpio is low when a flame is detected + return (mraa_gpio_read(dev->gpio) ? false : true); +} diff --git a/src/yg1006/yg1006.cxx b/src/yg1006/yg1006.cxx index 07be0ef1..0f89b29e 100644 --- a/src/yg1006/yg1006.cxx +++ b/src/yg1006/yg1006.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,24 +31,20 @@ using namespace upm; using namespace std; -YG1006::YG1006(int pin) +YG1006::YG1006(unsigned int pin) : + m_yg1006(yg1006_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_yg1006) + throw std::runtime_error(std::string(__FUNCTION__) + + ": water_init() failed"); } YG1006::~YG1006() { - mraa_gpio_close(m_gpio); + yg1006_close(m_yg1006); } bool YG1006::flameDetected() { - return (!mraa_gpio_read(m_gpio) ? true : false); + return yg1006_flame_detected(m_yg1006); } diff --git a/src/yg1006/yg1006.h b/src/yg1006/yg1006.h new file mode 100644 index 00000000..e876f60c --- /dev/null +++ b/src/yg1006/yg1006.h @@ -0,0 +1,74 @@ +/* + * 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 + + /** + * @brief YG1006 Flame Sensor + * + * UPM module for the YG1006 flame sensor. It detects a flame or any + * other light source in the 760-1,100 nm wavelength range. + * + * @snippet yg1006.cxx Interesting + */ + + /** + * Device context + */ + typedef struct _yg1006_context { + mraa_gpio_context gpio; + } *yg1006_context; + + /** + * YG1006 initializer + * + * @param pin Digital pin to use + * @return an initialized device context on success, NULL on error. + */ + yg1006_context yg1006_init(unsigned int pin); + + /** + * YG1006 close function + */ + void yg1006_close(yg1006_context dev); + + /** + * Determines whether a flame has been detected + * + * @return true if a flame or another comparable light source has + * been detected + */ + bool yg1006_flame_detected(const yg1006_context dev); + +#ifdef __cplusplus +} +#endif diff --git a/src/yg1006/yg1006.hpp b/src/yg1006/yg1006.hpp index a64f32db..7bd81599 100644 --- a/src/yg1006/yg1006.hpp +++ b/src/yg1006/yg1006.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 @@ -24,7 +24,7 @@ #pragma once #include -#include +#include "yg1006.h" namespace upm { /** @@ -32,6 +32,7 @@ namespace upm { * @defgroup yg1006 libupm-yg1006 * @ingroup seeed gpio light hak */ + /** * @library yg1006 * @sensor yg1006 @@ -53,25 +54,29 @@ namespace upm { */ class YG1006 { public: + /** * YG1006 constructor * * @param pin Digital pin to use */ - YG1006(int pin); + YG1006(unsigned int pin); + /** * YG1006 destructor */ ~YG1006(); + /** * Determines whether a flame has been detected * - * @return True if a flame or another comparable light source has been detected + * @return true if a flame or another comparable light source has + * been detected */ bool flameDetected(); private: - mraa_gpio_context m_gpio; + yg1006_context m_yg1006; }; } diff --git a/src/yg1006/yg1006_fti.c b/src/yg1006/yg1006_fti.c new file mode 100644 index 00000000..275a94c2 --- /dev/null +++ b/src/yg1006/yg1006_fti.c @@ -0,0 +1,82 @@ +/* + * 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 "yg1006.h" +#include + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_yg1006_name[] = "YG1006"; +const char upm_yg1006_description[] = "Flame sensor"; +const upm_protocol_t upm_yg1006_protocol[] = {UPM_GPIO}; +const upm_sensor_t upm_yg1006_category[] = {UPM_BINARY}; + +// forward declarations +const void* upm_yg1006_get_ft(upm_sensor_t sensor_type); +void* upm_yg1006_init_name(); +void upm_yg1006_close(void *dev); +upm_result_t upm_yg1006_flame_detected(void *dev, bool *value); + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = upm_yg1006_init_name, + .upm_sensor_close = upm_yg1006_close, +}; + +static const upm_binary_ft bft = +{ + .upm_binary_get_value = upm_yg1006_flame_detected, +}; + +const void* upm_yg1006_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_yg1006_init_name() +{ + return NULL; +} + +void upm_yg1006_close(void *dev) +{ + yg1006_close((yg1006_context)dev); +} + +upm_result_t upm_yg1006_flame_detected(void *dev, bool *value) +{ + *value = yg1006_flame_detected((yg1006_context)dev); + + return UPM_SUCCESS; +}