运行时出现Java RPi GPIO问题

时间:2016-03-22 18:42:18

标签: java raspberry-pi gpio

我的代码是这个

package me.Andrew.Keypad.Listener;

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 me.Andrew.Keypad.Main;

public class ButtonInput {
    public boolean isRunning = false;
    public GpioController gpio = GpioFactory.getInstance();
    Main MA = Main.getInstance();

    public HashMap<String, GpioPinDigitalInput> ColPins = null;
    public HashMap<String, GpioPinDigitalOutput> RowPins = null;

    public void start() {
        isRunning = true;
        ButtonListener();
    }

    public void stop() {
        isRunning = false;
    }

    private int ScanRow(int row) {
        int Col = 0;
        RowPins.get(String.valueOf(row)).high();
        if (ColPins.get("1").isHigh()) {
            Col = 1;
        } else if (ColPins.get("2").isHigh()) {
            Col = 2;
        } else if (ColPins.get("3").isHigh()) {
            Col = 3;
        } else if (ColPins.get("4").isHigh()) {
            Col = 4;
        }
        return Col;
    }

    private void ButtonListener() {

        Main.GPIOMeths.setupPins();
        RowPins = Main.store.RowPins;
        ColPins = Main.store.ColPins;

        int Response = 0;

        while (isRunning == true) {
            if (isRunning) {
                Response = ScanRow(1);

                if (Response == 1) {
                    stop();
                    buttonPressEvent("1");
                    break;
                }else if (Response == 2) {
                    stop();
                    buttonPressEvent("2");
                    break;
                }else if (Response == 3) {
                    stop();
                    buttonPressEvent("3");
                    break;
                }else if (Response == 4) {
                    stop();
                    buttonPressEvent("A");
                    break;
                }

                int Responsee = ScanRow(2);
                if (Responsee != 0) {
                    stop();
                    String BP = Main.Meths.getButton(2, Responsee);
                    buttonPressEvent(BP);
                    break;
                }

                int Responseee = ScanRow(3);
                if (Responseee != 0) {
                    stop();
                    String BP = Main.Meths.getButton(3, Responseee);
                    buttonPressEvent(BP);
                    break;
                }

                int Responseeee = ScanRow(4);
                if (Responseeee != 0) {
                    stop();
                    String BP = Main.Meths.getButton(4, Responseeee);
                    buttonPressEvent(BP);
                    break;
                }
            }

        }
    }

    public void buttonPressEvent(String ButtonPressed) {
        // String ButtonPressed = Main.Meths.getButton(Row, Col);
        if (ButtonPressed.equalsIgnoreCase("*")) {
            System.out.println("Code Logging Started");
            Main.store.CodeMode = true;
            Main.store.CodeEntered = "";
        } else if (ButtonPressed.equalsIgnoreCase("#")) {
            System.out.println("Code Logging Ended");
            if (Main.Meths.CompareCodes()) {
                System.out.println("Code Correct");
            } else {
                System.out.println("Code Incorrect");
            }
            Main.store.CodeMode = false;
        } else {
            if (Main.store.CodeMode == false) {
                System.out.println("Code Logging Not Running: " + ButtonPressed);
            } else {
                Main.store.CodeEntered = Main.store.CodeEntered + ButtonPressed;
                System.out.println("Code Entered: " + Main.store.CodeEntered);
            }
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
        } finally {
            start();
        }

    }
}

但是当我按下1,2,3时,A所有的顶行我得到了这个回复

root @ raspberrypi:/ home / pi #java -jar /home/pi/keypad.jar 代码记录未运行:7 代码记录未运行:0 代码记录未运行:9 代码记录未运行:A

代码记录未运行:7 代码记录未运行:5 代码记录未运行:3 代码记录未运行:A

1 个答案:

答案 0 :(得分:0)

已添加

RowPins.get(String.valueOf(row)).low();

在扫描行的末尾