001package jmri.jmrix.pi;
002
003import com.pi4j.io.gpio.GpioController;
004import com.pi4j.io.gpio.GpioFactory;
005
006import javax.annotation.CheckForNull;
007
008import jmri.jmrix.pi.simulator.GpioControllerSimulator;
009
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Provides an Adapter to allow the system connection memo and multiple
015 * RaspberryPi managers to be handled.
016 *
017 * @author Bob Jacobsen Copyright (C) 2001, 2002
018 * @author Paul Bender Copyright (C) 2015
019 */
020public class RaspberryPiAdapter extends jmri.jmrix.AbstractPortController {
021
022    private static boolean _isSimulator = false;
023
024    // in theory gpio can be static, because there will only ever
025    // be one, but the library handles the details that make it a
026    // singleton.
027    private GpioController gpio = null;
028
029    public RaspberryPiAdapter() {
030        this(false);
031    }
032
033    public RaspberryPiAdapter(boolean isSimulator) {
034        super(new RaspberryPiSystemConnectionMemo());
035        log.debug("RaspberryPi GPIO Adapter Constructor called");
036        setIsSimulator(isSimulator);
037        this.manufacturerName = RaspberryPiConnectionTypeList.PI;
038        try {
039            if (!isSimulator) {
040                gpio = GpioFactory.getInstance();
041            } else {
042                gpio = new GpioControllerSimulator();
043            }
044            opened = true;
045        } catch (UnsatisfiedLinkError er) {
046            log.error("Expected to run on Raspberry PI, but does not appear to be.");
047        }
048    }
049
050    public static boolean isSimulator() {
051        return _isSimulator;
052    }
053
054    private static void setIsSimulator(boolean isSimulator) {
055        _isSimulator = isSimulator;
056    }
057
058    @Override
059    public String getCurrentPortName() {
060        return "GPIO";
061    }
062
063    @Override
064    public void dispose() {
065        super.dispose();
066        gpio.shutdown(); // terminate all GPIO connections.
067    }
068
069    @Override
070    public void connect() {
071    }
072
073    @Override
074    public void configure() {
075        this.getSystemConnectionMemo().configureManagers();
076    }
077
078    @Override
079    public java.io.DataInputStream getInputStream() {
080        return null;
081    }
082
083    @Override
084    public java.io.DataOutputStream getOutputStream() {
085        return null;
086    }
087
088    @Override
089    public RaspberryPiSystemConnectionMemo getSystemConnectionMemo() {
090        return (RaspberryPiSystemConnectionMemo) super.getSystemConnectionMemo();
091    }
092
093    @Override
094    public void recover() {
095    }
096
097    /*
098    * Get the GPIO Controller associated with this object.
099    *
100    * @return the associaed GPIO Controller or null if none exists
101     */
102    @CheckForNull
103    public GpioController getGPIOController() {
104        return gpio;
105    }
106
107    private final static Logger log = LoggerFactory.getLogger(RaspberryPiAdapter.class);
108
109}