Advertisement
Guest User

Keypad

a guest
May 14th, 2015
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.26 KB | None | 0 0
  1. package application;
  2.  
  3. import com.pi4j.io.gpio.GpioController;
  4. import com.pi4j.io.gpio.GpioFactory;
  5. import com.pi4j.io.gpio.GpioPinDigitalInput;
  6. import com.pi4j.io.gpio.GpioPinDigitalOutput;
  7. import com.pi4j.io.gpio.Pin;
  8. import com.pi4j.io.gpio.PinPullResistance;
  9. import com.pi4j.io.gpio.PinState;
  10. import com.pi4j.io.gpio.RaspiPin;
  11. import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
  12. import com.pi4j.io.gpio.event.GpioPinListenerDigital;
  13.  
  14. import java.beans.PropertyChangeEvent;
  15. import java.beans.PropertyChangeListener;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18.  
  19. public class KeypadTest {
  20.  
  21.    private List<PropertyChangeListener> listener = new ArrayList<PropertyChangeListener>();
  22.    public static final String KEY = "key";
  23.    private char keyPressed;
  24.  
  25.    /** The Constant KEYPAD. */
  26.    private static final char layout[][] = {
  27.             { '1', '2', '3' },
  28.             { '4', '5', '6' },
  29.             { '7', '8', '9' },
  30.             { 'A', '0', 'B' }
  31.             };
  32.  
  33.    /** The gpio. */
  34.    private final GpioController gpio = GpioFactory.getInstance();
  35.  
  36.    /** The Constant PIN_1_IN. */
  37.    private static final Pin PIN_1_IN = RaspiPin.GPIO_24;
  38.  
  39.    /** The Constant PIN_2_IN. */
  40.    private static final Pin PIN_2_IN = RaspiPin.GPIO_25;
  41.  
  42.    /** The Constant PIN_3_IN. */
  43.    private static final Pin PIN_3_IN = RaspiPin.GPIO_26;
  44.  
  45.    /** The Constant PIN_4_IN. */
  46.    private static final Pin PIN_4_IN = RaspiPin.GPIO_23;
  47.  
  48.    /** The Constant PIN_5_OUT. */
  49.    private static final Pin PIN_5_OUT = RaspiPin.GPIO_27;
  50.  
  51.    /** The Constant PIN_6_OUT. */
  52.    private static final Pin PIN_6_OUT = RaspiPin.GPIO_28;
  53.  
  54.    /** The Constant PIN_7_OUT. */
  55.    private static final Pin PIN_7_OUT = RaspiPin.GPIO_29;
  56.  
  57.  
  58.    /** The Row 1. */
  59.    private final GpioPinDigitalInput row1 = gpio
  60.          .provisionDigitalInputPin(PIN_1_IN, PinPullResistance.PULL_UP);
  61.  
  62.    /** The Row 2. */
  63.    private final GpioPinDigitalInput row2 = gpio
  64.          .provisionDigitalInputPin(PIN_2_IN, PinPullResistance.PULL_UP);
  65.  
  66.    /** The Row 3. */
  67.    private final GpioPinDigitalInput row3 = gpio
  68.          .provisionDigitalInputPin(PIN_3_IN, PinPullResistance.PULL_UP);
  69.  
  70.    /** The Row 4. */
  71.    private final GpioPinDigitalInput row4 = gpio
  72.          .provisionDigitalInputPin(PIN_4_IN, PinPullResistance.PULL_UP);
  73.  
  74.    /** The Column 5. */
  75.    private final GpioPinDigitalOutput col5 = gpio
  76.          .provisionDigitalOutputPin(PIN_5_OUT);
  77.  
  78.    /** The Column 6. */
  79.    private final GpioPinDigitalOutput col6 = gpio
  80.          .provisionDigitalOutputPin(PIN_6_OUT);
  81.  
  82.    /** The Column 7. */
  83.    private final GpioPinDigitalOutput col7 = gpio
  84.          .provisionDigitalOutputPin(PIN_7_OUT);
  85.  
  86.  
  87.    /** The Columns. */
  88.    private final GpioPinDigitalOutput theColumns[] = { col5, col6,
  89.          col7 };
  90.  
  91.    /** The input. */
  92.    private GpioPinDigitalInput theInput;
  93.  
  94.    /** The in id. */
  95.    private int theLin;
  96.  
  97.    /** The in id. */
  98.    private int theCol;
  99.  
  100.    /**
  101.     * Instantiates a new piezo keypad.
  102.     */
  103.    public KeypadTest() {
  104.       initListeners();
  105.    }
  106.  
  107.    /**
  108.     * Find output.
  109.     *
  110.     * Sets output lines to high and then to low one by one. Then the input line
  111.     * is tested. If its state is low, we have the right output line and
  112.     * therefore a mapping to a key on the keypad.
  113.     */
  114.    private void findOutput() {
  115.       // now test the inputs by setting the outputs from high to low
  116.       // one by one
  117.       for (int myO = 0; myO < theColumns.length; myO++) {
  118.          for (final GpioPinDigitalOutput myTheOutput : theColumns) {
  119.             myTheOutput.high();
  120.          }
  121.  
  122.          theColumns[myO].low();
  123.  
  124.          // input found?
  125.          if (theInput.isLow()) {
  126.             theCol = myO;
  127.             checkPins();
  128.             try {
  129.                Thread.sleep(200);
  130.             } catch (InterruptedException e) {
  131.             }
  132.             break;
  133.          }
  134.       }
  135.  
  136.       for (final GpioPinDigitalOutput myTheOutput : theColumns) {
  137.          myTheOutput.low();
  138.       }
  139.    }
  140.  
  141.    /**
  142.     * Check pins.
  143.     *
  144.     * Determins the pressed key based on the activated GPIO pins.
  145.     */
  146.    private synchronized void checkPins() {
  147.  
  148.       notifyListeners(this, KEY, this.keyPressed,
  149.             this.keyPressed = layout[theLin - 1][theCol]);
  150.  
  151.        System.out.println(layout[theLin - 1][theCol]);
  152.    }
  153.  
  154.    /**
  155.     * Inits the listeners.
  156.     */
  157.    private void initListeners() {
  158.       row1.addListener(new GpioPinListenerDigital() {
  159.          @Override
  160.          public void handleGpioPinDigitalStateChangeEvent(
  161.                final GpioPinDigitalStateChangeEvent event) {
  162.             if (event.getState() == PinState.LOW) {
  163.                theInput = row1;
  164.                theLin = 1;
  165.                findOutput();
  166.             }
  167.          }
  168.       });
  169.       row2.addListener(new GpioPinListenerDigital() {
  170.          @Override
  171.          public void handleGpioPinDigitalStateChangeEvent(
  172.                final GpioPinDigitalStateChangeEvent event) {
  173.             if (event.getState() == PinState.LOW) {
  174.                theInput = row2;
  175.                theLin = 2;
  176.                findOutput();
  177.             }
  178.          }
  179.       });
  180.       row3.addListener(new GpioPinListenerDigital() {
  181.          @Override
  182.          public void handleGpioPinDigitalStateChangeEvent(
  183.                final GpioPinDigitalStateChangeEvent event) {
  184.             if (event.getState() == PinState.LOW) {
  185.                theInput = row3;
  186.                theLin = 3;
  187.                findOutput();
  188.             }
  189.          }
  190.       });
  191.       row4.addListener(new GpioPinListenerDigital() {
  192.          @Override
  193.          public void handleGpioPinDigitalStateChangeEvent(
  194.                final GpioPinDigitalStateChangeEvent event) {
  195.             if (event.getState() == PinState.LOW) {
  196.                theInput = row4;
  197.                theLin = 4;
  198.                findOutput();
  199.             }
  200.          }
  201.       });
  202.    }
  203.  
  204.    private void notifyListeners(Object object, String property, char oldValue,
  205.          char newValue) {
  206.       for (PropertyChangeListener name : listener) {
  207.          name.propertyChange(new PropertyChangeEvent(this, property,
  208.                oldValue, newValue));
  209.       }
  210.    }
  211.  
  212.    public void addChangeListener(PropertyChangeListener newListener) {
  213.       listener.add(newListener);
  214.    }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement