• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomepcDuinopcDuino Hardware ExperimentsThe button control LED light by java
Previous Next

The button control LED light by java

Posted by: Alvin Jin , March 4, 2014

[vc_row][vc_column][vc_column_text]fast[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Parts list” tab_id=”1393924603-1-71″][vc_column_text]1.one led lights

2.five Male to Female Dupont Line

3.one linker button

4.one pcduino

图片fast112[/vc_column_text][/vc_tab][vc_tab title=”Wiring diagram” tab_id=”1393924603-2-74″][vc_column_text]led GND pin connect to pcduin GND pin

led SIG pin connect to pcduino GPIO 8 pin

linker button GND pin connect to pcduino GND pin

linker button VCC pin connect to pcduino 5Vpin

linker button SIG pin connct to pcduino GPIO 9 pin

fastwifi0s0-125607453813901165461511[/vc_column_text][/vc_tab][vc_tab title=”Test running” tab_id=”1393927569446-2-6″][vc_column_text]when press the button ,led on,  release it ,led off

图片ff1ff2ff3[/vc_column_text][/vc_tab][vc_tab title=”Run code” tab_id=”1393927570609-3-6″][vc_column_text]GPIO Java control

package com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class GPIO_Pin {
    private String modeURI = "/sys/devices/virtual/misc/gpio/mode/";
    private String statusURI = "/sys/devices/virtual/misc/gpio/pin/";
    private int pin = 0;
    public static final String HIGH = "1", LOW = "0", INPUT = "0", OUTPUT = "1", INPUT_PU = "8";
    public GPIO_Pin(int pin) {
        modeURI += "gpio" + pin;
        statusURI += "gpio" + pin;
        this.pin = pin;
    }
    public GPIO_Pin(String pin) {
        // Finalize file paths
        modeURI += "gpio" + pin;
        statusURI += "gpio" + pin;
        this.pin =Integer.parseInt(pin);
    }
    public int getPin() {
        return pin;
    }
    public void overrideURI(String uri) {
        modeURI = uri + "mode/gpio" + pin;
        statusURI = uri + "pin/gpio" + pin;
    }
    public void setMode(String mode) {
        writeToFile(getModeURI(), mode);
    }
    public void set(String state) {
        writeToFile(getStatusURI(), state);
    }
    public void setHIGH() {
        writeToFile(getStatusURI(), HIGH);
    }
    public void setLOW() {
        writeToFile(getStatusURI(), LOW);
    }
    public void setModeINPUT() {
        writeToFile(getModeURI(), INPUT);
    }
    public void setModeOUTPUT() {
        writeToFile(getModeURI(), OUTPUT);
    }
    public void setModeINPUT_PU() {
        writeToFile(getModeURI(), INPUT_PU);
    }
    public String getModeURI() {
        return modeURI;
    }
    public String getStatusURI() {
        return statusURI;
    }
    public String getPinMode() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(getModeURI()));
            String data = reader.readLine();
            reader.close();
            return data;
        } catch (IOException e) {
        }
        return "";
    }
    public String getPinStatus() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(getStatusURI()));
            String data = reader.readLine();
            reader.close();
            return data;
        } catch (IOException e) {
        }
        return "";
    }
    private void writeToFile(String URI, String data) {
        try {
            File file = new File(URI);
            file.delete();
            File newFile = new File(URI);
            newFile.createNewFile();
            FileWriter writer = new FileWriter(URI);
            writer.write(data);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
test
package com.test;
public class Test {
    public static void main(String[] args) throws InterruptedException {
        GPIO_Pin ledpin=new GPIO_Pin(8);
        GPIO_Pin buttenpin=new GPIO_Pin(9);
        ledpin.setModeOUTPUT();
        buttenpin.setModeINPUT();
        while (true) {
        if("1".equals(buttenpin.getPinStatus()))
            ledpin.setHIGH();
         else
            ledpin.setLOW();
         Thread.sleep(4);
        }
    }
}

[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]

Tags: pcDuino Hardware Experiments

Share!
Tweet

Alvin Jin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors