diff --git a/components/src/main/java/com/opensourcewithslu/components/controllers/MicroSwitchController.java b/components/src/main/java/com/opensourcewithslu/components/controllers/MicroSwitchController.java new file mode 100644 index 00000000..4f7791ea --- /dev/null +++ b/components/src/main/java/com/opensourcewithslu/components/controllers/MicroSwitchController.java @@ -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[] diff --git a/components/src/main/resources/application.yml b/components/src/main/resources/application.yml index 64e3dc34..4599e380 100644 --- a/components/src/main/resources/application.yml +++ b/components/src/main/resources/application.yml @@ -89,6 +89,12 @@ pi4j: pull: PULL_DOWN debounce: 200000 provider: pigpio-digital-input + micro-switch: + name: Micro Switch + address: 19 + pull: PULL_DOWN + debounce: 200000 + provider: pigpio-digital-input pir-sensor: name: PIR Sensor address: 13 diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/MicroSwitchHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/MicroSwitchHelper.java new file mode 100644 index 00000000..13c0e430 --- /dev/null +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/MicroSwitchHelper.java @@ -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; + } + } +}