diff --git a/pi4j-core/src/main/java/com/pi4j/internal/IOCreator.java b/pi4j-core/src/main/java/com/pi4j/internal/IOCreator.java index 94c417375..197a115fb 100644 --- a/pi4j-core/src/main/java/com/pi4j/internal/IOCreator.java +++ b/pi4j-core/src/main/java/com/pi4j/internal/IOCreator.java @@ -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; @@ -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); + } + /** *
create.
* diff --git a/pi4j-core/src/main/java/com/pi4j/internal/ProviderAliases.java b/pi4j-core/src/main/java/com/pi4j/internal/ProviderAliases.java index 95d216fda..58ddab489 100644 --- a/pi4j-core/src/main/java/com/pi4j/internal/ProviderAliases.java +++ b/pi4j-core/src/main/java/com/pi4j/internal/ProviderAliases.java @@ -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; @@ -282,4 +283,15 @@ default1-Wire
+ * + * @param+ * 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. + *
+ */ +public interface OneWire + extends IO+ * This static method provides a convenient way to construct + * a {@link OneWireConfigBuilder} object. + *
+ * + * @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. + *+ * The device address is defined in the configuration and is used + * to identify the target 1-Wire device on the bus. + *
+ * + * @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. + *+ * This method serves as an alias for {@link #device()}. + *
+ * + * @return a {@code String} representing the 1-Wire device address. + */ + default String getDevice() { + return device(); + } +} diff --git a/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireBase.java b/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireBase.java new file mode 100644 index 000000000..a736b1238 --- /dev/null +++ b/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireBase.java @@ -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. + * + *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.
+ * + *The class integrates with the Pi4J I/O framework and ensures + * consistent behavior across all supported 1-Wire providers.
+ */ +public abstract class OneWireBase extends IOBase+ * This interface extends {@link IOConfig} and provides additional configuration options + * specific to 1-Wire devices. + *
+ */ +public interface OneWireConfig extends IOConfig+ * The device ID is a unique identifier used to specify the target 1-Wire device. + *
+ * + * @return a {@link String} representing the device ID. + */ + String device(); + + /** + * Retrieves the device ID associated with this configuration. + *+ * This method serves as an alias for {@link #device()} for convenience. + *
+ * + * @return a {@link String} representing the device ID. + */ + default String getDevice() { + return device(); + } + + /** + * Creates a new builder instance for configuring a 1-Wire interface. + *+ * The builder provides a fluent API for constructing a {@link OneWireConfig} object. + *
+ * + * @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); + } +} diff --git a/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireConfigBuilder.java b/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireConfigBuilder.java new file mode 100644 index 000000000..f84bf39f4 --- /dev/null +++ b/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireConfigBuilder.java @@ -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. + *+ * This interface extends {@link IOConfigBuilder} and {@link ConfigBuilder}, + * providing a fluent API to construct and customize 1-Wire configuration settings. + *
+ */ +public interface OneWireConfigBuilder extends + IOConfigBuilder+ * This factory method provides a convenient way to initialize + * a 1-Wire configuration builder within the given {@link Context}. + *
+ * + * @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. + *+ * The device ID uniquely identifies the target 1-Wire device + * and is a required configuration parameter. + *
+ * + * @param device a {@link String} representing the device ID. + * @return the {@link OneWireConfigBuilder} instance, for chaining. + */ + OneWireConfigBuilder device(String device); +} diff --git a/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireFileDataReaderWriter.java b/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireFileDataReaderWriter.java new file mode 100644 index 000000000..bf523a800 --- /dev/null +++ b/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireFileDataReaderWriter.java @@ -0,0 +1,86 @@ +package com.pi4j.io.onewire; + +/* + * #%L + * ********************************************************************** + * ORGANIZATION : Pi4J + * PROJECT : Pi4J :: LIBRARY :: Java Library (CORE) + * FILENAME : OneWireFileDataReaderWriter.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.exception.IOException; + +import java.util.List; + +public interface OneWireFileDataReaderWriter { + /** + * Read the entire content of a file on the device. + * + * @param fileName The name of the file to read. + * @return The file content as List. + * @throws IOException if the file does not exist, is not readable, or another I/O error occurs. + */ + List+ * The {@link OneWireProvider} interface is responsible for creating and managing + * instances of 1-Wire devices. It serves as a factory and configurator, enabling + * the creation of 1-Wire devices based on various parameters like device ID, custom ID, + * name, and description. + *
+ */ +public interface OneWireProvider extends Provider+ * This method is useful for building and creating 1-Wire devices when a + * configuration builder instance is already available. + *
+ * + * @param builder the configuration builder used to define the 1-Wire device. + * @param+ * This method provides a simpler way to create 1-Wire devices without + * requiring additional metadata like name or description. + *
+ * + * @param device the 1-Wire device address as a {@link String}. + * @param id a unique identifier for the 1-Wire device as a {@link String}. + * @param+ * This method allows for the creation of 1-Wire devices with an associated + * human-readable name for easier identification. + *
+ * + * @param device the 1-Wire device address as a {@link String}. + * @param id a unique identifier for the 1-Wire device as a {@link String}. + * @param name a human-readable name for the device as a {@link String}. + * @param+ * This method provides the most detailed way to create a 1-Wire device, + * including an optional description for additional metadata. + *
+ * + * @param device the 1-Wire device address as a {@link String}. + * @param id a unique identifier for the 1-Wire device as a {@link String}. + * @param name a human-readable name for the device as a {@link String}. + * @param description a descriptive text for the device as a {@link String}. + * @param+ * This class represents the configuration settings for a 1-Wire device + * and ensures that the required properties are properly initialized. + *
+ */ +public class DefaultOneWireConfig + extends IOConfigBase+ * This ensures that the configuration is always initialized + * with the required settings from a property map. + *
+ */ + private DefaultOneWireConfig() { + super(); + } + + /** + * Constructs a new {@link DefaultOneWireConfig} instance with the provided properties. + *+ * This constructor validates the presence of required properties, such as + * the device address, and sets default values for optional properties if they are missing. + *
+ * + * @param properties a {@link Map} containing configuration keys and values. + * @throws ConfigMissingRequiredKeyException if the required "device" key is missing. + */ + protected DefaultOneWireConfig(Map+ * The device address is a unique identifier for the 1-Wire device + * and is a required configuration property. + *
+ * + * @return a {@link String} representing the 1-Wire device address. + */ + @Override + public String device() { + return this.device; + } + + // Additional methods for 1-Wire configuration can be added here as needed. +} diff --git a/pi4j-core/src/main/java/com/pi4j/io/onewire/impl/DefaultOneWireConfigBuilder.java b/pi4j-core/src/main/java/com/pi4j/io/onewire/impl/DefaultOneWireConfigBuilder.java new file mode 100644 index 000000000..269b7302e --- /dev/null +++ b/pi4j-core/src/main/java/com/pi4j/io/onewire/impl/DefaultOneWireConfigBuilder.java @@ -0,0 +1,78 @@ +package com.pi4j.io.onewire.impl; + +/*- + * #%L + * ********************************************************************** + * ORGANIZATION : Pi4J + * PROJECT : Pi4J :: LIBRARY :: Java Library (CORE) + * FILENAME : DefaultOneWireConfigBuilder.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.onewire.OneWireConfig; +import com.pi4j.io.onewire.OneWireConfigBuilder; +import com.pi4j.io.impl.IOConfigBuilderBase; + +/** + *DefaultOneWireConfigBuilder class.
+ * This class is used to build and configure 1-Wire device configurations. + */ +public class DefaultOneWireConfigBuilder + extends IOConfigBuilderBase1-Wire.
+ * + * @return a {@link com.pi4j.provider.ProviderGroup} object. + */ + ProviderGroupnewInstance.
diff --git a/pi4j-core/src/main/java/module-info.java b/pi4j-core/src/main/java/module-info.java index d1f75028f..5fe0e4a27 100644 --- a/pi4j-core/src/main/java/module-info.java +++ b/pi4j-core/src/main/java/module-info.java @@ -65,6 +65,7 @@ opens com.pi4j.boardinfo.datareader; exports com.pi4j.boardinfo.util.command; opens com.pi4j.boardinfo.util.command; + exports com.pi4j.io.onewire; // extensibility service interfaces uses com.pi4j.extension.Plugin; diff --git a/pi4j-test/src/main/java/com/pi4j/test/OneWireTest.java b/pi4j-test/src/main/java/com/pi4j/test/OneWireTest.java new file mode 100644 index 000000000..da478bf1c --- /dev/null +++ b/pi4j-test/src/main/java/com/pi4j/test/OneWireTest.java @@ -0,0 +1,98 @@ +package com.pi4j.test; + +import com.pi4j.Pi4J; +import com.pi4j.context.Context; +import com.pi4j.io.onewire.OneWire; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OneWireTest { + + private static final Logger logger = LoggerFactory.getLogger(OneWireTest.class); + + // Device IDs for DS18B20 and DS2413 + private static final String DS18B20_DEVICE_ID = "28-06219443087e"; + private static final String DS2413_DEVICE_ID = "3a-0000005eaee8"; + + // Desired resolution for DS18B20 + private static final String DS18B20_RESOLUTION = "10"; // Set to 10-bit precision + + // Value to set DS2413 state (0xf2 means State 1 ON, State 2 OFF) + private static final byte DS2413_STATE_VALUE = (byte) 0xf2; + + public static void main(String[] args) { + // Initialize Pi4J context + Context pi4j = Pi4J.newAutoContext(); + + try { + // Handle DS18B20 sensor + handleDS18B20(pi4j); + + // Handle DS2413 sensor + handleDS2413(pi4j); + } catch (Exception e) { + logger.error("An error occurred: {}", e.getMessage()); + } finally { + // Shutdown Pi4J context + pi4j.shutdown(); + } + } + + private static void handleDS18B20(Context pi4j) { + logger.info("Interacting with DS18B20 sensor..."); + + var ds18b20Config = OneWire.newConfigBuilder(pi4j) + .id("ds18b20") + .device(DS18B20_DEVICE_ID) + .build(); + + // Read and set temperature resolution for DS18B20 + try { + var ds18b20 = pi4j.create(ds18b20Config); + // Set resolution + ds18b20.writeFile("resolution", DS18B20_RESOLUTION); + logger.info("Set DS18B20 resolution to {} bits", DS18B20_RESOLUTION); + + // Read temperature + String temperature = ds18b20.readFirstLine("temperature"); + + // Parse and log temperature + try { + long tempValue = Long.parseLong(temperature); + logger.info("Temperature: {}°C", tempValue / 100); + } catch (NumberFormatException e) { + logger.error("Failed to parse temperature: {}", temperature); + } + } catch (Exception e) { + logger.error("Failed to interact with DS18B20 sensor: {}", e.getMessage()); + } + } + + private static void handleDS2413(Context pi4j) { + logger.info("Interacting with DS2413 sensor..."); + + var ds2413Config = OneWire.newConfigBuilder(pi4j) + .id("ds2413") + .device(DS2413_DEVICE_ID) + .build(); + + // Read and set state for DS2413 + try { + var ds2413 = pi4j.create(ds2413Config); + + // Read current state before setting new value + String stateBefore = ds2413.readFirstLine("state"); + logger.info("Current state before update: {}", stateBefore); + + // Set new state to 0xf2 (State 1 ON, State 2 OFF) + ds2413.writeFile("state", DS2413_STATE_VALUE); + logger.info("Set DS2413 state to 0xf2 (State 1 ON, State 2 OFF)"); + + // Read current state after setting new value + String stateAfter = ds2413.readFirstLine("state"); + logger.info("Current state after update: {}", stateAfter); + } catch (Exception e) { + logger.error("Failed to interact with DS2413 sensor: {}", e.getMessage()); + } + } +} diff --git a/pi4j-test/src/test/java/com/pi4j/test/io/onewire/OneWireRawDataTest.java b/pi4j-test/src/test/java/com/pi4j/test/io/onewire/OneWireRawDataTest.java new file mode 100644 index 000000000..d1e02ad58 --- /dev/null +++ b/pi4j-test/src/test/java/com/pi4j/test/io/onewire/OneWireRawDataTest.java @@ -0,0 +1,103 @@ +package com.pi4j.test.io.onewire; + +/*- + * #%L + * ********************************************************************** + * ORGANIZATION : Pi4J + * PROJECT : Pi4J :: TESTING :: Unit/Integration Tests + * FILENAME : OneWireRawDataTest.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.Pi4J; +import com.pi4j.context.Context; +import com.pi4j.exception.Pi4JException; +import com.pi4j.io.onewire.OneWire; +import com.pi4j.plugin.mock.provider.onewire.MockOneWire; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@TestInstance(Lifecycle.PER_CLASS) +public class OneWireRawDataTest { + + private Context pi4j; + + @BeforeEach + public void beforeTest() throws Pi4JException { + pi4j = Pi4J.newContextBuilder() + .autoDetectMockPlugins() + .autoDetectPlatforms() + .build(); + } + + @AfterEach + public void afterTest() { + try { + pi4j.shutdown(); + } catch (Pi4JException ignored) { /* do nothing */ } + } + + @Test + public void shouldWriteAndReadStringData() { + var content = "24000" + System.lineSeparator() + "48000"; + var oneWire = createMockOneWire("28-000008d6bac6"); + + oneWire.writeFile("content", content); + + var fileContent = oneWire.readFile("content"); + + assertNotNull(fileContent); + assertEquals(2, fileContent.size()); + assertEquals("24000", fileContent.get(0)); + assertEquals("48000", fileContent.get(1)); + assertEquals(content, String.join(System.lineSeparator(), fileContent)); + } + + @Test + public void shouldWriteAndReadByteData() { + var content = new byte[]{0x01, 0x20, 0x30}; + var oneWire = createMockOneWire("28-000008d6bac6"); + + oneWire.writeFile("content", content); + + var fileContent = oneWire.readFileAsBytes("content"); + + assertNotNull(fileContent); + assertEquals(3, fileContent.length); + assertArrayEquals(content, fileContent); + } + + private MockOneWire createMockOneWire(String deviceId) { + var config = OneWire.newConfigBuilder(pi4j) + .id("my-one-wire") + .name("My 1-Wire") + .device(deviceId) + .build(); + return (MockOneWire) pi4j.oneWire().create(config); + } +} diff --git a/pi4j-test/src/test/java/com/pi4j/test/platform/ManualPlatformsTest.java b/pi4j-test/src/test/java/com/pi4j/test/platform/ManualPlatformsTest.java index e8a6e7c62..d0a2f8b51 100644 --- a/pi4j-test/src/test/java/com/pi4j/test/platform/ManualPlatformsTest.java +++ b/pi4j-test/src/test/java/com/pi4j/test/platform/ManualPlatformsTest.java @@ -31,15 +31,6 @@ import com.pi4j.Pi4J; import com.pi4j.context.Context; import com.pi4j.exception.Pi4JException; -import com.pi4j.plugin.mock.platform.MockPlatform; -import com.pi4j.plugin.mock.provider.gpio.analog.MockAnalogInputProvider; -import com.pi4j.plugin.mock.provider.gpio.analog.MockAnalogOutputProvider; -import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalInputProvider; -import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalOutputProvider; -import com.pi4j.plugin.mock.provider.i2c.MockI2CProvider; -import com.pi4j.plugin.mock.provider.pwm.MockPwmProvider; -import com.pi4j.plugin.mock.provider.serial.MockSerialProvider; -import com.pi4j.plugin.mock.provider.spi.MockSpiProvider; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; diff --git a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/LinuxFsPlugin.java b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/LinuxFsPlugin.java index a21d2a379..d214be667 100644 --- a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/LinuxFsPlugin.java +++ b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/LinuxFsPlugin.java @@ -32,11 +32,13 @@ import com.pi4j.extension.Plugin; import com.pi4j.extension.PluginService; import com.pi4j.plugin.linuxfs.internal.LinuxGpio; +import com.pi4j.plugin.linuxfs.internal.LinuxOneWire; +import com.pi4j.plugin.linuxfs.internal.LinuxPwm; import com.pi4j.plugin.linuxfs.provider.i2c.LinuxFsI2CProvider; import com.pi4j.plugin.linuxfs.provider.gpio.digital.LinuxFsDigitalInputProvider; import com.pi4j.plugin.linuxfs.provider.gpio.digital.LinuxFsDigitalOutputProvider; +import com.pi4j.plugin.linuxfs.provider.onewire.LinuxFsOneWireProvider; import com.pi4j.plugin.linuxfs.provider.pwm.LinuxFsPwmProvider; -import com.pi4j.plugin.linuxfs.internal.LinuxPwm; import com.pi4j.plugin.linuxfs.provider.spi.LinuxFsSpiProvider; import com.pi4j.provider.Provider; import org.slf4j.Logger; @@ -101,6 +103,13 @@ public class LinuxFsPlugin implements Plugin { public static final String I2C_PROVIDER_NAME = NAME + " I2C Provider"; public static final String I2C_PROVIDER_ID = ID + "-i2c"; + // 1-Wire Provider name and unique ID + public static final String ONE_WIRE_PROVIDER_NAME = NAME + " 1-Wire Provider"; + public static final String ONE_WIRE_PROVIDER_ID = ID + "-one-wire"; + +// // SPI Provider name and unique ID +// public static final String SPI_PROVIDER_NAME = NAME + " SPI Provider"; +// public static final String SPI_PROVIDER_ID = ID + "-spi"; // // Serial Provider name and unique ID // public static final String SERIAL_PROVIDER_NAME = NAME + " Serial Provider"; @@ -112,6 +121,7 @@ public class LinuxFsPlugin implements Plugin { public static String DEFAULT_GPIO_FILESYSTEM_PATH = LinuxGpio.DEFAULT_SYSTEM_PATH; public static String DEFAULT_PWM_FILESYSTEM_PATH = LinuxPwm.DEFAULT_SYSTEM_PATH; + public static String DEFAULT_ONE_WIRE_FILESYSTEM_PATH = LinuxOneWire.DEFAULT_SYSTEM_PATH; private Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -124,6 +134,7 @@ public void initialize(PluginService service) { // get Linux file system path for GPIO & PWM String gpioFileSystemPath = DEFAULT_GPIO_FILESYSTEM_PATH; String pwmFileSystemPath = DEFAULT_PWM_FILESYSTEM_PATH; + String oneWireFileSystemPath = DEFAULT_ONE_WIRE_FILESYSTEM_PATH; int pwmChip; if(BoardInfoHelper.usesRP1()) { @@ -158,7 +169,8 @@ public void initialize(PluginService service) { LinuxFsDigitalOutputProvider.newInstance(gpioFileSystemPath), LinuxFsPwmProvider.newInstance(pwmFileSystemPath, pwmChip), LinuxFsI2CProvider.newInstance(), - LinuxFsSpiProvider.newInstance() + LinuxFsSpiProvider.newInstance(), + LinuxFsOneWireProvider.newInstance(oneWireFileSystemPath) }; // register the LinuxFS I/O Providers with the plugin service diff --git a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/internal/LinuxOneWire.java b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/internal/LinuxOneWire.java new file mode 100644 index 000000000..1e80f7d4f --- /dev/null +++ b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/internal/LinuxOneWire.java @@ -0,0 +1,76 @@ +package com.pi4j.plugin.linuxfs.internal; + +import java.nio.file.Files; +import java.nio.file.Paths; + +/** + *LinuxOneWire class.
+ * + * @see "https://www.kernel.org/doc/html/latest/driver-api/w1.html" + */ +public class LinuxOneWire { + + /** ConstantDEFAULT_SYSTEM_PATH="/sys/bus/w1/devices" */
+ public static final String DEFAULT_SYSTEM_PATH = "/sys/bus/w1/devices";
+
+ protected final String systemPath;
+ protected final String deviceId;
+ protected final String devicePath;
+
+ /**
+ * Constructor for LinuxOneWire.
+ * + * @param systemPath a {@link String} object. + * @param deviceId a {@link String} object. + */ + public LinuxOneWire(String systemPath, String deviceId) { + this.systemPath = systemPath; + this.deviceId = deviceId; + this.devicePath = Paths.get(systemPath, deviceId).toString(); + } + + /** + *Constructor for LinuxOneWire.
+ * + * @param deviceId a {@link String} object. + */ + public LinuxOneWire(String deviceId) { + this(DEFAULT_SYSTEM_PATH, deviceId); + } + + /** + * Checks if the 1-Wire device is connected and accessible. + * + * @return {@code true} if the device is connected, {@code false} otherwise. + */ + public boolean isConnected() { + return Files.exists(Paths.get(devicePath)); + } + + /** + * Gets the Linux File System path for the 1-Wire system. + * + * @return The 1-Wire system path as a {@link String}. + */ + public String getSystemPath() { + return systemPath; + } + + /** + * Gets the Linux File System path for this 1-Wire device instance. + * + * @return The 1-Wire device path as a {@link String}. + */ + public String getDevicePath() { + return devicePath; + } + + /** + * Gets the device ID of this 1-Wire device. + * + * @return The device ID as a {@link String}. + */ + public String getDeviceId() { + return deviceId; + } +} diff --git a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWire.java b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWire.java new file mode 100644 index 000000000..7864ca7b9 --- /dev/null +++ b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWire.java @@ -0,0 +1,218 @@ +package com.pi4j.plugin.linuxfs.provider.onewire; + +/*- + * #%L + * ********************************************************************** + * ORGANIZATION : Pi4J + * PROJECT : Pi4J :: PLUGIN :: LinuxFS I/O Providers + * FILENAME : LinuxFsOneWire.java + * + * This file is part of the Pi4J project. More information about + * this project can be found here: https://pi4j.com/ + * ********************************************************************** + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + *+ * This provider manages the creation and configuration of OneWire instances that interact with + * the Linux filesystem for hardware communication. + */ +public class LinuxFsOneWireProviderImpl extends OneWireProviderBase implements LinuxFsOneWireProvider { + + /** + * The file system path where OneWire devices are located. + * This path is used to initialize and manage OneWire interactions. + */ + final String oneWireFileSystemPath; + + /** + * Constructs a new instance of {@code LinuxFsOneWireProviderImpl} with the specified filesystem path. + * + * @param oneWireFileSystemPath the path in the Linux filesystem where OneWire devices are located. + */ + public LinuxFsOneWireProviderImpl(String oneWireFileSystemPath) { + this.id = ID; // Assign the unique provider ID + this.name = NAME; // Assign the provider name + this.oneWireFileSystemPath = oneWireFileSystemPath; // Store the specified file system path + } + + /** + * Returns the priority of this provider. Providers with lower priority values + * are preferred during provider selection in the Pi4J framework. + * + * @return the priority value for this provider, default is 50. + */ + @Override + public int getPriority() { + return 50; + } + + /** + * Creates a new {@link OneWire} instance based on the provided configuration. + *
+ * This method initializes a filesystem-based OneWire instance using the provided
+ * configuration and registers it with the Pi4J context registry.
+ *
+ * @param config the {@link OneWireConfig} containing configuration details for the OneWire instance.
+ * @return a {@link OneWire} object representing the created OneWire instance.
+ */
+ @Override
+ public OneWire create(OneWireConfig config) {
+ LinuxOneWire oneWire = new LinuxOneWire(this.oneWireFileSystemPath, config.device());
+ LinuxFsOneWire fsOneWire = new LinuxFsOneWire(oneWire, this, config);
+ this.context.registry().add(fsOneWire);
+ return fsOneWire;
+ }
+}
+
diff --git a/plugins/pi4j-plugin-linuxfs/src/main/java/module-info.java b/plugins/pi4j-plugin-linuxfs/src/main/java/module-info.java
index a9bcdde63..64a280b7b 100644
--- a/plugins/pi4j-plugin-linuxfs/src/main/java/module-info.java
+++ b/plugins/pi4j-plugin-linuxfs/src/main/java/module-info.java
@@ -33,13 +33,14 @@
requires com.pi4j;
requires com.pi4j.library.linuxfs;
- requires jsch;
requires com.sun.jna;
+ requires jsch;
exports com.pi4j.plugin.linuxfs;
exports com.pi4j.plugin.linuxfs.provider.gpio.digital;
exports com.pi4j.plugin.linuxfs.provider.pwm;
exports com.pi4j.plugin.linuxfs.provider.i2c;
+ exports com.pi4j.plugin.linuxfs.provider.onewire;
exports com.pi4j.plugin.linuxfs.provider.spi;
provides com.pi4j.extension.Plugin
diff --git a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/Mock.java b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/Mock.java
index 4fada79c6..d7b4dda07 100644
--- a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/Mock.java
+++ b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/Mock.java
@@ -94,4 +94,10 @@ public class Mock {
public static final String SERIAL_PROVIDER_NAME = NAME + " Serial Provider";
/** Constant newInstance. Constructor for MockSerialProviderImpl.SERIAL_PROVIDER_ID="ID + -serial" */
public static final String SERIAL_PROVIDER_ID = ID + "-serial";
+
+ // 1-Wire Provider name and unique ID
+ /** Constant ONE_WIRE_PROVIDER_NAME="NAME + Serial Provider" */
+ public static final String ONE_WIRE_PROVIDER_NAME = NAME + " 1-Wire Provider";
+ /** Constant ONE_WIRE_PROVIDER_ID="ID + -serial" */
+ public static final String ONE_WIRE_PROVIDER_ID = ID + "-one-wire";
}
diff --git a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/MockPlugin.java b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/MockPlugin.java
index f5ca2beb1..5fd42bdae 100644
--- a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/MockPlugin.java
+++ b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/MockPlugin.java
@@ -35,6 +35,7 @@
import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalInputProvider;
import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalOutputProvider;
import com.pi4j.plugin.mock.provider.i2c.MockI2CProvider;
+import com.pi4j.plugin.mock.provider.onewire.MockOneWireProvider;
import com.pi4j.plugin.mock.provider.pwm.MockPwmProvider;
import com.pi4j.plugin.mock.provider.serial.MockSerialProvider;
import com.pi4j.plugin.mock.provider.spi.MockSpiProvider;
@@ -49,14 +50,15 @@
public class MockPlugin implements Plugin {
private final Provider[] providers = {
- MockAnalogInputProvider.newInstance(),
- MockAnalogOutputProvider.newInstance(),
- MockDigitalInputProvider.newInstance(),
- MockDigitalOutputProvider.newInstance(),
- MockPwmProvider.newInstance(),
- MockI2CProvider.newInstance(),
- MockSpiProvider.newInstance(),
- MockSerialProvider.newInstance(),
+ MockAnalogInputProvider.newInstance(),
+ MockAnalogOutputProvider.newInstance(),
+ MockDigitalInputProvider.newInstance(),
+ MockDigitalOutputProvider.newInstance(),
+ MockPwmProvider.newInstance(),
+ MockI2CProvider.newInstance(),
+ MockSpiProvider.newInstance(),
+ MockSerialProvider.newInstance(),
+ MockOneWireProvider.newInstance()
};
@Override
@@ -64,7 +66,9 @@ public boolean isMock() {
return true;
}
- /** {@inheritDoc} */
+ /**
+ * {@inheritDoc}
+ */
@Override
public void initialize(PluginService service) {
diff --git a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/platform/MockPlatform.java b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/platform/MockPlatform.java
index 14e2d9abc..726fb459c 100644
--- a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/platform/MockPlatform.java
+++ b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/platform/MockPlatform.java
@@ -36,6 +36,7 @@
import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalInputProvider;
import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalOutputProvider;
import com.pi4j.plugin.mock.provider.i2c.MockI2CProvider;
+import com.pi4j.plugin.mock.provider.onewire.MockOneWireProvider;
import com.pi4j.plugin.mock.provider.pwm.MockPwmProvider;
import com.pi4j.plugin.mock.provider.serial.MockSerialProvider;
import com.pi4j.plugin.mock.provider.spi.MockSpiProvider;
@@ -87,6 +88,8 @@ protected String[] getProviders() {
MockPwmProvider.ID,
MockSpiProvider.ID,
MockI2CProvider.ID,
- MockSerialProvider.ID };
+ MockSerialProvider.ID,
+ MockOneWireProvider.ID
+ };
}
}
diff --git a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWire.java b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWire.java
new file mode 100644
index 000000000..320b36b77
--- /dev/null
+++ b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWire.java
@@ -0,0 +1,198 @@
+package com.pi4j.plugin.mock.provider.onewire;
+
+/*-
+ * #%L
+ * **********************************************************************
+ * ORGANIZATION : Pi4J
+ * PROJECT : Pi4J :: PLUGIN :: Mock Platform & Providers
+ * FILENAME : MockOneWire.java
+ *
+ * This file is part of the Pi4J project. More information about
+ * this project can be found here: https://pi4j.com/
+ * **********************************************************************
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * NAME="Mock.ONE_WIRE_PROVIDER_NAME" */
+ String NAME = Mock.ONE_WIRE_PROVIDER_NAME;
+ /** Constant ID="Mock.ONE_WIRE_PROVIDER_ID" */
+ String ID = Mock.ONE_WIRE_PROVIDER_ID;
+ /**
+ *