-
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.
Touch Switch Implementation Issue 144 (#152)
* begin implementing touch switch helper and controller * added yml changes and initController method changes * added disableController method changes * testing changes for touch switch * added removeEventListener * adding javadoc comments for touch switch helper --------- Co-authored-by: Ruthvik Mannem <[email protected]> Co-authored-by: ruthvikm <[email protected]>
- Loading branch information
1 parent
e31aa11
commit d6faed1
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...nts/src/main/java/com/opensourcewithslu/components/controllers/TouchSwitchController.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,40 @@ | ||
package com.opensourcewithslu.components.controllers; | ||
|
||
import com.opensourcewithslu.inputdevices.TouchSwitchHelper; | ||
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; | ||
|
||
@Controller("/touchSwitch") | ||
public class TouchSwitchController { | ||
|
||
private final TouchSwitchHelper touchSwitchHelper; | ||
|
||
private final LEDHelper ledHelper; | ||
|
||
public TouchSwitchController(@Named("touch-switch-input") DigitalInput touchSwitch, | ||
@Named("led2") DigitalOutput led) { | ||
this.touchSwitchHelper = new TouchSwitchHelper(touchSwitch); | ||
this.ledHelper = new LEDHelper(led); | ||
} | ||
|
||
@Get("/enable") | ||
public void enableTouchSwitch() { | ||
touchSwitchHelper.addEventListener(e -> { | ||
if (touchSwitchHelper.isTouched) { | ||
ledHelper.ledOn(); | ||
} | ||
else { | ||
ledHelper.ledOff(); | ||
} | ||
}); | ||
} | ||
|
||
@Get("/disable") | ||
public void disableTouchSwitch() { | ||
touchSwitchHelper.removeEventListener(); | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
micronautpi4j-utils/src/main/java/com/opensourcewithslu/inputdevices/TouchSwitchHelper.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,67 @@ | ||
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; | ||
|
||
/** | ||
* The TouchSwitchHelper class is used to initialize a touch switch. | ||
*/ | ||
public class TouchSwitchHelper { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(SlideSwitchHelper.class); | ||
|
||
private final DigitalInput touchSwitchInput; | ||
|
||
private DigitalStateChangeListener touchSwitchInputListener; | ||
|
||
/** | ||
* Shows if the touch switch has been touched. | ||
*/ | ||
public boolean isTouched; | ||
|
||
/** | ||
* TouchSwitchHelper constructor. | ||
* @param touchSwitchInput A Pi4J DigitalInput object. | ||
*/ | ||
public TouchSwitchHelper(DigitalInput touchSwitchInput) | ||
{ | ||
this.touchSwitchInput = touchSwitchInput; | ||
this.isTouched = touchSwitchInput.isHigh(); | ||
|
||
initialize(); | ||
} | ||
|
||
/** | ||
* Initializes the listener that keeps track of if the touch switch has been touched or not. It is automatically called when the TouchSwitchHelper is instantiated. | ||
*/ | ||
public void initialize() | ||
{ | ||
log.info("Initializing Touch Switch"); | ||
|
||
touchSwitchInputListener = e-> isTouched = touchSwitchInput.isHigh(); | ||
touchSwitchInput.addListener(touchSwitchInputListener); | ||
} | ||
|
||
/** | ||
* Adds an event listener to the touch switch. | ||
* @param function A Pi4J DigitalStateChangeListener object. | ||
*/ | ||
public void addEventListener(DigitalStateChangeListener function) | ||
{ | ||
touchSwitchInputListener = function; | ||
touchSwitchInput.addListener(touchSwitchInputListener); | ||
} | ||
|
||
/** | ||
* Removes the event listener from the touch switch. | ||
*/ | ||
public void removeEventListener() | ||
{ | ||
if (touchSwitchInputListener != null) { | ||
touchSwitchInput.removeListener(touchSwitchInputListener); | ||
touchSwitchInputListener = null; | ||
} | ||
} | ||
} |