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
- {
- /**
- * <pre>
- * Keypad pin schema key = pina + pinb
- * (output/input) (column/row)
- * 1 = 4 8
- * 2 = 3 8
- * 3 = 2 8
- * A = 1 8
- * 4 = 4 7
- * 5 = 3 7
- * 6 = 2 7
- * B = 1 7
- * 7 = 4 6
- * 8 = 3 6
- * 9 = 2 6
- * C = 1 6
- * * = 4 5
- * 0 = 3 5
- * # = 2 5
- * D = 1 5
- * </pre>
- *
- * .
- */
- /** The Constant PINMAPPING. */
- private static final HashMap<String, String> PINMAPPING = new HashMap<String, String>();
- /** The gpio. */
- private final GpioController theGpio = GpioFactory.getInstance();
- /** The Constant PIN_1_IN. */
- private static final Pin PIN_1_OUT = RaspiPin.GPIO_01;
- /** The Constant PIN_2_IN. */
- private static final Pin PIN_2_OUT = RaspiPin.GPIO_02;
- /** The Constant PIN_3_IN. */
- private static final Pin PIN_3_OUT = RaspiPin.GPIO_03;
- /** The Constant PIN_4_OUT. */
- private static final Pin PIN_4_OUT = RaspiPin.GPIO_04;
- /** The Constant PIN_5_IN. */
- private static final Pin PIN_5_IN = RaspiPin.GPIO_05;
- /** The Constant PIN_6_IN. */
- private static final Pin PIN_6_IN = RaspiPin.GPIO_06;
- /** The Constant PIN_7_IN. */
- private static final Pin PIN_7_IN = RaspiPin.GPIO_07;
- /** The Constant PIN_8_IN. */
- private static final Pin PIN_8_IN = RaspiPin.GPIO_08;
- /** The pin1. */
- private final GpioPinDigitalOutput thePin1 = theGpio
- .provisionDigitalOutputPin(PIN_1_OUT);
- /** The pin2. */
- private final GpioPinDigitalOutput thePin2 = theGpio
- .provisionDigitalOutputPin(PIN_2_OUT);
- /** The pin3. */
- private final GpioPinDigitalOutput thePin3 = theGpio
- .provisionDigitalOutputPin(PIN_3_OUT);
- /** The pin4. */
- private final GpioPinDigitalOutput thePin4 = theGpio
- .provisionDigitalOutputPin(PIN_4_OUT);
- /** The pin5. */
- private final GpioPinDigitalInput thePin5 = theGpio
- .provisionDigitalInputPin(PIN_5_IN, PinPullResistance.PULL_UP);
- /** The pin6. */
- private final GpioPinDigitalInput thePin6 = theGpio
- .provisionDigitalInputPin(PIN_6_IN, PinPullResistance.PULL_UP);
- /** The pin7. */
- private final GpioPinDigitalInput thePin7 = theGpio
- .provisionDigitalInputPin(PIN_7_IN, PinPullResistance.PULL_UP);
- /** The pin8. */
- private final GpioPinDigitalInput thePin8 = theGpio
- .provisionDigitalInputPin(PIN_8_IN, PinPullResistance.PULL_UP);
- /** The outputs. */
- private final GpioPinDigitalOutput theOutputs[] = { thePin1,
- thePin2,
- thePin3,
- thePin4 };
- /** The input. */
- private GpioPinDigitalInput theInput;
- /** The in id. */
- private int theInId;
- /** The first pin. */
- private int theFirstPin = 0;
- /** The second pin. */
- private int theSecondPin = 0;
- /**
- * Instantiates a new piezo keypad.
- */
- public PiezoKeypad()
- {
- initMapping();
- initListeners();
- }
- /**
- * Check pins.
- *
- * Determins the pressed key based on the activated GPIO pins.
- */
- private synchronized void checkPins()
- {
- if (theFirstPin != 0 && theSecondPin != 0)
- {
- System.out.println("theFirstPin= " + theFirstPin
- + " theSecondPin= " + theSecondPin);
- System.out.println(PINMAPPING.get(String.valueOf(theFirstPin)
- + String.valueOf(theSecondPin)));
- theFirstPin = 0;
- theSecondPin = 0;
- }
- }
- /**
- * 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()
- {
- // now test the inputs by setting the outputs from high to low
- // one by one
- for (int myO = 0; myO < theOutputs.length; myO++)
- {
- for (final GpioPinDigitalOutput myTheOutput : theOutputs)
- {
- myTheOutput.high();
- }
- theOutputs[myO].low();
- // input found?
- if (theInput.isLow())
- {
- theSecondPin = theInId;
- if (myO == 0)
- {
- theFirstPin = 1;
- }
- else
- {
- theFirstPin = 4;
- }
- checkPins();
- break;
- }
- }
- for (final GpioPinDigitalOutput myTheOutput : theOutputs)
- {
- myTheOutput.low();
- }
- }
- /**
- * Inits the listeners.
- */
- private void initListeners()
- {
- thePin5.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- theInput = thePin5;
- theInId = 5;
- findOutput();
- }
- }
- });
- thePin6.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- theInput = thePin6;
- theInId = 6;
- findOutput();
- }
- }
- });
- thePin7.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- theInput = thePin7;
- theInId = 7;
- findOutput();
- }
- }
- });
- thePin8.addListener(new GpioPinListenerDigital()
- {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent aEvent)
- {
- if (aEvent.getState() == PinState.LOW)
- {
- theInput = thePin8;
- theInId = 8;
- findOutput();
- }
- }
- });
- }
- /**
- * Inits the mapping.
- *
- * Maps input/output line to key on keypad.
- */
- private void initMapping()
- {
- PINMAPPING.put("48", "1");
- PINMAPPING.put("38", "2");
- PINMAPPING.put("28", "3");
- PINMAPPING.put("18", "A");
- PINMAPPING.put("47", "4");
- PINMAPPING.put("37", "5");
- PINMAPPING.put("27", "6");
- PINMAPPING.put("17", "B");
- PINMAPPING.put("46", "7");
- PINMAPPING.put("36", "8");
- PINMAPPING.put("26", "9");
- PINMAPPING.put("16", "C");
- PINMAPPING.put("45", "*");
- PINMAPPING.put("35", "0");
- PINMAPPING.put("25", "#");
- PINMAPPING.put("15", "D");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement