Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application;
- 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;
- import java.beans.PropertyChangeEvent;
- import java.beans.PropertyChangeListener;
- import java.util.ArrayList;
- import java.util.List;
- public class KeypadTest {
- private List<PropertyChangeListener> listener = new ArrayList<PropertyChangeListener>();
- public static final String KEY = "key";
- private char keyPressed;
- /** The Constant KEYPAD. */
- private static final char layout[][] = {
- { '1', '2', '3' },
- { '4', '5', '6' },
- { '7', '8', '9' },
- { 'A', '0', 'B' }
- };
- /** The gpio. */
- private final GpioController gpio = GpioFactory.getInstance();
- /** The Constant PIN_1_IN. */
- private static final Pin PIN_1_IN = RaspiPin.GPIO_24;
- /** The Constant PIN_2_IN. */
- private static final Pin PIN_2_IN = RaspiPin.GPIO_25;
- /** The Constant PIN_3_IN. */
- private static final Pin PIN_3_IN = RaspiPin.GPIO_26;
- /** The Constant PIN_4_IN. */
- private static final Pin PIN_4_IN = RaspiPin.GPIO_23;
- /** The Constant PIN_5_OUT. */
- private static final Pin PIN_5_OUT = RaspiPin.GPIO_27;
- /** The Constant PIN_6_OUT. */
- private static final Pin PIN_6_OUT = RaspiPin.GPIO_28;
- /** The Constant PIN_7_OUT. */
- private static final Pin PIN_7_OUT = RaspiPin.GPIO_29;
- /** The Row 1. */
- private final GpioPinDigitalInput row1 = gpio
- .provisionDigitalInputPin(PIN_1_IN, PinPullResistance.PULL_UP);
- /** The Row 2. */
- private final GpioPinDigitalInput row2 = gpio
- .provisionDigitalInputPin(PIN_2_IN, PinPullResistance.PULL_UP);
- /** The Row 3. */
- private final GpioPinDigitalInput row3 = gpio
- .provisionDigitalInputPin(PIN_3_IN, PinPullResistance.PULL_UP);
- /** The Row 4. */
- private final GpioPinDigitalInput row4 = gpio
- .provisionDigitalInputPin(PIN_4_IN, PinPullResistance.PULL_UP);
- /** The Column 5. */
- private final GpioPinDigitalOutput col5 = gpio
- .provisionDigitalOutputPin(PIN_5_OUT);
- /** The Column 6. */
- private final GpioPinDigitalOutput col6 = gpio
- .provisionDigitalOutputPin(PIN_6_OUT);
- /** The Column 7. */
- private final GpioPinDigitalOutput col7 = gpio
- .provisionDigitalOutputPin(PIN_7_OUT);
- /** The Columns. */
- private final GpioPinDigitalOutput theColumns[] = { col5, col6,
- col7 };
- /** The input. */
- private GpioPinDigitalInput theInput;
- /** The in id. */
- private int theLin;
- /** The in id. */
- private int theCol;
- /**
- * Instantiates a new piezo keypad.
- */
- public KeypadTest() {
- initListeners();
- }
- /**
- * 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 < theColumns.length; myO++) {
- for (final GpioPinDigitalOutput myTheOutput : theColumns) {
- myTheOutput.high();
- }
- theColumns[myO].low();
- // input found?
- if (theInput.isLow()) {
- theCol = myO;
- checkPins();
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- }
- break;
- }
- }
- for (final GpioPinDigitalOutput myTheOutput : theColumns) {
- myTheOutput.low();
- }
- }
- /**
- * Check pins.
- *
- * Determins the pressed key based on the activated GPIO pins.
- */
- private synchronized void checkPins() {
- notifyListeners(this, KEY, this.keyPressed,
- this.keyPressed = layout[theLin - 1][theCol]);
- System.out.println(layout[theLin - 1][theCol]);
- }
- /**
- * Inits the listeners.
- */
- private void initListeners() {
- row1.addListener(new GpioPinListenerDigital() {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent event) {
- if (event.getState() == PinState.LOW) {
- theInput = row1;
- theLin = 1;
- findOutput();
- }
- }
- });
- row2.addListener(new GpioPinListenerDigital() {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent event) {
- if (event.getState() == PinState.LOW) {
- theInput = row2;
- theLin = 2;
- findOutput();
- }
- }
- });
- row3.addListener(new GpioPinListenerDigital() {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent event) {
- if (event.getState() == PinState.LOW) {
- theInput = row3;
- theLin = 3;
- findOutput();
- }
- }
- });
- row4.addListener(new GpioPinListenerDigital() {
- @Override
- public void handleGpioPinDigitalStateChangeEvent(
- final GpioPinDigitalStateChangeEvent event) {
- if (event.getState() == PinState.LOW) {
- theInput = row4;
- theLin = 4;
- findOutput();
- }
- }
- });
- }
- private void notifyListeners(Object object, String property, char oldValue,
- char newValue) {
- for (PropertyChangeListener name : listener) {
- name.propertyChange(new PropertyChangeEvent(this, property,
- oldValue, newValue));
- }
- }
- public void addChangeListener(PropertyChangeListener newListener) {
- listener.add(newListener);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement