Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application;
- import java.util.HashMap;
- import com.pi4j.io.gpio.GpioController;
- import com.pi4j.io.gpio.GpioFactory;
- import com.pi4j.io.gpio.GpioPinDigitalInput;
- import com.pi4j.io.gpio.GpioPinDigitalOutput;
- import com.pi4j.io.gpio.Pin;
- import com.pi4j.io.gpio.PinPullResistance;
- import com.pi4j.io.gpio.PinState;
- import com.pi4j.io.gpio.RaspiPin;
- import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
- import com.pi4j.io.gpio.event.GpioPinListenerDigital;
- public class RPi_4x4_Matrix {
- /** The Constant PINMAPPING. */
- private static final HashMap<String, String> PINMAPPING = new HashMap<String, String>();
- /** The gpio. */
- private final GpioController gpio = GpioFactory.getInstance();
- /** The COLUMN pins */
- private static final Pin COLUMN_1 = RaspiPin.GPIO_04;
- private static final Pin COLUMN_2 = RaspiPin.GPIO_03;
- private static final Pin COLUMN_3 = RaspiPin.GPIO_02;
- private static final Pin COLUMN_4 = RaspiPin.GPIO_01;
- /** The Digital COLUMN 1. */
- private GpioPinDigitalOutput theCOLUMN_1 = gpio
- .provisionDigitalOutputPin(COLUMN_1);
- /** The Digital COLUMN 2. */
- private GpioPinDigitalOutput theCOLUMN_2 = gpio
- .provisionDigitalOutputPin(COLUMN_2);
- /** The Digital COLUMN 3. */
- private GpioPinDigitalOutput theCOLUMN_3 = gpio
- .provisionDigitalOutputPin(COLUMN_3);
- /** The Digital COLUMN 4. */
- private GpioPinDigitalOutput theCOLUMN_4 = gpio
- .provisionDigitalOutputPin(COLUMN_4);
- /** The Digital Column Array **/
- private final GpioPinDigitalOutput theColumns[] = {
- theCOLUMN_1,
- theCOLUMN_2,
- theCOLUMN_3,
- theCOLUMN_4
- };
- /** The ROW pins */
- private static final Pin ROW_1 = RaspiPin.GPIO_08;
- private static final Pin ROW_2 = RaspiPin.GPIO_07;
- private static final Pin ROW_3 = RaspiPin.GPIO_06;
- private static final Pin ROW_4 = RaspiPin.GPIO_05;
- /** The Digital ROW 1. */
- private GpioPinDigitalInput theROW_1 = gpio
- .provisionDigitalInputPin(ROW_1, PinPullResistance.PULL_UP);
- /** The Digital ROW 2. */
- private GpioPinDigitalInput theROW_2 = gpio
- .provisionDigitalInputPin(ROW_2, PinPullResistance.PULL_UP);
- /** The Digital ROW 3. */
- private GpioPinDigitalInput theROW_3 = gpio
- .provisionDigitalInputPin(ROW_3, PinPullResistance.PULL_UP);
- /** The Digital ROW 4. */
- private GpioPinDigitalInput theROW_4 = gpio
- .provisionDigitalInputPin(ROW_4, PinPullResistance.PULL_UP);
- /** The RowInput */
- private GpioPinDigitalInput theRowInput;
- /** The row id */
- private int theInId;
- /** The row value */
- private int rowVal;
- /** The column value */
- private int colVal;
- /**
- * Find Column
- */
- private void findColumn() {
- for (int myO = 0; myO < theColumns.length; myO++) {
- for (final GpioPinDigitalOutput myTheCol : theColumns)
- {
- myTheCol.high();
- }
- theColumns[myO].low();
- // column found?
- if (theRowInput.isHigh()) {
- rowVal = theInId;
- if (myO == 0)
- {
- colVal = 1;
- }
- else if(myO == 1)
- {
- colVal = 2;
- }
- else if(myO == 2)
- {
- colVal = 3;
- }
- else if(myO == 3)
- {
- colVal = 4;
- }
- checkPins();
- break;
- }
- }
- for (final GpioPinDigitalOutput myTheCol : theColumns)
- {
- myTheCol.low();
- }
- } // end findColumn().
- /**
- * Check pins.
- *
- * Determins the pressed key based on the activated GPIO pins.
- */
- private synchronized void checkPins()
- {
- if (rowVal > 1 || rowVal < 4 && colVal > 1 || colVal < 4)
- {
- System.out.println("RowID:" + rowVal + " ColumnID:" + colVal);
- String key = Integer.toString(rowVal)
- + Integer.toString(colVal);
- System.out.println("Key: " + PINMAPPING.get(key));
- colVal = 0;
- rowVal = 0;
- }
- }
- /**
- * Instantiates a new piezo keypad.
- */
- public RPi_4x4_Matrix()
- {
- initMapping();
- initListeners();
- }
- /**
- * Inits the listeners.
- */
- private void initListeners()
- {
- theROW_1.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- System.out.println("The Row 1 is LOW");
- } else {
- System.out.println("The Row 1 is HIGH");
- theRowInput = theROW_1;
- theInId = 1;
- findColumn();
- }
- }
- });
- theROW_2.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- System.out.println("The Row 2 is LOW");
- } else {
- System.out.println("The Row 2 is HIGH");
- theRowInput = theROW_2;
- theInId = 2;
- findColumn();
- }
- }
- });
- theROW_3.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- System.out.println("The Row 3 is LOW");
- } else {
- System.out.println("The Row 3 is HIGH");
- theRowInput = theROW_3;
- theInId = 3;
- findColumn();
- }
- }
- });
- theROW_4.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- System.out.println("The Row 4 is LOW");
- } else {
- System.out.println("The Row 4 is HIGH");
- theRowInput = theROW_4;
- theInId = 4;
- findColumn();
- }
- }
- });
- }
- /**
- * Inits the mapping.
- *
- * Maps input/output line to key on keypad.
- */
- private void initMapping()
- {
- PINMAPPING.put("11", "1");
- PINMAPPING.put("12", "2");
- PINMAPPING.put("13", "3");
- PINMAPPING.put("14", "A");
- PINMAPPING.put("21", "4");
- PINMAPPING.put("22", "5");
- PINMAPPING.put("23", "6");
- PINMAPPING.put("24", "B");
- PINMAPPING.put("31", "7");
- PINMAPPING.put("32", "8");
- PINMAPPING.put("33", "9");
- PINMAPPING.put("34", "C");
- PINMAPPING.put("41", "*");
- PINMAPPING.put("42", "0");
- PINMAPPING.put("43", "#");
- PINMAPPING.put("44", "D");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement