Skip to content

Instantly share code, notes, and snippets.

@mbientlab
Last active August 29, 2015 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbientlab/11d00d8e980f2952dfe6 to your computer and use it in GitHub Desktop.
Save mbientlab/11d00d8e980f2952dfe6 to your computer and use it in GitHub Desktop.
Example using the timer and event module to stagger LED animations on different color channels
import com.mbientlab.metawear.api.MetaWearController;
import com.mbientlab.metawear.api.Module;
import com.mbientlab.metawear.api.controller.Event;
import com.mbientlab.metawear.api.controller.LED;
import com.mbientlab.metawear.api.controller.LED.ChannelDataWriter;
import com.mbientlab.metawear.api.controller.LED.ColorChannel;
import com.mbientlab.metawear.api.controller.Timer;
public class StaggeredLEDAnimation {
///< Duration for one pulse, in milliseconds
private static final short DURATION= 5000;
public static void programChannel(ChannelDataWriter writer) {
writer.withHighIntensity((byte) 31)
.withHighTime(DURATION / 2)
.withFallTime(DURATION / 2)
.withPulseDuration(DURATION)
.withRepeatCount((byte) 1).commit();
}
public static void setup(final MetaWearController mwCtrllr) {
final Event eventCtrllr= (Event) mwCtrllr.getModuleController(Module.EVENT);
final Timer timerCtrllr= (Timer) mwCtrllr.getModuleController(Module.TIMER);
final LED ledCtrllr= (LED) mwCtrllr.getModuleController(Module.LED);
mwCtrllr.addModuleCallback(new Timer.Callbacks() {
private byte green= -1, blue= -1;
@Override
public void receivedTimerId(byte timerId) {
eventCtrllr.recordMacro(Timer.Register.TIMER_NOTIFY, timerId);
if (green == -1) {
green= timerId;
programChannel(ledCtrllr.setColorChannel(ColorChannel.GREEN));
} else {
programChannel(ledCtrllr.setColorChannel(ColorChannel.BLUE));
mwCtrllr.removeModuleCallback(this);
}
eventCtrllr.stopRecord();
timerCtrllr.startTimer(timerId);
}
});
programChannel(ledCtrllr.setColorChannel(ColorChannel.RED));
///< For API < 1.7.0 the 'delay' parameter is reversed, use false to delay the timer instead of true
///< For API >= 1.7.0, the delay parameter is correctly used.
timerCtrllr.addTimer(DURATION, (short) 1, false);
timerCtrllr.addTimer(DURATION * 2, (short) 1, false);
///< Play the pattern with autoplay enabled
ledCtrllr.play(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment