Skip to content

Instantly share code, notes, and snippets.

@markuskeller1960
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markuskeller1960/76239e945d5fcd7111ec to your computer and use it in GitHub Desktop.
Save markuskeller1960/76239e945d5fcd7111ec to your computer and use it in GitHub Desktop.
The Button class controls a button on a raspberry PI board using Pi4J. In the observer pattern this class represents the subject.
/**
* The Button class controls a button on a raspberry PI board using
* Pi4J. In the observer pattern this class represents the subject.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Markus Keller
* @version 0.1
* @since 2014-06-27
*
*/
package ch.myraspberry.led.observer.component;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
public class Button extends Component {
public enum ButtonStatus {
DOWN, UP
}
private final GpioController gpio = GpioFactory.getInstance();
private final GpioPinDigitalInput button = gpio.provisionDigitalInputPin(
RaspiPin.GPIO_02, "button", PinPullResistance.PULL_DOWN);
private ButtonStatus status = ButtonStatus.UP;
private boolean pressed = false;
public Button() {
super();
addButtonListener();
}
@Override
public void terminate() {
gpio.shutdown();
}
public boolean isPressed() {
return pressed;
}
private void addButtonListener() {
button.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(
GpioPinDigitalStateChangeEvent event) {
handleButtonPressed(event);
}
});
}
@Override
public void notifyObservers(Object state) {
super.notifyObservers(state);
clearChanged();
}
@Override
protected void clearChanged() {
pressed = false;
super.clearChanged();
}
private void handleButtonPressed(GpioPinDigitalStateChangeEvent event) {
if (event.getState().isHigh()) {
status = ButtonStatus.DOWN;
} else if (event.getState().isLow() && status == ButtonStatus.DOWN) {
pressed = true;
status = ButtonStatus.UP;
} else if (event.getState().isLow()) {
}
notifyObservers(status);
clearChanged();
}
}
/**
* The Component class is the super class for all components that control
* digital components on a Raspbery PI board. In the observer pattern this
* class represents the subject.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Markus Keller
* @version 0.1
* @since 2014-06-27
*
*/
package ch.myraspberry.led.observer.component;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
abstract class Component extends Observable {
private List<Observer> observers = new ArrayList<>();
private boolean changed = false;
public abstract void terminate();
@Override
public void addObserver(Observer observer) {
if (!observers.contains(observer)) {
observers.add(observer);
}
}
@Override
public void deleteObserver(Observer observer) {
observers.remove(observer);
}
@Override
public int countObservers() {
return observers.size();
}
@Override
public void deleteObservers() {
observers.clear();
}
@Override
public boolean hasChanged() {
return changed;
}
@Override
public void notifyObservers() {
notifyComponentObservers(null);
}
@Override
public void notifyObservers(Object status) {
notifyComponentObservers(status);
}
@Override
protected void setChanged() {
changed = true;
}
@Override
protected void clearChanged() {
changed = false;
}
private void notifyComponentObservers(Object status) {
observers.forEach(o -> o.update(this, status));
setChanged();
}
}
/**
* The Led class controls a LED on a raspberry PI board using
* Pi4J. In the observer pattern this class represents the subject.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Markus Keller
* @version 0.1
* @since 2014-06-27
*
*/
package ch.myraspberry.led.observer.component;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
public class Led extends Component {
public enum LEDStatus {
ON, OFF
}
private final GpioController gpio = GpioFactory.getInstance();
private final GpioPinDigitalOutput led = gpio.provisionDigitalOutputPin(
RaspiPin.GPIO_01, "led", PinState.LOW);
private boolean off;
private LEDStatus status;
public Led() {
super();
led.setShutdownOptions(true, PinState.LOW, PinPullResistance.OFF);
turnOff();
}
@Override
public void terminate() {
gpio.shutdown();
}
public void turnOff() {
led.setState(PinState.LOW);
off = true;
status = LEDStatus.OFF;
notifyObservers(status);
}
public void turnOn() {
led.setState(PinState.HIGH);
off = false;
status = LEDStatus.ON;
notifyObservers(status);
}
public void toggle() {
led.toggle();
off = !off;
status = off ? status = LEDStatus.OFF : LEDStatus.ON;
notifyObservers(status);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment