Skip to content

Commit

Permalink
Touch Switch Implementation Issue 144 (#152)
Browse files Browse the repository at this point in the history
* 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
3 people authored Feb 19, 2024
1 parent e31aa11 commit d6faed1
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
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();
}
}
6 changes: 6 additions & 0 deletions components/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ pi4j:
pull: PULL_DOWN
debounce: 3000
provider: pigpio-digital-input
touch-switch-input:
name: Touch Switch Input
address: 17
pull: PULL_DOWN
debounce: 200000
provider: pigpio-digital-input
### DIGITAL OUTPUTS
digital-output:
# tag::digitalOutput[]
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 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;
}
}
}

0 comments on commit d6faed1

Please sign in to comment.