-
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.
Browse files
Browse the repository at this point in the history
* tilt switch controller and helper * added javadocs to helper * fixed error in TouchSwitchHelper * added comments to controller
- Loading branch information
Showing
4 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...ents/src/main/java/com/opensourcewithslu/components/controllers/TiltSwitchController.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,44 @@ | ||
package com.opensourcewithslu.components.controllers; | ||
|
||
import com.opensourcewithslu.inputdevices.TiltSwitchHelper; | ||
import com.opensourcewithslu.outputdevices.LEDHelper; | ||
import com.pi4j.io.gpio.digital.DigitalInput; | ||
import com.pi4j.io.gpio.digital.DigitalOutput; | ||
import io.micronaut.http.annotation.Controller; | ||
import io.micronaut.http.annotation.Get; | ||
import jakarta.inject.Named; | ||
|
||
//tag::ex[] | ||
@Controller("/tiltSwitch") | ||
public class TiltSwitchController { | ||
|
||
private final TiltSwitchHelper tiltSwitchHelper; // controls the tilt switch | ||
private final LEDHelper ledHelper; // controls the LED | ||
|
||
public TiltSwitchController(@Named("tilt-switch-input")DigitalInput tiltSwitch, | ||
@Named("led2")DigitalOutput led) { | ||
this.tiltSwitchHelper = new TiltSwitchHelper(tiltSwitch); | ||
this.ledHelper = new LEDHelper(led); | ||
} | ||
|
||
// The 'enable' method adds an event listener that listens for changes in the tilt switch | ||
// If it is tilted, the LED turns on and off otherwise | ||
@Get("/enable") | ||
public void enableTiltSwitch() { | ||
tiltSwitchHelper.addEventListener(e -> { | ||
if (tiltSwitchHelper.isTilted) { | ||
ledHelper.ledOn(); | ||
} | ||
else { | ||
ledHelper.ledOff(); | ||
} | ||
}); | ||
} | ||
|
||
// The disable method removes the event listener | ||
@Get("/disable") | ||
public void disableTiltSwitch() { | ||
tiltSwitchHelper.removeEventListener(); | ||
} | ||
} | ||
//end::ex[] |
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
66 changes: 66 additions & 0 deletions
66
pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/TiltSwitchHelper.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,66 @@ | ||
package com.opensourcewithslu.inputdevices; | ||
|
||
import com.pi4j.io.gpio.digital.DigitalInput; | ||
import com.pi4j.io.gpio.digital.DigitalStateChangeListener; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Helper class to control a tilt switch using Digital Input. | ||
* This class provides methods to initialize a tilt switch by adding or removing an event listener. | ||
*/ | ||
public class TiltSwitchHelper { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(TiltSwitchHelper.class); | ||
private final DigitalInput tiltSwitchInput; | ||
private DigitalStateChangeListener tiltSwitchInputListener; | ||
/** | ||
* Boolean variable that is true if the tilt switch is being tilted and false otherwise. | ||
*/ | ||
public boolean isTilted; | ||
|
||
/** | ||
* TiltSwitchHelper constructor. | ||
* @param tiltSwitchInput A Pi4J DigitalInput object. | ||
*/ | ||
public TiltSwitchHelper(DigitalInput tiltSwitchInput) | ||
{ | ||
this.tiltSwitchInput = tiltSwitchInput; | ||
this.isTilted = tiltSwitchInput.isHigh(); | ||
|
||
initialize(); | ||
} | ||
|
||
/** | ||
* Initializes the listener that keeps track of if the tilt switch is tilted. | ||
* It is automatically called when the TiltSwitchHelper is instantiated. | ||
*/ | ||
private void initialize() | ||
{ | ||
log.info("Initializing Tilt Switch"); | ||
|
||
tiltSwitchInputListener = e-> isTilted = tiltSwitchInput.isHigh(); | ||
tiltSwitchInput.addListener(tiltSwitchInputListener); | ||
} | ||
|
||
/** | ||
* Adds an event listener to the tilt switch. | ||
* @param function A Pi4J DigitalStateChangeListener object. | ||
*/ | ||
public void addEventListener(DigitalStateChangeListener function) | ||
{ | ||
tiltSwitchInputListener = function; | ||
tiltSwitchInput.addListener(tiltSwitchInputListener); | ||
} | ||
|
||
/** | ||
* Removes the event listener from the tilt switch. | ||
*/ | ||
public void removeEventListener() | ||
{ | ||
if (tiltSwitchInputListener != null) { | ||
tiltSwitchInput.removeListener(tiltSwitchInputListener); | ||
tiltSwitchInputListener = null; | ||
} | ||
} | ||
} |
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