diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index e3f8bc3e..93441dbe 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -88,12 +88,19 @@ link_directories (${MRAA_LIBDIR}) # grove* will use module grove add_example (hmc5883l) add_example (led) +add_example (groveled) add_example (relay) +add_example (groverelay) add_example (light) +add_example (grovelight) add_example (temperature) +add_example (grovetemp) add_example (button) +add_example (grovebutton) add_example (rotary) +add_example (groverotary) add_example (slide) +add_example (groveslide) add_example (buzzer-sound) add_example (nrf24l01-transmitter) add_example (nrf24l01-receiver) @@ -143,11 +150,15 @@ add_example (ds1307) add_example (a110x) add_example (gp2y0a) add_example (moisture) +add_example (grovemoisture) add_example (ehr) +add_example (groveehr) add_example (ta12200) add_example (grovelinefinder) add_example (vdiv) +add_example (grovevdiv) add_example (water) +add_example (grovewater) add_example (guvas12d) add_example (mpr121) add_example (yg1006) @@ -155,6 +166,7 @@ add_example (wt5001) add_example (ppd42ns) add_example (mq303a) add_example (speaker) +add_example (grovespeaker) add_example (rfr359f) add_example (biss0001) add_example (rotaryencoder) @@ -168,10 +180,15 @@ add_example (hmtrp) add_example (nunchuck) add_example (otp538u) add_example (collision) +add_example (grovecollision) add_example (electromagnet) +add_example (groveelectromagnet) add_example (emg) +add_example (groveemg) add_example (o2) +add_example (groveo2) add_example (gsr) +add_example (grovegsr) add_example (ina132) add_example (l298) add_example (l298-stepper) @@ -180,6 +197,7 @@ add_example (grovemd) add_example (grovemd-stepper) add_example (pca9685) add_example (eldriver) +add_example (groveeldriver) add_example (adafruitss) add_example (adafruitms1438) add_example (adafruitms1438-stepper) @@ -215,6 +233,7 @@ add_example (mg811) add_example (wheelencoder) add_example (sm130) add_example (gprs) +add_example (grovegprs) add_example (lm35) add_example (micsv89) add_example (xbee) diff --git a/examples/c++/grovebutton.cxx b/examples/c++/grovebutton.cxx new file mode 100644 index 00000000..3534d16f --- /dev/null +++ b/examples/c++/grovebutton.cxx @@ -0,0 +1,49 @@ +/* + * Author: Sarah Knepper + * 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 "grove.hpp" + +int +main(int argc, char **argv) +{ + // This example uses GPIO 0 +//! [Interesting] + + // Create the button object using GPIO pin 0 + upm::GroveButton* button = new upm::GroveButton(0); + + // Read the input and print, waiting one second between readings + while( 1 ) { + std::cout << button->name() << " value is " << button->value() << std::endl; + sleep(1); + } + + // Delete the button object + delete button; +//! [Interesting] + + return 0; +} diff --git a/examples/c++/grovecollision.cxx b/examples/c++/grovecollision.cxx new file mode 100644 index 00000000..e7ad0f8d --- /dev/null +++ b/examples/c++/grovecollision.cxx @@ -0,0 +1,69 @@ +/* +* 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. +*/ +#include +#include +#include +#include "grovecollision.hpp" + +using namespace std; + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main(int argc, char **argv) +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // The was tested with the Grove Collision Sensor + // Instantiate a Grove Collision on digital pin D2 + upm::GroveCollision* collision = new upm::GroveCollision(2); + + bool collisionState = false; + cout << "No collision" << endl; + while (shouldRun) + { + if (collision->isColliding() && !collisionState) + { + cout << "Collision!" << endl; + collisionState = true; + } + else if (collisionState) + { + cout << "No collision" << endl; + collisionState = false; + } + } + +//! [Interesting] + cout << "Exiting" << endl; + + delete collision; + return 0; +} diff --git a/examples/c++/groveehr.cxx b/examples/c++/groveehr.cxx new file mode 100644 index 00000000..73775606 --- /dev/null +++ b/examples/c++/groveehr.cxx @@ -0,0 +1,78 @@ +/* + * 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 "groveehr.hpp" + +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 Ear-clip Heart Rate sensor on digital pin D2 + upm::GroveEHR* heart = new upm::GroveEHR(2); + + // set the beat counter to 0, init the clock and start counting beats + heart->clearBeatCounter(); + heart->initClock(); + heart->startBeatCounter(); + + while (shouldRun) + { + // we grab these just for display purposes in this example + uint32_t millis = heart->getMillis(); + uint32_t beats = heart->beatCounter(); + + // heartRate() requires that at least 5 seconds pass before + // returning anything other than 0 + int hr = heart->heartRate(); + + // output milliseconds passed, beat count, and computed heart rate + cout << "Millis: " << millis << " Beats: " << beats; + cout << " Heart Rate: " << hr << endl; + + sleep(1); + } + + heart->stopBeatCounter(); +//! [Interesting] + + cout << "Exiting..." << endl; + + delete heart; + return 0; +} diff --git a/examples/c++/groveeldriver.cxx b/examples/c++/groveeldriver.cxx new file mode 100644 index 00000000..9c1d561d --- /dev/null +++ b/examples/c++/groveeldriver.cxx @@ -0,0 +1,66 @@ +/* +* 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. +*/ +#include +#include +#include +#include "groveeldriver.hpp" + +using namespace std; + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main(int argc, char **argv) +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // The was tested with the Grove El Driver Module + // Instantiate a Grove El Driver on digital pin D2 + upm::GroveElDriver* eldriver = new upm::GroveElDriver(2); + + bool lightState = true; + + while (shouldRun) + { + if (lightState) + eldriver->on(); + else + eldriver->off(); + lightState = !lightState; + sleep(1); + } + +//! [Interesting] + eldriver->off(); + cout << "Exiting" << endl; + + delete eldriver; + return 0; +} diff --git a/examples/c++/groveelectromagnet.cxx b/examples/c++/groveelectromagnet.cxx new file mode 100644 index 00000000..bbd4d33e --- /dev/null +++ b/examples/c++/groveelectromagnet.cxx @@ -0,0 +1,79 @@ +/* +* 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. +*/ +#include +#include +#include +#include "groveelectromagnet.hpp" + +using namespace std; + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +float get_time() +{ + return ((float)(clock()))/CLOCKS_PER_SEC; +} + +int main(int argc, char **argv) +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // The was tested with the Grove Electromagnetic Module + // Instantiate a Grove Electromagnet on digital pin D2 + upm::GroveElectromagnet* magnet = new upm::GroveElectromagnet(2); + cout << "Starting up magnet...." << endl; + magnet->off(); + + bool magnetState = false; + float time_passed = get_time(); + + // Turn magnet on and off every 5 seconds + while (shouldRun) + { + if ((get_time() - time_passed) > 5.0) + { + magnetState = !magnetState; + if (magnetState) + magnet->on(); + else + magnet->off(); + cout << "Turning magnet " << ((magnetState) ? "on" : "off") << endl; + time_passed = get_time(); + } + } + +//! [Interesting] + magnet->off(); + cout << "Exiting" << endl; + + delete magnet; + return 0; +} diff --git a/examples/c++/groveemg.cxx b/examples/c++/groveemg.cxx new file mode 100644 index 00000000..3a049dd3 --- /dev/null +++ b/examples/c++/groveemg.cxx @@ -0,0 +1,60 @@ +/* +* 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. +*/ +#include +#include +#include +#include "groveemg.hpp" + +using namespace std; + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main(int argc, char **argv) +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // The was tested with the GroveEMG Muscle Signal Reader Sensor Module + // Instantiate a GroveEMG on analog pin A0 + upm::GroveEMG *emg = new upm::GroveEMG(0); + cout << "Calibrating...." << endl; + emg->calibrate(); + + while (shouldRun) + { + cout << emg->value() << endl; + usleep(100000); + } + +//! [Interesting] + cout << "Exiting" << endl; + delete emg; + return 0; +} diff --git a/examples/c++/grovegprs.cxx b/examples/c++/grovegprs.cxx new file mode 100644 index 00000000..f882cba7 --- /dev/null +++ b/examples/c++/grovegprs.cxx @@ -0,0 +1,111 @@ +/* + * Author: Jon Trulson + * 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. + */ + +#include +#include +#include +#include +#include "grovegprs.hpp" + +using namespace std; +using namespace upm; + +void printUsage(char *progname) +{ + cout << "Usage: " << progname << " [AT command]" << endl; + cout << endl; + + cout << "If an argument is supplied on the command line, that argument is" + << endl; + cout << "sent to the module and the response is printed out." << endl; + cout << endl; + cout << "If no argument is used, then the manufacturer and the current" + << endl; + cout << "saved profiles are queried and the results printed out." << endl; + cout << endl; + cout << endl; +} + +// simple helper function to send a command and wait for a response +void sendCommand(upm::GroveGPRS* sensor, string cmd) +{ + // commands need to be terminated with a carriage return + cmd += "\r"; + + sensor->writeDataStr(cmd); + + // wait up to 1 second + if (sensor->dataAvailable(1000)) + { + cout << "Returned: " << sensor->readDataStr(1024) << endl; + } + else + { + cerr << "Timed out waiting for response" << endl; + } +} + + +int main(int argc, char **argv) +{ +//! [Interesting] + + // Instantiate a GroveGPRS Module on UART 0 + upm::GroveGPRS* sensor = new upm::GroveGPRS(0); + + // Set the baud rate, 19200 baud is the default. + if (sensor->setBaudRate(19200) != mraa::SUCCESS) + { + cerr << "Failed to set tty baud rate" << endl; + return 1; + } + + printUsage(argv[0]); + + if (argc > 1) + { + cout << "Sending command line argument (" << argv[1] << ")..." << endl; + sendCommand(sensor, argv[1]); + } + else + { + // query the module manufacturer + cout << "Querying module manufacturer (AT+CGMI)..." << endl; + sendCommand(sensor, "AT+CGMI"); + + sleep(1); + + // query the saved profiles + cout << "Querying the saved profiles (AT&V)..." << endl; + sendCommand(sensor, "AT&V"); + + // A comprehensive list is available from the datasheet at: + // http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf + } + +//! [Interesting] + + delete sensor; + return 0; +} diff --git a/examples/c++/grovegsr.cxx b/examples/c++/grovegsr.cxx new file mode 100644 index 00000000..6068e791 --- /dev/null +++ b/examples/c++/grovegsr.cxx @@ -0,0 +1,63 @@ +/* + * 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. + */ + +#include +#include +#include +#include "grovegsr.hpp" + +using namespace std; + +bool shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main() +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // The was tested with the GroveGSR Galvanic Skin Response Sensor module. + + // Instantiate a GroveGSR on analog pin A0 + upm::GroveGSR *gsr = new upm::GroveGSR(0); + cout << "Calibrating...." << endl; + gsr->calibrate(); + + while (shouldRun) + { + cout << gsr->value() << endl; + usleep(500000); + } +//! [Interesting] + + cout << "Exiting" << endl; + + delete gsr; + return 0; +} diff --git a/examples/c++/groveled.cxx b/examples/c++/groveled.cxx new file mode 100644 index 00000000..8f7e752d --- /dev/null +++ b/examples/c++/groveled.cxx @@ -0,0 +1,55 @@ +/* + * Author: Brendan Le Foll + * Contributions: Sarah Knepper + * 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 "grove.hpp" + +int +main(int argc, char **argv) +{ +//! [Interesting] + + // Create the Grove LED object using GPIO pin 2 + upm::GroveLed* led = new upm::GroveLed(2); + + // Print the name + std::cout << led->name() << std::endl; + + // Turn the LED on and off 10 times, pausing one second + // between transitions + for (int i=0; i < 10; i++) { + led->on(); + sleep(1); + led->off(); + sleep(1); + } + + // Delete the Grove LED object + delete led; +//! [Interesting] + + return 0; +} diff --git a/examples/c++/grovelight.cxx b/examples/c++/grovelight.cxx new file mode 100644 index 00000000..18f8f6c0 --- /dev/null +++ b/examples/c++/grovelight.cxx @@ -0,0 +1,49 @@ +/* + * Author: Brendan Le Foll + * Contributions: Sarah Knepper + * 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 "grove.hpp" + +int +main(int argc, char **argv) +{ +//! [Interesting] + // Create the light sensor object using AIO pin 0 + upm::GroveLight* light = new upm::GroveLight(0); + + // Read the input and print both the raw value and a rough lux value, + // waiting one second between readings + while( 1 ) { + std::cout << light->name() << " raw value is " << light->raw_value() << + ", which is roughly " << light->value() << " lux" << std::endl; + sleep(1); + } + + // Delete the light sensor object + delete light; +//! [Interesting] + return 0; +} diff --git a/examples/c++/grovemoisture.cxx b/examples/c++/grovemoisture.cxx new file mode 100644 index 00000000..f63a088d --- /dev/null +++ b/examples/c++/grovemoisture.cxx @@ -0,0 +1,75 @@ +/* + * 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 "grovemoisture.hpp" + +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 Moisture sensor on analog pin A0 + upm::GroveMoisture* moisture = new upm::GroveMoisture(0); + + // Values (approximate): + // 0-300, sensor in air or dry soil + // 300-600, sensor in humid soil + // 600+, sensor in wet soil or submerged in water. + // Read the value every second and print the corresponding moisture level + while (shouldRun) + { + int val = moisture->value(); + cout << "Moisture value: " << val << ", "; + if (val >= 0 && val < 300) + cout << "dry"; + else if (val >= 300 && val < 600) + cout << "moist"; + else + cout << "wet"; + + cout << endl; + + sleep(1); + } +//! [Interesting] + + cout << "Exiting" << endl; + + delete moisture; + return 0; +} diff --git a/examples/c++/groveo2.cxx b/examples/c++/groveo2.cxx new file mode 100644 index 00000000..79741b52 --- /dev/null +++ b/examples/c++/groveo2.cxx @@ -0,0 +1,57 @@ +/* +* 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. +*/ +#include +#include +#include +#include "groveo2.hpp" + +using namespace std; + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main(int argc, char **argv) +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // The was tested with the O2 Oxygen Concentration Sensor Module + // Instantiate a GroveO2 on analog pin A0 + upm::GroveO2 *O2 = new upm::GroveO2(0); + while (shouldRun) + { + cout << "The output voltage is: " << O2->voltageValue() << "mV" << endl; + usleep(100000); + } + +//! [Interesting] + cout << "Exiting" << endl; + delete O2; + return 0; +} diff --git a/examples/c++/groverelay.cxx b/examples/c++/groverelay.cxx new file mode 100644 index 00000000..92971399 --- /dev/null +++ b/examples/c++/groverelay.cxx @@ -0,0 +1,58 @@ +/* + * Author: Sarah Knepper + * 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. + */ + +#include +#include +#include "grove.hpp" + +int +main(int argc, char **argv) +{ + // This example uses GPIO 0 +//! [Interesting] + + // Create the relay switch object using GPIO pin 0 + upm::GroveRelay* relay = new upm::GroveRelay(0); + + // Close and then open the relay switch 3 times, + // waiting one second each time. The LED on the relay switch + // will light up when the switch is on (closed). + // The switch will also make a noise between transitions. + for ( int i = 0; i < 3; i++ ) { + relay->on(); + if ( relay->isOn() ) + std::cout << relay->name() << " is on" << std::endl; + sleep(1); + relay->off(); + if ( relay->isOff() ) + std::cout << relay->name() << " is off" << std::endl; + sleep(1); + } + + // Delete the relay switch object + delete relay; +//! [Interesting] + + return 0; +} diff --git a/examples/c++/groverotary.cxx b/examples/c++/groverotary.cxx new file mode 100644 index 00000000..aa2ee57f --- /dev/null +++ b/examples/c++/groverotary.cxx @@ -0,0 +1,57 @@ +/* + * Author: Mihai Tudor Panu + * 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 "grove.hpp" + +using namespace std; + +int main () +{ +//! [Interesting] + // Instantiate a rotary sensor on analog pin A0 + upm::GroveRotary* knob = new upm::GroveRotary(0); + + // Print sensor name to confirm it initialized properly + cout << knob->name() << endl; + + while(true) { + float abs_value = knob->abs_value(); // Absolute raw value + float abs_deg = knob->abs_deg(); // Absolute degrees + float abs_rad = knob->abs_rad(); // Absolute radians + float rel_value = knob->rel_value(); // Relative raw value + float rel_deg = knob->rel_deg(); // Relative degrees + float rel_rad = knob->rel_rad(); // Relative radians + + fprintf(stdout, "Absolute: %4d raw %5.2f deg = %3.2f rad Relative: %4d raw %5.2f deg %3.2f rad\n", + (int16_t)abs_value, abs_deg, abs_rad, (int16_t)rel_value, rel_deg, rel_rad); + + usleep(2500000); // Sleep for 2.5s + } +//! [Interesting] + delete knob; + return 0; +} diff --git a/examples/c++/groveslide.cxx b/examples/c++/groveslide.cxx new file mode 100644 index 00000000..04b8bb4e --- /dev/null +++ b/examples/c++/groveslide.cxx @@ -0,0 +1,50 @@ +/* + * Author: Mihai Tudor Panu + * 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 "grove.hpp" + +using namespace std; + +int main () +{ +//! [Interesting] + upm::GroveSlide* slide = new upm::GroveSlide(0); // Instantiate new grove slide potentiometer on analog pin A0 + + cout << slide->name() << endl; + + while(true) { + float adc_value = slide->raw_value(); // Read raw value + float volts = slide->voltage_value(); // Read voltage, board reference set at 5.0V + fprintf(stdout, "%4d = %.2f V\n", (uint16_t)adc_value, volts); + + usleep(2500000); // Sleep for 2.5s + } +//! [Interesting] + delete slide; + return 0; +} diff --git a/examples/c++/grovespeaker.cxx b/examples/c++/grovespeaker.cxx new file mode 100644 index 00000000..4e139e5b --- /dev/null +++ b/examples/c++/grovespeaker.cxx @@ -0,0 +1,49 @@ +/* + * 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 "grovespeaker.hpp" + +using namespace std; + +int main () +{ +//! [Interesting] + // Instantiate a Grove Speaker on digital pin D2 + upm::GroveSpeaker* speaker = new upm::GroveSpeaker(2); + + // Play all 7 of the lowest notes + speaker->playAll(); + + // Play a medium C-sharp + speaker->playSound('c', true, "med"); +//! [Interesting] + + cout << "Exiting" << endl; + + delete speaker; + return 0; +} diff --git a/examples/c++/grovetemp.cxx b/examples/c++/grovetemp.cxx new file mode 100644 index 00000000..2a21bc15 --- /dev/null +++ b/examples/c++/grovetemp.cxx @@ -0,0 +1,55 @@ +/* + * Author: Brendan Le Foll + * Contributions: Sarah Knepper + * 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 "grove.hpp" + +int +main(int argc, char **argv) +{ +//! [Interesting] + + // Create the temperature sensor object using AIO pin 0 + upm::GroveTemp* temp = new upm::GroveTemp(0); + std::cout << temp->name() << std::endl; + + // Read the temperature ten times, printing both the Celsius and + // equivalent Fahrenheit temperature, waiting one second between readings + for (int i=0; i < 10; i++) { + int celsius = temp->value(); + int fahrenheit = (int) (celsius * 9.0/5.0 + 32.0); + printf("%d degrees Celsius, or %d degrees Fahrenheit\n", + celsius, fahrenheit); + sleep(1); + } + + // Delete the temperature sensor object + delete temp; +//! [Interesting] + + return 0; +} diff --git a/examples/c++/grovevdiv.cxx b/examples/c++/grovevdiv.cxx new file mode 100644 index 00000000..0f903338 --- /dev/null +++ b/examples/c++/grovevdiv.cxx @@ -0,0 +1,67 @@ +/* + * 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 "grovevdiv.hpp" + +using namespace std; + +bool shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + + +int main () +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // Instantiate a Grove Voltage Divider sensor on analog pin A0 + upm::GroveVDiv* vDiv = new upm::GroveVDiv(0); + + // collect data and output measured voltage according to the setting + // of the scaling switch (3 or 10) + while (shouldRun) + { + unsigned int val = vDiv->value(100); + float gain3val = vDiv->computedValue(3, val); + float gain10val = vDiv->computedValue(10, val); + cout << "ADC value: " << val << " Gain 3: " << gain3val + << "v Gain 10: " << gain10val << "v" << endl; + + sleep(1); + } +//! [Interesting] + + cout << "Exiting..." << endl; + + delete vDiv; + return 0; +} diff --git a/examples/c++/grovewater.cxx b/examples/c++/grovewater.cxx new file mode 100644 index 00000000..e13f8e7d --- /dev/null +++ b/examples/c++/grovewater.cxx @@ -0,0 +1,65 @@ +/* + * 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 "grovewater.hpp" + +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 Water sensor on digital pin D2 + upm::GroveWater* water = new upm::GroveWater(2); + + while (shouldRun) + { + bool val = water->isWet(); + if (val) + cout << "Sensor is wet." << endl; + else + cout << "Sensor is dry." << endl; + + sleep(1); + } +//! [Interesting] + + cout << "Exiting..." << endl; + + delete water; + return 0; +} diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index 7395a5c3..0290a79e 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -31,30 +31,47 @@ add_example(DS1307Sample ds1307) add_example(ENC03RSample enc03r) add_example(ES08ASample servo) add_example(ButtonSample button) +add_example(GroveButtonSample grove) add_example(Button_intrSample button) +add_example(GroveButton_intrSample grove) add_example(Collision collision) add_example(EHRSample ehr) +add_example(GroveEHRSample groveehr) add_example(Emg emg) +add_example(GroveEmg groveemg) add_example(Gsr gsr) +add_example(GroveGsr grovegsr) add_example(GroveLed_multiSample grove) add_example(LEDSample led) add_example(LightSample light) +add_example(GroveLightSample grove) add_example(GroveLineFinderSample grovelinefinder) +add_example(GroveLed_multiSample grove) +add_example(GroveLEDSample grove) add_example(GroveMDSample grovemd) add_example(MoistureSample moisture) +add_example(GroveMoistureSample grovemoisture) add_example(GroveMQ3 gas) add_example(GroveMQ9 gas) add_example(O2Example o2) +add_example(GroveO2Example groveo2) add_example(GroveQTouch at42qt1070) add_example(RelaySample relay) +add_example(GroveRelaySample grove) add_example(RotarySample rotary) +add_example(GroveRotarySample grove) add_example(GROVESCAMSample grovescam) add_example(SCAMSample scam) add_example(SlideSample slide) +add_example(GroveSlideSample grove) add_example(SpeakerSample speaker) +add_example(GroveSpeakerSample grovespeaker) add_example(TemperatureSample temperature) +add_example(GroveTempSample grove) add_example(VDivSample vdiv) +add_example(GroveVDivSample grovevdiv) add_example(WaterSample water) +add_example(GroveWaterSample grovewater) add_example(GroveWFSSample grovewfs) add_example(GUVAS12DSample guvas12d) add_example(H3LIS331DLSample h3lis331dl) diff --git a/examples/java/GroveButtonSample.java b/examples/java/GroveButtonSample.java new file mode 100644 index 00000000..9be6e2fc --- /dev/null +++ b/examples/java/GroveButtonSample.java @@ -0,0 +1,40 @@ +/* + * Author: Stefan Andritoiu + * 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. + */ + +public class GroveButtonSample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Create the button object using GPIO pin 2 + upm_grove.GroveButton button = new upm_grove.GroveButton(2); + + while (true) { + System.out.println(button.name() + " value is " + button.value()); + + Thread.sleep(1000); + } + // ! [Interesting] + } + +} \ No newline at end of file diff --git a/examples/java/GroveButton_intrSample.java b/examples/java/GroveButton_intrSample.java new file mode 100644 index 00000000..ad0d72ee --- /dev/null +++ b/examples/java/GroveButton_intrSample.java @@ -0,0 +1,53 @@ +/* + * Author: Stefan Andritoiu + * 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. + */ + +public class GroveButton_intrSample { + + public static int counter = 0; + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + upm_grove.GroveButton b = new upm_grove.GroveButton(2); + + ButtonISR callback = new ButtonISR(); + b.installISR(2, callback); + + while (true) { + System.out.println("Counter: " + counter); + Thread.sleep(1000); + } + // ! [Interesting] + } +} + +class ButtonISR implements Runnable { + public ButtonISR() { + super(); + } + + public void run() { + GroveButton_intrSample.counter++; + System.out.println("Button pressed!"); + } +} \ No newline at end of file diff --git a/examples/java/GroveCollision.java b/examples/java/GroveCollision.java new file mode 100644 index 00000000..e1ace72e --- /dev/null +++ b/examples/java/GroveCollision.java @@ -0,0 +1,48 @@ +/* + * Author: Abhishek Malik + * 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. + */ + +import upm_grovecollision.*; + +public class GroveCollision { + + public static void main(String[] args) { + // Initializing the sensor on D2 on the Base Shield + upm_grovecollision.GroveCollision collision = new upm_grovecollision.GroveCollision(2); + boolean collisionState = false; + + // ! [Interesting] + while(true){ + if(collision.isColliding() && !collisionState){ + System.out.println("Collision!!"); + collisionState = true; + } + else if (collisionState){ + System.out.println("No Collision!"); + collisionState = false; + } + } + // ! [Interesting] + } + +} diff --git a/examples/java/GroveEHRSample.java b/examples/java/GroveEHRSample.java new file mode 100644 index 00000000..ddec36ac --- /dev/null +++ b/examples/java/GroveEHRSample.java @@ -0,0 +1,53 @@ +/* + * Author: Stefan Andritoiu + * 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. + */ + +//NOT TESTED!!! +public class GroveEHRSample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Instantiate a Grove Ear-clip Heart Rate sensor on digital pin D2 + upm_groveehr.GroveEHR heart = new upm_groveehr.GroveEHR(2); + + // set the beat counter to 0, init the clock and start counting beats + heart.clearBeatCounter(); + heart.initClock(); + heart.startBeatCounter(); + + while (true) { + long millis = heart.getMillis(); + long beats = heart.beatCounter(); + + // heartRate() requires that at least 5 seconds pass before + // returning anything other than 0 + int hr = heart.heartRate(); + + // output milliseconds passed, beat count, and computed heart rate + System.out.println("Millis: " + millis + ", Beats: " + beats + ", Heart rate: " + hr); + + Thread.sleep(1000); + } + // ! [Interesting] + } +} \ No newline at end of file diff --git a/examples/java/GroveEmg.java b/examples/java/GroveEmg.java new file mode 100644 index 00000000..98277011 --- /dev/null +++ b/examples/java/GroveEmg.java @@ -0,0 +1,47 @@ +/* + * Author: Abhishek Malik + * 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. + */ +import upm_groveemg.GroveEMG; + +public class GroveEmg { + + public static void main(String[] args) { + // TODO Auto-generated method stub + //! [Interesting] + // Instantiating the Grove EMG sensor on Analog pin 0 + GroveEMG emg = new GroveEMG(0); + System.out.println("Calibrating ... "); + emg.calibrate(); + + while(true){ + System.out.println("EMG Val: "+emg.value()); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + System.out.println("The following exception occurred: "+e.getMessage()); + } + } + } + //! [Interesting] +} diff --git a/examples/java/GroveGsr.java b/examples/java/GroveGsr.java new file mode 100644 index 00000000..d7e9a69c --- /dev/null +++ b/examples/java/GroveGsr.java @@ -0,0 +1,50 @@ +/* + * Author: Abhishek Malik + * 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. + */ + +import upm_grovegsr.GroveGSR; + +public class GroveGsr { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + //! [Interesting] + // Instantiate a Grove GSR sensor on analog pin A0 + GroveGSR gsr = new GroveGSR(0); + System.out.println("Calibrating..."); + gsr.calibrate(); + + while(true){ + System.out.println("Value: "+gsr.value()); + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + System.out.println("The following exception occurred: "+e.getMessage()); + } + } + //! [Interesting] + } + +} diff --git a/examples/java/GroveLEDSample.java b/examples/java/GroveLEDSample.java new file mode 100644 index 00000000..b9964490 --- /dev/null +++ b/examples/java/GroveLEDSample.java @@ -0,0 +1,39 @@ +/* + * Author: Andrei Vasiliu + * 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. + */ + +public class GroveLEDSample { + public static void main (String args[]) throws InterruptedException { + //! [Interesting] + upm_grove.GroveLed led = new upm_grove.GroveLed(2); + + for (int i = 0; i < 10; ++i) { + led.on(); + Thread.sleep(1000); + led.off(); + Thread.sleep(1000); + } + led.delete(); + //! [Interesting] + } +} diff --git a/examples/java/GroveLightSample.java b/examples/java/GroveLightSample.java new file mode 100644 index 00000000..611b744b --- /dev/null +++ b/examples/java/GroveLightSample.java @@ -0,0 +1,41 @@ +/* + * Author: Stefan Andritoiu + * 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. + */ + +public class GroveLightSample { + public static void main(String args[]) throws InterruptedException { + // ! [Interesting] + upm_grove.GroveLight gl = new upm_grove.GroveLight(2); + + while (true) { + float raw_value = gl.raw_value(); + float value = gl.value(); + + System.out.println("raw value: " + raw_value); + System.out.println("value: " + value); + + Thread.sleep(1000); + } + // ! [Interesting] + } +} \ No newline at end of file diff --git a/examples/java/GroveMoistureSample.java b/examples/java/GroveMoistureSample.java new file mode 100644 index 00000000..8fb32d6e --- /dev/null +++ b/examples/java/GroveMoistureSample.java @@ -0,0 +1,47 @@ +/* + * Author: Stefan Andritoiu + * 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. + */ + +public class GroveMoistureSample { + public static void main(String args[]) throws InterruptedException { + // ! [Interesting] + upm_grovemoisture.GroveMoisture gm = new upm_grovemoisture.GroveMoisture(1); + + while (true) { + int moisture_val = gm.value(); + String result; + + if (moisture_val >= 0 && moisture_val < 300) + result = "Dry"; + else if ((moisture_val >= 0 && moisture_val < 300)) + result = "Moist"; + else + result = "Wet"; + + System.out.println("Moisture Value: " + moisture_val + ", " + result); + + Thread.sleep(1000); + } + // ! [Interesting] + } +} \ No newline at end of file diff --git a/examples/java/GroveO2Example.java b/examples/java/GroveO2Example.java new file mode 100644 index 00000000..6ef0e61e --- /dev/null +++ b/examples/java/GroveO2Example.java @@ -0,0 +1,46 @@ +/* + * Author: Abhishek Malik + * 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. + */ + +import upm_groveo2.GroveO2; + +public class GroveO2Example { + + public static void main(String[] args) { + // TODO Auto-generated method stub + //! [Interesting] + // Initializing the Grove O2 sensor on the A) analog pin + GroveO2 o2 = new GroveO2(0); + + while(true){ + System.out.println("The output voltage is: "+o2.voltageValue()); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + System.out.println("The following exception has occurred: "+e.getMessage()); + } + } + //! [Interesting] + } +} diff --git a/examples/java/GroveRelaySample.java b/examples/java/GroveRelaySample.java new file mode 100644 index 00000000..56a044b9 --- /dev/null +++ b/examples/java/GroveRelaySample.java @@ -0,0 +1,44 @@ +/* + * Author: Stefan Andritoiu + * 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. + */ + +public class GroveRelaySample { + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Create the button object using UART + upm_grove.GroveRelay relay = new upm_grove.GroveRelay(5); + + for (int i = 0; i < 3; i++) { + relay.on(); + if (relay.isOn()) + System.out.println("Relay is on"); + Thread.sleep(1000); + + relay.off(); + if (relay.isOff()) + System.out.println("Relay is off"); + Thread.sleep(1000); + } + // ! [Interesting] + } +} \ No newline at end of file diff --git a/examples/java/GroveRotarySample.java b/examples/java/GroveRotarySample.java new file mode 100644 index 00000000..f1b98c74 --- /dev/null +++ b/examples/java/GroveRotarySample.java @@ -0,0 +1,49 @@ +/* + * Author: Stefan Andritoiu + * 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. + */ + +public class GroveRotarySample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + upm_grove.GroveRotary knob = new upm_grove.GroveRotary(0); + + while (true) { + float abs_value = knob.abs_value(); // Absolute raw value + float abs_deg = knob.abs_deg(); // Absolute degrees + float abs_rad = knob.abs_rad(); // Absolute radians + float rel_value = knob.rel_value(); // Relative raw value + float rel_deg = knob.rel_deg(); // Relative degrees + float rel_rad = knob.rel_rad(); // Relative radians + + System.out.println("Absolute: " + abs_value + " raw, " + abs_deg + " deg, " + abs_rad + + " rad"); + System.out.println("Relative: " + rel_value + " raw, " + rel_deg + " deg, " + rel_rad + + " rad"); + + Thread.sleep(3000); + } + // ! [Interesting] + } + +} \ No newline at end of file diff --git a/examples/java/GroveSlideSample.java b/examples/java/GroveSlideSample.java new file mode 100644 index 00000000..b1597130 --- /dev/null +++ b/examples/java/GroveSlideSample.java @@ -0,0 +1,42 @@ +/* + * Author: Andrei Vasiliu + * 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. + */ + +public class GroveSlideSample { + public static void main (String args[]) throws InterruptedException { + //! [Interesting] + // Instantiate new grove slide potentiometer on analog pin A0 + upm_grove.GroveSlide slide = new upm_grove.GroveSlide(0); + + while (true) { + float raw_value = slide.raw_value(); + float value = slide.voltage_value(); + + System.out.println("raw value: " + raw_value); + System.out.println("value: " + value); + + Thread.sleep(2500); + } + //! [Interesting] + } +} diff --git a/examples/java/GroveSpeakerSample.java b/examples/java/GroveSpeakerSample.java new file mode 100644 index 00000000..2e96c893 --- /dev/null +++ b/examples/java/GroveSpeakerSample.java @@ -0,0 +1,40 @@ +/* + * Author: Andrei Vasiliu + * 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. + */ + +public class GroveSpeakerSample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Instantiate a Grove Speaker on digital pin D2 + upm_grovespeaker.GroveSpeaker speaker = new upm_grovespeaker.GroveSpeaker(2); + + // Play all 7 of the lowest notes + speaker.playAll(); + + // Play a medium C-sharp + speaker.playSound('c', true, "med"); + // ! [Interesting] + } + +} \ No newline at end of file diff --git a/examples/java/GroveTempSample.java b/examples/java/GroveTempSample.java new file mode 100644 index 00000000..68788273 --- /dev/null +++ b/examples/java/GroveTempSample.java @@ -0,0 +1,43 @@ +/* + * Author: Andrei Vasiliu + * 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. + */ + +public class GroveTempSample { + public static void main (String args[]) throws InterruptedException { + //! [Interesting] + upm_grove.GroveTemp temp = new upm_grove.GroveTemp(3); + + for (int i = 0; i < 10; ++i) { + + int celsius = temp.value(); + int fahrneheit = celsius * 2 + 32; + + System.out.println("Celsius: " + celsius); + System.out.println("Fahrneheit: " + fahrneheit); + + Thread.sleep(1000); + } + temp.delete(); + //! [Interesting] + } +} diff --git a/examples/java/GroveVDivSample.java b/examples/java/GroveVDivSample.java new file mode 100644 index 00000000..579f2c3b --- /dev/null +++ b/examples/java/GroveVDivSample.java @@ -0,0 +1,50 @@ +/* + * Author: Stefan Andritoiu + * 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. + */ + +//NOT TESTED!!! +public class GroveVDivSample { + private static final short gain3 = 3; + private static final short gain10 = 10; + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Instantiate a Grove Voltage Divider sensor on analog pin A0 + upm_grovevdiv.GroveVDiv vDiv = new upm_grovevdiv.GroveVDiv(0); + + // collect data and output measured voltage according to the setting + // of the scaling switch (3 or 10) + while (true) { + long val = vDiv.value(100); + float gain3val = vDiv.computedValue(gain3, val); + float gain10val = vDiv.computedValue(gain10, val); + + System.out.println("ADC value: " + val + ", Gain 3: " + gain3val + "v, Gain 10: " + + gain10val); + + Thread.sleep(1000); + } + // ! [Interesting] + } + +} \ No newline at end of file diff --git a/examples/java/GroveWaterSample.java b/examples/java/GroveWaterSample.java new file mode 100644 index 00000000..f275df77 --- /dev/null +++ b/examples/java/GroveWaterSample.java @@ -0,0 +1,45 @@ +/* + * Author: Stefan Andritoiu + * 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. + */ + +public class GroveWaterSample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Instantiate a Grove Water sensor on digital pin D2 + upm_grovewater.GroveWater water = new upm_grovewater.GroveWater(2); + + while (true) { + boolean val = water.isWet(); + if (val) { + System.out.println("Sensor is wet"); + } else { + System.out.println("Sensor is dry"); + } + + Thread.sleep(1000); + } + // ! [Interesting] + } + +} \ No newline at end of file diff --git a/examples/javascript/grovebutton.js b/examples/javascript/grovebutton.js new file mode 100644 index 00000000..428396ad --- /dev/null +++ b/examples/javascript/grovebutton.js @@ -0,0 +1,35 @@ +/* + * Author: Sarah Knepper + * 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. + */ + +// Load Grove module +var groveSensor = require('jsupm_grove'); + +// Create the button object using GPIO pin 0 +var button = new groveSensor.GroveButton(0); + +// Read the input and print, waiting one second between readings +function readButtonValue() { + console.log(button.name() + " value is " + button.value()); +} +setInterval(readButtonValue, 1000); diff --git a/examples/javascript/grovecollision.js b/examples/javascript/grovecollision.js new file mode 100644 index 00000000..7fa1c0a9 --- /dev/null +++ b/examples/javascript/grovecollision.js @@ -0,0 +1,60 @@ +/* +* 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 collision_lib = require("jsupm_grovecollision"); + +// The was tested with the Grove Collision Sensor +// Instantiate a Grove Collision on digital pin D2 +var collision_obj = new collision_lib.GroveCollision(2); + +var collisionState = false; +console.log("No collision"); + +// Having an infinate loop prevents nodeJS from catching Cntl-C +// We need to catch Cntl-C to clean up memory +// Instead, we check the collision sensor every millisecond +var myInterval = setInterval(function() +{ + if (collision_obj.isColliding() && !collisionState) + { + console.log("Collision!"); + collisionState = true; + } + else if (!collision_obj.isColliding() && collisionState) + { + console.log("No collision"); + collisionState = false; + } +}, 1); + +// When exiting: clear interval, clean up memory, and print message +process.on('SIGINT', function() +{ + clearInterval(myInterval); + collision_obj = null; + collision_lib.cleanUp(); + collision_lib = null; + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/javascript/groveehr.js b/examples/javascript/groveehr.js new file mode 100644 index 00000000..40684900 --- /dev/null +++ b/examples/javascript/groveehr.js @@ -0,0 +1,62 @@ +/* +* 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. +*/ + +// Load heart rate sensor module +var heartRateSensor = require('jsupm_groveehr'); +// Instantiate a Grove Ear-clip Heart Rate sensor on digital pin D2 +var myHeartRateSensor = new heartRateSensor.GroveEHR(2); + +// set the beat counter to 0, init the clock and start counting beats +myHeartRateSensor.clearBeatCounter(); +myHeartRateSensor.initClock(); +myHeartRateSensor.startBeatCounter(); + +var millis, beats, hr; +var myInterval = setInterval(function() +{ + // we grab these just for display purposes in this example + millis = myHeartRateSensor.getMillis(); + beats = myHeartRateSensor.beatCounter(); + + // heartRate() requires that at least 5 seconds pass before + // returning anything other than 0 + hr = myHeartRateSensor.heartRate(); + + // output milliseconds passed, beat count, and computed heart rate + console.log("Millis: " + millis + " Beats: " + beats + + " Heart Rate: " + hr); +}, 1000); + +// Print message when exiting +process.on('SIGINT', function() +{ + clearInterval(myInterval); + myHeartRateSensor.stopBeatCounter(); + myHeartRateSensor = null + heartRateSensor.cleanUp(); + heartRateSensor = null; + + console.log("Exiting"); + process.exit(0); +}); diff --git a/examples/javascript/groveeldriver.js b/examples/javascript/groveeldriver.js new file mode 100644 index 00000000..0cf03c90 --- /dev/null +++ b/examples/javascript/groveeldriver.js @@ -0,0 +1,52 @@ +/* +* 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 eldriver_lib = require("jsupm_groveeldriver"); + +// The was tested with the Grove El Driver Module +// Instantiate a Grove El Driver on digital pin D2 +var eldriver_obj = new eldriver_lib.GroveElDriver(2); + +var lightState = true; + +var myInterval = setInterval(function() +{ + if (lightState) + eldriver_obj.on(); + else + eldriver_obj.off(); + lightState = !lightState; +}, 1000); + +// When exiting: turn off EL wire, clear interval, and print message +process.on('SIGINT', function() +{ + clearInterval(myInterval); + eldriver_obj.off(); + eldriver_obj = null; + eldriver_lib.cleanUp(); + eldriver_lib = null; + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/javascript/groveelectromagnet.js b/examples/javascript/groveelectromagnet.js new file mode 100644 index 00000000..8a75b713 --- /dev/null +++ b/examples/javascript/groveelectromagnet.js @@ -0,0 +1,55 @@ +/* +* 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 electromagnet_lib = require("jsupm_groveelectromagnet"); +// The was tested with the Grove Electromagnetic Module +// Instantiate a Grove Electromagnet on digital pin D2 +var electromagnet_obj = new electromagnet_lib.GroveElectromagnet(2); +console.log("Starting up magnet...."); +electromagnet_obj.off(); + +var magnetState = false; + +// Turn magnet on and off every 5 seconds +var myInterval = setInterval(function() +{ + magnetState = !magnetState; + if (magnetState) + electromagnet_obj.on(); + else + electromagnet_obj.off(); + console.log("Turning magnet " + ((magnetState) ? "on" : "off")); +}, 5000); + +// When exiting: clear interval, turn off magnet, run memory cleanup, and print message +process.on('SIGINT', function() +{ + clearInterval(myInterval); + electromagnet_obj.off(); + electromagnet_obj = null; + electromagnet_lib.cleanUp(); + electromagnet_lib = null; + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/javascript/groveemg.js b/examples/javascript/groveemg.js new file mode 100644 index 00000000..4ddcb6e6 --- /dev/null +++ b/examples/javascript/groveemg.js @@ -0,0 +1,46 @@ +/* +* 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 groveemg_lib = require("jsupm_groveemg"); + +// The was tested with the GroveEMG Muscle Signal Reader Sensor Module +// Instantiate a GroveEMG on analog pin A0 +var groveemg_obj = new groveemg_lib.GroveEMG(0); +console.log("Calibrating...."); +groveemg_obj.calibrate(); + +var myInterval = setInterval(function() +{ + console.log(groveemg_obj.value()); +}, 100); + +// When exiting: clear interval, and print message +process.on('SIGINT', function() +{ + clearInterval(myInterval); + groveemg_obj = null; + groveemg_lib.cleanUp(); + groveemg_lib = null; + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/javascript/grovegprs.js b/examples/javascript/grovegprs.js new file mode 100644 index 00000000..dda80acd --- /dev/null +++ b/examples/javascript/grovegprs.js @@ -0,0 +1,111 @@ +/* + * Author: Jon Trulson + * 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 sensorObj = require('jsupm_grovegprs'); + + +/************** Functions **************/ +function printUsage(progname) +{ + var outputStr = "Usage: " + progname + " [AT command]\n\n" + + "If an argument is supplied on the command line, that argument is\n" + + "sent to the module and the response is printed out.\n\n" + + "If no argument is used, then the manufacturer and the current\n" + + "saved profiles are queried and the results printed out.\n\n" + console.log(outputStr); +} + +// simple helper function to send a command and wait for a response +function sendCommand(sensor, cmd, callback) +{ + // commands need to be terminated with a carriage return + cmd += "\r"; + sensor.writeDataStr(cmd); + + // wait up to 1 second + if (sensor.dataAvailable(1000)) + { + console.log("Returned: " + sensor.readDataStr(1024)); + } + else + console.log("Timed out waiting for response"); + + if (callback) + callback(); +} + +/************** Main code **************/ +// Instantiate a GROVEGPRS Module on UART 0 +var sensor = new sensorObj.GroveGPRS(0); + +// Set the baud rate, 19200 baud is the default. +if (sensor.setBaudRate(19200)) +{ + console.log("Failed to set baud rate"); + process.exit(0); +} + +printUsage(process.argv[1]); + +// Note: in nodeJS, command-line argument 0 is "node". +// Command-line argument 1 is "grovegprs.js" +// If you have a third argument, then it's a command +if (process.argv.length > 2) +{ + console.log("Sending command line argument (" + process.argv[2] + ")..."); + sendCommand(sensor, process.argv[2]); +} +else +{ + // sending this command as a synchronous callback ensures better timing + var callbackFunc = function() + { + setTimeout(function() + { + // query the saved profiles + console.log("Querying the saved profiles (AT&V)..."); + sendCommand(sensor, "AT&V"); + + // A comprehensive list is available from the + // datasheet at: + // http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf + }, 1000); + }; + + // query the module manufacturer + console.log("Querying module manufacturer (AT+CGMI)..."); + sendCommand(sensor, "AT+CGMI", callbackFunc); +} + + +/************** Exit code **************/ +process.on('SIGINT', function() +{ + sensor = null; + sensorObj.cleanUp(); + sensorObj = null; + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/javascript/grovegsr.js b/examples/javascript/grovegsr.js new file mode 100644 index 00000000..6b98af19 --- /dev/null +++ b/examples/javascript/grovegsr.js @@ -0,0 +1,49 @@ +/* +* 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 grovegsr_lib = require("jsupm_grovegsr"); + +// The was tested with the GroveGSR Galvanic Skin Response Sensor module. + +// Instantiate a GroveGSR on analog pin A0 +var grovegsr_obj = new grovegsr_lib.GroveGSR(0); +console.log("Calibrating...."); +grovegsr_obj.calibrate(); + +var myInterval = setInterval(function() +{ + console.log(grovegsr_obj.value()); +}, 500); + + +// When exiting: clear interval, and print message +process.on('SIGINT', function() +{ + clearInterval(myInterval); + grovegsr_obj = null; + grovegsr_lib.cleanUp(); + grovegsr_lib = null; + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/javascript/groveled.js b/examples/javascript/groveled.js new file mode 100644 index 00000000..7314d39f --- /dev/null +++ b/examples/javascript/groveled.js @@ -0,0 +1,46 @@ +/* + * Author: Sarah Knepper + * 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. + */ + +// Load Grove module +var groveSensor = require('jsupm_grove'); + +// Create the Grove LED object using GPIO pin 2 +var led = new groveSensor.GroveLed(2); + +// Print the name +console.log(led.name()); + +// Turn the LED on and off 10 times, pausing one second +// between transitions +var i = 0; +var waiting = setInterval(function() { + if ( i % 2 == 0 ) { + led.on(); + } else { + led.off(); + } + i++; + if ( i == 20 ) clearInterval(waiting); + }, 1000); + diff --git a/examples/javascript/grovelight.js b/examples/javascript/grovelight.js new file mode 100644 index 00000000..d09b4627 --- /dev/null +++ b/examples/javascript/grovelight.js @@ -0,0 +1,37 @@ +/* + * Author: Sarah Knepper + * 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. + */ + +// Load Grove module +var groveSensor = require('jsupm_grove'); + +// Create the light sensor object using AIO pin 0 +var light = new groveSensor.GroveLight(0); + +// Read the input and print both the raw value and a rough lux value, +// waiting one second between readings +function readLightSensorValue() { + console.log(light.name() + " raw value is " + light.raw_value() + + ", which is roughly " + light.value() + " lux"); +} +setInterval(readLightSensorValue, 1000); diff --git a/examples/javascript/grovemoisture.js b/examples/javascript/grovemoisture.js new file mode 100644 index 00000000..76fa9ae9 --- /dev/null +++ b/examples/javascript/grovemoisture.js @@ -0,0 +1,54 @@ +/* +* 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. +*/ + +//Load Grove Moisture module +var grove_moisture = require('jsupm_grovemoisture'); + +// Instantiate a Grove Moisture sensor on analog pin A0 +var myMoistureObj = new grove_moisture.GroveMoisture(0); + +// Values (approximate): +// 0-300, sensor in air or dry soil +// 300-600, sensor in humid soil +// 600+, sensor in wet soil or submerged in water +// Read the value every second and print the corresponding moisture level +setInterval(function() +{ + var result; + var moisture_val = parseInt(myMoistureObj.value()); + if (moisture_val >= 0 && moisture_val < 300) + result = "Dry"; + else if (moisture_val >= 300 && moisture_val < 600) + result = "Moist"; + else + result = "Wet"; + console.log("Moisture value: " + moisture_val + ", " + result); +}, 1000); + +// Print message when exiting +process.on('SIGINT', function() +{ + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/javascript/groveo2.js b/examples/javascript/groveo2.js new file mode 100644 index 00000000..fcf3755c --- /dev/null +++ b/examples/javascript/groveo2.js @@ -0,0 +1,50 @@ +/* +* 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 groveO2_lib = require("jsupm_groveo2"); + +// The was tested with the O2 Oxygen Concentration Sensor Module +// Instantiate a GroveO2 on analog pin A0 +var groveO2_obj = new groveO2_lib.GroveO2(0); + +var myInterval = setInterval(function() +{ + console.log("The output voltage is: " + roundNum(groveO2_obj.voltageValue(), 5) + "mV"); +}, 100); + +function roundNum(num, decimalPlaces) +{ + var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000)); + return (Math.round((num + extraNum) * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces)); +} + +// When exiting: clear interval, and print message +process.on('SIGINT', function() +{ + clearInterval(myInterval); + groveO2_obj = null; + groveO2_lib.cleanUp(); + groveO2_lib = null; + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/javascript/groverelay.js b/examples/javascript/groverelay.js new file mode 100644 index 00000000..a8fefe7b --- /dev/null +++ b/examples/javascript/groverelay.js @@ -0,0 +1,49 @@ +/* + * Author: Sarah Knepper + * 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. + */ + +// Load Grove module +var groveSensor = require('jsupm_grove'); + +// Create the relay switch object using GPIO pin 0 +var relay = new groveSensor.GroveRelay(0); + +// Close and then open the relay switch 3 times, +// waiting one second each time. The LED on the relay switch +// will light up when the switch is on (closed). +// The switch will also make a noise between transitions. +var i = 0; +var waiting = setInterval(function() { + if ( i % 2 == 0 ) { + relay.on(); + if ( relay.isOn() ) + console.log(relay.name() + " is on"); + } else { + relay.off(); + if ( relay.isOff() ) + console.log(relay.name() + " is off"); + } + i++; + if ( i == 6) clearInterval(waiting); + }, 1000); + diff --git a/examples/javascript/groverotary.js b/examples/javascript/groverotary.js new file mode 100644 index 00000000..040f38c6 --- /dev/null +++ b/examples/javascript/groverotary.js @@ -0,0 +1,49 @@ +/* + * Author: Mihai Tudor Panu + * 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. + */ + +//setup/Initialization +var upm_grove = require('jsupm_grove'); + +//setup access analog input Analog pin #0 (A0) +var groveRotary = new upm_grove.GroveRotary(0); + +loop(); + +function loop() +{ + var abs = groveRotary.abs_value(); + var absdeg = groveRotary.abs_deg(); + var absrad = groveRotary.abs_rad(); + + var rel = groveRotary.rel_value(); + var reldeg = groveRotary.rel_deg(); + var relrad = groveRotary.rel_rad(); + + //write the knob value to the console in different formats + console.log("Abs: " + abs + " " + Math.round(parseInt(absdeg)) + " " + absrad.toFixed(3)); + console.log("Rel: " + rel + " " + Math.round(parseInt(reldeg)) + " " + relrad.toFixed(3)); + + //wait 2 s and call function again + setTimeout(loop, 2000); +} diff --git a/examples/javascript/groveslide.js b/examples/javascript/groveslide.js new file mode 100644 index 00000000..bd16eeee --- /dev/null +++ b/examples/javascript/groveslide.js @@ -0,0 +1,42 @@ +/* + * Author: Mihai Tudor Panu + * 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 upm_grove = require('jsupm_grove'); + +//setup access analog input Analog pin #0 (A0) +var groveSlide = new upm_grove.GroveSlide(0); + +loop(); + +function loop() +{ + var raw = groveSlide.raw_value(); + var volts = groveSlide.voltage_value(); + + //write the slider values to the console + console.log("Slider Value: " + raw + " = " + volts.toFixed(2) + " V"); + + //wait 2 s then call function again + setTimeout(loop, 2000); +} diff --git a/examples/javascript/grovespeaker.js b/examples/javascript/grovespeaker.js new file mode 100644 index 00000000..de92225e --- /dev/null +++ b/examples/javascript/grovespeaker.js @@ -0,0 +1,41 @@ +/* +* 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. +*/ + +//Load Grove Speaker module +var groveSpeaker = require('jsupm_grovespeaker'); +// Instantiate a Grove Speaker on digital pin D2 +var mySpeaker = new groveSpeaker.GroveSpeaker(2); + +// Play all 7 of the lowest notes +mySpeaker.playAll(); + +// Play a medium C-sharp +mySpeaker.playSound('c', true, "med"); + +// Print message when exiting +process.on('SIGINT', function() +{ + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/javascript/grovetemp.js b/examples/javascript/grovetemp.js new file mode 100644 index 00000000..dba3954c --- /dev/null +++ b/examples/javascript/grovetemp.js @@ -0,0 +1,42 @@ +/* + * Author: Sarah Knepper + * 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. + */ + +// Load Grove module +var groveSensor = require('jsupm_grove'); + +// Create the temperature sensor object using AIO pin 0 +var temp = new groveSensor.GroveTemp(0); +console.log(temp.name()); + +// Read the temperature ten times, printing both the Celsius and +// equivalent Fahrenheit temperature, waiting one second between readings +var i = 0; +var waiting = setInterval(function() { + var celsius = temp.value(); + var fahrenheit = celsius * 9.0/5.0 + 32.0; + console.log(celsius + " degrees Celsius, or " + + Math.round(fahrenheit) + " degrees Fahrenheit"); + i++; + if (i == 10) clearInterval(waiting); + }, 1000); diff --git a/examples/javascript/grovevdiv.js b/examples/javascript/grovevdiv.js new file mode 100644 index 00000000..5fb393e4 --- /dev/null +++ b/examples/javascript/grovevdiv.js @@ -0,0 +1,51 @@ +/* +* 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 voltageDivider = require('jsupm_grovevdiv'); +// Instantiate a Grove Voltage Divider sensor on analog pin A0 +var myVoltageDivider = new voltageDivider.GroveVDiv(0); + +// collect data and output measured voltage according to the setting +// of the scaling switch (3 or 10) +var val, gain3val, gain10val; +function getVoltageInfo() +{ + val = myVoltageDivider.value(100); + gain3val = myVoltageDivider.computedValue(3, val); + gain10val = myVoltageDivider.computedValue(10, val); + console.log("ADC value: " + val + " Gain 3: " + gain3val + + "v Gain 10: " + gain10val + "v"); +} + +setInterval(getVoltageInfo, 1000); + +// Print message when exiting +process.on('SIGINT', function() +{ + myVoltageDivider = null; + voltageDivider.cleanUp(); + voltageDivider = null; + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/javascript/grovewater.js b/examples/javascript/grovewater.js new file mode 100644 index 00000000..d953106e --- /dev/null +++ b/examples/javascript/grovewater.js @@ -0,0 +1,46 @@ +/* + * 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. + */ + +// Load Grove module +var waterSensor = require('jsupm_grovewater'); + +// Instantiate a Grove Water sensor on digital pin D2 +var water = new waterSensor.GroveWater(2); + +// Read whether the sensor is wet/dry, waiting one second between readings +function readWaterState() +{ + if (water.isWet()) + console.log("Sensor is wet"); + else + console.log("Sensor is dry"); +} +setInterval(readWaterState, 1000); + +// Print message when exiting +process.on('SIGINT', function() +{ + console.log("Exiting..."); + process.exit(0); +}); diff --git a/examples/python/grovebutton.py b/examples/python/grovebutton.py new file mode 100644 index 00000000..c62c399a --- /dev/null +++ b/examples/python/grovebutton.py @@ -0,0 +1,35 @@ +# Author: Sarah Knepper +# 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. + +import time +import pyupm_grove as grove + +# Create the button object using GPIO pin 0 +button = grove.GroveButton(0) + +# Read the input and print, waiting one second between readings +while 1: + print button.name(), ' value is ', button.value() + time.sleep(1) + +# Delete the button object +del button diff --git a/examples/python/grovecollision.py b/examples/python/grovecollision.py new file mode 100644 index 00000000..6cdd05cf --- /dev/null +++ b/examples/python/grovecollision.py @@ -0,0 +1,57 @@ +#!/usr/bin/python +# 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. + +import time, sys, signal, atexit +import pyupm_grovecollision as upmGrovecollision + +# The was tested with the Grove Collision Sensor +# Instantiate a Grove Collision on digital pin D2 +myGrovecollision = upmGrovecollision.GroveCollision(2) + + +## Exit handlers ## +# This stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This lets you run code on exit, +# including functions from myGrovecollision +def exitHandler(): + print "Exiting" + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + + +collisionState = False +print "No collision" + +while(1): + if (myGrovecollision.isColliding() and not collisionState): + print "Collision!" + collisionState = True + elif (not myGrovecollision.isColliding() and collisionState): + print "No collision" + collisionState = False diff --git a/examples/python/groveehr.py b/examples/python/groveehr.py new file mode 100644 index 00000000..3dd0ee22 --- /dev/null +++ b/examples/python/groveehr.py @@ -0,0 +1,68 @@ +#!/usr/bin/python +# 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. + + +import time, sys, signal, atexit +import pyupm_groveehr as upmGroveehr + +# Instantiate a Grove Ear-clip Heart Rate sensor on digital pin D2 +myHeartRateSensor = upmGroveehr.GroveEHR(2) + + +## Exit handlers ## +# This stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This lets you run code on exit, +# including functions from myHeartRateSensor +def exitHandler(): + myHeartRateSensor.stopBeatCounter() + print "Exiting" + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + + +# set the beat counter to 0, init the clock and start counting beats +myHeartRateSensor.clearBeatCounter() +myHeartRateSensor.initClock() +myHeartRateSensor.startBeatCounter() + +while(1): + # we grab these (millis and flowCount) just for display + # purposes in this example + millis = myHeartRateSensor.getMillis() + beats = myHeartRateSensor.beatCounter() + + # heartRate() requires that at least 5 seconds pass before + # returning anything other than 0 + fr = myHeartRateSensor.heartRate() + + # output milliseconds passed, beat count, and computed heart rate + outputStr = "Millis: {0} Beats: {1} Heart Rate: {2}".format( + millis, beats, fr) + print outputStr + time.sleep(1) diff --git a/examples/python/groveeldriver.py b/examples/python/groveeldriver.py new file mode 100644 index 00000000..c9fd9360 --- /dev/null +++ b/examples/python/groveeldriver.py @@ -0,0 +1,57 @@ +#!/usr/bin/python +# 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. + +import time, sys, signal, atexit +import pyupm_groveeldriver as upmGroveeldriver + +# The was tested with the Grove El Driver Module +# Instantiate a Grove El Driver on digital pin D2 +myEldriver = upmGroveeldriver.GroveElDriver(2) + + +## Exit handlers ## +# This function stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This function lets you run code on exit, including functions from myEldriver +def exitHandler(): + print "Exiting" + myEldriver.off() + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + + +lightState = True + +while(1): + if (lightState): + myEldriver.on() + else: + myEldriver.off() + lightState = not lightState + + time.sleep(1) diff --git a/examples/python/groveelectromagnet.py b/examples/python/groveelectromagnet.py new file mode 100644 index 00000000..c84af688 --- /dev/null +++ b/examples/python/groveelectromagnet.py @@ -0,0 +1,60 @@ +#!/usr/bin/python +# 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. + +import time, sys, signal, atexit +import pyupm_groveelectromagnet as upmGroveelectromagnet + +# This was tested with the Grove Electromagnetic Module +# Instantiate a Grove Electromagnet on digital pin D2 +myElectromagnet = upmGroveelectromagnet.GroveElectromagnet(2) + + +## Exit handlers ## +# This stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This lets you run code on exit, +# including functions from myElectromagnet +def exitHandler(): + print "Exiting" + myElectromagnet.off() + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + + +magnetState = False + +# Turn magnet on and off every 5 seconds +while(1): + magnetState = not magnetState + if (magnetState): + myElectromagnet.on() + else: + myElectromagnet.off() + print "Turning magnet", ("on" if magnetState else "off") + + time.sleep(5) diff --git a/examples/python/groveemg.py b/examples/python/groveemg.py new file mode 100644 index 00000000..898db266 --- /dev/null +++ b/examples/python/groveemg.py @@ -0,0 +1,52 @@ +#!/usr/bin/python +# 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. + +import time, sys, signal, atexit +import pyupm_groveemg as upmGroveemg + +# Tested with the GroveEMG Muscle Signal Reader Sensor Module +# Instantiate a GroveEMG on analog pin A0 +myEMG = upmGroveemg.GroveEMG(0) + + +## Exit handlers ## +# This stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This lets you run code on exit, including functions from myEMG +def exitHandler(): + print "Exiting" + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + + +print "Calibrating...." +myEMG.calibrate() + +while (1): + print myEMG.value() + time.sleep(.1) diff --git a/examples/python/grovegprs.py b/examples/python/grovegprs.py new file mode 100644 index 00000000..d441e38a --- /dev/null +++ b/examples/python/grovegprs.py @@ -0,0 +1,86 @@ +#!/usr/bin/python +# Author: Jon Trulson +# 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. + +import time, sys, signal, atexit +import pyupm_grovegprs as sensorObj + +# Instantiate a GroveGPRS Module on UART 0 +sensor = sensorObj.GroveGPRS(0) + +## Exit handlers ## +# This stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This function lets you run code on exit +def exitHandler(): + print "Exiting" + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + +# Set the baud rate, 19200 baud is the default. +if (sensor.setBaudRate(19200)): + print "Failed to set baud rate" + sys.exit(0) + + +usageStr = ("Usage:\n" +"If an argument is supplied on the command line, that argument is\n" +"sent to the module and the response is printed out.\n\n" +"If no argument is used, then the manufacturer and the current\n" +"saved profiles are queried and the results printed out.\n\n") +print usageStr + +# simple helper function to send a command and wait for a response +def sendCommand(sensor, cmd): + # commands need to be terminated with a carriage return + cmd += "\r"; + sensor.writeDataStr(cmd) + + # wait up to 1 second + if (sensor.dataAvailable(1000)): + print "Returned: ", + print sensor.readDataStr(1024) + else: + print "Timed out waiting for response" + + +if (len(sys.argv) > 1): + print "Sending command line argument (" + sys.argv[1] + ")..." + sendCommand(sensor, sys.argv[1]) +else: + # query the module manufacturer + print "Querying module manufacturer (AT+CGMI)..." + sendCommand(sensor, "AT+CGMI"); + + time.sleep(1); + + # query the saved profiles + print "Querying the saved profiles (AT&V)..." + sendCommand(sensor, "AT&V"); + + # A comprehensive list is available from the datasheet at: + # http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf diff --git a/examples/python/grovegsr.py b/examples/python/grovegsr.py new file mode 100644 index 00000000..993a6b0f --- /dev/null +++ b/examples/python/grovegsr.py @@ -0,0 +1,53 @@ +#!/usr/bin/python +# 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. + +import time, sys, signal, atexit +import pyupm_grovegsr as upmGrovegsr + +# Tested with the GroveGSR Galvanic Skin Response Sensor module. + +# Instantiate a GroveGSR on analog pin A0 +myGSR = upmGrovegsr.GroveGSR(0) + + +## Exit handlers ## +# This stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This lets you run code on exit, including functions from myGSR +def exitHandler(): + print "Exiting" + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + + +print "Calibrating...." +myGSR.calibrate() + +while (1): + print myGSR.value() + time.sleep(.5) diff --git a/examples/python/groveled.py b/examples/python/groveled.py new file mode 100644 index 00000000..eb104bf0 --- /dev/null +++ b/examples/python/groveled.py @@ -0,0 +1,41 @@ +# Author: Sarah Knepper +# 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. + +import time +import pyupm_grove as grove + +# Create the Grove LED object using GPIO pin 2 +led = grove.GroveLed(2) + +# Print the name +print led.name() + +# Turn the LED on and off 10 times, pausing one second +# between transitions +for i in range (0,10): + led.on() + time.sleep(1) + led.off() + time.sleep(1) + +# Delete the Grove LED object +del led diff --git a/examples/python/grovelight.py b/examples/python/grovelight.py new file mode 100644 index 00000000..a1939d75 --- /dev/null +++ b/examples/python/grovelight.py @@ -0,0 +1,37 @@ +# Author: Sarah Knepper +# 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. + +import time +import pyupm_grove as grove + +# Create the light sensor object using AIO pin 0 +light = grove.GroveLight(0) + +# Read the input and print both the raw value and a rough lux value, +# waiting one second between readings +while 1: + print light.name() + " raw value is %d" % light.raw_value() + \ + ", which is roughly %d" % light.value() + " lux"; + time.sleep(1) + +# Delete the light sensor object +del light diff --git a/examples/python/grovemoisture.py b/examples/python/grovemoisture.py new file mode 100644 index 00000000..78d3fe47 --- /dev/null +++ b/examples/python/grovemoisture.py @@ -0,0 +1,61 @@ +#!/usr/bin/python +# 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. + +import time, sys, signal, atexit +import pyupm_grovemoisture as upmMoisture + +# Instantiate a Grove Moisture sensor on analog pin A0 +myMoisture = upmMoisture.GroveMoisture(0) + + +## Exit handlers ## +# This function stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This function lets you run code on exit, including functions from myMoisture +def exitHandler(): + print "Exiting" + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + + +# Values (approximate): +# 0-300, sensor in air or dry soil +# 300-600, sensor in humid soil +# 600+, sensor in wet soil or submerged in water + +# Read the value every second and print the corresponding moisture level +while(1): + moisture_val = myMoisture.value() + if (moisture_val >= 0 and moisture_val < 300): + result = "Dry" + elif (moisture_val >= 300 and moisture_val < 600): + result = "Moist" + else: + result = "Wet" + print "Moisture value: {0}, {1}".format(moisture_val, result) + time.sleep(1) diff --git a/examples/python/groveo2.py b/examples/python/groveo2.py new file mode 100644 index 00000000..03f612bb --- /dev/null +++ b/examples/python/groveo2.py @@ -0,0 +1,51 @@ +#!/usr/bin/python +# 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. + +import time, sys, signal, atexit +import pyupm_groveo2 as upmGroveo2 + +# This was tested with the O2 Oxygen Concentration Sensor Module +# Instantiate a GroveO2 on analog pin A0 +myGroveO2 = upmGroveo2.GroveO2(0) + + +## Exit handlers ## +# This stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This lets you run code on exit, including functions from myGroveO2 +def exitHandler(): + print "Exiting" + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + + +while(1): + print "The output voltage is: {0}mV".format( + myGroveO2.voltageValue()) + + time.sleep(.1) diff --git a/examples/python/groverelay.py b/examples/python/groverelay.py new file mode 100644 index 00000000..44f033fa --- /dev/null +++ b/examples/python/groverelay.py @@ -0,0 +1,44 @@ +# Author: Sarah Knepper +# 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. + +import time +import pyupm_grove as grove + +# Create the relay switch object using GPIO pin 0 +relay = grove.GroveRelay(0) + +# Close and then open the relay switch 3 times, +# waiting one second each time. The LED on the relay switch +# will light up when the switch is on (closed). +# The switch will also make a noise between transitions. +for i in range (0,3): + relay.on() + if relay.isOn(): + print relay.name(), 'is on' + time.sleep(1) + relay.off() + if relay.isOff(): + print relay.name(), 'is off' + time.sleep(1) + +# Delete the relay switch object +del relay diff --git a/examples/python/groverotary.py b/examples/python/groverotary.py new file mode 100644 index 00000000..6d62c3c8 --- /dev/null +++ b/examples/python/groverotary.py @@ -0,0 +1,45 @@ +# Author: Mihai Tudor Panu +# 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. + +from time import sleep +import pyupm_grove as grove + +# New knob on AIO pin 0 +knob = grove.GroveRotary(0) + +# Loop indefinitely +while True: + + # Read values + abs = knob.abs_value() + absdeg = knob.abs_deg() + absrad = knob.abs_rad() + + rel = knob.rel_value() + reldeg = knob.rel_deg() + relrad = knob.rel_rad() + + print "Abs values: %4d" % int(abs) , " raw %4d" % int(absdeg), "deg = %5.2f" % absrad , " rad ", + print "Rel values: %4d" % int(rel) , " raw %4d" % int(reldeg), "deg = %5.2f" % relrad , " rad" + + # Sleep for 2.5 s + sleep(2.5) diff --git a/examples/python/groveslide.py b/examples/python/groveslide.py new file mode 100644 index 00000000..894fdc81 --- /dev/null +++ b/examples/python/groveslide.py @@ -0,0 +1,39 @@ +# Author: Mihai Tudor Panu +# 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. + +from time import sleep +import pyupm_grove as grove + +# New Grove Slider on AIO pin 0 +slider = grove.GroveSlide(0) + +# Loop indefinitely +while True: + + # Read values + raw = slider.raw_value() + volts = slider.voltage_value() + + print "Slider value: ", raw , " = %.2f" % volts , " V" + + # Sleep for 2.5 s + sleep(2.5) diff --git a/examples/python/grovespeaker.py b/examples/python/grovespeaker.py new file mode 100644 index 00000000..6e53b3c5 --- /dev/null +++ b/examples/python/grovespeaker.py @@ -0,0 +1,34 @@ +#!/usr/bin/python +# 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. + +import time, sys, signal, atexit +import pyupm_grovespeaker as upmGrovespeaker + +# Instantiate a Grove Speaker on digital pin D2 +mySpeaker = upmGrovespeaker.GroveSpeaker(2) + +# Play all 7 of the lowest notes +mySpeaker.playAll() + +# Play a medium C-sharp +mySpeaker.playSound('c', True, "med") diff --git a/examples/python/grovetemp.py b/examples/python/grovetemp.py new file mode 100644 index 00000000..bd9cc244 --- /dev/null +++ b/examples/python/grovetemp.py @@ -0,0 +1,41 @@ +# Author: Brendan Le Foll +# Contributions: Sarah Knepper +# 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. + +import time +import pyupm_grove as grove + +# Create the temperature sensor object using AIO pin 0 +temp = grove.GroveTemp(0) +print temp.name() + +# Read the temperature ten times, printing both the Celsius and +# equivalent Fahrenheit temperature, waiting one second between readings +for i in range(0, 10): + celsius = temp.value() + fahrenheit = celsius * 9.0/5.0 + 32.0; + print "%d degrees Celsius, or %d degrees Fahrenheit" \ + % (celsius, fahrenheit) + time.sleep(1) + +# Delete the temperature sensor object +del temp diff --git a/examples/python/grovevdiv.py b/examples/python/grovevdiv.py new file mode 100644 index 00000000..7719277c --- /dev/null +++ b/examples/python/grovevdiv.py @@ -0,0 +1,54 @@ +#!/usr/bin/python +# 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. + +import time, sys, signal, atexit +import pyupm_grovevdiv as upmGrovevdiv + +# Instantiate a Grove Voltage Divider sensor on analog pin A0 +myVoltageDivider = upmGrovevdiv.GroveVDiv(0) + + +## Exit handlers ## +# This stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This function lets you run code on exit, +# including functions from myVoltageDivider +def exitHandler(): + print "Exiting" + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + + +while(1): + val = myVoltageDivider.value(100) + gain3val = myVoltageDivider.computedValue(3, val) + gain10val = myVoltageDivider.computedValue(10, val) + print "ADC value: {0} Gain 3: {1}v Gain 10: {2}v".format( + val, gain3val, gain10val) + + time.sleep(1) diff --git a/examples/python/grovewater.py b/examples/python/grovewater.py new file mode 100644 index 00000000..54a3d149 --- /dev/null +++ b/examples/python/grovewater.py @@ -0,0 +1,51 @@ +#!/usr/bin/python +# 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. + +import time, sys, signal, atexit +import pyupm_grovewater as upmGrovewater + +# Instantiate a Grove Water sensor on digital pin D2 +myWaterSensor = upmGrovewater.GroveWater(2) + + +## Exit handlers ## +# This function stops python from printing a stacktrace when you hit control-C +def SIGINTHandler(signum, frame): + raise SystemExit + +# This function lets you run code on exit, including functions from myWaterSensor +def exitHandler(): + print "Exiting" + sys.exit(0) + +# Register exit handlers +atexit.register(exitHandler) +signal.signal(signal.SIGINT, SIGINTHandler) + + +while(1): + if (myWaterSensor.isWet()): + print "Sensor is wet" + else: + print "Sensor is dry" + time.sleep(1) diff --git a/src/groveeldriver/CMakeLists.txt b/src/groveeldriver/CMakeLists.txt new file mode 100644 index 00000000..2ed19797 --- /dev/null +++ b/src/groveeldriver/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "groveeldriver") +set (libdescription "upm groveeldriver sensor module") +set (module_src ${libname}.cxx) +set (module_hpp ${libname}.hpp) +upm_module_init("-lrt") diff --git a/src/groveeldriver/groveeldriver.cxx b/src/groveeldriver/groveeldriver.cxx new file mode 100644 index 00000000..322d6660 --- /dev/null +++ b/src/groveeldriver/groveeldriver.cxx @@ -0,0 +1,62 @@ +/* + * 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. + */ + +#include +#include + +#include "groveeldriver.hpp" + +using namespace upm; + +GroveElDriver::GroveElDriver(int 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_OUT); +} + +GroveElDriver::~GroveElDriver() +{ + mraa_gpio_close(m_gpio); +} + +void GroveElDriver::on() +{ + mraa_result_t error = MRAA_SUCCESS; + error = mraa_gpio_write (m_gpio, HIGH); + if (error != MRAA_SUCCESS) + mraa_result_print(error); +} + +void GroveElDriver::off() +{ + mraa_result_t error = MRAA_SUCCESS; + error = mraa_gpio_write (m_gpio, LOW); + if (error != MRAA_SUCCESS) + mraa_result_print(error); +} diff --git a/src/groveeldriver/groveeldriver.hpp b/src/groveeldriver/groveeldriver.hpp new file mode 100644 index 00000000..7ed7963b --- /dev/null +++ b/src/groveeldriver/groveeldriver.hpp @@ -0,0 +1,78 @@ +/* + * 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. + */ +#pragma once + +#include + +#define HIGH 1 +#define LOW 0 + +namespace upm { + /** + * @brief Grove EL Driver Module library + * @defgroup groveeldriver libupm-groveeldriver + * @ingroup seeed gpio electric + */ + + /** + * @library groveeldriver + * @sensor groveeldriver + * @comname Grove EL Driver + * @type electric + * @man seeed + * @con gpio + * + * @brief API for the Grove EL Driver Module + * + * The Grove EL Driver allows you to easily light up an + * EL wire with just one single Grove cable. + * + * @image html groveeldriver.jpg + * @snippet groveeldriver.cxx Interesting + */ + class GroveElDriver { + public: + /** + * Grove EL Driver constructor + * + * @param pin Digital pin to use + */ + GroveElDriver(int pin); + /** + * Grove EL Driver destructor + */ + ~GroveElDriver(); + /** + * Turns the EL wire on + */ + void on(); + /** + * Turns the EL wire off + */ + void off(); + + private: + mraa_gpio_context m_gpio; + }; +} diff --git a/src/groveeldriver/javaupm_groveeldriver.i b/src/groveeldriver/javaupm_groveeldriver.i new file mode 100644 index 00000000..f6e67877 --- /dev/null +++ b/src/groveeldriver/javaupm_groveeldriver.i @@ -0,0 +1,19 @@ +%module javaupm_groveeldriver +%include "../upm.i" + +%{ + #include "groveeldriver.hpp" +%} + +%include "groveeldriver.hpp" + +%pragma(java) jniclasscode=%{ + static { + try { + System.loadLibrary("javaupm_groveeldriver"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. \n" + e); + System.exit(1); + } + } +%} \ No newline at end of file diff --git a/src/groveeldriver/jsupm_groveeldriver.i b/src/groveeldriver/jsupm_groveeldriver.i new file mode 100644 index 00000000..bfd05ac1 --- /dev/null +++ b/src/groveeldriver/jsupm_groveeldriver.i @@ -0,0 +1,8 @@ +%module jsupm_groveeldriver +%include "../upm.i" + +%{ + #include "groveeldriver.hpp" +%} + +%include "groveeldriver.hpp" diff --git a/src/groveeldriver/pyupm_groveeldriver.i b/src/groveeldriver/pyupm_groveeldriver.i new file mode 100644 index 00000000..002797d3 --- /dev/null +++ b/src/groveeldriver/pyupm_groveeldriver.i @@ -0,0 +1,11 @@ +// Include doxygen-generated documentation +%include "pyupm_doxy2swig.i" +%module pyupm_groveeldriver +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "groveeldriver.hpp" +%{ + #include "groveeldriver.hpp" +%} diff --git a/src/grovespeaker/CMakeLists.txt b/src/grovespeaker/CMakeLists.txt new file mode 100644 index 00000000..171bfd69 --- /dev/null +++ b/src/grovespeaker/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "grovespeaker") +set (libdescription "upm grovespeaker speaker module") +set (module_src ${libname}.cxx) +set (module_hpp ${libname}.hpp) +upm_module_init() diff --git a/src/grovespeaker/grovespeaker.cxx b/src/grovespeaker/grovespeaker.cxx new file mode 100644 index 00000000..61d25481 --- /dev/null +++ b/src/grovespeaker/grovespeaker.cxx @@ -0,0 +1,150 @@ +/* + * 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. + */ + +#include +#include +#include + +#include "grovespeaker.hpp" + +using namespace upm; + +GroveSpeaker::GroveSpeaker(int pin) +{ + if ( !(m_gpio = mraa_gpio_init(pin)) ) + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_gpio_init() failed, invalid pin?"); + + mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT); + m_note_list['a'] = storeNote(1136, 1073, 568, 536, 284, 268); + m_note_list['b'] = storeNote(1012, 0, 506, 0, 253, 0); + m_note_list['c'] = storeNote(1911, 1804, 956, 902, 478, 451); + m_note_list['d'] = storeNote(1703, 1607, 851, 804, 426, 402); + m_note_list['e'] = storeNote(1517, 0, 758, 0, 379, 0); + m_note_list['f'] = storeNote(1432, 1351, 716, 676, 358, 338); + m_note_list['g'] = storeNote(1276, 1204, 638, 602, 319, 301); +} + +GroveSpeaker::~GroveSpeaker() +{ + mraa_gpio_close(m_gpio); +} + +NoteData GroveSpeaker::storeNote(int noteDelayLow, int noteDelayLowSharp, + int noteDelayMed, int noteDelayMedSharp, + int noteDelayHigh, int noteDelayHighSharp) +{ + NoteData note; + note.delayTimeLow = noteDelayLow; + note.delayTimeLowSharp = noteDelayLowSharp; + note.delayTimeMed = noteDelayMed; + note.delayTimeMedSharp = noteDelayMedSharp; + note.delayTimeHigh = noteDelayHigh; + note.delayTimeHighSharp = noteDelayHighSharp; + return note; +} + +void GroveSpeaker::playAll() +{ + playSound('c', false, "low"); + usleep(200000); + playSound('d', false, "low"); + usleep(200000); + playSound('e', false, "low"); + usleep(200000); + playSound('f', false, "low"); + usleep(200000); + playSound('g', false, "low"); + usleep(500000); + playSound('a', false, "low"); + usleep(500000); + playSound('b', false, "low"); + usleep(500000); +} + +void GroveSpeaker::playSound(char letter, bool sharp, std::string vocalWeight) +{ + std::map::iterator it = m_note_list.find(letter); + if(it == m_note_list.end()) + { + std::cout << "The key " << letter << " doesn't exist." << std::endl; + return; + } + NoteData nd = it->second; + int delayTime; + if (sharp) + { + if (vocalWeight.compare("low") == 0) + delayTime = nd.delayTimeLowSharp; + else if (vocalWeight.compare("med") == 0) + delayTime = nd.delayTimeMedSharp; + else if (vocalWeight.compare("high") == 0) + delayTime = nd.delayTimeHighSharp; + else + { + std::cout << "Correct voice weight values are low, med, or high" + << std::endl; + return; + } + } + else + { + if (vocalWeight.compare("low") == 0) + delayTime = nd.delayTimeLow; + else if (vocalWeight.compare("med") == 0) + delayTime = nd.delayTimeMed; + else if (vocalWeight.compare("high") == 0) + delayTime = nd.delayTimeHigh; + else + { + std::cout << "Correct voice weight values are low, med, or high" + << std::endl; + return; + } + } + // If delayTime is zero, that means you tried to choose a sharp note + // for a note that has no sharp + if (sharp && !delayTime) + { + std::cout << "The key " << letter << " doesn't have a sharp note." + << std::endl; + return; + } + sound(delayTime); +} + +void GroveSpeaker::sound(int note_delay) +{ + mraa_result_t error = MRAA_SUCCESS; + for (int i = 0; i < 100; i++) + { + error = mraa_gpio_write (m_gpio, HIGH); + usleep(note_delay); + error = mraa_gpio_write (m_gpio, LOW); + usleep(note_delay); + } + if (error != MRAA_SUCCESS) + mraa_result_print(error); +} + diff --git a/src/grovespeaker/grovespeaker.hpp b/src/grovespeaker/grovespeaker.hpp new file mode 100644 index 00000000..535dc484 --- /dev/null +++ b/src/grovespeaker/grovespeaker.hpp @@ -0,0 +1,103 @@ +/* + * 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. + */ +#pragma once + +#include +#include +#include +#include + +#define HIGH 1 +#define LOW 0 + +namespace upm { + /** + * @brief Grove Speaker library + * @defgroup grovespeaker libupm-grovespeaker + * @ingroup seeed gpio sound hak + */ +typedef struct +{ + int delayTimeLow; + int delayTimeLowSharp; + int delayTimeMed; + int delayTimeMedSharp; + int delayTimeHigh; + int delayTimeHighSharp; +} NoteData; + /** + * @library grovespeaker + * @sensor grovespeaker + * @comname Grove Speaker + * @type sound + * @man seeed + * @con gpio + * @kit hak + * + * @brief API for the Grove Speaker + * + * UPM module for the Grove Speaker. + * This sensor can generate different tones and sounds depending on the + * frequency of the input signal. + * + * @image html grovespeaker.jpg + * @snippet grovespeaker.cxx Interesting + */ + class GroveSpeaker { + public: + /** + * Grove Speaker constructor + * + * @param pin Digital pin to use + */ + GroveSpeaker(int pin); + /** + * GroveSpeaker destructor + */ + ~GroveSpeaker(); + /** + * Plays all alto notes (lowest notes) + * + */ + void playAll(); + /** + * Plays a sound and a note whether it's sharp or not + * + * @param letter Character name of the note + * ('a', 'b', 'c', 'd', 'e', 'f', or 'g') + * @param sharp If true, plays a sharp version of the note; otherwise, does not play the note + * @param vocalWeight String to determine whether to play a low ("low"), + * a medium ("med"), or a high ("high") note + */ + void playSound(char letter, bool sharp, std::string vocalWeight); + + private: + mraa_gpio_context m_gpio; + std::map m_note_list; + void sound(int note_delay); + NoteData storeNote(int noteDelayLow, int noteDelayLowSharp, + int noteDelayMed, int noteDelayMedSharp, + int noteDelayHigh, int noteDelayHighSharp); + }; +} diff --git a/src/grovespeaker/javaupm_grovespeaker.i b/src/grovespeaker/javaupm_grovespeaker.i new file mode 100644 index 00000000..4af4564a --- /dev/null +++ b/src/grovespeaker/javaupm_grovespeaker.i @@ -0,0 +1,19 @@ +%module javaupm_grovespeaker +%include "../upm.i" + +%{ + #include "grovespeaker.hpp" +%} + +%include "grovespeaker.hpp" + +%pragma(java) jniclasscode=%{ + static { + try { + System.loadLibrary("javaupm_grovespeaker"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. \n" + e); + System.exit(1); + } + } +%} \ No newline at end of file diff --git a/src/grovespeaker/jsupm_grovespeaker.i b/src/grovespeaker/jsupm_grovespeaker.i new file mode 100644 index 00000000..ef71438f --- /dev/null +++ b/src/grovespeaker/jsupm_grovespeaker.i @@ -0,0 +1,8 @@ +%module jsupm_grovespeaker +%include "../upm.i" + +%{ + #include "grovespeaker.hpp" +%} + +%include "grovespeaker.hpp" diff --git a/src/grovespeaker/pyupm_grovespeaker.i b/src/grovespeaker/pyupm_grovespeaker.i new file mode 100644 index 00000000..667c872f --- /dev/null +++ b/src/grovespeaker/pyupm_grovespeaker.i @@ -0,0 +1,11 @@ +// Include doxygen-generated documentation +%include "pyupm_doxy2swig.i" +%module pyupm_grovespeaker +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "grovespeaker.hpp" +%{ + #include "grovespeaker.hpp" +%}