
com.pi4j.io.i2c.I2CBase Maven / Gradle / Ivy
Show all versions of pi4j-core Show documentation
package com.pi4j.io.i2c;
/*-
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: LIBRARY :: Java Library (CORE)
* FILENAME : I2CBase.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: https://pi4j.com/
* **********************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.pi4j.context.Context;
import com.pi4j.exception.ShutdownException;
import com.pi4j.io.IOBase;
import com.pi4j.io.i2c.impl.DefaultI2CRegister;
import java.util.concurrent.Callable;
/**
* Abstract I2CBase class.
*
* @author Robert Savage (http://www.savagehomeautomation.com)
* @version $Id: $Id
* @param
*/
public abstract class I2CBase extends IOBase implements I2C {
protected boolean isOpen;
protected final T i2CBus;
/**
* Constructor for I2CBase.
*
* @param provider a {@link I2CProvider} object.
* @param config a {@link I2CConfig} object.
* @param i2CBus a {@link I2CBus} object.
*/
public I2CBase(I2CProvider provider, I2CConfig config, T i2CBus) {
super(provider, config);
this.isOpen = true;
this.i2CBus = i2CBus;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isOpen() {
return this.isOpen;
}
/**
* {@inheritDoc}
*/
@Override
public void close() {
this.isOpen = false;
}
/**
* {@inheritDoc}
*
* Get an encapsulated interface for reading and writing to a specific I2C device register
*/
public I2CRegister getRegister(int address) {
return new DefaultI2CRegister(this, address);
}
@Override
public V execute(Callable action) {
if (action == null)
throw new NullPointerException("Parameter 'action' is mandatory!");
return this.i2CBus.execute(this, action);
}
/**
* {@inheritDoc}
*/
@Override
public I2C shutdown(Context context) throws ShutdownException {
// if this I2C device is still open, then we need to close it since we are shutting down
if (this.isOpen()) {
try {
close();
} catch (Exception e) {
throw new ShutdownException(e);
}
}
return this;
}
}