mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Merge pull request #3997 from gohai/for-ben-14
Medium-sized I/O updates
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0"?>
|
||||
<project name="Processing I/O Library" default="build">
|
||||
<project name="Processing Hardware I/O Library" default="build">
|
||||
|
||||
<target name="clean" description="Clean the build directories">
|
||||
<delete dir="bin" />
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
name = I/O
|
||||
name = Hardware I/O
|
||||
version = 1
|
||||
|
||||
@@ -1 +1 @@
|
||||
name = I/O for Raspberry Pi and other Linux-based computers
|
||||
name = Hardware I/O for Raspberry Pi and other Linux-based computers
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Copyright (c) The Processing Foundation 2015
|
||||
I/O library developed by Gottfried Haider as part of GSOC 2015
|
||||
Hardware I/O library developed by Gottfried Haider as part of GSoC 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
/*
|
||||
Copyright (c) The Processing Foundation 2015
|
||||
I/O library developed by Gottfried Haider as part of GSOC 2015
|
||||
Hardware I/O library developed by Gottfried Haider as part of GSoC 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
@@ -31,6 +31,9 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @webref
|
||||
*/
|
||||
public class GPIO {
|
||||
|
||||
// those constants are generally the same as in Arduino.h
|
||||
@@ -43,9 +46,18 @@ public class GPIO {
|
||||
public static final int HIGH = 1;
|
||||
|
||||
public static final int NONE = 0;
|
||||
public static final int CHANGE = 1; // trigger when level changes
|
||||
public static final int FALLING = 2; // trigger when level changes from high to low
|
||||
public static final int RISING = 3; // trigger when level changes from low to high
|
||||
/**
|
||||
* trigger when level changes
|
||||
*/
|
||||
public static final int CHANGE = 1;
|
||||
/**
|
||||
* trigger when level changes from high to low
|
||||
*/
|
||||
public static final int FALLING = 2;
|
||||
/**
|
||||
* trigger when level changes from low to high
|
||||
*/
|
||||
public static final int RISING = 3;
|
||||
|
||||
protected static Map<Integer, Thread> irqThreads = new HashMap<Integer, Thread>();
|
||||
protected static boolean serveInterrupts = true;
|
||||
@@ -77,19 +89,15 @@ public class GPIO {
|
||||
|
||||
|
||||
/**
|
||||
* Calls a function when the value of an INPUT pin changes
|
||||
*
|
||||
* Don't use enableInterrupt() and waitForInterrupt() in combination with
|
||||
* this function, as they are orthogonal. The sketch method provided must
|
||||
* accept a single integer (int) parameter, which is the number of the GPIO
|
||||
* pin that the interrupt occured on.
|
||||
* Calls a function when the value of an input pin changes
|
||||
* @param pin GPIO pin
|
||||
* @param parent this
|
||||
* @param parent typically use "this"
|
||||
* @param method name of sketch method to call
|
||||
* @param mode when to call: GPIO.CHANGE, GPIO.FALLING or GPIO.RISING
|
||||
* @see noInterrupts
|
||||
* @see interrupts
|
||||
* @see releaseInterrupt
|
||||
* @webref
|
||||
*/
|
||||
public static void attachInterrupt(int pin, PApplet parent, String method, int mode) {
|
||||
if (irqThreads.containsKey(pin)) {
|
||||
@@ -158,63 +166,13 @@ public class GPIO {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pauses the execution of the sketch
|
||||
*
|
||||
* Calling this function will have an influence on the framerate
|
||||
* the sketch is going to achieve.
|
||||
* @param ms milliseconds to pause
|
||||
*/
|
||||
protected static void delay(int ms) {
|
||||
try {
|
||||
Thread.sleep(ms);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pauses the execution of the sketch
|
||||
*
|
||||
* Note: Both the operating system, as well as Processing, are not what is
|
||||
* called "hard real-time" systems. In other words: there are many factors,
|
||||
* outside of the control of the programmer, which can influence the execution
|
||||
* of the program in minute ways. Those are generally not an issue, or even
|
||||
* noticeable using a desktop operating system, but they can be a factor, when
|
||||
* the timing of a particular sequence of events is critical. For example, one
|
||||
* might to wait a very specific number amount of time after receiving an
|
||||
* interrupt before changing the state of an output pin. When programming with
|
||||
* micro-controllers, as found on the Arduino Uno, there very little between
|
||||
* your code and the actual hardware, and multiple executions of the same
|
||||
* sketch will probably match each other almost to the very tick of a clock
|
||||
* (which happens at the speed of 16 MHz). Systems running full-fledged
|
||||
* desktop operating systems, such as Linux, are generally multi-tasking,
|
||||
* which means that the operating system allocates small slices of time to
|
||||
* the many different processes that run concurrently. The effect of this is
|
||||
* often offset by the sheer clock speeds that such computers run. But
|
||||
* regardless: if you require your sketch to adhere to a very specific timing,
|
||||
* you might be disappointed.
|
||||
* @param us microseconds to pause
|
||||
*/
|
||||
protected static void delayMicroseconds(int us) {
|
||||
int ms = (int)(us / 1000);
|
||||
int ns = (us - (ms * 1000)) * 1000;
|
||||
try {
|
||||
Thread.sleep(ms, ns);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of an input pin
|
||||
*
|
||||
* You need to set the pin to INPUT by calling pinMode before calling
|
||||
* this function.
|
||||
* @param pin GPIO pin
|
||||
* @return GPIO.HIGH (1) or GPIO.LOW (0)
|
||||
* @see pinMode
|
||||
* @see digitalWrite
|
||||
* @webref
|
||||
*/
|
||||
public static int digitalRead(int pin) {
|
||||
checkValidPin(pin);
|
||||
@@ -240,16 +198,12 @@ public class GPIO {
|
||||
|
||||
|
||||
/**
|
||||
* Sets an output pin to HIGH or LOW
|
||||
*
|
||||
* You need set the pin to OUTPUT by calling pinMode before calling this
|
||||
* function. It is not possible to enable or disable internal pull-up
|
||||
* resistors for inputs using this function, which is something that's
|
||||
* supported on Arduino.
|
||||
* Sets an output pin to be either high or low
|
||||
* @param pin GPIO pin
|
||||
* @param value GPIO.HIGH or GPIO.LOW
|
||||
* @param value GPIO.HIGH (1) or GPIO.LOW (0)
|
||||
* @see pinMode
|
||||
* @see digitalRead
|
||||
* @webref
|
||||
*/
|
||||
public static void digitalWrite(int pin, int value) {
|
||||
checkValidPin(pin);
|
||||
@@ -279,16 +233,7 @@ public class GPIO {
|
||||
|
||||
|
||||
/**
|
||||
* Sets an output pin to HIGH or LOW
|
||||
*
|
||||
* You need set the pin to OUTPUT by calling pinMode before calling this
|
||||
* function. It is not possible to enable or disable internal pull-up
|
||||
* resistors for inputs using this function, which is something that's
|
||||
* supported on Arduino.
|
||||
* @param pin GPIO pin
|
||||
* @param value true or false
|
||||
* @see pinMode
|
||||
* @see digitalRead
|
||||
*/
|
||||
public static void digitalWrite(int pin, boolean value) {
|
||||
if (value) {
|
||||
@@ -300,31 +245,24 @@ public class GPIO {
|
||||
|
||||
|
||||
/**
|
||||
* Disables an interrupt for an INPUT pin
|
||||
*
|
||||
* Use this function only in combination with enableInterrupt() and
|
||||
* waitForInterrupt(). This should not be called when attachInterrupt()
|
||||
* is being used.
|
||||
* Disables an interrupt for an input pin
|
||||
* @param pin GPIO pin
|
||||
* @see enableInterrupt
|
||||
* @see waitForInterrupt
|
||||
*/
|
||||
public static void disableInterrupt(int pin) {
|
||||
protected static void disableInterrupt(int pin) {
|
||||
enableInterrupt(pin, NONE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Enables an interrupt for an INPUT pin
|
||||
*
|
||||
* Use this function only when calling waitForInterrupt(). This should not
|
||||
* be called when attachInterrupt() is being used.
|
||||
* Enables an interrupt for an input pin
|
||||
* @param pin GPIO pin
|
||||
* @param mode what to wait for: GPIO.CHANGE, GPIO.FALLING or GPIO.RISING
|
||||
* @see waitForInterrupt
|
||||
* @see disableInterrupt
|
||||
*/
|
||||
public static void enableInterrupt(int pin, int mode) {
|
||||
protected static void enableInterrupt(int pin, int mode) {
|
||||
checkValidPin(pin);
|
||||
|
||||
String out;
|
||||
@@ -353,14 +291,10 @@ public class GPIO {
|
||||
|
||||
/**
|
||||
* Allows interrupts to happen
|
||||
*
|
||||
* You can use noInterrupts() and interrupts() in tandem to make sure no interrupts
|
||||
* are occuring while your sketch is doing a particular task. This is only relevant
|
||||
* when using attachInterrupt(), not for waitForInterrupt(). By default, interrupts
|
||||
* are enabled.
|
||||
* @see attachInterrupt
|
||||
* @see noInterrupts
|
||||
* @see releaseInterrupt
|
||||
* @webref
|
||||
*/
|
||||
public static void interrupts() {
|
||||
serveInterrupts = true;
|
||||
@@ -369,14 +303,10 @@ public class GPIO {
|
||||
|
||||
/**
|
||||
* Prevents interrupts from happpening
|
||||
*
|
||||
* You can use noInterrupts() and interrupts() in tandem to make sure no interrupts
|
||||
* are occuring while your sketch is doing a particular task. This is only relevant
|
||||
* when using attachInterrupt(), not for waitForInterrupt(). By default, interrupts
|
||||
* are enabled.
|
||||
* @see attachInterrupt
|
||||
* @see interrupts
|
||||
* @see releaseInterrupt
|
||||
* @webref
|
||||
*/
|
||||
public static void noInterrupts() {
|
||||
serveInterrupts = false;
|
||||
@@ -384,16 +314,13 @@ public class GPIO {
|
||||
|
||||
|
||||
/**
|
||||
* Sets a pin to INPUT or OUTPUT
|
||||
*
|
||||
* While pins are implicitly set to input by default on Arduino, it is
|
||||
* necessary to call this function for any pin you want to access later,
|
||||
* including input pins.
|
||||
* Configures a pin to act either as input or output
|
||||
* @param pin GPIO pin
|
||||
* @param mode GPIO.INPUT or GPIO.OUTPUT
|
||||
* @see digitalRead
|
||||
* @see digitalWrite
|
||||
* @see releasePin
|
||||
* @webref
|
||||
*/
|
||||
public static void pinMode(int pin, int mode) {
|
||||
checkValidPin(pin);
|
||||
@@ -447,14 +374,12 @@ public class GPIO {
|
||||
|
||||
|
||||
/**
|
||||
* Stops listening for interrupts on an INPUT pin
|
||||
*
|
||||
* Use this function only in combination with attachInterrupt(). This should
|
||||
* not be called when enableInterrupt() and waitForInterrupt() are being used.
|
||||
* Stops listening for interrupts on an input pin
|
||||
* @param pin GPIO pin
|
||||
* @see attachInterrupt
|
||||
* @see noInterrupts
|
||||
* @see interrupts
|
||||
* @webref
|
||||
*/
|
||||
public static void releaseInterrupt(int pin) {
|
||||
Thread t = irqThreads.get(pin);
|
||||
@@ -477,11 +402,9 @@ public class GPIO {
|
||||
|
||||
/**
|
||||
* Gives ownership of a pin back to the operating system
|
||||
*
|
||||
* Without calling this function the pin will remain in the current
|
||||
* state even after the sketch has been closed.
|
||||
* @param pin GPIO pin
|
||||
* @see pinMode
|
||||
* @webref
|
||||
*/
|
||||
public static void releasePin(int pin) {
|
||||
checkValidPin(pin);
|
||||
@@ -501,16 +424,31 @@ public class GPIO {
|
||||
|
||||
|
||||
/**
|
||||
* Waits for the value of an INPUT pin to change
|
||||
* Waits for the value of an input pin to change
|
||||
* @param pin GPIO pin
|
||||
* @param mode what to wait for: GPIO.CHANGE, GPIO.FALLING or GPIO.RISING
|
||||
* @param timeout don't wait more than timeout milliseconds (-1 waits indefinitely)
|
||||
* @return true if the interrupt occured, false if the timeout occured
|
||||
* @webref
|
||||
*/
|
||||
public static boolean waitForInterrupt(int pin, int mode, int timeout) {
|
||||
enableInterrupt(pin, mode);
|
||||
return waitForInterrupt(pin, timeout);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Waits for the value of an input pin to change
|
||||
*
|
||||
* Make sure to setup the interrupt with enableInterrupt() before calling
|
||||
* this function. A timeout value of -1 waits indefinitely.
|
||||
* @param pin GPIO pin
|
||||
* @param timeout don't wait more than timeout milliseconds
|
||||
* @return true if the interrupt occured, false if the timeout occured
|
||||
* @see enableInterrupt
|
||||
* @see disableInterrupt
|
||||
*/
|
||||
public static boolean waitForInterrupt(int pin, int timeout) {
|
||||
protected static boolean waitForInterrupt(int pin, int timeout) {
|
||||
checkValidPin(pin);
|
||||
|
||||
String fn = String.format("/sys/class/gpio/gpio%d/value", pin);
|
||||
@@ -531,15 +469,16 @@ public class GPIO {
|
||||
|
||||
|
||||
/**
|
||||
* Waits for the value of an INPUT pin to change
|
||||
* Waits for the value of an input pin to change
|
||||
*
|
||||
* Make sure to setup the interrupt with enableInterrupt() before calling
|
||||
* this function. This function will wait indefinitely for an interrupt
|
||||
* to occur.
|
||||
* @parm pin GPIO pin
|
||||
* @see enableInterrupt
|
||||
* @see disableInterrupt
|
||||
*/
|
||||
public static void waitForInterrupt(int pin) {
|
||||
protected static void waitForInterrupt(int pin) {
|
||||
waitForInterrupt(pin, -1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
/*
|
||||
Copyright (c) The Processing Foundation 2015
|
||||
I/O library developed by Gottfried Haider as part of GSOC 2015
|
||||
Hardware I/O library developed by Gottfried Haider as part of GSoC 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
@@ -29,6 +29,9 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* @webref
|
||||
*/
|
||||
public class I2C {
|
||||
|
||||
protected String dev;
|
||||
@@ -39,10 +42,10 @@ public class I2C {
|
||||
|
||||
|
||||
/**
|
||||
* Opens an I2C device as master
|
||||
*
|
||||
* @param dev device name
|
||||
* Opens an I2C interface as master
|
||||
* @param dev interface name
|
||||
* @see list
|
||||
* @webref
|
||||
*/
|
||||
public I2C(String dev) {
|
||||
NativeInterface.loadLibrary();
|
||||
@@ -55,7 +58,27 @@ public class I2C {
|
||||
|
||||
|
||||
/**
|
||||
* Close the I2C device
|
||||
* Begins a transmission to an attached device
|
||||
* @see write
|
||||
* @see read
|
||||
* @see endTransmission
|
||||
* @webref
|
||||
*/
|
||||
public void beginTransmission(int slave) {
|
||||
// addresses 120 (0x78) to 127 are additionally reserved
|
||||
if (0x78 <= slave) {
|
||||
System.err.println("beginTransmission expects a 7 bit address, try shifting one bit to the right");
|
||||
throw new IllegalArgumentException("Illegal address");
|
||||
}
|
||||
this.slave = slave;
|
||||
transmitting = true;
|
||||
out = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Closes the I2C device
|
||||
* @webref
|
||||
*/
|
||||
public void close() {
|
||||
NativeInterface.closeDevice(handle);
|
||||
@@ -72,43 +95,11 @@ public class I2C {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins a transmission to an attached device
|
||||
*
|
||||
* I2C addresses consist of 7 bits plus one bit that indicates whether
|
||||
* the device is being read from or written to. Some datasheets list
|
||||
* the address in an 8 bit form (7 address bits + R/W bit), while others
|
||||
* provide the address in a 7 bit form, with the address in the lower
|
||||
* 7 bits. This function expects the address in the lower 7 bits, the
|
||||
* same way as in Arduino's Wire library, and as shown in the output
|
||||
* of the i2cdetect tool.
|
||||
* If the address provided in a datasheet is greater than 127 (hex 0x7f)
|
||||
* or there are separate addresses for read and write operations listed,
|
||||
* which vary exactly by one, then you want to shif the this number by
|
||||
* one bit to the right before passing it as an argument to this function.
|
||||
* @param slave 7 bit address of slave device
|
||||
* @see write
|
||||
* @see read
|
||||
* @see endTransmission
|
||||
*/
|
||||
public void beginTransmission(int slave) {
|
||||
// addresses 120 (0x78) to 127 are additionally reserved
|
||||
if (0x78 <= slave) {
|
||||
System.err.println("beginTransmission expects a 7 bit address, try shifting one bit to the right");
|
||||
throw new IllegalArgumentException("Illegal address");
|
||||
}
|
||||
this.slave = slave;
|
||||
transmitting = true;
|
||||
out = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ends the current transmissions
|
||||
*
|
||||
* This executes any queued writes.
|
||||
* @see beginTransmission
|
||||
* @see write
|
||||
* @webref
|
||||
*/
|
||||
public void endTransmission() {
|
||||
if (!transmitting) {
|
||||
@@ -130,8 +121,9 @@ public class I2C {
|
||||
|
||||
|
||||
/**
|
||||
* Lists all available I2C devices
|
||||
* Lists all available I2C interfaces
|
||||
* @return String array
|
||||
* @webref
|
||||
*/
|
||||
public static String[] list() {
|
||||
ArrayList<String> devs = new ArrayList<String>();
|
||||
@@ -153,15 +145,12 @@ public class I2C {
|
||||
|
||||
/**
|
||||
* Reads bytes from the attached device
|
||||
*
|
||||
* You must call beginTransmission() before calling this function. This function
|
||||
* also ends the current transmisison and sends any data that was queued using
|
||||
* write() before.
|
||||
* @param len number of bytes to read
|
||||
* @return bytes read from device
|
||||
* @see beginTransmission
|
||||
* @see write
|
||||
* @see endTransmission
|
||||
* @webref
|
||||
*/
|
||||
public byte[] read(int len) {
|
||||
if (!transmitting) {
|
||||
@@ -186,14 +175,11 @@ public class I2C {
|
||||
|
||||
/**
|
||||
* Adds bytes to be written to the device
|
||||
*
|
||||
* You must call beginTransmission() before calling this function.
|
||||
* The actual writing takes part when read() or endTransmission() is being
|
||||
* called.
|
||||
* @param out bytes to be written
|
||||
* @see beginTransmission
|
||||
* @see read
|
||||
* @see endTransmission
|
||||
* @webref
|
||||
*/
|
||||
public void write(byte[] out) {
|
||||
if (!transmitting) {
|
||||
@@ -212,11 +198,7 @@ public class I2C {
|
||||
|
||||
|
||||
/**
|
||||
* Adds bytes to be written to the device
|
||||
*
|
||||
* You must call beginTransmission() before calling this function.
|
||||
* The actual writing takes part when read() or endTransmission() is being
|
||||
* called.
|
||||
* Adds bytes to be written to the attached device
|
||||
* @param out string to be written
|
||||
* @see beginTransmission
|
||||
* @see read
|
||||
@@ -228,11 +210,7 @@ public class I2C {
|
||||
|
||||
|
||||
/**
|
||||
* Adds a byte to be written to the device
|
||||
*
|
||||
* You must call beginTransmission() before calling this function.
|
||||
* The actual writing takes part when read() or endTransmission() is being
|
||||
* called.
|
||||
* Adds a byte to be written to the attached device
|
||||
* @param out single byte to be written (0-255)
|
||||
* @see beginTransmission
|
||||
* @see read
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
/*
|
||||
Copyright (c) The Processing Foundation 2015
|
||||
I/O library developed by Gottfried Haider as part of GSOC 2015
|
||||
Hardware I/O library developed by Gottfried Haider as part of GSoC 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
@@ -32,6 +32,9 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* @webref
|
||||
*/
|
||||
public class LED {
|
||||
|
||||
protected String dev;
|
||||
@@ -44,6 +47,7 @@ public class LED {
|
||||
* Opens a LED device
|
||||
* @param dev device name
|
||||
* @see list
|
||||
* @webref
|
||||
*/
|
||||
public LED(String dev) {
|
||||
NativeInterface.loadLibrary();
|
||||
@@ -95,11 +99,27 @@ public class LED {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the brightness
|
||||
* @param bright 0.0 (off) to 1.0 (maximum)
|
||||
* @webref
|
||||
*/
|
||||
public void brightness(float bright) {
|
||||
String fn = "/sys/class/leds/" + dev + "/brightness";
|
||||
if (bright < 0.0 || 1.0 < bright) {
|
||||
System.err.println("Brightness must be between 0.0 and 1.0.");
|
||||
throw new IllegalArgumentException("Illegal argument");
|
||||
}
|
||||
int ret = NativeInterface.writeFile(fn, Integer.toString((int)(bright * maxBrightness)));
|
||||
if (ret < 0) {
|
||||
throw new RuntimeException(fn + ": " + NativeInterface.getError(ret));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Restores the previous state
|
||||
*
|
||||
* Without calling this function the LED will remain in the current
|
||||
* state even after the sketch has been closed.
|
||||
* @webref
|
||||
*/
|
||||
public void close() {
|
||||
// restore previous settings
|
||||
@@ -120,6 +140,7 @@ public class LED {
|
||||
/**
|
||||
* Lists all available LED devices
|
||||
* @return String array
|
||||
* @webref
|
||||
*/
|
||||
public static String[] list() {
|
||||
ArrayList<String> devs = new ArrayList<String>();
|
||||
@@ -135,21 +156,4 @@ public class LED {
|
||||
Arrays.sort(tmp);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the brightness
|
||||
* @param bright 0.0 (off) to 1.0 (maximum)
|
||||
*/
|
||||
public void set(float bright) {
|
||||
String fn = "/sys/class/leds/" + dev + "/brightness";
|
||||
if (bright < 0.0 || 1.0 < bright) {
|
||||
System.err.println("Brightness must be between 0.0 and 1.0.");
|
||||
throw new IllegalArgumentException("Illegal argument");
|
||||
}
|
||||
int ret = NativeInterface.writeFile(fn, Integer.toString((int)(bright * maxBrightness)));
|
||||
if (ret < 0) {
|
||||
throw new RuntimeException(fn + ": " + NativeInterface.getError(ret));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
/*
|
||||
Copyright (c) The Processing Foundation 2015
|
||||
I/O library developed by Gottfried Haider as part of GSOC 2015
|
||||
Hardware I/O library developed by Gottfried Haider as part of GSoC 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
/*
|
||||
Copyright (c) The Processing Foundation 2015
|
||||
I/O library developed by Gottfried Haider as part of GSOC 2015
|
||||
Hardware I/O library developed by Gottfried Haider as part of GSoC 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
@@ -32,6 +32,9 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* @webref
|
||||
*/
|
||||
public class PWM {
|
||||
|
||||
int channel;
|
||||
@@ -42,6 +45,7 @@ public class PWM {
|
||||
* Opens a PWM channel
|
||||
* @param channel PWM channel
|
||||
* @see list
|
||||
* @webref
|
||||
*/
|
||||
public PWM(String channel) {
|
||||
NativeInterface.loadLibrary();
|
||||
@@ -81,10 +85,11 @@ public class PWM {
|
||||
|
||||
|
||||
/**
|
||||
* Disables the output
|
||||
* Disables the PWM output
|
||||
* @webref
|
||||
*/
|
||||
public void clear() {
|
||||
String fn = String.format("/sys/class/pwm/%s/gpio%d/enable", chip, channel);
|
||||
String fn = String.format("/sys/class/pwm/%s/pwm%d/enable", chip, channel);
|
||||
int ret = NativeInterface.writeFile(fn, "0");
|
||||
if (ret < 0) {
|
||||
throw new RuntimeException(NativeInterface.getError(ret));
|
||||
@@ -94,9 +99,7 @@ public class PWM {
|
||||
|
||||
/**
|
||||
* Gives ownership of a channel back to the operating system
|
||||
*
|
||||
* Without calling this function the channel will remain in the current
|
||||
* state even after the sketch has been closed.
|
||||
* @webref
|
||||
*/
|
||||
public void close() {
|
||||
// XXX: implicit clear()?
|
||||
@@ -118,6 +121,7 @@ public class PWM {
|
||||
/**
|
||||
* Lists all available PWM channels
|
||||
* @return String array
|
||||
* @webref
|
||||
*/
|
||||
public static String[] list() {
|
||||
ArrayList<String> devs = new ArrayList<String>();
|
||||
@@ -148,28 +152,31 @@ public class PWM {
|
||||
* Enables the PWM output
|
||||
* @param period cycle period in Hz
|
||||
* @param duty duty cycle, 0.0 (always off) to 1.0 (always on)
|
||||
* @webref
|
||||
*/
|
||||
public void set(int period, float duty) {
|
||||
// set period
|
||||
String fn = fn = String.format("/sys/class/pwm/%s/gpio%d/period", chip, channel);
|
||||
String fn = fn = String.format("/sys/class/pwm/%s/pwm%d/period", chip, channel);
|
||||
// convert to nanoseconds
|
||||
int ret = NativeInterface.writeFile(fn, String.format("%d", (int)(1000000000 / period)));
|
||||
if (ret < 0) {
|
||||
throw new RuntimeException(fn + ": " + NativeInterface.getError(ret));
|
||||
}
|
||||
|
||||
// set duty cycle
|
||||
fn = fn = String.format("/sys/class/pwm/%s/gpio%d/duty", chip, channel);
|
||||
fn = fn = String.format("/sys/class/pwm/%s/pwm%d/duty_cycle", chip, channel);
|
||||
if (duty < 0.0 || 1.0 < duty) {
|
||||
System.err.println("Duty cycle must be between 0.0 and 1.0.");
|
||||
throw new IllegalArgumentException("Illegal argument");
|
||||
}
|
||||
// convert to nanoseconds
|
||||
ret = NativeInterface.writeFile(fn, String.format("%d", (int)((1000000000 * duty) / period)));
|
||||
if (ret < 0) {
|
||||
throw new RuntimeException(fn + ": " + NativeInterface.getError(ret));
|
||||
}
|
||||
|
||||
// enable output
|
||||
fn = String.format("/sys/class/pwm/%s/gpio%d/enable", chip, channel);
|
||||
fn = String.format("/sys/class/pwm/%s/pwm%d/enable", chip, channel);
|
||||
ret = NativeInterface.writeFile(fn, "1");
|
||||
if (ret < 0) {
|
||||
throw new RuntimeException(fn + ": " + NativeInterface.getError(ret));
|
||||
@@ -179,11 +186,8 @@ public class PWM {
|
||||
|
||||
/**
|
||||
* Enables the PWM output with a preset period of 1 kHz
|
||||
*
|
||||
* This period approximately matches the dedicated PWM pins on
|
||||
* the Arduino Uno, which have a frequency of 980 Hz.
|
||||
* It is recommended to use set(period, duty) instead.
|
||||
* @param duty duty cycle, 0.0 (always off) to 1.0 (always on)
|
||||
* @webref
|
||||
*/
|
||||
public void set(float duty) {
|
||||
set(1000, duty);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
/*
|
||||
Copyright (c) The Processing Foundation 2015
|
||||
I/O library developed by Gottfried Haider as part of GSOC 2015
|
||||
Hardware I/O library developed by Gottfried Haider as part of GSoC 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
@@ -23,6 +23,9 @@
|
||||
package processing.io;
|
||||
|
||||
|
||||
/**
|
||||
* @webref
|
||||
*/
|
||||
public class RPI {
|
||||
|
||||
/*
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
/*
|
||||
Copyright (c) The Processing Foundation 2015
|
||||
I/O library developed by Gottfried Haider as part of GSOC 2015
|
||||
Hardware I/O library developed by Gottfried Haider as part of GSoC 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
@@ -31,14 +31,35 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @webref
|
||||
*/
|
||||
public class SPI {
|
||||
|
||||
public static final int MODE0 = 0; // CPOL=0, CPHA=0, most common
|
||||
public static final int MODE1 = 1; // CPOL=0, CPHA=1
|
||||
public static final int MODE2 = 2; // CPOL=1, CPHA=0
|
||||
public static final int MODE3 = 3; // CPOL=1, CPHA=1
|
||||
public static final int MSBFIRST = 0; // most significant bit first, most common
|
||||
public static final int LSBFIRST = 1; // least significant bit first
|
||||
/**
|
||||
* CPOL=0, CPHA=0, most common
|
||||
*/
|
||||
public static final int MODE0 = 0;
|
||||
/**
|
||||
* CPOL=0, CPHA=1
|
||||
*/
|
||||
public static final int MODE1 = 1;
|
||||
/**
|
||||
* CPOL=1, CPHA=0
|
||||
*/
|
||||
public static final int MODE2 = 2;
|
||||
/**
|
||||
* CPOL=1, CPHA=1
|
||||
*/
|
||||
public static final int MODE3 = 3;
|
||||
/**
|
||||
* most significant bit first, most common
|
||||
*/
|
||||
public static final int MSBFIRST = 0;
|
||||
/**
|
||||
* least significant bit first
|
||||
*/
|
||||
public static final int LSBFIRST = 1;
|
||||
|
||||
protected int dataOrder = 0;
|
||||
protected String dev;
|
||||
@@ -49,9 +70,10 @@ public class SPI {
|
||||
|
||||
|
||||
/**
|
||||
* Opens an SPI interface
|
||||
* Opens an SPI interface as master
|
||||
* @param dev device name
|
||||
* @see list
|
||||
* @webref
|
||||
*/
|
||||
public SPI(String dev) {
|
||||
NativeInterface.loadLibrary();
|
||||
@@ -64,7 +86,8 @@ public class SPI {
|
||||
|
||||
|
||||
/**
|
||||
* Closes the I2C interface
|
||||
* Closes the SPI interface
|
||||
* @webref
|
||||
*/
|
||||
public void close() {
|
||||
NativeInterface.closeDevice(handle);
|
||||
@@ -84,6 +107,7 @@ public class SPI {
|
||||
/**
|
||||
* Lists all available SPI interfaces
|
||||
* @return String array
|
||||
* @webref
|
||||
*/
|
||||
public static String[] list() {
|
||||
ArrayList<String> devs = new ArrayList<String>();
|
||||
@@ -106,8 +130,9 @@ public class SPI {
|
||||
/**
|
||||
* Configures the SPI interface
|
||||
* @param maxSpeed maximum transmission rate in Hz, 500000 (500 kHz) is a resonable default
|
||||
* @param dataOrder whether data is send with the first- or least significant bit first (SPI.MSBFIRST or SPI.LSBFIRST, the former is more common)
|
||||
* @param mode SPI.MODE0 to SPI.MODE3 (see https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Clock_polarity_and_phase)
|
||||
* @param dataOrder whether data is send with the first- or least-significant bit first (SPI.MSBFIRST or SPI.LSBFIRST, the former is more common)
|
||||
* @param mode <a href="https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Clock_polarity_and_phase">SPI.MODE0 to SPI.MODE3</br>
|
||||
* @webref
|
||||
*/
|
||||
public void settings(int maxSpeed, int dataOrder, int mode) {
|
||||
this.maxSpeed = maxSpeed;
|
||||
@@ -118,12 +143,9 @@ public class SPI {
|
||||
|
||||
/**
|
||||
* Transfers data over the SPI bus
|
||||
*
|
||||
* With SPI, data is simultaneously being exchanged between the master device
|
||||
* and the slave device. For every byte that is being sent out, there's also
|
||||
* one byte being read in.
|
||||
* @param out bytes to send
|
||||
* @return bytes read in (array is the same length as out)
|
||||
* @webref
|
||||
*/
|
||||
public byte[] transfer(byte[] out) {
|
||||
// track the current setting per device across multiple instances
|
||||
@@ -150,10 +172,6 @@ public class SPI {
|
||||
|
||||
/**
|
||||
* Transfers data over the SPI bus
|
||||
*
|
||||
* With SPI, data is simultaneously being exchanged between the master device
|
||||
* and the slave device. For every byte that is being sent out, there's also
|
||||
* one byte being read in.
|
||||
* @param out string to send
|
||||
* @return bytes read in (array is the same length as out)
|
||||
*/
|
||||
@@ -164,10 +182,6 @@ public class SPI {
|
||||
|
||||
/**
|
||||
* Transfers data over the SPI bus
|
||||
*
|
||||
* With SPI, data is simultaneously being exchanged between the master device
|
||||
* and the slave device. For every byte that is being sent out, there's also
|
||||
* one byte being read in.
|
||||
* @param out single byte to send
|
||||
* @return bytes read in (array is the same length as out)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user