Created
June 28, 2014 08:45
-
-
Save skrb/5248e443ac808c4d462b to your computer and use it in GitHub Desktop.
SwitchOnOff.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Java Embedded Raspberry Pi GPIO app | |
* スイッチポチポチ♪ | |
* 用意するもの | |
* ジャンパーケーブル(オス×メス:2本、オス×オス:1本) | |
* タクトスイッチ(モーメンタリ) | |
* 抵抗 10kΩ(茶・黒・橙・金) | |
*/ | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.RandomAccessFile; | |
public class SwitchOnOff { | |
static final String GPIO_OUT = "out"; | |
static final String GPIO_IN = "in"; | |
static final String GPIO_ON = "1"; | |
static final String GPIO_OFF = "0"; | |
static final String GPIO_CH = "18"; | |
public static void main(String[] args) { | |
try { | |
//Refresh GPIO_CH | |
FileWriter unexport = new FileWriter("/sys/class/gpio/unexport"); | |
FileWriter export = new FileWriter("/sys/class/gpio/export"); | |
File exportFileCheck = new File("/sys/class/gpio/gpio" + GPIO_CH); | |
if (exportFileCheck.exists()) { | |
unexport.write(GPIO_CH); | |
unexport.flush(); | |
} | |
export.write(GPIO_CH); | |
export.flush(); | |
//Direction GPIO_CH | |
FileWriter direction = new FileWriter("/sys/class/gpio/gpio2" + GPIO_CH + "/direction"); | |
direction.write(GPIO_IN); | |
direction.flush(); | |
// Create GPIO Reader | |
RandomAccessFile accesser = new RandomAccessFile("/sys/class/gpio/gpio" + GPIO_CH + "/value", "r"); | |
byte[] inData = new byte[256]; | |
//GPIO_CH Read Loop | |
while(true) { | |
accesser.seek(0); | |
accesser.read(inData); | |
String data = new String(inData); | |
System.out.println(data); | |
Thread.sleep(200); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment