In summary, my project is a JavaFX application which controls a stepper motor, controlled by the RaspberryPi using the Pi4j library. The first version of my program was a simpler Java implementation run through the terminal. In v1, I had 2 classes: main and motor. The gpio initialization for the motor was done in the motor class and the buttons within the main class. To operate the motor, I created an instance of the motor class and access with motor.run();
.
In v2, I'm adding a scheduler into the program, which I'm trying to implement with the Quartz API, but now I need to access the motor class from not just main, but the schedule class as well. In my schedule class, I receive parameters for the job and trigger from main and the job is either motor.up()
or motor.down()
. I can't access a non-static method from a static context is what the issue is. If I create another instance of motor within schedule, it will throw errors for re-initializing the gpio pins.
I can't initialize the gpio in the main class because then I have the same non-static to static issue running motor methods off the main inits.
If I create an instance of main within schedule and access like shade.motor.up()
I don't have any compile errors, but this doesn't seem correct because every time I create a new job a new instance of my main GUI is created.
Is there a proper way to globally initialize the GPIO so I don't have to worry about creating multiple instances of the motor class.
I'm still figuring out how to schedule the jobs so the code is very incomplete, but this is basically pseudo code for how I want to implement it.
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;
//
}
}