Skip to content

Instantly share code, notes, and snippets.

@dopefishh
Created October 31, 2012 13:12
Show Gist options
  • Save dopefishh/3986958 to your computer and use it in GitHub Desktop.
Save dopefishh/3986958 to your computer and use it in GitHub Desktop.
Java library for gpio raspberry
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Easy to use GPIO library for raspberry pi
*
* @author Mart Lubbers
* @version 1.0
* @date 31-10-2012
*/
public class GPIOLibrary
{
public static final String EXP = "/sys/class/gpio/";
public final int[] PINS = { 0, 1, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23, 24, 25 };
private boolean[] used;
/**
* Constructor, needed to add the shutdown hook to prevent locked gpio pins
*/
public GPIOLibrary()
{
used = new boolean[PINS.length];
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override public void run()
{
System.out.println("Force close intercepted. Unexporting remaining pins...");
for(int i = 0; i<used.length; i++)
if(used[i])
unExport(PINS[i]);
System.out.println("Done... Goodbye!");
}
});
}
/**
* Lets the user export a pin.
* @param in true for a "in" pin and false for a "out" pin
* @param n pin number. If invalid the functions does nothing
*/
public void exportPin(boolean in, int n)
{
if (!validPin(n) || inUse(n))
{
System.out.println("Pin in use or invalid");
}
else
{
try
{
PrintWriter export = new PrintWriter(new FileWriter(EXP + "export", true));
export.write(new Integer(n).toString());
export.close();
PrintWriter dir = new PrintWriter(new FileWriter(EXP + "gpio" + n + "/direction", true));
dir.write(in ? "in" : "out");
dir.close();
setUsed(n, true);
}
catch (IOException e)
{
System.out.println("File not writable, are you root?");
e.printStackTrace();
}
}
}
/**
* Lets the user unlock the gpio pins for later use
* @param n pin number. If invalid the function does nothing
*/
public void unExport(int n)
{
if (!validPin(n))
{
System.out.println("Pin invalid");
}
else
{
try
{
PrintWriter unexport = new PrintWriter(new FileWriter(EXP + "unexport", true));
unexport.write(new Integer(n).toString());
unexport.close();
setUsed(n, false);
}
catch (IOException e)
{
System.out.println("File not writable, are you root?");
e.printStackTrace();
}
}
}
/**
* Returns the value of pin n
* @param n pin number.
* @return pin value(0, 1). If invalid the function returns -1
*/
public int readPin(int n)
{
if (!validPin(n))
{
System.out.println("Not exported or invalid");
return -1;
}
else
{
try
{
FileInputStream in = new FileInputStream(new File(EXP + "/gpio" + n + "/value"));
int i = in.read();
in.close();
return i==30 ? 0 : 1;
}
catch (FileNotFoundException e)
{
System.out.println("File not found, did you export?");
e.printStackTrace();
return -1;
}
catch (IOException e)
{
System.out.println("File not writable, are you root?");
e.printStackTrace();
return -1;
}
}
}
/**
* Writes a value to the pin
* @param n pin number. If invalid the function does nothing
* @param out true for 1 and false for 0
*/
public void writePin(int n, boolean out)
{
if (!validPin(n))
{
System.out.println("Not exported or invalid");
}
else
{
try
{
PrintWriter pin;
pin = new PrintWriter(new FileWriter(EXP + "gpio" + n + "/value"));
pin.write(out ? "1" : "0");
pin.close();
}
catch (IOException e)
{
System.out.println("File not writable, are you root?");
System.out.println("Did you export the pin as out?");
e.printStackTrace();
}
}
}
/**
* Help function to check if the pin number is valid
* @param n pin number
* @return valid
*/
private boolean validPin(int n)
{
for (int i = 0; i < PINS.length; i++)
if (n == PINS[i])
return true;
return false;
}
/**
* Help function to check if the pin is in use
* @param n pin number
* @return in use
*/
private boolean inUse(int n)
{
for (int i = 0; i < PINS.length; i++)
if (n == PINS[i])
return used[i];
return false;
}
/**
* Help function to set the pin as used or unused
* @param n pin number
* @param p used or unused
*/
private void setUsed(int n, boolean p)
{
for (int i = 0; i < PINS.length; i++)
if (n == PINS[i])
used[i] = p;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment