Skip to content
Open
12 changes: 12 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 @@ -34,6 +34,8 @@
import com.pi4j.io.i2c.I2C;
import com.pi4j.io.i2c.I2CConfig;
import com.pi4j.io.i2c.I2CConfigBuilder;
import com.pi4j.io.onewire.OneWire;
import com.pi4j.io.onewire.OneWireConfig;
import com.pi4j.io.pwm.Pwm;
import com.pi4j.io.pwm.PwmConfig;
import com.pi4j.io.pwm.PwmConfigBuilder;
Expand Down Expand Up @@ -146,6 +148,16 @@ default Serial create(SerialConfig config) {
return create(config, Serial.class);
}

/**
* Creates a new {@link OneWire} instance using the specified configuration.
*
* @param config the {@link com.pi4j.io.onewire.OneWireConfig} object containing the configuration settings.
* @return a new {@link OneWire} object configured as specified.
*/
default OneWire create(OneWireConfig config) {
return create(config, OneWire.class);
}

/**
* <p>create.</p>
*
Expand Down
12 changes: 12 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/internal/ProviderAliases.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.pi4j.io.gpio.digital.DigitalInputProvider;
import com.pi4j.io.gpio.digital.DigitalOutputProvider;
import com.pi4j.io.i2c.I2CProvider;
import com.pi4j.io.onewire.OneWireProvider;
import com.pi4j.io.pwm.PwmProvider;
import com.pi4j.io.serial.SerialProvider;
import com.pi4j.io.spi.SpiProvider;
Expand Down Expand Up @@ -282,4 +283,15 @@ default <T extends I2CProvider> T getI2CProvider() throws ProviderException{
default <T extends SerialProvider> T getSerialProvider() throws ProviderException{
return this.serial();
}

/**
* <p>1-Wire</p>
*
* @param <T> a T object.
* @return a T object.
* @throws ProviderException if any.
*/
default <T extends OneWireProvider> T oneWire() throws ProviderException {
return this.provider(IOType.ONE_WIRE);
}
}
7 changes: 6 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 @@ -32,6 +32,10 @@
import com.pi4j.io.i2c.I2CConfig;
import com.pi4j.io.i2c.I2CConfigBuilder;
import com.pi4j.io.i2c.I2CProvider;
import com.pi4j.io.onewire.OneWire;
import com.pi4j.io.onewire.OneWireConfig;
import com.pi4j.io.onewire.OneWireConfigBuilder;
import com.pi4j.io.onewire.OneWireProvider;
import com.pi4j.io.pwm.Pwm;
import com.pi4j.io.pwm.PwmConfig;
import com.pi4j.io.pwm.PwmConfigBuilder;
Expand Down Expand Up @@ -62,7 +66,8 @@ public enum IOType {
I2C(I2CProvider.class, com.pi4j.io.i2c.I2C.class, I2CConfig.class, I2CConfigBuilder.class),
SPI(SpiProvider.class, Spi.class, I2CConfig.class, I2CConfigBuilder.class),
@Deprecated(forRemoval = true)
SERIAL(SerialProvider.class, Serial.class, SerialConfig.class, SerialConfigBuilder.class);
SERIAL(SerialProvider.class, Serial.class, SerialConfig.class, SerialConfigBuilder.class),
ONE_WIRE(OneWireProvider.class, OneWire.class, OneWireConfig.class, OneWireConfigBuilder.class);

private Class<? extends Provider> providerClass;
private Class<? extends IO> ioClass;
Expand Down
83 changes: 83 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/io/onewire/OneWire.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.pi4j.io.onewire;

/*
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: LIBRARY :: Java Library (CORE)
* FILENAME : OneWire.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;
import com.pi4j.io.IODataReader;
import com.pi4j.io.IODataWriter;

/**
* Interface representing a 1-Wire I/O communication in the Pi4J library.
* <p>
* This interface facilitates 1-Wire bus/device communications by providing
* methods for configuration, state management, and data interaction.
* It extends {@link IO}, {@link IODataWriter}, {@link IODataReader},
* and {@link OneWireFileDataReaderWriter}, and supports auto-closing of resources.
* </p>
*/
public interface OneWire
extends IO<OneWire, OneWireConfig, OneWireProvider>, OneWireFileDataReaderWriter {

/**
* Creates a new configuration builder for a 1-Wire interface.
* <p>
* This static method provides a convenient way to construct
* a {@link OneWireConfigBuilder} object.
* </p>
*
* @param context the {@link Context} associated with this configuration.
* @return a {@link OneWireConfigBuilder} instance to build the configuration.
*/
static OneWireConfigBuilder newConfigBuilder(Context context) {
return OneWireConfigBuilder.newInstance(context);
}

/**
* Retrieves the 1-Wire device address for this interface instance.
* <p>
* The device address is defined in the configuration and is used
* to identify the target 1-Wire device on the bus.
* </p>
*
* @return a {@code String} representing the 1-Wire device address.
*/
default String device() {
return config().device();
}

/**
* Retrieves the 1-Wire device address for this interface instance.
* <p>
* This method serves as an alias for {@link #device()}.
* </p>
*
* @return a {@code String} representing the 1-Wire device address.
*/
default String getDevice() {
return device();
}
}
57 changes: 57 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.pi4j.io.onewire;

/*-
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: LIBRARY :: Java Library (CORE)
* FILENAME : OneWireBase.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.io.IOBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Base class for managing 1-Wire communication.
*
* <p>This abstract class provides core functionality and lifecycle management
* for 1-Wire communication interfaces within the Pi4J library. It is intended
* to be extended by specific 1-Wire implementations.</p>
*
* <p>The class integrates with the Pi4J I/O framework and ensures
* consistent behavior across all supported 1-Wire providers.</p>
*/
public abstract class OneWireBase extends IOBase<OneWire, OneWireConfig, OneWireProvider> implements OneWire {

/** Logger instance for debugging and tracing operations. */
private final Logger logger = LoggerFactory.getLogger(this.getClass());

/**
* Constructs a new {@code OneWireBase} instance.
*
* @param provider the {@link OneWireProvider} used to manage 1-Wire communication.
* @param config the {@link OneWireConfig} specifying configuration settings for this instance.
*/
public OneWireBase(OneWireProvider provider, OneWireConfig config) {
super(provider, config);
logger.trace("Created OneWireBase instance with config: {}", config);
}
}
79 changes: 79 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.pi4j.io.onewire;

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

/**
* Interface defining the configuration settings for a 1-Wire interface in the Pi4J library.
* <p>
* This interface extends {@link IOConfig} and provides additional configuration options
* specific to 1-Wire devices.
* </p>
*/
public interface OneWireConfig extends IOConfig<OneWireConfig> {

/**
* Key used for accessing the device configuration property.
*/
String DEVICE_KEY = "device";

/**
* Retrieves the device ID associated with this configuration.
* <p>
* The device ID is a unique identifier used to specify the target 1-Wire device.
* </p>
*
* @return a {@link String} representing the device ID.
*/
String device();

/**
* Retrieves the device ID associated with this configuration.
* <p>
* This method serves as an alias for {@link #device()} for convenience.
* </p>
*
* @return a {@link String} representing the device ID.
*/
default String getDevice() {
return device();
}

/**
* Creates a new builder instance for configuring a 1-Wire interface.
* <p>
* The builder provides a fluent API for constructing a {@link OneWireConfig} object.
* </p>
*
* @param context the {@link Context} associated with this configuration.
* @return a {@link OneWireConfigBuilder} instance for building the configuration.
*/
static OneWireConfigBuilder newBuilder(Context context) {
return OneWireConfigBuilder.newInstance(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.pi4j.io.onewire;

/*-
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: LIBRARY :: Java Library (CORE)
* FILENAME : OneWireConfigBuilder.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.config.ConfigBuilder;
import com.pi4j.context.Context;
import com.pi4j.io.IOConfigBuilder;
import com.pi4j.io.onewire.impl.DefaultOneWireConfigBuilder;

/**
* Builder interface for configuring 1-Wire interfaces in the Pi4J library.
* <p>
* This interface extends {@link IOConfigBuilder} and {@link ConfigBuilder},
* providing a fluent API to construct and customize 1-Wire configuration settings.
* </p>
*/
public interface OneWireConfigBuilder extends
IOConfigBuilder<OneWireConfigBuilder, OneWireConfig>,
ConfigBuilder<OneWireConfigBuilder, OneWireConfig> {

/**
* Creates a new instance of {@link OneWireConfigBuilder}.
* <p>
* This factory method provides a convenient way to initialize
* a 1-Wire configuration builder within the given {@link Context}.
* </p>
*
* @param context the {@link Context} for the application environment.
* @return a {@link OneWireConfigBuilder} instance for building configurations.
*/
static OneWireConfigBuilder newInstance(Context context) {
return DefaultOneWireConfigBuilder.newInstance(context);
}

/**
* Specifies the device ID for the 1-Wire interface.
* <p>
* The device ID uniquely identifies the target 1-Wire device
* and is a required configuration parameter.
* </p>
*
* @param device a {@link String} representing the device ID.
* @return the {@link OneWireConfigBuilder} instance, for chaining.
*/
OneWireConfigBuilder device(String device);
}
Loading