Pin 21/27不适用于Pi4j

时间:2014-02-05 21:44:39

标签: java raspberry-pi gpio

我有一个Raspberry Pi rev。 2(512mb ram版)。我有一些我控制Pi4J的LED。除了我连接/连接到引脚21/27的LED外,每个LED都工作。我已经更换了LED,看看是否有一个坏了,但没有坏掉。 我在google上做了一些研究,发现this。不幸的是我找不到将Pin21更改为27的方法,因为Pi4j使用了另一种编号方案。 (编号见here

我在代码中找不到错误,但看起来像这样:

    包gpio;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;

public class GPIOController {

    final long second = 1000L;

    final GpioController gpio = GpioFactory.getInstance();

    final GpioPinDigitalOutput red_1 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_03);
    final GpioPinDigitalOutput red_2 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_02);

    final GpioPinDigitalOutput yellow_1 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_00);
    final GpioPinDigitalOutput yellow_2 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_07);

    final GpioPinDigitalOutput green_1 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_06);
    final GpioPinDigitalOutput green_2 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_05);
    final GpioPinDigitalOutput green_3 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_04);
    final GpioPinDigitalOutput green_4 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_01);

    public GPIOController() {
        gpio.shutdown();
    }

    // level 1 = red_1, level 8 = green_4
    public void pulse(int level) {
        allLow();
        switch(level) {
        case 1:
            red_1.pulse(second);
            break;
        case 2:
            red_2.pulse(second);
            break;
        case 3:
            yellow_1.pulse(second);
            break;
        case 4:
            yellow_2.pulse(second);
            break;
        case 5:
            green_1.pulse(second);
            break;
        case 6:
            green_2.pulse(second);
            break;
        case 7:
            green_3.pulse(second);
            break;
        case 8:
            green_4.pulse(second);
            break;
        }
    }

    public void high(int level) {
        allLow();
        switch(level) {
        case 1:
            red_1.high();
            break;
        case 2:
            red_2.high();
            break;
        case 3:
            yellow_1.high();
            break;
        case 4:
            yellow_2.high();
            break;
        case 5:
            green_1.high();
            break;
        case 6:
            green_2.high();
            break;
        case 7:
            green_3.high();
            break;
        case 8:
            green_4.high();
            break;
        }
    }

    public void allLow() {
        gpio.shutdown();
    }

}

我通过这个类调用方法:

package connection;

import gpio.GPIOController;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Receiver {

    private ServerSocket server;
    private Socket client;
    private DataInputStream clientIn;
    private DataOutputStream clientOut;

    private GPIOController gpioController = new GPIOController();

    public Receiver(int port) throws IOException {
        server = new ServerSocket(port);
        listenForClient();
    }

    // client listener in a new thread
    public void listenForClient() {
        Runnable listener = new Runnable() {
            public void run() {
                while (true)
                    try {
                        // only the most recently connected client
                        // can control the LEDs
                        client = server.accept();
                    } catch (IOException e) {
                        e.printStackTrace(System.out);
                    }

            }
        };
        Thread listenerThread = new Thread(listener);
        listenerThread.setName("Client Listener");
        listenerThread.start();
    }

    // command listener in a new thread
    public void listenForCommands() {
        final Runnable listener = new Runnable() {
            public void run() {
                while (true) {
                    try {
                        if (client != null) {
                            DataInputStream clientIn = new DataInputStream(
                                client.getInputStream());
                            String msg = clientIn.readUTF();
                            handleMessage(msg);
                        }
                    } catch (IOException e) {
                        e.printStackTrace(System.out);
                    }
                }
            }
        };
        Thread listenerThread = new Thread(listener);
        listenerThread.setName("Command Listener");
        listenerThread.start();
    }

    public void handleMessage(String msg) {
        String[] command = msg.split(" ");
        String commandType = command[0]; // "pulse" or "permanent"
        for (int i = 1; i < command.length; i++) {
            try {
                if (commandType.equals("pulse"))
                    gpioController.pulse(Integer.parseInt(command[i]));
                else
                    gpioController.high(Integer.parseInt(command[i]));
            } catch (Exception e) {
                try {
                    clientOut = new DataOutputStream(client.getOutputStream());
                    clientOut.writeUTF(e.getMessage());
                } catch (IOException ioe) {
                    ioe.printStackTrace(System.out);
                }
            }
        }
    }

    public static void main(String[] args) {
        try {
            new Receiver(8374).listenForCommands();
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
    }

}

我能做些什么来让Pin工作(包括快速和肮脏的解决方案)?顺便说说;我不想使用P5针脚,因为我使用的是PiCobbler。我也想为此使用Java。 提前谢谢。

塞利姆

1 个答案:

答案 0 :(得分:0)

所以,我猜这是一个与发行版有关的错误。 sudo apt-get dist-upgrade修正了它。