Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new IO type and providers and interface Sensor #199

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/internal/IOCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
import com.pi4j.io.spi.Spi;
import com.pi4j.io.spi.SpiConfig;
import com.pi4j.io.spi.SpiConfigBuilder;
import com.pi4j.io.sensor.Sensor;
import com.pi4j.io.sensor.SensorConfig;


public interface IOCreator {

Expand Down Expand Up @@ -225,4 +228,14 @@ default Spi create(SpiConfigBuilder config) {
default Serial create(SerialConfigBuilder config) {
return create(config.build());
}

/**
* <p>create.</p>
*
* @param config a {@link com.pi4j.io.sensor.SensorConfig} object.
* @return a {@link com.pi4j.io.sensor.Sensor} object.
*/
default Sensor create(SensorConfig config) {
return create(config, Sensor.class);
}
}
67 changes: 47 additions & 20 deletions pi4j-core/src/main/java/com/pi4j/internal/ProviderAliases.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@
import com.pi4j.provider.Provider;
import com.pi4j.provider.exception.ProviderException;
import com.pi4j.provider.exception.ProviderNotFoundException;
import com.pi4j.io.sensor.SensorProvider;


public interface ProviderAliases {

/**
* <p>provider.</p>
*
* @param ioType a {@link IOType} object.
* @param <T> a T object.
* @param <T> a T object.
* @return a T object.
* @throws ProviderNotFoundException if any.
*/
Expand All @@ -54,11 +56,11 @@ public interface ProviderAliases {
* <p>provider.</p>
*
* @param ioType a {@link IOType} object.
* @param <T> a T object.
* @param <T> a T object.
* @return a T object.
* @throws ProviderNotFoundException if any.
*/
default <T extends Provider> T getProvider(IOType ioType) throws ProviderNotFoundException{
default <T extends Provider> T getProvider(IOType ioType) throws ProviderNotFoundException {
return provider(ioType);
}

Expand All @@ -81,7 +83,7 @@ default <T extends AnalogInputProvider> T ain() throws ProviderException {
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends AnalogOutputProvider> T aout() throws ProviderException{
default <T extends AnalogOutputProvider> T aout() throws ProviderException {
return analogOutput();
}

Expand All @@ -92,7 +94,7 @@ default <T extends AnalogOutputProvider> T aout() throws ProviderException{
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends DigitalInputProvider> T din() throws ProviderException{
default <T extends DigitalInputProvider> T din() throws ProviderException {
return digitalInput();
}

Expand All @@ -103,7 +105,7 @@ default <T extends DigitalInputProvider> T din() throws ProviderException{
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends DigitalOutputProvider> T dout() throws ProviderException{
default <T extends DigitalOutputProvider> T dout() throws ProviderException {
return digitalOutput();
}

Expand All @@ -125,7 +127,7 @@ default <T extends AnalogInputProvider> T analogInput() throws ProviderException
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends AnalogOutputProvider> T analogOutput() throws ProviderException{
default <T extends AnalogOutputProvider> T analogOutput() throws ProviderException {
return this.provider(IOType.ANALOG_OUTPUT);
}

Expand All @@ -136,7 +138,7 @@ default <T extends AnalogOutputProvider> T analogOutput() throws ProviderExcepti
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends DigitalInputProvider> T digitalInput() throws ProviderException{
default <T extends DigitalInputProvider> T digitalInput() throws ProviderException {
return this.provider(IOType.DIGITAL_INPUT);
}

Expand All @@ -147,7 +149,7 @@ default <T extends DigitalInputProvider> T digitalInput() throws ProviderExcepti
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends DigitalOutputProvider> T digitalOutput() throws ProviderException{
default <T extends DigitalOutputProvider> T digitalOutput() throws ProviderException {
return this.provider(IOType.DIGITAL_OUTPUT);
}

Expand All @@ -158,7 +160,7 @@ default <T extends DigitalOutputProvider> T digitalOutput() throws ProviderExcep
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends PwmProvider> T pwm() throws ProviderException{
default <T extends PwmProvider> T pwm() throws ProviderException {
return this.provider(IOType.PWM);
}

Expand All @@ -169,7 +171,7 @@ default <T extends PwmProvider> T pwm() throws ProviderException{
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends SpiProvider> T spi() throws ProviderException{
default <T extends SpiProvider> T spi() throws ProviderException {
return this.provider(IOType.SPI);
}

Expand All @@ -180,7 +182,7 @@ default <T extends SpiProvider> T spi() throws ProviderException{
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends I2CProvider> T i2c() throws ProviderException{
default <T extends I2CProvider> T i2c() throws ProviderException {
return this.provider(IOType.I2C);
}

Expand All @@ -191,7 +193,7 @@ default <T extends I2CProvider> T i2c() throws ProviderException{
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends SerialProvider> T serial() throws ProviderException{
default <T extends SerialProvider> T serial() throws ProviderException {
return this.provider(IOType.SERIAL);
}

Expand All @@ -213,7 +215,7 @@ default <T extends AnalogInputProvider> T getAnalogInputProvider() throws Provid
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends AnalogOutputProvider> T getAnalogOutputProvider() throws ProviderException{
default <T extends AnalogOutputProvider> T getAnalogOutputProvider() throws ProviderException {
return this.analogOutput();
}

Expand All @@ -224,7 +226,7 @@ default <T extends AnalogOutputProvider> T getAnalogOutputProvider() throws Prov
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends DigitalInputProvider> T getDigitalInputProvider() throws ProviderException{
default <T extends DigitalInputProvider> T getDigitalInputProvider() throws ProviderException {
return this.digitalInput();
}

Expand All @@ -235,7 +237,7 @@ default <T extends DigitalInputProvider> T getDigitalInputProvider() throws Prov
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends DigitalOutputProvider> T getDigitalOutputProvider() throws ProviderException{
default <T extends DigitalOutputProvider> T getDigitalOutputProvider() throws ProviderException {
return this.digitalOutput();
}

Expand All @@ -246,7 +248,7 @@ default <T extends DigitalOutputProvider> T getDigitalOutputProvider() throws Pr
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends PwmProvider> T getPwmProvider() throws ProviderException{
default <T extends PwmProvider> T getPwmProvider() throws ProviderException {
return this.pwm();
}

Expand All @@ -257,7 +259,7 @@ default <T extends PwmProvider> T getPwmProvider() throws ProviderException{
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends SpiProvider> T getSpiProvider() throws ProviderException{
default <T extends SpiProvider> T getSpiProvider() throws ProviderException {
return this.spi();
}

Expand All @@ -268,7 +270,7 @@ default <T extends SpiProvider> T getSpiProvider() throws ProviderException{
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends I2CProvider> T getI2CProvider() throws ProviderException{
default <T extends I2CProvider> T getI2CProvider() throws ProviderException {
return this.i2c();
}

Expand All @@ -279,7 +281,32 @@ default <T extends I2CProvider> T getI2CProvider() throws ProviderException{
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends SerialProvider> T getSerialProvider() throws ProviderException{
default <T extends SerialProvider> T getSerialProvider() throws ProviderException {
return this.serial();
}


/**
* <p>sensor.</p>
*
* @param <T> a T object.
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends SensorProvider> T sensor() throws ProviderException {
return this.provider(IOType.SENSOR);
}


/**
* <p>serial.</p>
*
* @param <T> a T object.
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends SensorProvider> T getSensorProvider() throws ProviderException {
return this.sensor();
}
}

10 changes: 9 additions & 1 deletion pi4j-core/src/main/java/com/pi4j/io/IOType.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
import com.pi4j.io.pwm.PwmConfig;
import com.pi4j.io.pwm.PwmConfigBuilder;
import com.pi4j.io.pwm.PwmProvider;
import com.pi4j.io.sensor.Sensor;
import com.pi4j.io.sensor.SensorConfig;
import com.pi4j.io.sensor.SensorConfigBuilder;
import com.pi4j.io.sensor.SensorProvider;
import com.pi4j.io.serial.Serial;
import com.pi4j.io.serial.SerialConfig;
import com.pi4j.io.serial.SerialConfigBuilder;
Expand All @@ -61,7 +65,8 @@ public enum IOType {
PWM(PwmProvider.class, Pwm.class, PwmConfig.class, PwmConfigBuilder.class),
I2C(I2CProvider.class, com.pi4j.io.i2c.I2C.class, I2CConfig.class, I2CConfigBuilder.class),
SPI(SpiProvider.class, Spi.class, I2CConfig.class, I2CConfigBuilder.class),
SERIAL(SerialProvider.class, Serial.class, SerialConfig.class, SerialConfigBuilder.class);
SERIAL(SerialProvider.class, Serial.class, SerialConfig.class, SerialConfigBuilder.class),
SENSOR(SensorProvider.class, Sensor.class, SensorConfig.class, SensorConfigBuilder .class);

private Class<? extends Provider> providerClass;
private Class<? extends IO> ioClass;
Expand Down Expand Up @@ -336,6 +341,9 @@ public static IOType parse(String ioType) {
if(ioType.equalsIgnoreCase("uart")) return SERIAL;
if(ioType.equalsIgnoreCase("rs232")) return SERIAL;

// SENSOR
if(ioType.startsWith("sensor")) return SENSOR;
if(ioType.equalsIgnoreCase("sensor")) return SENSOR;

throw new IllegalArgumentException("Unknown IO TYPE: " + ioType);
}
Expand Down
101 changes: 101 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/io/sensor/Sensor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.pi4j.io.sensor;

/*
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: LIBRARY :: Java Library (CORE)
* FILENAME : Sensor.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.io.IO;

/**
* <p>Sensor interface.</p>
*
* @author Tom Aarts
* @version $Id: $Id
*/
public interface Sensor extends IO<Sensor, SensorConfig, SensorProvider> {


int DEFAULT_BUS = 1;
int DEFAULT_ADDRESS = 0x76;

/**
* <p>newConfigBuilder.</p>
*
* @param context {@link Context}
* @return a {@link com.pi4j.io.sensor.SensorConfigBuilder} object.
*/
static SensorConfigBuilder newConfigBuilder(Context context) {
return SensorConfigBuilder.newInstance(context);
}


/**
* Perform any/all actions to reset the sensor
*/
default void resetSensor() {
}

/**
* Perform any/all actions to initialize the sensor
*/
default void initSensor() {
}

/*** @return temperature in centigrade
*/
default double temperatureC() {
return 0;
}

/** @return temperature in fahrenheit
*/
default double temperatureF() {
return 0;
}


/**
* @return presure in Pa units
*/
default double pressurePa() {
return 0;
}

/**
* @return pressure in Inches Mercury
*/
default double pressureIn() {
return 0;
}

/**
* @return pressure in millibar
*/
default double pressureMb() {
return 0;
}

}
Loading