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.

Comments

This discussion has been closed.