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;
- /**
- * The Class PiezoKeypad.
- */
- public class PiezoKeypad {
- /** The Constant PINMAPPING. */
- private static final HashMap<String, String> PINMAPPING = new HashMap<String, String>();
- /** The gpio. */
- private final GpioController theGpio = GpioFactory.getInstance();
- /** The Constant Inputs - Column */
- private static final Pin PIN_1_IN = RaspiPin.GPIO_01;
- private static final Pin PIN_2_IN = RaspiPin.GPIO_02;
- private static final Pin PIN_3_IN = RaspiPin.GPIO_03;
- private static final Pin PIN_4_IN = RaspiPin.GPIO_04;
- /** The Constant Outputs - Row */
- private static final Pin PIN_5_OUT = RaspiPin.GPIO_05;
- private static final Pin PIN_6_OUT = RaspiPin.GPIO_06;
- private static final Pin PIN_7_OUT = RaspiPin.GPIO_07;
- private static final Pin PIN_8_OUT = RaspiPin.GPIO_08;
- /** The pin1. */
- private final GpioPinDigitalInput COLUMN_1 = theGpio
- .provisionDigitalInputPin(PIN_1_IN, PinPullResistance.PULL_UP );
- /** The pin2. */
- private final GpioPinDigitalInput COLUMN_2 = theGpio
- .provisionDigitalInputPin(PIN_2_IN, PinPullResistance.PULL_UP);
- /** The pin3. */
- private final GpioPinDigitalInput COLUMN_3 = theGpio
- .provisionDigitalInputPin(PIN_3_IN, PinPullResistance.PULL_UP);
- /** The pin4. */
- private final GpioPinDigitalInput COLUMN_4 = theGpio
- .provisionDigitalInputPin(PIN_4_IN, PinPullResistance.PULL_UP);
- /** The pin5. */
- private final GpioPinDigitalOutput ROW_1 = theGpio
- .provisionDigitalOutputPin(PIN_5_OUT);
- /** The pin6. */
- private final GpioPinDigitalOutput ROW_2 = theGpio
- .provisionDigitalOutputPin(PIN_6_OUT);
- /** The pin7. */
- private final GpioPinDigitalOutput ROW_3 = theGpio
- .provisionDigitalOutputPin(PIN_7_OUT);
- /** The pin8. */
- private final GpioPinDigitalOutput ROW_4 = theGpio
- .provisionDigitalOutputPin(PIN_8_OUT);
- /** The Rows. */
- private final GpioPinDigitalOutput theOutpus[] = { ROW_1,
- ROW_2,
- ROW_3,
- ROW_4 };
- /** The Row id. */
- private int theRowId;
- /** The Column. */
- private GpioPinDigitalInput theColumn;
- /** The Column id. */
- private int theColId;
- /**
- * Instantiates a new piezo keypad.
- */
- public PiezoKeypad()
- {
- initMapping();
- initListeners();
- }
- /**
- * Inits the listeners.
- */
- private void initListeners()
- {
- COLUMN_1.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- theColumn = COLUMN_1;
- theColId = 1;
- findOutput();
- }
- }
- });
- COLUMN_2.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- theColumn = COLUMN_2;
- theColId = 2;
- findOutput();;
- }
- }
- });
- COLUMN_3.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- theColumn = COLUMN_3;
- theColId = 3;
- findOutput();
- }
- }
- });
- COLUMN_4.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- theColumn = COLUMN_4;
- theColId = 4;
- findOutput();
- }
- }
- });
- }
- /**
- * Find output.
- *
- * Sets output lines to high and then to low one by one.
- * Then the input line is tested. If its state is low, we have the right output line and therefore
- * a mapping to a key on the keypad.
- */
- private void findOutput()
- {
- // Set all Row pins to HIGH
- for (int myO = 0; myO < theOutpus.length; myO++)
- {
- for(GpioPinDigitalOutput myTheRow : theOutpus)
- {
- myTheRow.high();;
- }
- theOutpus[myO].low();;
- // column found?
- if (theColumn.isLow())
- {
- theRowId = myO+1;
- checkPins();
- break;
- }
- for(GpioPinDigitalOutput myTheRow : theOutpus)
- {
- myTheRow.low();;
- }
- }
- }
- /**
- * Check pins.
- *
- * Determins the pressed key based on the activated GPIO pins.
- */
- private synchronized void checkPins()
- {
- if (theRowId != 0 && theColId != 0)
- {
- System.out.println("RowID:" + theRowId + " ColumnID:" + theColId);
- String key = Integer.toString(theRowId)
- + Integer.toString(theColId);
- System.out.println("Key: " + PINMAPPING.get(key));
- }
- }
- /**
- * Inits the mapping.
- *
- * Maps input/output line to key on keypad.
- */
- private void initMapping()
- {
- PINMAPPING.put("44", "1");
- PINMAPPING.put("43", "2");
- PINMAPPING.put("42", "3");
- PINMAPPING.put("41", "A");
- PINMAPPING.put("34", "4");
- PINMAPPING.put("33", "5");
- PINMAPPING.put("32", "6");
- PINMAPPING.put("31", "B");
- PINMAPPING.put("24", "7");
- PINMAPPING.put("23", "8");
- PINMAPPING.put("22", "9");
- PINMAPPING.put("21", "C");
- PINMAPPING.put("14", "*");
- PINMAPPING.put("13", "0");
- PINMAPPING.put("12", "#");
- PINMAPPING.put("11", "D");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement