-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
308 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
32 changes: 32 additions & 0 deletions
32
components/src/main/java/com/opensourcewithslu/components/controllers/FanController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/FanHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.opensourcewithslu.outputdevices; | ||
|
||
import com.pi4j.io.pwm.Pwm; | ||
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 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"); | ||
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); | ||
pwm.on(speed); // Adjust the duty cycle based on the speed | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ micronaut: | |
netty: | ||
default: | ||
allocator: | ||
max-order: 3 | ||
max-order: 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
pi4micronaut-utils/src/test/java/com/opensourcewithslu/outputdevices/FanHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |