Instantly share code, notes, and snippets.
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save ymakino/10013793 to your computer and use it in GitHub Desktop.
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
package emergencybutton; | |
import com.pi4j.io.gpio.GpioController; | |
import com.pi4j.io.gpio.GpioFactory; | |
import com.pi4j.io.gpio.GpioPinDigitalInput; | |
import com.pi4j.io.gpio.GpioPinDigitalOutput; | |
import com.pi4j.io.gpio.PinPullResistance; | |
import com.pi4j.io.gpio.PinState; | |
import com.pi4j.io.gpio.RaspiPin; | |
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent; | |
import com.pi4j.io.gpio.event.GpioPinListenerDigital; | |
import echowand.common.EPC; | |
import echowand.info.EmergencyButtonInfo; | |
import echowand.info.NodeProfileInfo; | |
import echowand.logic.MainLoop; | |
import echowand.logic.RequestDispatcher; | |
import echowand.logic.TooManyObjectsException; | |
import echowand.logic.TransactionManager; | |
import echowand.net.Inet4Subnet; | |
import echowand.net.SubnetException; | |
import echowand.object.AnnounceRequestProcessor; | |
import echowand.object.LocalObject; | |
import echowand.object.LocalObjectDateTimeDelegate; | |
import echowand.object.LocalObjectDefaultDelegate; | |
import echowand.object.LocalObjectManager; | |
import echowand.object.LocalObjectNotifyDelegate; | |
import echowand.object.NodeProfileObjectDelegate; | |
import echowand.object.ObjectData; | |
import echowand.object.RemoteObjectManager; | |
import echowand.object.SetGetRequestProcessor; | |
import echowand.util.ConstraintByte; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author NaTcH | |
* @author Yoshiki Makino | |
*/ | |
class EmergencyButtonController { | |
private LocalObject object; | |
private GpioPinDigitalOutput gpioOutput; | |
static final ObjectData STATE_FOUND = new ObjectData ((byte)0x41); | |
static final ObjectData STATE_NOTFOUND = new ObjectData ((byte)0x42); | |
public EmergencyButtonController(LocalObject object, GpioPinDigitalOutput out){ | |
this.object = object; | |
this.gpioOutput = out; | |
} | |
public synchronized void setEmergency(boolean emergency){ | |
if (emergency == isEmergency()) { | |
return; | |
} | |
if (emergency) { | |
object.forceSetData(EPC.xB1, STATE_FOUND); | |
gpioOutput.blink(100); | |
} else { | |
object.forceSetData(EPC.xB1, STATE_NOTFOUND); | |
gpioOutput.blink(0); | |
gpioOutput.setState(PinState.LOW); | |
} | |
} | |
public boolean isEmergency(){ | |
return getStateData().equals(STATE_FOUND); | |
} | |
public ObjectData getStateData(){ | |
return object.getData(EPC.xB1); | |
} | |
} | |
class EmergencyButtonDataDelegate extends LocalObjectDefaultDelegate { | |
private EmergencyButtonController emergencyBtn; | |
public EmergencyButtonDataDelegate(EmergencyButtonController emergencyBtn) { | |
this.emergencyBtn = emergencyBtn; | |
} | |
@Override | |
public void setData(SetState result, LocalObject obj, EPC epc, ObjectData newData, ObjectData curData){ | |
if (epc == EPC.xBF){ | |
// if reset status is setted | |
if(Integer.parseInt(newData.toString())==0) { | |
System.out.println("==== Reset State ===="); | |
emergencyBtn.setEmergency(false); | |
} | |
} | |
} | |
} | |
class EmergencyButtonListener implements GpioPinListenerDigital { | |
EmergencyButtonController emergencyBtn; | |
public EmergencyButtonListener(EmergencyButtonController emergencyBtn) { | |
this.emergencyBtn = emergencyBtn; | |
} | |
@Override | |
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { | |
if (event.getState().isHigh() && !emergencyBtn.isEmergency()) { | |
System.out.println("!!!!!!!! ALERT !!!!!!!!"); | |
emergencyBtn.setEmergency(true); | |
} | |
} | |
} | |
class ResetEmergencyButtonListener implements GpioPinListenerDigital { | |
EmergencyButtonController emergencyBtn; | |
public ResetEmergencyButtonListener(EmergencyButtonController button) { | |
this.emergencyBtn = button; | |
} | |
@Override | |
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { | |
// System.out.println("resetPin : "+resetPin.getState().getValue()); | |
if (event.getState().isLow()) { | |
emergencyBtn.setEmergency(false); | |
} | |
} | |
} | |
public class EmergencyButton implements Runnable{ | |
private Inet4Subnet subnet; | |
private TransactionManager transactionManager; | |
private RemoteObjectManager remoteManager; | |
private LocalObjectManager localManager; | |
private RequestDispatcher dispatcher; | |
final GpioController gpio; | |
final GpioPinDigitalInput gpioPin; | |
final GpioPinDigitalInput resetPin; | |
final GpioPinDigitalOutput outputPin; | |
public EmergencyButton() { | |
gpio = GpioFactory.getInstance(); | |
gpioPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_DOWN); | |
resetPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05, PinPullResistance.PULL_UP); | |
outputPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, PinState.LOW); | |
outputPin.setShutdownOptions(true, PinState.LOW, PinPullResistance.OFF); | |
} | |
public NodeProfileInfo getNodeProfileInfo() { | |
NodeProfileInfo info = new NodeProfileInfo(); | |
info.add(EPC.x97, true, false, false, 2); | |
info.add(EPC.x98, true, false, false, 4); | |
return info; | |
} | |
public EmergencyButtonInfo getEmergencyButtonInfo() { | |
EmergencyButtonInfo info = new EmergencyButtonInfo(); | |
ConstraintByte constraint = new ConstraintByte((byte) 0x00); | |
info.add(EPC.xBF, true, true, false, new byte[]{0x00}, constraint); | |
return info; | |
} | |
private void initialize() throws SubnetException { | |
subnet = new Inet4Subnet(); | |
// subnet.startService(); | |
transactionManager = new TransactionManager(subnet); | |
remoteManager = new RemoteObjectManager(); | |
localManager = new LocalObjectManager(); | |
dispatcher = new RequestDispatcher(); | |
dispatcher.addRequestProcessor(new SetGetRequestProcessor(localManager)); | |
dispatcher.addRequestProcessor(new AnnounceRequestProcessor(localManager, remoteManager)); | |
} | |
private void createNodeProfileObject() throws TooManyObjectsException { | |
NodeProfileInfo nodeProfileInfo = getNodeProfileInfo(); | |
LocalObject nodeProfileObject = new LocalObject(nodeProfileInfo); | |
nodeProfileObject.addDelegate(new LocalObjectNotifyDelegate(subnet, transactionManager)); | |
nodeProfileObject.addDelegate(new NodeProfileObjectDelegate(localManager)); | |
nodeProfileObject.addDelegate(new LocalObjectDateTimeDelegate()); | |
localManager.add(nodeProfileObject); | |
} | |
private void createEmergencyButton() throws TooManyObjectsException { | |
EmergencyButtonInfo emergencyButtonInfo = getEmergencyButtonInfo(); | |
LocalObject emergencyBtnObject = new LocalObject(emergencyButtonInfo); | |
EmergencyButtonController emergencyBtn = new EmergencyButtonController(emergencyBtnObject,outputPin); | |
emergencyBtnObject.addDelegate(new LocalObjectNotifyDelegate(subnet, transactionManager)); | |
emergencyBtnObject.addDelegate(new EmergencyButtonDataDelegate(emergencyBtn)); | |
gpioPin.addListener(new EmergencyButtonListener(emergencyBtn)); | |
resetPin.addListener(new ResetEmergencyButtonListener(emergencyBtn)); | |
localManager.add(emergencyBtnObject); | |
new EmergencyButtonThread(emergencyBtn).start(); | |
} | |
private void startMainThread() { | |
MainLoop mainLoop = new MainLoop(); | |
mainLoop.setSubnet(subnet); | |
mainLoop.addListener(transactionManager); | |
mainLoop.addListener(dispatcher); | |
Thread mainThread = new Thread(mainLoop); | |
// mainThread.setDaemon(true); | |
System.out.println("Start MainLoop"); | |
mainThread.start(); | |
} | |
@Override | |
public void run() { | |
try { | |
initialize(); | |
createNodeProfileObject(); | |
createEmergencyButton(); | |
// System.out.println("MainThread..."); | |
startMainThread(); | |
} catch (Exception ex) { | |
Logger.getLogger(EmergencyButton.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
public static void main(String[] args) throws InterruptedException { | |
EmergencyButton btn = new EmergencyButton(); | |
Thread mainThread = new Thread(btn); | |
// System.out.println("Thread Start..."); | |
mainThread.start(); | |
} | |
private class EmergencyButtonThread extends Thread { | |
private EmergencyButtonController emergencyBtn; | |
public EmergencyButtonThread(EmergencyButtonController emergencyBtn) { | |
this.emergencyBtn = emergencyBtn; | |
} | |
public void run() { | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException ex) { | |
// do nothing | |
} | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
for (;;) { | |
try { | |
System.out.print("> "); | |
String line = reader.readLine().trim(); | |
if (line.toLowerCase().equals("reset")) { | |
emergencyBtn.setEmergency(false); | |
System.out.println("OK"); | |
} else if (line.toLowerCase().equals("push")) { | |
emergencyBtn.setEmergency(true); | |
System.out.println("OK"); | |
} else if (line.toLowerCase().equals("quit")) { | |
System.exit(0); | |
} else { | |
System.out.println("Command list:"); | |
System.out.println("reset) Reset the emergency button"); | |
System.out.println("push) Push the emergency button"); | |
System.out.println("quit) Quit this program"); | |
} | |
} catch (IOException ex) { | |
Logger.getLogger(EmergencyButton.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment