From bbcdb1e0871492f1ff501299e8dde6c7a3055e90 Mon Sep 17 00:00:00 2001 From: Mihai Tudor Panu Date: Thu, 22 Mar 2018 15:29:01 -0700 Subject: [PATCH] up2: adding platform examples for built-in user leds Signed-off-by: Mihai Tudor Panu --- examples/platform/CMakeLists.txt | 3 + examples/platform/Up2Leds.java | 87 +++++++++++++++++++++++++++++ examples/platform/up2-leds.cpp | 95 ++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 examples/platform/Up2Leds.java create mode 100644 examples/platform/up2-leds.cpp diff --git a/examples/platform/CMakeLists.txt b/examples/platform/CMakeLists.txt index 23006e8..08b7bb4 100644 --- a/examples/platform/CMakeLists.txt +++ b/examples/platform/CMakeLists.txt @@ -1,6 +1,9 @@ add_executable (gpio_edison gpio_edison.c) target_link_libraries (gpio_edison mraa) +add_executable (up2-leds up2-leds.cpp) +target_link_libraries (up2-leds mraa) + include_directories(${PROJECT_SOURCE_DIR}/api) include_directories(${PROJECT_SOURCE_DIR}/api/mraa) diff --git a/examples/platform/Up2Leds.java b/examples/platform/Up2Leds.java new file mode 100644 index 0000000..53fedc1 --- /dev/null +++ b/examples/platform/Up2Leds.java @@ -0,0 +1,87 @@ +/* + * Author: Mihai Tudor Panu + * Copyright (c) 2018 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 mraa.Led; +import mraa.Platform; +import mraa.mraa; + +public class Up2Leds { + static { + try { + System.loadLibrary("mraajava"); + } catch (UnsatisfiedLinkError e) { + System.err.println( "Native code library failed to load." + + " See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + // Define led names and min/max brightness + final static String[] ledNames = {"red", "green", "yellow", "blue"}; + final static int minBrightness = 0; + final static int maxBrightness = 255; + + public static void main(String argv[]) throws InterruptedException { + + // Perform a basic platform and version check + if (mraa.getPlatformType() != Platform.INTEL_UP2) { + System.out.println("This example is meant for the UP Squared board."); + System.out.println("Running it on different platforms will likely require code changes!"); + } + + if (mraa.getVersion().compareTo("v1.9.0") < 0) { + System.out.println("You need MRAA version 1.9.0 or newer to run this sample!"); + System.exit(1); + } + + // Define and initialize our LEDs + Led[] leds = new Led[ledNames.length]; + for (int i = 0; i < ledNames.length; i++) { + leds[i] = new Led(ledNames[i]); + } + + // Add a hook for handling keyboard interrupts and cleanup + Runtime.getRuntime().addShutdownHook(new Thread() + { + @Override + public void run() + { + System.out.println("Exiting..."); + for (Led led : leds) { + led.setBrightness(minBrightness); + } + } + }); + + // Main loop + while (!Thread.interrupted()) { + for (Led led : leds) { + led.setBrightness(maxBrightness); + Thread.sleep(200); + led.setBrightness(minBrightness); + } + Thread.sleep(200); + } + } +} diff --git a/examples/platform/up2-leds.cpp b/examples/platform/up2-leds.cpp new file mode 100644 index 0000000..0bbead6 --- /dev/null +++ b/examples/platform/up2-leds.cpp @@ -0,0 +1,95 @@ +/* + * Author: Mihai Tudor Panu + * Copyright (c) 2018 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 + +#include "mraa/common.hpp" +#include "mraa/led.hpp" + +// Define min and max brightness +#define LED_ON 255 +#define LED_OFF 0 + +// This boolean controls the main loop +volatile bool should_run = true; + +// Interrupt signal handler function +void +sig_handler(int signum) +{ + if (signum == SIGINT) { + std::cout << "Exiting..." << std::endl; + should_run = false; + } +} + +// Main function +int +main() +{ + // Perform a basic platform and version check + if (mraa::getPlatformType() != mraa::INTEL_UP2) { + std::cout << "This example is meant for the UP Squared board." << std::endl; + std::cout << "Running it on different platforms will likely require code changes!" << std::endl; + } + + if (mraa::getVersion().compare("v1.9.0") < 0) { + std::cout << "You need MRAA version 1.9.0 or newer to run this sample!" << std::endl; + return 1; + } + + // Register handler for keyboard interrupts + signal(SIGINT, sig_handler); + + // Define our LEDs + std::string led_names[] = {"red", "green", "yellow", "blue"}; + int led_no = sizeof(led_names)/sizeof(led_names[0]); + mraa::Led **leds = new mraa::Led*[led_no]; + + // Initialize them + for (int i = 0; i < led_no; i++) { + leds[i] = new mraa::Led(led_names[i].c_str()); + } + + // Blink them in order and pause in-between + while (should_run) { + for (int i = 0; i < led_no; i++) { + leds[i]->setBrightness(LED_ON); + usleep(200000); + leds[i]->setBrightness(LED_OFF); + } + usleep(200000); + } + + // Cleanup and exit + for (int i = 0; i < led_no; i++) { + delete leds[i]; + } + delete [] leds; + return 0; +}