From 40e05958925ec42942cb0ecd3df2cedf70579140 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Wed, 10 Dec 2014 13:19:29 -0700 Subject: [PATCH] yg1006: Initial implementation This module implements support for the yg1006 flame sensor. It was tested with the Grove Flame Sensor. Signed-off-by: Jon Trulson Signed-off-by: Zion Orent Signed-off-by: Sarah Knepper --- examples/CMakeLists.txt | 3 ++ examples/javascript/yg1006.js | 47 +++++++++++++++++++++++++ examples/yg1006.cxx | 66 +++++++++++++++++++++++++++++++++++ src/yg1006/CMakeLists.txt | 5 +++ src/yg1006/jsupm_yg1006.i | 8 +++++ src/yg1006/pyupm_yg1006.i | 9 +++++ src/yg1006/yg1006.cxx | 51 +++++++++++++++++++++++++++ src/yg1006/yg1006.h | 64 +++++++++++++++++++++++++++++++++ 8 files changed, 253 insertions(+) create mode 100644 examples/javascript/yg1006.js create mode 100644 examples/yg1006.cxx create mode 100644 src/yg1006/CMakeLists.txt create mode 100644 src/yg1006/jsupm_yg1006.i create mode 100644 src/yg1006/pyupm_yg1006.i create mode 100644 src/yg1006/yg1006.cxx create mode 100644 src/yg1006/yg1006.h diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index fc369ac8..dce3eadb 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -67,6 +67,7 @@ add_executable (guvas12d-example guvas12d.cxx) add_executable (groveloudness-example groveloudness.cxx) add_executable (mpr121-example mpr121.cxx) add_executable (ublox6-example ublox6.cxx) +add_executable (yg1006-example yg1006.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -121,6 +122,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/guvas12d) include_directories (${PROJECT_SOURCE_DIR}/src/groveloudness) include_directories (${PROJECT_SOURCE_DIR}/src/mpr121) include_directories (${PROJECT_SOURCE_DIR}/src/ublox6) +include_directories (${PROJECT_SOURCE_DIR}/src/yg1006) target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT}) @@ -191,3 +193,4 @@ target_link_libraries (guvas12d-example guvas12d ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveloudness-example groveloudness ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (mpr121-example mpr121 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (ublox6-example ublox6 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (yg1006-example yg1006 ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/javascript/yg1006.js b/examples/javascript/yg1006.js new file mode 100644 index 00000000..be6887dc --- /dev/null +++ b/examples/javascript/yg1006.js @@ -0,0 +1,47 @@ +/*jslint node:true, vars:true, bitwise:true, unparam:true */ +/*jshint unused:true */ +/*global */ +/* +* Author: Zion Orent +* Copyright (c) 2014 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. +*/ + +var flameSensor = require('jsupm_yg1006'); + +// Instantiate a flame sensor on digital pin D2 +var myFlameSensor = new flameSensor.YG1006(2); + +// Check every second for the presence of a flame +setInterval(function() +{ + if (myFlameSensor.flameDetected()) + console.log("Flame detected."); + else + console.log("No flame detected."); +}, 1000); + +// Print message when exiting +process.on('SIGINT', function() +{ + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/yg1006.cxx b/examples/yg1006.cxx new file mode 100644 index 00000000..07dbfb5a --- /dev/null +++ b/examples/yg1006.cxx @@ -0,0 +1,66 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2014 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 "yg1006.h" + +using namespace std; + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + + +int main () +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // Instantiate a yg1006 flame sensor on digital pin D2 + upm::YG1006* flame = new upm::YG1006(2); + + // check every second for the presence of a flame + while (shouldRun) + { + bool val = flame->flameDetected(); + if (val) + cout << "Flame detected." << endl; + else + cout << "No flame detected." << endl; + + sleep(1); + } +//! [Interesting] + + cout << "Exiting..." << endl; + + delete flame; + return 0; +} diff --git a/src/yg1006/CMakeLists.txt b/src/yg1006/CMakeLists.txt new file mode 100644 index 00000000..8f585498 --- /dev/null +++ b/src/yg1006/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "yg1006") +set (libdescription "upm yg1006 flame sensor module") +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init() diff --git a/src/yg1006/jsupm_yg1006.i b/src/yg1006/jsupm_yg1006.i new file mode 100644 index 00000000..1b83ad17 --- /dev/null +++ b/src/yg1006/jsupm_yg1006.i @@ -0,0 +1,8 @@ +%module jsupm_yg1006 +%include "../upm.i" + +%{ + #include "yg1006.h" +%} + +%include "yg1006.h" diff --git a/src/yg1006/pyupm_yg1006.i b/src/yg1006/pyupm_yg1006.i new file mode 100644 index 00000000..209a5345 --- /dev/null +++ b/src/yg1006/pyupm_yg1006.i @@ -0,0 +1,9 @@ +%module pyupm_yg1006 +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "yg1006.h" +%{ + #include "yg1006.h" +%} diff --git a/src/yg1006/yg1006.cxx b/src/yg1006/yg1006.cxx new file mode 100644 index 00000000..43d1c6a3 --- /dev/null +++ b/src/yg1006/yg1006.cxx @@ -0,0 +1,51 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2014 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" + +using namespace upm; +using namespace std; + +YG1006::YG1006(int pin) +{ + if ( !(m_gpio = mraa_gpio_init(pin)) ) + { + cerr << __FUNCTION__ << ": mraa_gpio_init() failed" << endl; + return; + } + + mraa_gpio_dir(m_gpio, MRAA_GPIO_IN); +} + +YG1006::~YG1006() +{ + mraa_gpio_close(m_gpio); +} + +bool YG1006::flameDetected() +{ + return (!mraa_gpio_read(m_gpio) ? true : false); +} diff --git a/src/yg1006/yg1006.h b/src/yg1006/yg1006.h new file mode 100644 index 00000000..a3414c83 --- /dev/null +++ b/src/yg1006/yg1006.h @@ -0,0 +1,64 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2014 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 + +namespace upm { + + /** + * @brief C++ API for the YG1006 flame sensor + * + * UPM module for the YG1006 flame sensor. It detects flame or any + * other light in the 760nm - 1100nm wavelength range. + * + * @ingroup gpio + * @snippet yg1006.cxx Interesting + */ + class YG1006 { + public: + /** + * YG1006 digital flame sensor constructor + * + * @param pin digital pin to use + */ + YG1006(int pin); + /** + * YG1006 Destructor + */ + ~YG1006(); + /** + * Determine whether a flame has been detected + * + * @return True if a flame or other comparable light source is detected + */ + bool flameDetected(); + + private: + mraa_gpio_context m_gpio; + }; +} + +