-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351 from Pi4J/feature/i2c-execute
[Major] I2C extensions, fixed and cleanup
- Loading branch information
Showing
15 changed files
with
377 additions
and
177 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...linuxfs/provider/i2c/CheckedFunction.java → ...java/com/pi4j/common/CheckedFunction.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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.pi4j.io.i2c; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* This interface defines method to be performed on an I2C bus. Most importantly the {@link #execute(I2C, Callable)} | ||
* allows to perform bulk operations on the bus in a thread safe manner. | ||
*/ | ||
public interface I2CBus { | ||
|
||
/** | ||
* Executes the given action, which typically performs multiple I2C reads and/or writes on the I2C bus in a thread | ||
* safe manner, i.e. the bus is blocked till the action is completed. | ||
* | ||
* @param i2c the device for which to perform the action | ||
* @param action the action to perform | ||
* @param <R> the result type of the action, if any | ||
* | ||
* @return the result of the action | ||
*/ | ||
<R> R execute(I2C i2c, Callable<R> action); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.pi4j.io.i2c; | ||
|
||
import com.pi4j.exception.Pi4JException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
import static java.text.MessageFormat.format; | ||
|
||
public abstract class I2CBusBase implements I2CBus { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(I2CBusBase.class); | ||
|
||
public static final long DEFAULT_LOCK_ACQUIRE_TIMEOUT = 1000; | ||
public static final TimeUnit DEFAULT_LOCK_ACQUIRE_TIMEOUT_UNITS = TimeUnit.MILLISECONDS; | ||
|
||
protected final int bus; | ||
|
||
protected final long lockAquireTimeout; | ||
protected final TimeUnit lockAquireTimeoutUnit; | ||
private final ReentrantLock lock = new ReentrantLock(true); | ||
|
||
public I2CBusBase(I2CConfig config) { | ||
if (config.bus() == null) | ||
throw new IllegalArgumentException("I2C bus must be specified"); | ||
|
||
this.bus = config.getBus(); | ||
|
||
this.lockAquireTimeout = DEFAULT_LOCK_ACQUIRE_TIMEOUT; | ||
this.lockAquireTimeoutUnit = DEFAULT_LOCK_ACQUIRE_TIMEOUT_UNITS; | ||
} | ||
|
||
protected <R> R _execute(I2C i2c, Callable<R> action) { | ||
if (i2c == null) | ||
throw new NullPointerException("Parameter 'i2c' is mandatory!"); | ||
if (action == null) | ||
throw new NullPointerException("Parameter 'action' is mandatory!"); | ||
try { | ||
if (this.lock.tryLock() || this.lock.tryLock(this.lockAquireTimeout, this.lockAquireTimeoutUnit)) { | ||
try { | ||
return action.call(); | ||
} finally { | ||
this.lock.unlock(); | ||
} | ||
} else { | ||
throw new Pi4JException( | ||
format("Failed to get I2C lock on bus {0} after {1} {2}", this.bus, this.lockAquireTimeout, | ||
this.lockAquireTimeoutUnit)); | ||
} | ||
} catch (InterruptedException e) { | ||
logger.error("Failed locking {}-{}", getClass().getSimpleName(), this.bus, e); | ||
throw new RuntimeException("Could not obtain an access-lock!", e); | ||
} catch (Exception e) { | ||
throw new Pi4JException("Failed to execute action for device " + i2c.device() + " on bus " + this.bus, e); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.