Last active
August 29, 2015 14:23
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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