1

总之,我的项目是一个控制步进电机的 JavaFX 应用程序,由 RaspberryPi 使用 Pi4j 库控制。我的程序的第一个版本是通过终端运行的更简单的 Java 实现。在 v1 中,我有 2 个类:主类和电机类。电机的 gpio 初始化是在电机类和主类中的按钮中完成的。为了操作电机,我创建了电机类的实例并使用motor.run();.

在 v2 中,我在程序中添加了一个调度程序,我正在尝试使用 Quartz API 来实现它,但现在我不仅需要从主类访问电机类,还需要从调度类访问电机类。在我的日程安排类中,我从 main 接收作业和触发器的参数,并且作业是motor.up()or motor.down()。我无法从静态上下文访问非静态方法是问题所在。如果我在计划内创建另一个电机实例,它将引发重新初始化 gpio 引脚的错误。

我无法在主类中初始化 gpio,因为那时我有相同的非静态到静态问题,从主初始化运行电机方法。

如果我在计划内创建一个 main 实例并像shade.motor.up()我没有任何编译错误一样访问,但这似乎不正确,因为每次我创建一个新作业时都会创建一个主 GUI 的新实例。

是否有适当的方法来全局初始化 GPIO,所以我不必担心创建电机类的多个实例。

我仍在研究如何安排作业,因此代码非常不完整,但这基本上是我想要实现它的伪代码。

public class Scheduler  implements Job {
    Autoshade shade = new Autoshade();
    int hour;
    int minute;
    int position;
    int target;
    String description;

    public Scheduler() {

    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        if (target > position) {
            try {
                shade.motor.down(target-position);
            } catch (InterruptedException ex) {
                Logger.getLogger(Scheduler.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (target < position) {
            try {
                shade.motor.up(position-target);
            } catch (InterruptedException ex) {
                Logger.getLogger(Scheduler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void addJob(int hour, int minute, int position, int target, String description) {
        this.hour = hour;
        this.minute = minute;
        this.position = position;
        this.target = target;
        this.description = description;
        //

    }

}
4

0 回答 0