Advertisement
Guest User

4x4 Keypad Java Pi4J

a guest
May 19th, 2015
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.23 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. public class RPi_4x4_Matrix {
  17.    
  18.    /** The Constant PINMAPPING. */
  19.    private static final HashMap<String, String> PINMAPPING = new HashMap<String, String>();
  20.  
  21.    /** The gpio. */
  22.    private final GpioController gpio = GpioFactory.getInstance();
  23.    
  24.    /** The COLUMN pins */
  25.    private static final Pin COLUMN_1 = RaspiPin.GPIO_04;
  26.    private static final Pin COLUMN_2 = RaspiPin.GPIO_03;
  27.    private static final Pin COLUMN_3 = RaspiPin.GPIO_02;
  28.    private static final Pin COLUMN_4 = RaspiPin.GPIO_01;
  29.    
  30.    /** The Digital COLUMN 1. */
  31.    private GpioPinDigitalOutput theCOLUMN_1 = gpio
  32.            .provisionDigitalOutputPin(COLUMN_1);
  33.  
  34.    /** The Digital COLUMN 2. */
  35.    private GpioPinDigitalOutput theCOLUMN_2 = gpio
  36.            .provisionDigitalOutputPin(COLUMN_2);
  37.  
  38.    /** The Digital COLUMN 3. */
  39.    private GpioPinDigitalOutput theCOLUMN_3 = gpio
  40.            .provisionDigitalOutputPin(COLUMN_3);
  41.  
  42.    /** The Digital COLUMN 4. */
  43.    private GpioPinDigitalOutput theCOLUMN_4 = gpio
  44.            .provisionDigitalOutputPin(COLUMN_4);
  45.    
  46.    /** The Digital Column Array **/
  47.    private final GpioPinDigitalOutput theColumns[] = {
  48.            theCOLUMN_1,
  49.            theCOLUMN_2,
  50.            theCOLUMN_3,
  51.            theCOLUMN_4
  52.    };
  53.    
  54.    /** The ROW pins */
  55.    private static final Pin ROW_1 = RaspiPin.GPIO_08;
  56.    private static final Pin ROW_2 = RaspiPin.GPIO_07;
  57.    private static final Pin ROW_3 = RaspiPin.GPIO_06;
  58.    private static final Pin ROW_4 = RaspiPin.GPIO_05;
  59.    
  60.    /** The Digital ROW 1. */
  61.    private GpioPinDigitalInput theROW_1 = gpio
  62.            .provisionDigitalInputPin(ROW_1, PinPullResistance.PULL_UP);
  63.  
  64.    /** The Digital ROW 2. */
  65.    private GpioPinDigitalInput theROW_2 = gpio
  66.             .provisionDigitalInputPin(ROW_2, PinPullResistance.PULL_UP);
  67.  
  68.    /** The Digital ROW 3. */
  69.    private GpioPinDigitalInput theROW_3 = gpio
  70.             .provisionDigitalInputPin(ROW_3, PinPullResistance.PULL_UP);
  71.  
  72.    /** The Digital ROW 4. */
  73.    private GpioPinDigitalInput theROW_4 = gpio
  74.             .provisionDigitalInputPin(ROW_4, PinPullResistance.PULL_UP);
  75.    
  76.    
  77.    /** The RowInput */
  78.    private GpioPinDigitalInput theRowInput;
  79.    
  80.    /** The row id */
  81.    private int theInId;
  82.    
  83.    /** The row value */
  84.    private int rowVal;
  85.    
  86.    /** The column value */
  87.    private int colVal;
  88.    
  89.    
  90.    /**
  91.     * Find Column
  92.     */
  93.     private void findColumn() {
  94.         for (int myO = 0; myO < theColumns.length; myO++) {
  95.             for (final GpioPinDigitalOutput myTheCol : theColumns)
  96.             {
  97.                 myTheCol.high();
  98.             }
  99.            
  100.             theColumns[myO].low();
  101.            
  102.              // column found?
  103.              if (theRowInput.isHigh()) {
  104.  
  105.                  rowVal = theInId;
  106.                  
  107.                  if (myO == 0)
  108.                  {
  109.                     colVal = 1;
  110.                  }
  111.                  else if(myO == 1)
  112.                  {
  113.                     colVal = 2;
  114.                  }
  115.                  else if(myO == 2)
  116.                  {
  117.                      colVal = 3;
  118.                  }
  119.                  else if(myO == 3)
  120.                  {
  121.                      colVal = 4;
  122.                  }
  123.                  checkPins();
  124.                  break;
  125.              }
  126.         }
  127.         for (final GpioPinDigitalOutput myTheCol : theColumns)
  128.         {
  129.             myTheCol.low();
  130.         }
  131.        
  132.     } // end findColumn().
  133.    
  134.    
  135.     /**
  136.         * Check pins.
  137.         *
  138.         * Determins the pressed key based on the activated GPIO pins.
  139.         */
  140.        private synchronized void checkPins()
  141.        {
  142.           if (rowVal > 1 || rowVal < 4 && colVal > 1 || colVal < 4)
  143.           {
  144.              System.out.println("RowID:" + rowVal + " ColumnID:" + colVal);
  145.  
  146.              String key = Integer.toString(rowVal)
  147.                          + Integer.toString(colVal);
  148.              System.out.println("Key: " + PINMAPPING.get(key));
  149.              
  150.               colVal = 0;
  151.               rowVal = 0;
  152.           }
  153.        }
  154.  
  155.    /**
  156.     * Instantiates a new piezo keypad.
  157.     */
  158.    public RPi_4x4_Matrix()
  159.    {
  160.       initMapping();
  161.       initListeners();
  162.    }
  163.  
  164.    /**
  165.     * Inits the listeners.
  166.     */
  167.    private void initListeners()
  168.    {
  169.       theROW_1.addListener(new GpioPinListenerDigital()
  170.       {
  171.          @Override
  172.          public void handleGpioPinDigitalStateChangeEvent(
  173.                final GpioPinDigitalStateChangeEvent aEvent)
  174.          {
  175.             if (aEvent.getState() == PinState.LOW)
  176.             {
  177.                 System.out.println("The Row 1 is LOW");
  178.             } else {
  179.                 System.out.println("The Row 1 is HIGH");
  180.                 theRowInput = theROW_1;
  181.                 theInId = 1;
  182.                 findColumn();
  183.             }
  184.          }
  185.       });
  186.       theROW_2.addListener(new GpioPinListenerDigital()
  187.       {
  188.          @Override
  189.          public void handleGpioPinDigitalStateChangeEvent(
  190.                final GpioPinDigitalStateChangeEvent aEvent)
  191.          {
  192.             if (aEvent.getState() == PinState.LOW)
  193.             {
  194.                 System.out.println("The Row 2 is LOW");
  195.             } else {
  196.                 System.out.println("The Row 2 is HIGH");
  197.                 theRowInput = theROW_2;
  198.                 theInId = 2;
  199.                 findColumn();
  200.             }
  201.          }
  202.       });
  203.       theROW_3.addListener(new GpioPinListenerDigital()
  204.       {
  205.          @Override
  206.          public void handleGpioPinDigitalStateChangeEvent(
  207.                final GpioPinDigitalStateChangeEvent aEvent)
  208.          {
  209.             if (aEvent.getState() == PinState.LOW)
  210.             {
  211.                 System.out.println("The Row 3 is LOW");
  212.             } else {
  213.                 System.out.println("The Row 3 is HIGH");
  214.                 theRowInput = theROW_3;
  215.                 theInId = 3;
  216.                 findColumn();
  217.             }
  218.          }
  219.       });
  220.       theROW_4.addListener(new GpioPinListenerDigital()
  221.       {
  222.          @Override
  223.          public void handleGpioPinDigitalStateChangeEvent(
  224.                final GpioPinDigitalStateChangeEvent aEvent)
  225.          {
  226.             if (aEvent.getState() == PinState.LOW)
  227.             {
  228.                 System.out.println("The Row 4 is LOW");
  229.             } else {
  230.                 System.out.println("The Row 4 is HIGH");
  231.                 theRowInput = theROW_4;
  232.                 theInId = 4;
  233.                 findColumn();
  234.             }
  235.          }
  236.       });
  237.    }
  238.  
  239.  
  240.    
  241.     /**
  242.         * Inits the mapping.
  243.         *
  244.         * Maps input/output line to key on keypad.
  245.         */
  246.        private void initMapping()
  247.        {
  248.           PINMAPPING.put("11", "1");
  249.           PINMAPPING.put("12", "2");
  250.           PINMAPPING.put("13", "3");
  251.           PINMAPPING.put("14", "A");
  252.           PINMAPPING.put("21", "4");
  253.           PINMAPPING.put("22", "5");
  254.           PINMAPPING.put("23", "6");
  255.           PINMAPPING.put("24", "B");
  256.           PINMAPPING.put("31", "7");
  257.           PINMAPPING.put("32", "8");
  258.           PINMAPPING.put("33", "9");
  259.           PINMAPPING.put("34", "C");
  260.           PINMAPPING.put("41", "*");
  261.           PINMAPPING.put("42", "0");
  262.           PINMAPPING.put("43", "#");
  263.           PINMAPPING.put("44", "D");
  264.        }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement