티스토리 뷰

OS/Banana Pi /BPI-Bit

Banana Pi - BPI-M2+ pi4j

파란크리스마스 2017. 3. 12. 14:52
728x90

BPI-M2+ pi4j

출처 : The Pi4J Project - Home

현재 pi4j는 BPI-M2+ 지원하지 않아 수정한 jar를 배포합니다.

pi4j-core.jar

GPIO를 이용하여 LED 제어

/*
 * #%L
 * **********************************************************************
 * ORGANIZATION  :  Pi4J
 * PROJECT       :  Pi4J :: Java Examples
 * FILENAME      :  ControlGpioExample.java
 *
 * This file is part of the Pi4J project. More information about
 * this project can be found here:  http://www.pi4j.com/
 * **********************************************************************
 * %%
 * Copyright (C) 2012 - 2016 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
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */
 
import com.pi4j.io.gpio.BPIM2PGpioProvider;
import com.pi4j.io.gpio.BPIM2PPin;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
 
/**
 * This example code demonstrates how to perform simple state
 * control of a GPIO pin on the Raspberry Pi.
 *
 * @author Robert Savage
 */
public class ControlGpioExample {
 
    public static void main(String[] args) throws InterruptedException {

        GpioFactory.setDefaultProvider(new BPIM2PGpioProvider());

        System.out.println("<--Pi4J--> GPIO Control Example ... started.");
 
        // create gpio controller
        final GpioController gpio = GpioFactory.getInstance();
 
        // provision gpio pin #01 as an output pin and turn on
        final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(BPIM2PPin.GPIO_00, "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 ControlGpioExample");
    }
}

실행

$ sudo java -cp .:pi4j-core.jar:pi4j-gpio-extension.jar ControlGpioExample
platformId = raspberrypi / label = Raspberry Pi
platformId = bananapi / label = BananaPi
platformId = bananapro / label = BananaPro
platformId = odroid / label = Odroid
platformId = BPI_M2P / label = BPI_M2P
piboardRev: Hardware string: Hardware   : sun8i
Hardware:Hardware       : sun8i
piboardRev:  3
piboardId: Revision string: Revision    : 0000
wiringPi: wiringPiSetup called
piboardRev: Hardware string: Hardware   : sun8i
Hardware:Hardware       : sun8i
piboardRev:  3
piboardRev: Hardware string: Hardware   : sun8i
Hardware:Hardware       : sun8i
piboardRev:  3
piboardId: Revision string: Revision    : 0000
wiringPiSetup: wiringPiMode = 0
<--Pi4J--> GPIO Control Example ... started.
pinMode,2272,pin:1,mode:1
func:sunxi_set_pin_mode pin:1, MODE:1 bank:0 index:1 phyaddr:0x1c20800
read reg val: 0x77222212 offset:4
Out mode ready set val: 0x77222212
Out mode set over reg val: 0x77222212
func:sunxi_digitalWrite pin:1, value:1 bank:0 index:1 phyaddr:0x1c20810
befor write reg val: 0x8002,index:1
HIGH val set over reg val: 0x8002
--> GPIO state should be: ON
func:sunxi_digitalWrite pin:1, value:0 bank:0 index:1 phyaddr:0x1c20810
befor write reg val: 0x8002,index:1
LOW val set over reg val: 0x8000
--> GPIO state should be: OFF
func:sunxi_digitalRead pin:1,bank:0 index:1 phyaddr:0x1c20810
***** read reg val: 0x0,bank:0,index:1,line:1389
func:sunxi_digitalWrite pin:1, value:1 bank:0 index:1 phyaddr:0x1c20810
befor write reg val: 0x8000,index:1
HIGH val set over reg val: 0x8002
--> GPIO state should be: ON
func:sunxi_digitalRead pin:1,bank:0 index:1 phyaddr:0x1c20810
***** read reg val: 0x1,bank:0,index:1,line:1389
func:sunxi_digitalWrite pin:1, value:0 bank:0 index:1 phyaddr:0x1c20810
befor write reg val: 0x8002,index:1
LOW val set over reg val: 0x8000
--> GPIO state should be: OFF
--> GPIO state should be: ON for only 1 second
func:sunxi_digitalWrite pin:1, value:1 bank:0 index:1 phyaddr:0x1c20810
befor write reg val: 0x8000,index:1
HIGH val set over reg val: 0x8002
func:sunxi_digitalWrite pin:1, value:0 bank:0 index:1 phyaddr:0x1c20810
befor write reg val: 0x8002,index:1
LOW val set over reg val: 0x8000
func:sunxi_digitalWrite pin:1, value:0 bank:0 index:1 phyaddr:0x1c20810
befor write reg val: 0x8000,index:1
LOW val set over reg val: 0x8000
Exiting ControlGpioExample

실행 결과

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함