Advertisement
Guest User

4x4 Keypad Java

a guest
May 15th, 2015
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.57 KB | None | 0 0
  1. package application;
  2. import java.util.HashMap;
  3.  
  4. import com.pi4j.io.gpio.GpioController;
  5. import com.pi4j.io.gpio.GpioFactory;
  6. import com.pi4j.io.gpio.GpioPinDigitalInput;
  7. import com.pi4j.io.gpio.GpioPinDigitalOutput;
  8. import com.pi4j.io.gpio.Pin;
  9. import com.pi4j.io.gpio.PinPullResistance;
  10. import com.pi4j.io.gpio.PinState;
  11. import com.pi4j.io.gpio.RaspiPin;
  12. import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
  13. import com.pi4j.io.gpio.event.GpioPinListenerDigital;
  14.  
  15. /**
  16.  * The Class PiezoKeypad.
  17.  */
  18. public class PiezoKeypad
  19. {
  20.  
  21.    /**
  22.     * <pre>
  23.     * Keypad pin schema key = pina + pinb      
  24.     *     (output/input) (column/row)  
  25.     * 1 =   4   8  
  26.     * 2 =   3   8  
  27.     * 3 =   2   8
  28.     * A =   1   8
  29.     * 4 =   4   7  
  30.     * 5 =   3   7  
  31.     * 6 =   2   7
  32.     * B =   1   7
  33.     * 7 =   4   6  
  34.     * 8 =   3   6  
  35.     * 9 =   2   6
  36.     * C =   1   6  
  37.     * * =   4   5  
  38.     * 0 =   3   5  
  39.     * # =   2   5
  40.     * D =   1   5
  41.     * </pre>
  42.     *
  43.     * .
  44.     */
  45.  
  46.    /** The Constant PINMAPPING. */
  47.    private static final HashMap<String, String> PINMAPPING = new HashMap<String, String>();
  48.  
  49.    /** The gpio. */
  50.    private final GpioController theGpio = GpioFactory.getInstance();
  51.  
  52.    /** The Constant PIN_1_IN. */
  53.    private static final Pin PIN_1_OUT = RaspiPin.GPIO_01;
  54.    
  55.    /** The Constant PIN_2_IN. */
  56.    private static final Pin PIN_2_OUT = RaspiPin.GPIO_02;
  57.  
  58.    /** The Constant PIN_3_IN. */
  59.    private static final Pin PIN_3_OUT = RaspiPin.GPIO_03;
  60.  
  61.    /** The Constant PIN_4_OUT. */
  62.    private static final Pin PIN_4_OUT = RaspiPin.GPIO_04;
  63.  
  64.    /** The Constant PIN_5_IN. */
  65.    private static final Pin PIN_5_IN = RaspiPin.GPIO_05;
  66.  
  67.    /** The Constant PIN_6_IN. */
  68.    private static final Pin PIN_6_IN = RaspiPin.GPIO_06;
  69.  
  70.    /** The Constant PIN_7_IN. */
  71.    private static final Pin PIN_7_IN = RaspiPin.GPIO_07;
  72.  
  73.    /** The Constant PIN_8_IN. */
  74.    private static final Pin PIN_8_IN = RaspiPin.GPIO_08;
  75.  
  76.    /** The pin1. */
  77.    private final GpioPinDigitalOutput thePin1 = theGpio
  78.          .provisionDigitalOutputPin(PIN_1_OUT);
  79.    
  80.    /** The pin2. */
  81.    private final GpioPinDigitalOutput thePin2 = theGpio
  82.          .provisionDigitalOutputPin(PIN_2_OUT);
  83.  
  84.    /** The pin3. */
  85.    private final GpioPinDigitalOutput thePin3 = theGpio
  86.          .provisionDigitalOutputPin(PIN_3_OUT);
  87.  
  88.    /** The pin4. */
  89.    private final GpioPinDigitalOutput thePin4 = theGpio
  90.          .provisionDigitalOutputPin(PIN_4_OUT);
  91.  
  92.    /** The pin5. */
  93.    private final GpioPinDigitalInput thePin5 = theGpio
  94.          .provisionDigitalInputPin(PIN_5_IN, PinPullResistance.PULL_UP);
  95.  
  96.    /** The pin6. */
  97.    private final GpioPinDigitalInput thePin6 = theGpio
  98.          .provisionDigitalInputPin(PIN_6_IN, PinPullResistance.PULL_UP);
  99.  
  100.    /** The pin7. */
  101.    private final GpioPinDigitalInput thePin7 = theGpio
  102.          .provisionDigitalInputPin(PIN_7_IN, PinPullResistance.PULL_UP);
  103.  
  104.    /** The pin8. */
  105.    private final GpioPinDigitalInput thePin8 = theGpio
  106.          .provisionDigitalInputPin(PIN_8_IN, PinPullResistance.PULL_UP);
  107.  
  108.    /** The outputs. */
  109.    private final GpioPinDigitalOutput theOutputs[] = { thePin1,
  110.                                                        thePin2,
  111.                                                        thePin3,
  112.                                                        thePin4 };
  113.  
  114.    /** The input. */
  115.    private GpioPinDigitalInput theInput;
  116.  
  117.    /** The in id. */
  118.    private int theInId;
  119.  
  120.    /** The first pin. */
  121.    private int theFirstPin = 0;
  122.  
  123.    /** The second pin. */
  124.    private int theSecondPin = 0;
  125.  
  126.    /**
  127.     * Instantiates a new piezo keypad.
  128.     */
  129.    public PiezoKeypad()
  130.    {
  131.       initMapping();
  132.  
  133.       initListeners();
  134.    }
  135.  
  136.    /**
  137.     * Check pins.
  138.     *
  139.     * Determins the pressed key based on the activated GPIO pins.
  140.     */
  141.    private synchronized void checkPins()
  142.    {
  143.       if (theFirstPin != 0 && theSecondPin != 0)
  144.       {
  145.          System.out.println("theFirstPin= " + theFirstPin
  146.                           + " theSecondPin= " + theSecondPin);
  147.          
  148.          System.out.println(PINMAPPING.get(String.valueOf(theFirstPin)
  149.                                          + String.valueOf(theSecondPin)));
  150.          
  151.          theFirstPin = 0;
  152.          theSecondPin = 0;
  153.       }
  154.    }
  155.  
  156.    /**
  157.     * Find output.
  158.     *
  159.     * Sets output lines  to high and then to low one by one.
  160.     * Then the input line is tested. If its state is low, we have the right output line and therefore
  161.     * a mapping to a key on the keypad.
  162.     */
  163.    private void findOutput()
  164.    {
  165.       // now test the inputs by setting the outputs from high to low
  166.       // one by one
  167.       for (int myO = 0; myO < theOutputs.length; myO++)
  168.       {
  169.          for (final GpioPinDigitalOutput myTheOutput : theOutputs)
  170.          {
  171.             myTheOutput.high();
  172.          }
  173.  
  174.          theOutputs[myO].low();
  175.  
  176.          // input found?
  177.          if (theInput.isLow())
  178.          {
  179.             theSecondPin = theInId;
  180.  
  181.             if (myO == 0)
  182.             {
  183.                theFirstPin = 1;
  184.             }
  185.             else
  186.             {
  187.                theFirstPin = 4;
  188.             }
  189.  
  190.             checkPins();
  191.             break;
  192.          }
  193.       }
  194.  
  195.       for (final GpioPinDigitalOutput myTheOutput : theOutputs)
  196.       {
  197.          myTheOutput.low();
  198.       }
  199.    }
  200.  
  201.    /**
  202.     * Inits the listeners.
  203.     */
  204.    private void initListeners()
  205.    {
  206.       thePin5.addListener(new GpioPinListenerDigital()
  207.       {
  208.          @Override
  209.          public void handleGpioPinDigitalStateChangeEvent(
  210.                final GpioPinDigitalStateChangeEvent aEvent)
  211.          {
  212.             if (aEvent.getState() == PinState.LOW)
  213.             {
  214.                theInput = thePin5;
  215.                theInId = 5;
  216.                findOutput();
  217.             }
  218.          }
  219.       });
  220.       thePin6.addListener(new GpioPinListenerDigital()
  221.       {
  222.          @Override
  223.          public void handleGpioPinDigitalStateChangeEvent(
  224.                final GpioPinDigitalStateChangeEvent aEvent)
  225.          {
  226.             if (aEvent.getState() == PinState.LOW)
  227.             {
  228.                theInput = thePin6;
  229.                theInId = 6;
  230.                findOutput();
  231.             }
  232.          }
  233.       });
  234.       thePin7.addListener(new GpioPinListenerDigital()
  235.       {
  236.          @Override
  237.          public void handleGpioPinDigitalStateChangeEvent(
  238.                final GpioPinDigitalStateChangeEvent aEvent)
  239.          {
  240.             if (aEvent.getState() == PinState.LOW)
  241.             {
  242.                theInput = thePin7;
  243.                theInId = 7;
  244.                findOutput();
  245.             }
  246.          }
  247.       });
  248.       thePin8.addListener(new GpioPinListenerDigital()
  249.       {
  250.          @Override
  251.          public void handleGpioPinDigitalStateChangeEvent(
  252.                final GpioPinDigitalStateChangeEvent aEvent)
  253.          {
  254.             if (aEvent.getState() == PinState.LOW)
  255.             {
  256.                theInput = thePin8;
  257.                theInId = 8;
  258.                findOutput();
  259.             }
  260.          }
  261.       });
  262.    }
  263.  
  264.    /**
  265.     * Inits the mapping.
  266.     *
  267.     * Maps input/output line to key on keypad.
  268.     */
  269.    private void initMapping()
  270.    {
  271.       PINMAPPING.put("48", "1");
  272.       PINMAPPING.put("38", "2");
  273.       PINMAPPING.put("28", "3");
  274.       PINMAPPING.put("18", "A");
  275.       PINMAPPING.put("47", "4");
  276.       PINMAPPING.put("37", "5");
  277.       PINMAPPING.put("27", "6");
  278.       PINMAPPING.put("17", "B");
  279.       PINMAPPING.put("46", "7");
  280.       PINMAPPING.put("36", "8");
  281.       PINMAPPING.put("26", "9");
  282.       PINMAPPING.put("16", "C");
  283.       PINMAPPING.put("45", "*");
  284.       PINMAPPING.put("35", "0");
  285.       PINMAPPING.put("25", "#");
  286.       PINMAPPING.put("15", "D");
  287.    }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement