To accomplish this, I have the following code (I'm using an MCP23017 to do the job):
private static final GpioController gpio = GpioFactory.getInstance();
final MCP23017GpioProvider mcp23017GpioProvider = new MCP23017GpioProvider(I2CBus.BUS_1, 0x20);
Button switch_01_BtnA = new GpioButtonComponent(
gpio.provisionDigitalInputPin(mcp23017GpioProvider, MCP23017Pin.GPIO_B0
, "MyInput-B0", PinPullResistance.OFF
)
, PinState.HIGH
, PinState.LOW
);
Relay relay_V01 = new GpioRelayComponent(
gpio.provisionDigitalOutputPin(mcp23017GpioProvider, MCP23017Pin.GPIO_B1
, "MyOutput-B1", PinState.LOW
)
);
LED switch_01_Led1 = new GpioLEDComponent(
gpio.provisionDigitalOutputPin(mcp23017GpioProvider, MCP23017Pin.GPIO_A7
, "MyOutput-A7", PinState.LOW
)
);
// create switch listener
switch_01_BtnA.addListener(new ButtonPressedListener() {
public void onButtonPressed(ButtonEvent event) {
// display console message
AnsiConsole.out().println(Ansi.ansi()
.fgBright(Ansi.Color.CYAN)
.a("Switch_01_BtnA pressed").reset()
);
relay_V01.toggle();
if (relay_V01.getState() == RelayState.OPEN) {
switch_01_Led1.blink(0);
switch_01_Led1.off();
} else {
switch_01_Led1.blink(500);
}
relayOn = !relayOn;
}
});
switch_01_BtnA.addListener(new ButtonReleasedListener() {
public void onButtonReleased(ButtonEvent event) {
// display console message
AnsiConsole.out().println(Ansi.ansi()
.fgBright(Ansi.Color.CYAN)
.a("Switch_01_BtnA released").reset()
);
}
});
Although I'm having both ButtonPressedListener as well as ButtonReleasedListener, I'm only interested in the ButtonPressedListener. Hence, the "logic" is within the onButtonPressed() method.
To know if I have to switch on or off the blinking, I'm checking the state of the relay after I've toggled the relay. And here's where the problem lies:
So far, I was not able to debug this decently, but I'm afraid it has something to do with the cached state of the relay...
Unless someone finds something wrong in the above code?
PS: I've also tried to switch the initial state of switch_01_BtnA while creating the instance, but that doesn't solve the issue...
PPS: It would be really nice if the ToggleSwitch component would indeed implement the real toggle function. Maybe I wouldn't have to rely on the getState() of the relay component then...
...
if (relay_V01.getState() == RelayState.CLOSED) {
switch_01_Led1.blink(0);
switch_01_Led1.off();
relay_V01.open();
} else {
switch_01_Led1.blink(500);
relay_V01.close();
}
..
switchAanrechtRechtsKnopA.addListener(new SwitchListener() {
@Override
public void onStateChange(SwitchStateChangeEvent event) {
if (event.getNewState() == SwitchState.ON) {
relayOnderlichtKeuken.toggle();
if (relayOnderlichtKeuken.isClosed()) {
switchAanrechtRechtsLedA.on();
} else {
switchAanrechtRechtsLedA.off();
}
}
System.out.println("[ SWITCH S2_A PRESSED ]: " + event.getNewState());
}
});
Any idea if the issues were maybe solved with the official Pi4J release (I was using the SNAPSHOT version initially, now I moved to the official Pi4J release...)?