From bfe36a80f6c6697a2b7a0136c36a3769f7a3a527 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Sun, 29 Sep 2024 22:44:01 -0500 Subject: [PATCH 1/5] Added fan control feature --- .../inputdevices/FanController.java | 36 ++++++++++++++++++ .../outputdevices/FanHelper.java | 38 +++++++++++++++++++ .../src/main/resources/application.yml | 10 +++++ .../com/opensourcewithslu/FanController.java | 4 ++ .../mock/MockDigitalInput.java | 2 +- 5 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/FanController.java create mode 100644 pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java create mode 100644 pi4micronaut-utils/src/test/java/com/opensourcewithslu/FanController.java diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/FanController.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/FanController.java new file mode 100644 index 00000000..b20914f3 --- /dev/null +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/FanController.java @@ -0,0 +1,36 @@ +package com.myapp; + +import com.pi4j.io.pwm.Pwm; +import io.micronaut.context.annotation.Value; +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Get; +import jakarta.inject.Inject; + +@Controller("/fan") +public class FanController { + + private final FANHelper fanHelper; + + @Inject + public FanController(@Value("${pi4j.pwm.fan.address}") int fanPin, Pwm pwm) { + this.fanHelper = new FANHelper(pwm); + } + + @Get("/turnon") + public String startFan() { + fanHelper.start(); + return "Fan started at full speed"; + } + + @Get("/turndown") + public String stopFan() { + fanHelper.stop(); + return "Fan stopped"; + } + + @Get("/setspeed/{speed}") + public String setFanSpeed(int speed) { + fanHelper.setSpeed(speed); + return "Fan speed set to " + speed; + } +} diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java new file mode 100644 index 00000000..84a37d8b --- /dev/null +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java @@ -0,0 +1,38 @@ +package com.myapp; + +import com.pi4j.io.pwm.Pwm; +import com.pi4j.io.pwm.PwmConfig; +import com.pi4j.io.pwm.PwmType; +import com.pi4j.plugin.pigpio.provider.pwm.PiGpioPwmProviderImpl; +import com.pi4j.library.pigpio.PiGpio; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FANHelper { + + private static final Logger log = LoggerFactory.getLogger(FANHelper.class); + private final Pwm pwm; + + // Constructor that takes Pwm + public FANHelper(Pwm pwm) { + this.pwm = pwm; + } + + public void start() { + log.info("Starting Fan at full speed"); + pwm.on(1024); // 1024 is full speed (100% duty cycle) + } + + public void stop() { + log.info("Stopping Fan"); + pwm.off(); // Turn off the fan (0% duty cycle) + } + + public void setSpeed(int speed) { + if (speed < 0 || speed > 1024) { + throw new IllegalArgumentException("Speed must be between 0 and 1024"); + } + log.info("Setting fan speed to " + speed); + pwm.on(speed); // Adjust the duty cycle based on the speed + } +} diff --git a/pi4micronaut-utils/src/main/resources/application.yml b/pi4micronaut-utils/src/main/resources/application.yml index d0acad51..d3ed41f8 100644 --- a/pi4micronaut-utils/src/main/resources/application.yml +++ b/pi4micronaut-utils/src/main/resources/application.yml @@ -5,3 +5,13 @@ netty: default: allocator: max-order: 3 + +pi4j: + pwm: + fan: + name: FAN + address: 17 + initial: 0 + shutdown: 0 + provider: pigpio-pwm + pwm-type: hardware \ No newline at end of file diff --git a/pi4micronaut-utils/src/test/java/com/opensourcewithslu/FanController.java b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/FanController.java new file mode 100644 index 00000000..35b9b727 --- /dev/null +++ b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/FanController.java @@ -0,0 +1,4 @@ +package com.opensourcewithslu; + +public class FanController { +} diff --git a/pi4micronaut-utils/src/test/java/com/opensourcewithslu/mock/MockDigitalInput.java b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/mock/MockDigitalInput.java index b8edee75..340dac68 100644 --- a/pi4micronaut-utils/src/test/java/com/opensourcewithslu/mock/MockDigitalInput.java +++ b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/mock/MockDigitalInput.java @@ -7,7 +7,7 @@ import com.pi4j.io.gpio.digital.*; /* -The ockDigitalInput class implements the DigitalInput interface, which requires +The MockDigitalInput class implements the DigitalInput interface, which requires all methods to be implemented, even if they are not used in the mock. These methods have default or no operation implementations to fulfill the interface contract. they provided an empty method("return null") because the methods are required by the interface, From f73ccb0f02ef6c44d92b43bcafd09518b1159be5 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Mon, 30 Sep 2024 13:06:30 -0500 Subject: [PATCH 2/5] Modified according to the request changes --- .../inputdevices/FanController.java | 36 ------------------- .../outputdevices/FanHelper.java | 8 ++--- .../src/main/resources/application.yml | 12 +------ .../com/opensourcewithslu/FanController.java | 4 --- 4 files changed, 5 insertions(+), 55 deletions(-) delete mode 100644 pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/FanController.java delete mode 100644 pi4micronaut-utils/src/test/java/com/opensourcewithslu/FanController.java diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/FanController.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/FanController.java deleted file mode 100644 index b20914f3..00000000 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/FanController.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.myapp; - -import com.pi4j.io.pwm.Pwm; -import io.micronaut.context.annotation.Value; -import io.micronaut.http.annotation.Controller; -import io.micronaut.http.annotation.Get; -import jakarta.inject.Inject; - -@Controller("/fan") -public class FanController { - - private final FANHelper fanHelper; - - @Inject - public FanController(@Value("${pi4j.pwm.fan.address}") int fanPin, Pwm pwm) { - this.fanHelper = new FANHelper(pwm); - } - - @Get("/turnon") - public String startFan() { - fanHelper.start(); - return "Fan started at full speed"; - } - - @Get("/turndown") - public String stopFan() { - fanHelper.stop(); - return "Fan stopped"; - } - - @Get("/setspeed/{speed}") - public String setFanSpeed(int speed) { - fanHelper.setSpeed(speed); - return "Fan speed set to " + speed; - } -} diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java index 84a37d8b..95121d90 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java @@ -1,4 +1,4 @@ -package com.myapp; +package com.opensourcewithslu.outputdevices; import com.pi4j.io.pwm.Pwm; import com.pi4j.io.pwm.PwmConfig; @@ -8,13 +8,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class FANHelper { +public class FanHelper { - private static final Logger log = LoggerFactory.getLogger(FANHelper.class); + private static final Logger log = LoggerFactory.getLogger(FanHelper.class); private final Pwm pwm; // Constructor that takes Pwm - public FANHelper(Pwm pwm) { + public FanHelper(Pwm pwm) { this.pwm = pwm; } diff --git a/pi4micronaut-utils/src/main/resources/application.yml b/pi4micronaut-utils/src/main/resources/application.yml index d3ed41f8..068f0ced 100644 --- a/pi4micronaut-utils/src/main/resources/application.yml +++ b/pi4micronaut-utils/src/main/resources/application.yml @@ -4,14 +4,4 @@ micronaut: netty: default: allocator: - max-order: 3 - -pi4j: - pwm: - fan: - name: FAN - address: 17 - initial: 0 - shutdown: 0 - provider: pigpio-pwm - pwm-type: hardware \ No newline at end of file + max-order: 3 \ No newline at end of file diff --git a/pi4micronaut-utils/src/test/java/com/opensourcewithslu/FanController.java b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/FanController.java deleted file mode 100644 index 35b9b727..00000000 --- a/pi4micronaut-utils/src/test/java/com/opensourcewithslu/FanController.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.opensourcewithslu; - -public class FanController { -} From ee089ec5bf3b8e201df22473d1b792f211ca3a99 Mon Sep 17 00:00:00 2001 From: yrlmanoharreddy Date: Mon, 30 Sep 2024 21:04:57 -0500 Subject: [PATCH 3/5] Added Contributing.md file --- CONTRIBUTING.md | 142 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..c96d06f4 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,142 @@ +### Contribute to the Pi4Micronaut Library + +## Get Familiar with the Library + + # Before making contributions,understand the purpose and functionality of the Pi4Micronaut library. + + # Review the library documentation, any related articles, or tutorials. + +## Set Up Your Development Environment + + # Fork the library’s repository from the GitHub. + + # Clone your fork locally. + + # Follow setup instructions provided in the repository’s README or ADOC files. + +## Understand the Contribution Process + + # Familiarize yourself with the library’s contribution guidelines. + + # Understand the community guidelines. + + # Find out the preferred method of communication (e.g., issues, mailing list, discord). + +## Identify a Way to Contribute + + # Bug fixes: Look for open issues tagged as 'bug' or report new ones. + + # New features: Discuss new ideas before implementing, to gauge interest and get guidance. + + # Documentation: Contribute to the README, ADOC or other documentation. + + # Testing: Improve or expand the test suite. + + # Refactoring: Optimize existing code or improve its readability. + +## Making Changes + + # Always create a new branch for your changes. + + # Follow the library’s coding style and standards. + + # Write clean, well-documented code. + + # Add or update tests for your changes, if necessary. + + # Commit frequently with meaningful commit messages. + +## Test Your Changes + + # Ensure that all tests pass. + + # Manually test your changes for unforeseen issues. + + # Ensure your changes do not introduce regressions. + + # Use your own hardware to test the new component integration. + + # Note: A test suite will be developed in future to test the components without the use of external hardware + +## Signing the Contributor License Agreement + + # While creating a pull request, you’ll be prompted to sign a Contributor License Agreement. Please do so by logging in with your GitHub account. + +## Submit a Pull Request (PR) + + # Push your changes to your forked repository. Create a pull request from your branch to the main library’s main branch. + + # In the PR description, explain your changes, motivations, and any decisions made. + + # Link to any related issues or discussions. + +## Respond to Feedback + + # Maintainers or other contributors might provide feedback. Be open to suggestions and make necessary revisions. + + # Engage in a constructive dialogue to ensure the quality of the contribution. + +## Stay Updated + + # Keep your fork synchronized with the main repository to ease future contributions. + + # Regularly check for updates or changes in the library’s contribution guidelines. + +## Engage with the Community + + # Attend community meetings or join chat groups. + + # Help other contributors or users when you can. + + # Note: While your contribution is highly valued, there’s no guarantee that all pull requests will be merged. It depends on the library’s direction, quality of the contribution, and decisions of the maintainers. + +Thanks for considering a contribution to the Pi4Micronaut library! Your involvement helps make the project better for everyone. + + +### 3.1. How to Create a New Component + +If its compatible with a Raspberry Pi then it should work well with the Pi4Micronaut. The following steps should encompass how most components are added to the library. Start by creating a new Issue to suggest changes. + +## Determine the communication type for the component which you want to use. For example, Buzzer works with PWM and LCD1602 works with I2C. + +## Setup the circuit. + +## Add Component to the Application yml + + # The new component will need to be added to the application yml found at components/src/main/resources/application.yml. + + # More information on the application.yml found in Communicating with a Hardware Component + +## Create a Helper: + + # A Helper is what the Controller calls to do an action. For example, to change the color of an RGB LED the controller will take the request to change it. The Controller will then call the change color method in the helper. The helper then takes all the actions needed to change the color of the LED. + + # See the RBG Helper for an example of a Helper. + + # All Helpers should be kept here: pi4micronaut-utils\src\main\java\com\opensourcewithslu\(inputdevices or outputdevices) + +## Create a Controller: + + # Controllers define and handle interactions with a given component. The Controller of a component will have a @Controller("/example") right above the class declaration that acts as the endpoint for requests to the component. Instead of "example", you should name the endpoint something that is identifiable to the component. Each method of the Controller should have a @Get("/exampleEndPoint") above the method declaration. The endpoint for the method should have the same name as the method and any parameters should be included in the endpoint /exampleEndPoint/{parameter1},{parameter2}. + + # See the RGB Controller for an example of a Controller. + + # Consult the Micronaut Documentation for more explanation on Controllers. + + # All Controllers should be kept here: components\src\main\java\com\opensourcewithslu\components\controllers + +## Thoroughly test: + + # Contributors should thoroughly test their integrations + + # When submitting a pull request, make sure to include how you tested the component, any circuits that you may have used, and how to run any examples you may have created. + + # It is important that reviewers are able to replicated your work in order to properly test the implementation. + +## Create documentation for the component: + + # Create an .adoc file with the component name as the file name. + + # Make sure to include all the information that the other components. Simply copy/paste an existing components documentation and edit as needed. + + # Add the file here: pi4micronaut-utils/src/docs/asciidoc/components under either input or output components. \ No newline at end of file From 64eb45ce508df41252c5e8d20fb71a4ac271ff77 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Fri, 4 Oct 2024 19:43:38 -0500 Subject: [PATCH 4/5] Add fan control logic in Fancontroller.java and update application.yml --- .../components/controllers/FanController.java | 32 +++++++++++++++++++ components/src/main/resources/application.yml | 12 +++++-- 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 components/src/main/java/com/opensourcewithslu/components/controllers/FanController.java diff --git a/components/src/main/java/com/opensourcewithslu/components/controllers/FanController.java b/components/src/main/java/com/opensourcewithslu/components/controllers/FanController.java new file mode 100644 index 00000000..99d9ac23 --- /dev/null +++ b/components/src/main/java/com/opensourcewithslu/components/controllers/FanController.java @@ -0,0 +1,32 @@ +package com.opensourcewithslu.components.controllers; + +import com.opensourcewithslu.outputdevices.FanHelper; +import com.pi4j.io.pwm.Pwm; +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Get; +import io.micronaut.http.annotation.PathVariable; +import jakarta.inject.Named; + +@Controller("/fan") +public class FanController { + private final FanHelper fanHelper; + + public FanController(@Named("fan") Pwm fa) { + this.fanHelper = new FanHelper(fa); + } + + @Get("/start") + public void start() { + fanHelper.start(); + } + + @Get("/stop") + public void stop() { + fanHelper.stop(); + } + + @Get("/setSpeed/{speed}") + public void setSpeed(@PathVariable int speed) { + fanHelper.setSpeed(speed); + } +} \ No newline at end of file diff --git a/components/src/main/resources/application.yml b/components/src/main/resources/application.yml index 4599e380..9fff77ac 100644 --- a/components/src/main/resources/application.yml +++ b/components/src/main/resources/application.yml @@ -35,6 +35,15 @@ pi4j: provider: pigpio-pwm initial: 0 shutdown: 0 + fan: + name: FAN + address: 18 + initial: 0 + shutdown: 0 + provider: pigpio-pwm + pwm-type: hardware + + # end::pwm[] # tag::i2c[] @@ -179,5 +188,4 @@ pi4j: provider: pigpio-pwm initials: 0, 0, 0 shutdowns: 0, 0, 0 - # end::multiPWM[] - + # end::multiPWM[] \ No newline at end of file From 929bda9e9b48f86c8d056ace10e1c85a084bfb54 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Sat, 5 Oct 2024 15:11:32 -0500 Subject: [PATCH 5/5] Added FanHerlper and FanHelperTest classes --- .../outputdevices/FanHelper.java | 43 ++++++++++--- .../outputdevices/FanHelperTest.java | 63 +++++++++++++++++++ 2 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 pi4micronaut-utils/src/test/java/com/opensourcewithslu/outputdevices/FanHelperTest.java diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java index 95121d90..13b30752 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java @@ -1,38 +1,61 @@ package com.opensourcewithslu.outputdevices; import com.pi4j.io.pwm.Pwm; -import com.pi4j.io.pwm.PwmConfig; -import com.pi4j.io.pwm.PwmType; -import com.pi4j.plugin.pigpio.provider.pwm.PiGpioPwmProviderImpl; -import com.pi4j.library.pigpio.PiGpio; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.concurrent.TimeUnit; + +/** + * Helper class to control a fan using PWM (Pulse Width Modulation). + */ public class FanHelper { private static final Logger log = LoggerFactory.getLogger(FanHelper.class); private final Pwm pwm; - // Constructor that takes Pwm - public FanHelper(Pwm pwm) { - this.pwm = pwm; + /** + * Constructor to initialize the FanHelper with a PWM instance. + * + * @param fa the PWM instance to control the fan + */ + public FanHelper(Pwm fa) { + this.pwm = fa; } + /** + * Starts the fan at full speed (1024 duty cycle) for 10 seconds, then stops it. + */ public void start() { log.info("Starting Fan at full speed"); - pwm.on(1024); // 1024 is full speed (100% duty cycle) + try { + pwm.on(1024); // Full speed + TimeUnit.SECONDS.sleep(10); // Let the fan run for 10 seconds + pwm.off(); // Turn off the fan + } catch (InterruptedException e) { + log.error("Test interrupted", e); + } } + /** + * Stops the fan by turning off the PWM (0% duty cycle). + */ public void stop() { log.info("Stopping Fan"); pwm.off(); // Turn off the fan (0% duty cycle) } + /** + * Sets the fan speed by adjusting the PWM duty cycle. + * + * @param speed the desired fan speed (duty cycle), must be between 0 and 1024 + * @throws IllegalArgumentException if the speed is out of bounds + */ public void setSpeed(int speed) { if (speed < 0 || speed > 1024) { throw new IllegalArgumentException("Speed must be between 0 and 1024"); } - log.info("Setting fan speed to " + speed); + log.info("Setting fan speed to {}", speed); pwm.on(speed); // Adjust the duty cycle based on the speed } -} +} \ No newline at end of file diff --git a/pi4micronaut-utils/src/test/java/com/opensourcewithslu/outputdevices/FanHelperTest.java b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/outputdevices/FanHelperTest.java new file mode 100644 index 00000000..af57f3af --- /dev/null +++ b/pi4micronaut-utils/src/test/java/com/opensourcewithslu/outputdevices/FanHelperTest.java @@ -0,0 +1,63 @@ +package com.opensourcewithslu.outputdevices; +import com.pi4j.io.pwm.Pwm; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import static org.mockito.Mockito.*; + +class FanHelperTest { + + + @Mock + private Pwm pwm; // Mock Pwm class + + private FanHelper fanhelper; + + private AutoCloseable mocks; + @BeforeEach + void setUp() { + mocks = MockitoAnnotations.openMocks(this); + pwm = mock(Pwm.class); + fanhelper = new FanHelper(pwm); + } + + @AfterEach + void tearDown() throws Exception { + mocks.close(); + } + + @Test + void testStartFan() { + // Call the start() method + fanhelper.start(); + + // Verify that pwm.on(1024) is called to start the fan at full speed + verify(pwm, times(1)).on(1024); + + // Verify that pwm.off() is called to stop the fan after 10 seconds + verify(pwm, times(1)).off(); + } + + @Test + void testStopFan() { + // Call the stop() method + fanhelper.stop(); + + // Verify that pwm.off() is called + verify(pwm, times(1)).off(); + } + + @Test + void testSetFanSpeed() { + int speed = 512; + + // Call the setSpeed() method + fanhelper.setSpeed(speed); + + // Verify that pwm.on(speed) is called with the correct argument + verify(pwm, times(1)).on(speed); + } + +} \ No newline at end of file