Home | History | Annotate | Download | only in java
      1 import mraa.Dir;
      2 import mraa.Gpio;
      3 import mraa.Platform;
      4 import mraa.mraa;
      5 
      6 /*
      7  * Author: Brendan Le Foll <brendan.le.foll (at) intel.com>
      8  * Copyright (c) 2014 Intel Corporation.
      9  * Author: Jakub Kramarz <jkramarz (at) virtuslab.com>
     10  * Copyright (c) 2015 VirtusLab
     11  *
     12  * Permission is hereby granted, free of charge, to any person obtaining
     13  * a copy of this software and associated documentation files (the
     14  * "Software"), to deal in the Software without restriction, including
     15  * without limitation the rights to use, copy, modify, merge, publish,
     16  * distribute, sublicense, and/or sell copies of the Software, and to
     17  * permit persons to whom the Software is furnished to do so, subject to
     18  * the following conditions:
     19  *
     20  * The above copyright notice and this permission notice shall be
     21  * included in all copies or substantial portions of the Software.
     22  *
     23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     27  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     28  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     29  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     30  */
     31 
     32 public class BlinkOnboard {
     33     static {
     34         try {
     35             System.loadLibrary("mraajava");
     36         } catch (UnsatisfiedLinkError e) {
     37             System.err.println(
     38                     "Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +
     39                             e);
     40             System.exit(1);
     41         }
     42     }
     43     public static void main(String argv[]) throws InterruptedException {
     44         Platform platform = mraa.getPlatformType();
     45         Gpio gpio, gpio_in;
     46         if (platform == Platform.INTEL_GALILEO_GEN1) {
     47             gpio = new Gpio(3);
     48         } else if (platform == Platform.INTEL_MINNOWBOARD_MAX) {
     49             gpio = new Gpio(21);
     50         } else {
     51             gpio = new Gpio(13);
     52         }
     53         System.out.format("Welcome to libmraa\n Version: %s\n Running on %s\n",
     54                 mraa.getVersion(), platform.toString());
     55 
     56         gpio.dir(Dir.DIR_OUT);
     57         // on platforms with physical button use gpio_in
     58         if (platform == Platform.INTEL_MINNOWBOARD_MAX) {
     59             gpio_in = new Gpio(14);
     60             gpio_in.dir(Dir.DIR_IN);
     61             System.out.println("Press and hold S1 to stop, Press SW1 to shutdown!");
     62         } else {
     63             gpio_in = null;
     64         }
     65 
     66         boolean state = false;
     67         while (true) {
     68             if (gpio_in != null && gpio_in.read() == 0) {
     69                 return;
     70             }
     71             if (state) {
     72                 state = false;
     73                 gpio.write(1);
     74             } else {
     75                 state = true;
     76                 gpio.write(0);
     77             }
     78             Thread.sleep(1000);
     79         }
     80     }
     81 }