Macro for PowerStatus
Hello!
I'm trying to write a macro to make the led indicate if the sensor is currently charging (green led = charging, red led = not charging). I am working with FW 1.4.1.
My code is:
final Macro macro = mwBoard.getModule(Macro.class);
macro.ereaseAll();
macro.startRecord(true);
final Led led = mwBoard.getModule(Led.class);
Timer timer = mwBoard.getModule(Timer.class);
timer.scheduleAsync(100, true, new CodeBlock() {
@Override
public void program() {
settingsModule.readCurrentPowerStatusAsync().continueWith(task -> {
int battery_level = task.getResult().intValue();
if (battery_level == 1) {
led.stop(true);
led.editPattern(Led.Color.GREEN, Led.PatternPreset.SOLID)
.commit();
led.play();
} else {
led.stop(true);
led.editPattern(Led.Color.RED, Led.PatternPreset.SOLID)
.commit();
led.play();
}
return null;
});
}
}).continueWith((Task<Timer.ScheduledTask> task) -> {
Timer.ScheduledTask scheduledTask = task.getResult();
scheduledTask.start();
return null;
});
macro.endRecordAsync().continueWithTask(task1 ->{
macro.execute(task1.getResult());
return null;
});
What I get is not something I expected:
- The led sometimes turns from red to green when put in charge, but never the other way around
- If repetitively connecting and disconnecting the USB to power supply the sensor gets disconnected
I know there have been already similar questions, but they are quite old and I didn't find them 100% clarifying.
This discussion has been closed.
Comments
As stated in the macro documentation:
And timer documentation:
The code are you programming consists of non-MetaWear functions. For your specific use case, you will need to use the data processing functions in the Route API. You can refer to the Macro unit tests for example code:
https://github.com/mbientlab/MetaWear-SDK-Android/blob/3.5.0/library/src/test/java/com/mbientlab/metawear/TestMacro.java#L113
You also need to chain async tasks not nest them.