Skip to content

Commit

Permalink
Tilt Switch implementation. Issue #40 (#232)
Browse files Browse the repository at this point in the history
* tilt switch controller and helper

* added javadocs to helper

* fixed error in TouchSwitchHelper

* added comments to controller
  • Loading branch information
joefol authored May 1, 2024
1 parent 3bd63e8 commit 161f75f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.opensourcewithslu.components.controllers;

import com.opensourcewithslu.inputdevices.TiltSwitchHelper;
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("/tiltSwitch")
public class TiltSwitchController {

private final TiltSwitchHelper tiltSwitchHelper; // controls the tilt switch
private final LEDHelper ledHelper; // controls the LED

public TiltSwitchController(@Named("tilt-switch-input")DigitalInput tiltSwitch,
@Named("led2")DigitalOutput led) {
this.tiltSwitchHelper = new TiltSwitchHelper(tiltSwitch);
this.ledHelper = new LEDHelper(led);
}

// The 'enable' method adds an event listener that listens for changes in the tilt switch
// If it is tilted, the LED turns on and off otherwise
@Get("/enable")
public void enableTiltSwitch() {
tiltSwitchHelper.addEventListener(e -> {
if (tiltSwitchHelper.isTilted) {
ledHelper.ledOn();
}
else {
ledHelper.ledOff();
}
});
}

// The disable method removes the event listener
@Get("/disable")
public void disableTiltSwitch() {
tiltSwitchHelper.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 @@ -101,6 +101,12 @@ pi4j:
pull: PULL_DOWN
debounce: 3000
provider: pigpio-digital-input
tilt-switch-input:
name: Tilt Switch Input
address: 17
pull: PULL_DOWN
debounce: 5000
provider: pigpio-digital-input
# end::digitalInput[]

# tag::digitalOutput[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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;

/**
* Helper class to control a tilt switch using Digital Input.
* This class provides methods to initialize a tilt switch by adding or removing an event listener.
*/
public class TiltSwitchHelper {

private static final Logger log = LoggerFactory.getLogger(TiltSwitchHelper.class);
private final DigitalInput tiltSwitchInput;
private DigitalStateChangeListener tiltSwitchInputListener;
/**
* Boolean variable that is true if the tilt switch is being tilted and false otherwise.
*/
public boolean isTilted;

/**
* TiltSwitchHelper constructor.
* @param tiltSwitchInput A Pi4J DigitalInput object.
*/
public TiltSwitchHelper(DigitalInput tiltSwitchInput)
{
this.tiltSwitchInput = tiltSwitchInput;
this.isTilted = tiltSwitchInput.isHigh();

initialize();
}

/**
* Initializes the listener that keeps track of if the tilt switch is tilted.
* It is automatically called when the TiltSwitchHelper is instantiated.
*/
private void initialize()
{
log.info("Initializing Tilt Switch");

tiltSwitchInputListener = e-> isTilted = tiltSwitchInput.isHigh();
tiltSwitchInput.addListener(tiltSwitchInputListener);
}

/**
* Adds an event listener to the tilt switch.
* @param function A Pi4J DigitalStateChangeListener object.
*/
public void addEventListener(DigitalStateChangeListener function)
{
tiltSwitchInputListener = function;
tiltSwitchInput.addListener(tiltSwitchInputListener);
}

/**
* Removes the event listener from the tilt switch.
*/
public void removeEventListener()
{
if (tiltSwitchInputListener != null) {
tiltSwitchInput.removeListener(tiltSwitchInputListener);
tiltSwitchInputListener = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public class TouchSwitchHelper {

private static final Logger log = LoggerFactory.getLogger(SlideSwitchHelper.class);
private static final Logger log = LoggerFactory.getLogger(TouchSwitchHelper.class);

private final DigitalInput touchSwitchInput;

Expand All @@ -36,7 +36,7 @@ public TouchSwitchHelper(DigitalInput touchSwitchInput)
/**
* 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()
private void initialize()
{
log.info("Initializing Touch Switch");

Expand Down

0 comments on commit 161f75f

Please sign in to comment.