From 6135e932e48aff97ccdbe92ae932be919eae6d2d Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Tue, 13 Jan 2015 18:11:18 -0700 Subject: [PATCH] grove_mcfled: Add C++ and JS examples, utilizing existing GroveLed driver. The module adds examples that utilize the existing GroveLed driver for the Grove Multi Color Flash LED. Signed-off-by: Jon Trulson Signed-off-by: Zion Orent Signed-off-by: John Van Drasek --- examples/CMakeLists.txt | 2 + examples/grove_mcfled.cxx | 68 +++++++++++++++++++++++++++++ examples/javascript/grove_mcfled.js | 46 +++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 examples/grove_mcfled.cxx create mode 100644 examples/javascript/grove_mcfled.js diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 9d9ec10e..84a793c8 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -75,6 +75,7 @@ add_executable (grovespeaker-example grovespeaker.cxx) add_executable (rfr359f-example rfr359f.cxx) add_executable (biss0001-example biss0001.cxx) add_executable (my9221-example my9221.cxx) +add_executable (grove_mcfled-example grove_mcfled.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -215,3 +216,4 @@ target_link_libraries (grovespeaker-example grovespeaker ${CMAKE_THREAD_LIBS_INI target_link_libraries (rfr359f-example rfr359f ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (biss0001-example biss0001 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (my9221-example my9221 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (grove_mcfled-example grove ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/grove_mcfled.cxx b/examples/grove_mcfled.cxx new file mode 100644 index 00000000..16a6eba1 --- /dev/null +++ b/examples/grove_mcfled.cxx @@ -0,0 +1,68 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2014-2015 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 "grove.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 grove LED on D2. Here we are controlling a Grove + // Multi-color flash LED. We just just need to turn it on - it will + // then cycle through various colors (red, green, blue, purple) on it's + // own until turned off. + upm::GroveLed* led = new upm::GroveLed(2); + + // start the light show + led->on(); + + // just sleep until interrupted + while (shouldRun) + sleep(1); + +//! [Interesting] + + led->off(); + cout << "Exiting..." << endl; + + delete led; + + return 0; +} diff --git a/examples/javascript/grove_mcfled.js b/examples/javascript/grove_mcfled.js new file mode 100644 index 00000000..f5392691 --- /dev/null +++ b/examples/javascript/grove_mcfled.js @@ -0,0 +1,46 @@ +/*jslint node:true, vars:true, bitwise:true, unparam:true */ +/*jshint unused:true */ +/* +* Author: Zion Orent +* Copyright (c) 2015 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 upmled = require("jsupm_grove"); +// Initialize on GPIO 2 +var myled = new upmled.GroveLed(2); +myled.on(); + +// Dummy setInterval, just to keep program from quitting +// This way, the LED turns off when you quit the program +var myInterval = setInterval(function() +{ + ; +}, 1000); + +// When exiting: turn off LED, clear interval, and print message +process.on('SIGINT', function() +{ + clearInterval(myInterval); + myled.off(); + console.log("Exiting..."); + process.exit(0); +});