Raspberry Pi GPIO和swing计时器无法正常工作

时间:2016-11-26 20:45:36

标签: java swing timer raspberry-pi gpio

我正在开发一个项目,可以跟踪定期发生的事件。该项目将收集有关该事件的数据,然后通过电子邮件在该时间段结束时报告发生的事件。

我正在使用Raspberry Pi 2板,事件由GPIO输入触发。我们使用GPIO处理程序和javax.swing.Timer来处理延迟。

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;
import com.myproject.MySettings;

public class main {
    private Timer eventTimer = new Timer(0, null);
    private static int counter = 0;
    private static MySettings = new MySettings();

    public static void main(String[] args) throws InterruptedException {
        final GpioController gpio = GpioFactory.getInstance();
        final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullDownResistance.PULL_DOWN);
        myButton.setShutdownOptions(true);

        eventTimer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent eventArgs) {
                // send e-mail
                counter = 0;
                eventTimer.stop();
            }
        });

        myButton.addListener(new GpioPinListenerDigital() {
            @Override
            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent eventArgs) {
                if (myButton.isHigh()) {
                    counter++;
                    if (!eventTimer.isRunning()) {
                        MySettings.update();
                        eventTimer.setDelay(MySettings.frequency * 60 * 1000);  // frequency is in minutes. Set delay to milliseconds
                        eventTimer.start();
                    } // else timer is running so do nothing
                }
            }
        });

        while(true) {
            Thread.sleep(300);
        }
    }
};

我们使用以下测试应用程序从中获取了GPIO部分。使用以下代码,摆动计时器似乎在PI上正常工作:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;


public class tester {
private static Timer eventMonitorTimer = null;// = new Timer(0, null);//
private static long startTime = 0;
private static int count = 0;

public static void main(String[] args) throws InterruptedException {
    System.out.println("Hello World");

    System.out.println("Creating timer...");
    eventMonitorTimer = new Timer(1000, new ActionListener() {
        // do stuff         @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            System.out.println("Delay: " + eventMonitorTimer.getDelay());
            count++;
            System.out.println(System.currentTimeMillis());
            eventMonitorTimer.setDelay(count * 1000);
        }
    });
    System.out.println("Timer created");
    if (!eventMonitorTimer.isRunning()) {
        System.out.println("Starting timer...");
        eventMonitorTimer.start();
    }
        while(true){
            Thread.sleep(300);
        }
    }
};

上面的代码每次都增加了延迟,并且计时器似乎正常工作。但是使用第一个代码示例和GPIO事件处理程序,它似乎无法正常工作。发生GPIO事件时,计时器似乎立即被触发。这不是我们想要的。任何想法在这里发生了什么?

0 个答案:

没有答案