Using the Button on the C

Hi all, 

I have trouble to use the button on the C board - below code does not work and it will be a super simple solution, I know.

public void enableButton()
{
try {
Switch button = mwBoard.getModule(Switch.class);
Log.i("MainActivity", "Got Button, enabling...");

button.routeData().fromSensor().monitor(new DataSignal.ActivityHandler() {
@Override
public void onSignalActive(Map<String, DataProcessor> map, DataSignal.DataToken dataToken) {
Log.i("MainActivity", "Button pressed.");
}
});

} catch (UnsupportedModuleException e) {
e.printStackTrace();
}

}

I am using the Android code on a Nexus 5X, I hope there are no device issues. 




Comments

  • You need to use a stream not a monitor.

  • OK, so probably something like this: but where is the "Switch" documented, e.g. "button" is just an idea, where do I find a list of topics that are allowed for the Switch?
    button.routeData().fromSensor().stream("button").commit().onComplete(new AsyncOperation.CompletionHandler<RouteManager>() {
    @Override
    public void success(RouteManager result) {
    Log.i("MainActivity", "Button clicked.");
    }
    });
  • OK, looked at the sample app and it uses the switch module a few times. None of that seems to work for me though. Right now I do this;

    button.routeData().fromSensor()
    .process("accumprocesser", "accumulator")
    .process("math?operation=modulus&rhs=2")
    .split()
    .branch().process("comparison?operation=eq&reference=1").monitor(new DataSignal.ActivityHandler() {
    @Override

    public void onSignalActive(Map<String, DataProcessor> processors, DataSignal.DataToken token) {
    Log.i("MainActivity", "BUTTON 1");
    }
    })
    .branch().process("comparison?operation=eq&reference=0").monitor(new DataSignal.ActivityHandler() {
    @Override
    public void onSignalActive(Map<String, DataProcessor> processors, DataSignal.DataToken token) {
    Log.i("MainActivity", "BUTTON 0");
    }
    })
    .end()
    .commit();

    I get a Button 1 and Button 0 log output when starting up. But when I then press the button, nothing happens....


  • The Android Switch class is not the same as the MetaWear Switch class.  The MetaWear Switch is a simple class with only 1 function and is only used here in the sample app.

    There seems to be some confusion over the concepts of a stream and monitor, and how to use them.  A stream effectively tells the MetaWear to send data live to your mobile device.  Once a stream is setup, you have to attach MessageHandler to it to process the incoming data.  On the other hand, a monitor simply watches for signal activity, which is when new data is created, and executes MetaWear commands in response to new activity.  If a function is not a part of the com.mbientlab.metawear package, it's not a MetaWear command.

    In your stream example, you still have to subscribe to the data stream and in the monitor example, you have to fill in MetaWear commands to execute.

    You should start by copy and pasting the full code as is from the sample code in the Routing Sensor Data section to convince yourself that everything is properly setup.
  • Many thx, @Eric, it works! Code below for all others that want to use the Switch class.

    try {
    Switch switchModule = mwBoard.getModule(Switch.class);

    switchModule.routeData().fromSensor().stream("switch_stream").commit()
    .onComplete(new AsyncOperation.CompletionHandler<RouteManager>() {
    @Override
    public void success(RouteManager result) {
    result.subscribe("switch_stream", new RouteManager.MessageHandler() {
    @Override
    public void process(Message msg) {
    boolean pressed = msg.getData(Boolean.class);
    Log.i(TAG, "Switch " + ((pressed) ? "pressed." : "released."));

    }
    });
    }
    });
    } catch (UnsupportedModuleException ignored) {
    }
This discussion has been closed.