Tinker Board : 자바를 이용한 GPIO LED 제어

OS/Tinker Board 2018.08.19 00:59 Posted by 파란크리스마스

출처

Tinker Board S : 자바를 이용한 GPIO LED 제어

저의 경우 자바로 GPIO 제어하는 것이 좋은 점은 서버 구성하기 편하기 때문인데, 라즈베리파이의 경우 pi4j가 잘 되어 있지만, 아직 팅커보드는 pi4j가 지원하지 않아, 다른 라이브러리를 찾아보니 diozero 라이브러리가 gifhub에 있었고, 몇가지 설치 방법이 pi4j 보다 어려움이 있어서 정리 해보았습니다. 

우선 Device I/O library 소스를 다운 받아서 설치하고, GPIO 번호 설정도 pi4j와 달라서 고생을 했는데, GPIO 표에서 GPIO.ASUS를 사용하니 잘 되었습니다.

Tinker Board : GPIO

이미지 출처 : GPIO - Tinker Board Wiki

jdk 설치

$ sudo apt-get install default-jdk

Device I/O library 설치

$ sudo apt-get install mercurial
$ hg clone http://hg.openjdk.java.net/dio/dev
$ cd dev/
$ export PI_TOOLS=/usr
$ export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-armhf
$ export CROSS_TOOL=/usr/bin/
$ make
$ cd ..
$ sudo mv ~/dev /opt/dio
$ sudo chown -R root.root /opt/dio
$ sudo cp -r /opt/dio/build/deviceio/lib/* $JAVA_HOME/jre/lib

diozero 다운로드

Diozero JAR 라이브러리가 압축된 ZIP파일을 Google 드라이브에서 다운로드

diozero 압축해제

$ unzip diozero-distribution-0.11-SNAPSHOT-bin.zip
$ cd diozero-distribution-0.11-SNAPSHOT/

배선

LED+ : 3번핀 (GPIO.ASUS : 252)
LED- : 9번핀 (GND)

TestLEDMain.java

import org.pmw.tinylog.Logger;

import com.diozero.devices.LED;
import com.diozero.util.BoardInfo;
import com.diozero.util.DeviceFactoryHelper;
import com.diozero.util.RuntimeIOException;
import com.diozero.util.SleepUtil;
import com.diozero.util.SystemInfo;

public class TestLEDMain {

	public static void main(String[] args) throws Exception {
		String hardware = "Rockchip (Device Tree)";
		String revision = "0000";
		BoardInfo board_info = SystemInfo.lookupLocalBoardInfo(hardware, revision, null);
		System.out.println(hardware + "/" + revision + ": " + board_info);

		if (args.length < 1) {
			Logger.error("Usage: {} <gpio>", TestLEDMain.class.getName());
			System.exit(1);
		}
		test(board_info, Integer.parseInt(args[0]));
	}
	
	public static void test(BoardInfo board_info, int pin) {
		
		try (LED led = new LED(pin)) {
			Logger.info("On");
			led.on();
			SleepUtil.sleepSeconds(1);
			Logger.info("Off");
			led.off();
			SleepUtil.sleepSeconds(1);
			Logger.info("Toggle");
			led.toggle();
			SleepUtil.sleepSeconds(1);
			Logger.info("Toggle");
			led.toggle();
			SleepUtil.sleepSeconds(1);
			
			Logger.info("Blink 10 times");
			led.blink(0.5f, 0.5f, 10, false);
			
			Logger.info("Done");
		} catch (RuntimeIOException e) {
			Logger.error(e, "Error: {}", e);
		} finally {
			// Required if there are non-daemon threads that will prevent the
			// built-in clean-up routines from running
			DeviceFactoryHelper.getNativeDeviceFactory().close();
		}
	}
}

TestLEDMain 실행

실행시 LED+ 연결된 3번핀의 GPIO.ASUS 번호 252를 인자로 전달

TestLEDMain.java

$ sudo java -cp .:tinylog-1.3.5.jar:commons-math3-3.6.1.jar:dio.jar:pigpioj-java-2.2.jar TestLEDMain 252
Rockchip (Device Tree)/0000: BoardInfo [make=Asus, model=Tinker Board, memory=2048, libraryPath=tinkerboard, adcVRef=1.8]
03:40:41.377 [main] DEBUG com.diozero.util.DeviceFactoryHelper.init - Using native device factory class JdkDeviceIoDeviceFactory
03:40:41.443 [main] INFO TestLEDMain.test - On
03:40:41.482 [main] DEBUG com.diozero.util.LibraryLoader.loadLibrary - Loaded library 'diozero-system-utils' from classpath
03:40:42.485 [main] INFO TestLEDMain.test - Off
03:40:43.486 [main] INFO TestLEDMain.test - Toggle
03:40:44.488 [main] INFO TestLEDMain.test - Toggle
03:40:45.489 [main] INFO TestLEDMain.test - Blink 10 times
03:40:55.495 [main] INFO TestLEDMain.test - Done
03:40:55.495 [main] DEBUG com.diozero.api.DigitalOutputDevice.close - close()
03:40:55.496 [main] DEBUG com.diozero.internal.provider.AbstractDevice.close - close(), key=Native-GPIO-252
03:40:55.497 [main] DEBUG com.diozero.internal.provider.jdkdio11.JdkDeviceIoGpioOutputDevice.closeDevice - closeDevice()
03:40:55.498 [main] DEBUG com.diozero.internal.DeviceStates.closed - closed(Native-GPIO-252)
03:40:55.502 [main] DEBUG com.diozero.internal.provider.AbstractDeviceFactory.close - close()
03:40:55.503 [main] DEBUG com.diozero.internal.DeviceStates.closeAll - closeAll()
03:40:55.531 [DIO-Zero Shutdown Handler] DEBUG com.diozero.util.ShutdownHandlerThread.run - Shutdown handler running
03:40:55.532 [DIO-Zero Shutdown Handler] DEBUG com.diozero.internal.provider.AbstractDeviceFactory.close - close()
03:40:55.533 [DIO-Zero Shutdown Handler] DEBUG com.diozero.internal.DeviceStates.closeAll - closeAll()
03:40:55.533 [DIO-Zero Shutdown Handler] DEBUG com.diozero.util.ShutdownHandlerThread.run - Shutdown handler finished

실행결과