source.count() reset and timed reset

Hi,

I'm trying to use a Metamotion R switch to send out a message.
I'd like to send it out after pressing two times and then reset the counter; so far I managed to send message out after pushing two times but I cannot figure out how to reset the count, below a snippet of my code:

public void readSwitch() {
    final Switch mwSwitch = metawear.getModule(Switch.class);

mwSwitch.state().addRouteAsync(new RouteBuilder() {
@Override
public void configure(RouteComponent source) {
source.count().filter(Comparison.GTE, (byte) 4).stream(new Subscriber() {
@Override
public void apply(final Data data, Object... env) {

if (data.value(Boolean.class)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(
MainActivity.this,
"HELLO!\nTHIS IS SWITCH MESSAGE :-)",
Toast.LENGTH_SHORT
).show();
}
});
new DataProcessor.CounterEditor() {
@Override
public void reset() {
// how to reset data count ?!?;
}
@Override
public void set(int value) { }
};
}
}
});
}
});
}
I've also tried with:
source.delay((byte) 2).limit(Passthrough.COUNT, (byte) 2).stream(new Subscriber()) ...
but of course after the second pushing  it's not possible to repeat the procedure ... unless app is restarted and I'd like to avoid this behaviour.

Further I'd also like to reset data count if after pushing the first time second one doesn't come within a couple of seconds; I've tried using android countdown timer but it seems it was totally ignored, how I can achieve this "timed" data reset?

thanks
regards

Comments

  • Name the counter with a name component and use the edit function in the DataProcessor module to get the appropriate editor object. See the documentation and unit test:

    You can schedule a reset with the MetaWear timer and will also have to add in the logic to start/stop the timer.
  • edited June 14
    Hi Eric,

    thanks for your previous reply; I got how to reset the switch counter but, unfortunately, still fail on how to schedule an action by metawear timer.
    I even went through some post having the same topic on this forum but still cannot get it, so far my code is:
    Timer mwTimer;
    Timer.ScheduledTask lateToast;
    public void showToast3SecondsLater() {
    mwTimer = metawear.getModule(Timer.class);
    mwTimer.scheduleAsync(3000, (short) 1, false, new CodeBlock() {
    @Override
    public void program() {
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    Toast.makeText(
    getApplicationContext(),
    "Hello,\nthis is 3 seconds delay Toast ...",
    Toast.LENGTH_SHORT
    ).show();
    }
    });
    }
    }).continueWithTask(new Continuation<Timer.ScheduledTask, Task<Timer.ScheduledTask>>() {
    @Override
    public Task<Timer.ScheduledTask> then(Task<Timer.ScheduledTask> task) throws Exception {
    lateToast = task.getResult();
    lateToast.start();
    return null;
    }
    });
    }

    When I run this code (strangely ?) the Toast appears immediately altough it's been declared as AsyncTask, besides if I check for Task error I get NullPointerException: Attempt to read from null array.

    Would you please be so kind to clarify the "foggy" in my mind?
    Thanks
    Regards


  • MetaWear timers are only for MetaWear commands.  If you are doing Android related tasks, you will have to use the timer APIs provided by the Android SDK.
  • Hi Eric, 

    finally I got it! :-)

    my apologies for yesterday's nonsense post (a bit of frustration) and thanks for your patience.
    regards
Sign In or Register to comment.