Skip to content

Commit

Permalink
Completes #233 :Micro Switch Implementation (#234)
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
SwindleA authored May 1, 2024
1 parent 161f75f commit 635f3b6
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
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[]
6 changes: 6 additions & 0 deletions components/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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;
}
}
}

0 comments on commit 635f3b6

Please sign in to comment.