Timer¶
The Timer class schedules tasks to be executed periodically on the board.
Scheduling a Task¶
To schedule a task, call the scheduleTask method, passing in a Timer.Task encapsulating the MetaWear commands to execute. The method returns an AsyncOperation object that wraps a controller object used to manage the scheduled task.
import com.mbientlab.metawear.AsyncOperation;
import com.mbientlab.metawear.module.Gpio;
import com.mbientlab.metawear.module.Timer;
final Gpio gpioModule= mwBoard.getModule(Gpio.class);
// Schedule a task that reads the adc value every 1000ms (1s)
AsyncOperation<Timer.Controller> taskResult= mwBoard.getModule(Timer.class)
.scheduleTask(new Timer.Task() {
@Override
public void commands() {
gpioModule.readAnalogIn((byte) 0, Gpio.AnalogReadMode.ADC);
}
}, 1000, false);
Controller¶
The Timer.Controller class manages a scheduled task. You can start, stop, and remove a task. Typically, controllers are returned as a result of scheduling a task, though if you know the numerical ID of a controller, you can directly access it by calling getController.
taskResult.onComplete(new CompletionHandler<Timer.Controller>() {
@Override
public void success(Timer.Controller result) {
// start executing the task
result.start()
}
});