-
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
* touch switch logger typo fix * draft of micro switch helper, controller and application.yml * fix injection in controller micro switch * added controller comments Signed-off-by: Adrian Swindle <[email protected]> * debounced updated Signed-off-by: Adrian Swindle <[email protected]> --------- Signed-off-by: Adrian Swindle <[email protected]>
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...nts/src/main/java/com/opensourcewithslu/components/controllers/MicroSwitchController.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,49 @@ | ||
package com.opensourcewithslu.components.controllers; | ||
|
||
import com.opensourcewithslu.inputdevices.MicroSwitchHelper; | ||
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("/microSwitch") | ||
public class MicroSwitchController { | ||
|
||
private final MicroSwitchHelper microSwitchHelper; | ||
|
||
private final LEDHelper ledHelper1; | ||
private final LEDHelper ledHelper2; | ||
|
||
|
||
public MicroSwitchController(@Named("micro-switch") DigitalInput microSwitch, | ||
@Named("led") DigitalOutput led1, | ||
@Named("led2") DigitalOutput led2) { | ||
this.microSwitchHelper = new MicroSwitchHelper(microSwitch); | ||
this.ledHelper1 = new LEDHelper(led1); | ||
this.ledHelper2 = new LEDHelper(led2); | ||
} | ||
//enables the micro switch. The LEDs will switch states as the switch is pressed | ||
@Get("/enable") | ||
public void enableMicroSwitch() { | ||
microSwitchHelper.addEventListener(e -> { | ||
if (microSwitchHelper.isPressed) { | ||
ledHelper1.ledOff(); | ||
ledHelper2.ledOn(); | ||
|
||
} | ||
else { | ||
ledHelper1.ledOn(); | ||
ledHelper2.ledOff(); | ||
} | ||
}); | ||
} | ||
//disable the micro switch | ||
@Get("/disable") | ||
public void disableMicroSwitch() { | ||
microSwitchHelper.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
67 changes: 67 additions & 0 deletions
67
pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/MicroSwitchHelper.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 MicroSwitchHelper class is used to initialize a micro switch. | ||
*/ | ||
public class MicroSwitchHelper { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(MicroSwitchHelper.class); | ||
|
||
private final DigitalInput microSwitchInput; | ||
|
||
private DigitalStateChangeListener microSwitchInputListener; | ||
|
||
/** | ||
* Shows if the micro switch has been pressed. | ||
*/ | ||
public boolean isPressed; | ||
|
||
/** | ||
* MicroSwitchHelper constructor. | ||
* @param microSwitchInput A Pi4J DigitalInput object. | ||
*/ | ||
public MicroSwitchHelper(DigitalInput microSwitchInput) | ||
{ | ||
this.microSwitchInput = microSwitchInput; | ||
this.isPressed = microSwitchInput.isHigh(); | ||
|
||
initialize(); | ||
} | ||
|
||
/** | ||
* Initializes the listener that keeps track of if the micro switch has been pressed or not. It is automatically called when the MicroSwitchHelper is instantiated. | ||
*/ | ||
public void initialize() | ||
{ | ||
log.info("Initializing Micro Switch"); | ||
|
||
microSwitchInputListener = e-> isPressed = microSwitchInput.isHigh(); | ||
microSwitchInput.addListener(microSwitchInputListener); | ||
} | ||
|
||
/** | ||
* Adds an event listener to the micro switch. | ||
* @param function A Pi4J DigitalStateChangeListener object. | ||
*/ | ||
public void addEventListener(DigitalStateChangeListener function) | ||
{ | ||
microSwitchInputListener = function; | ||
microSwitchInput.addListener(microSwitchInputListener); | ||
} | ||
|
||
/** | ||
* Removes the event listener from the micro switch. | ||
*/ | ||
public void removeEventListener() | ||
{ | ||
if (microSwitchInputListener != null) { | ||
microSwitchInput.removeListener(microSwitchInputListener); | ||
microSwitchInputListener = null; | ||
} | ||
} | ||
} |