Advertisement
Guest User

4x4 Keypad Java Pi4J

a guest
May 17th, 2015
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.49 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.    /** The Constant PINMAPPING. */
  21.    private static final HashMap<String, String> PINMAPPING = new HashMap<String, String>();
  22.  
  23.    /** The gpio. */
  24.    private final GpioController theGpio = GpioFactory.getInstance();
  25.  
  26.    /** The Constant Inputs - Column */
  27.    private static final Pin PIN_1_IN = RaspiPin.GPIO_01;
  28.    private static final Pin PIN_2_IN = RaspiPin.GPIO_02;
  29.    private static final Pin PIN_3_IN = RaspiPin.GPIO_03;
  30.    private static final Pin PIN_4_IN = RaspiPin.GPIO_04;
  31.  
  32.    /** The Constant Outputs - Row */
  33.    private static final Pin PIN_5_OUT = RaspiPin.GPIO_05;
  34.    private static final Pin PIN_6_OUT = RaspiPin.GPIO_06;
  35.    private static final Pin PIN_7_OUT = RaspiPin.GPIO_07;
  36.    private static final Pin PIN_8_OUT = RaspiPin.GPIO_08;
  37.  
  38.    /** The pin1. */
  39.    private final GpioPinDigitalInput COLUMN_1 = theGpio
  40.          .provisionDigitalInputPin(PIN_1_IN, PinPullResistance.PULL_UP );
  41.    
  42.    /** The pin2. */
  43.    private final GpioPinDigitalInput COLUMN_2 = theGpio
  44.          .provisionDigitalInputPin(PIN_2_IN, PinPullResistance.PULL_UP);
  45.  
  46.    /** The pin3. */
  47.    private final GpioPinDigitalInput COLUMN_3 = theGpio
  48.          .provisionDigitalInputPin(PIN_3_IN, PinPullResistance.PULL_UP);
  49.  
  50.    /** The pin4. */
  51.    private final GpioPinDigitalInput COLUMN_4 = theGpio
  52.          .provisionDigitalInputPin(PIN_4_IN, PinPullResistance.PULL_UP);
  53.  
  54.    /** The pin5. */
  55.    private final GpioPinDigitalOutput ROW_1 = theGpio
  56.          .provisionDigitalOutputPin(PIN_5_OUT);
  57.  
  58.    /** The pin6. */
  59.    private final GpioPinDigitalOutput ROW_2 = theGpio
  60.          .provisionDigitalOutputPin(PIN_6_OUT);
  61.  
  62.    /** The pin7. */
  63.    private final GpioPinDigitalOutput ROW_3 = theGpio
  64.          .provisionDigitalOutputPin(PIN_7_OUT);
  65.  
  66.    /** The pin8. */
  67.    private final GpioPinDigitalOutput ROW_4 = theGpio
  68.          .provisionDigitalOutputPin(PIN_8_OUT);
  69.  
  70.    /** The Rows. */
  71.    private final GpioPinDigitalOutput theOutpus[] = { ROW_1,
  72.                                                     ROW_2,
  73.                                                     ROW_3,
  74.                                                     ROW_4 };
  75.  
  76.    /** The Row id. */
  77.    private int theRowId;
  78.    
  79.    /** The Column. */
  80.    private GpioPinDigitalInput theColumn;
  81.  
  82.    /** The Column id. */
  83.    private int theColId;
  84.  
  85.    
  86.    /**
  87.     * Instantiates a new piezo keypad.
  88.     */
  89.    public PiezoKeypad()
  90.    {
  91.       initMapping();
  92.  
  93.       initListeners();
  94.    }
  95.  
  96.    /**
  97.     * Inits the listeners.
  98.     */
  99.    private void initListeners()
  100.    {
  101.       COLUMN_1.addListener(new GpioPinListenerDigital()
  102.       {
  103.          @Override
  104.          public void handleGpioPinDigitalStateChangeEvent(
  105.                final GpioPinDigitalStateChangeEvent aEvent)
  106.          {
  107.             if (aEvent.getState() == PinState.LOW)
  108.             {
  109.                theColumn = COLUMN_1;
  110.                theColId = 1;
  111.                findOutput();
  112.             }
  113.          }
  114.       });
  115.       COLUMN_2.addListener(new GpioPinListenerDigital()
  116.       {
  117.          @Override
  118.          public void handleGpioPinDigitalStateChangeEvent(
  119.                final GpioPinDigitalStateChangeEvent aEvent)
  120.          {
  121.             if (aEvent.getState() == PinState.LOW)
  122.             {
  123.                theColumn = COLUMN_2;
  124.                theColId = 2;
  125.                findOutput();;
  126.             }
  127.          }
  128.       });
  129.       COLUMN_3.addListener(new GpioPinListenerDigital()
  130.       {
  131.          @Override
  132.          public void handleGpioPinDigitalStateChangeEvent(
  133.                final GpioPinDigitalStateChangeEvent aEvent)
  134.          {
  135.             if (aEvent.getState() == PinState.LOW)
  136.             {
  137.                theColumn = COLUMN_3;
  138.                theColId = 3;
  139.                findOutput();
  140.             }
  141.          }
  142.       });
  143.       COLUMN_4.addListener(new GpioPinListenerDigital()
  144.       {
  145.          @Override
  146.          public void handleGpioPinDigitalStateChangeEvent(
  147.                final GpioPinDigitalStateChangeEvent aEvent)
  148.          {
  149.             if (aEvent.getState() == PinState.LOW)
  150.             {
  151.                theColumn = COLUMN_4;
  152.                theColId = 4;
  153.                findOutput();
  154.             }
  155.          }
  156.       });
  157.    }
  158.    
  159.    /**
  160.     * Find output.
  161.     *
  162.     * Sets output lines to high and then to low one by one.
  163.     * Then the input line is tested. If its state is low, we have the right output line and therefore
  164.     * a mapping to a key on the keypad.
  165.     */
  166.    private void findOutput()
  167.    {
  168.        // Set all Row pins to HIGH
  169.        for (int myO = 0; myO < theOutpus.length; myO++)
  170.        {
  171.          for(GpioPinDigitalOutput myTheRow : theOutpus)
  172.          {
  173.              myTheRow.high();;
  174.          }
  175.          theOutpus[myO].low();;
  176.  
  177.          // column found?
  178.          if (theColumn.isLow())
  179.          {
  180.             theRowId = myO+1;
  181.             checkPins();
  182.             break;
  183.          }
  184.          for(GpioPinDigitalOutput myTheRow : theOutpus)
  185.          {
  186.              myTheRow.low();;
  187.          }
  188.       }
  189.    }
  190.    
  191.    /**
  192.     * Check pins.
  193.     *
  194.     * Determins the pressed key based on the activated GPIO pins.
  195.     */
  196.    private synchronized void checkPins()
  197.    {
  198.       if (theRowId != 0 && theColId != 0)
  199.       {
  200.          System.out.println("RowID:" + theRowId + " ColumnID:" + theColId);
  201.  
  202.          String key = Integer.toString(theRowId)
  203.                      + Integer.toString(theColId);
  204.          System.out.println("Key: " + PINMAPPING.get(key));
  205.       }
  206.    }
  207.  
  208.    /**
  209.     * Inits the mapping.
  210.     *
  211.     * Maps input/output line to key on keypad.
  212.     */
  213.    private void initMapping()
  214.    {
  215.       PINMAPPING.put("44", "1");
  216.       PINMAPPING.put("43", "2");
  217.       PINMAPPING.put("42", "3");
  218.       PINMAPPING.put("41", "A");
  219.       PINMAPPING.put("34", "4");
  220.       PINMAPPING.put("33", "5");
  221.       PINMAPPING.put("32", "6");
  222.       PINMAPPING.put("31", "B");
  223.       PINMAPPING.put("24", "7");
  224.       PINMAPPING.put("23", "8");
  225.       PINMAPPING.put("22", "9");
  226.       PINMAPPING.put("21", "C");
  227.       PINMAPPING.put("14", "*");
  228.       PINMAPPING.put("13", "0");
  229.       PINMAPPING.put("12", "#");
  230.       PINMAPPING.put("11", "D");
  231.    }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement