pi4j--使用Java实现树莓派IO控制

硬件:zero W;系统:jdk-8u231-linux-arm32-vfp-hflt.tar.gz;pi4j:pi4j-1.2.deb

1、使用dpkg命令安装

dpkg安装命令:

sudo  dpkg  -i   deb文件名

修复依赖关系:

sudo  apt-get  install  -f

查看安装列表:

sudo  dpkg  -l

 卸载软件:

sudo dpkg  -r  软件名

 效果:

pi4j--使用Java实现树莓派IO控制_第1张图片

2、来自官网的介绍

Pin Numbering - Raspberry Pi Zero W

  • Numbering Scheme
  • Expansion Header
  • J8 Pinout (40-pin Header)
  • Additional Resources

Numbering Scheme

Pi4J uses an abstract pin numbering scheme to help insulate software from hardware changes.
Pi4J implements the same pin number scheme as the Wiring Pi project. More information about the WiringPi pin number scheme can be found here: http://wiringpi.com/pins/

Pi4J provides a RaspiPin enumeration that is used to manage the accessible GPIO pins.

Expansion Header

The Raspberry Pi Zero W board contains a single 40-pin expansion header labeled as 'J8' providing access to 28 GPIO pins.
(Pins 1, 2, 39 & 40 are also labeled below.)

pi4j--使用Java实现树莓派IO控制_第2张图片

 

J8 Pinout (40-pin Header)

The diagram below illustrates the GPIO pinout using the Pi4J/WiringPi GPIO numbering scheme.

 pi4j--使用Java实现树莓派IO控制_第3张图片

Additional Resources

  • Please visit the usage page for additional details on how to control these pins using Pi4J.
  • Click here for more information on the J8 header.
  • Click here for more information the Raspberry Pi pin functions.

3、使用

安装完成后,在下面的目录中能找到lib和example文件,
/opt/pi4j/lib 
/opt/pi4j/examples

在编译程序时,需要包含Pi4J的库文件路径:

javac -classpath .:classes:/opt/pi4j/lib/'*' ...

运行程序命令为:

java -classpath .:classes:/opt/pi4j/lib/'*' ...

4、demo(来自pi4j官网)

/*
 * #%L
 * **********************************************************************
 * ORGANIZATION  :  Pi4J
 * PROJECT       :  Pi4J :: Java Examples
 * FILENAME      :  BroadcomPinNumberingGpioExample.java
 *
 * This file is part of the Pi4J project. More information about
 * this project can be found here:  https://www.pi4j.com/
 * **********************************************************************
 * %%
 * Copyright (C) 2012 - 2019 Pi4J
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.pi4j.io.gpio.*;

/**
 * This example code demonstrates how to perform simple state
 * control of a GPIO pin on the Raspberry Pi
 * using the Broadcom chipset GPIO pin numbering scheme.
 *
 * @author Robert Savage
 */
public class BroadcomPinNumberingGpioExample {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("<--Pi4J--> GPIO Control Example ... started.");

        // in order to use the Broadcom GPIO pin numbering scheme, we need to configure the
        // GPIO factory to use a custom configured Raspberry Pi GPIO provider
        GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));

        // create gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        // provision broadcom gpio pin #02 as an output pin and turn on
        final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiBcmPin.GPIO_02, "MyLED", PinState.HIGH);

        // set shutdown state for this pin
        pin.setShutdownOptions(true, PinState.LOW);

        System.out.println("--> GPIO state should be: ON");

        Thread.sleep(5000);

        // turn off gpio pin #01
        pin.low();
        System.out.println("--> GPIO state should be: OFF");

        Thread.sleep(5000);

        // toggle the current state of gpio pin #01 (should turn on)
        pin.toggle();
        System.out.println("--> GPIO state should be: ON");

        Thread.sleep(5000);

        // toggle the current state of gpio pin #01  (should turn off)
        pin.toggle();
        System.out.println("--> GPIO state should be: OFF");

        Thread.sleep(5000);

        // turn on gpio pin #01 for 1 second and then off
        System.out.println("--> GPIO state should be: ON for only 1 second");
        pin.pulse(1000, true); // set second argument to 'true' use a blocking call

        // stop all GPIO activity/threads by shutting down the GPIO controller
        // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
        gpio.shutdown();

        System.out.println("Exiting BroadcomPinNumberingGpioExample");
    }
}
//END SNIPPET: control-gpio-snippet

5、java.lang.UnsatisfiedLinkError异常的解决方法


 

你可能感兴趣的:(树莓派,Java,ARM-Linux)