kurbatov / firmata4j

Firmata client written in Java.
MIT License
87 stars 45 forks source link

onPinStateReceive should not update pin value #55

Closed oliviercoma closed 2 years ago

oliviercoma commented 2 years ago

According to the Firmata documentation, the pin state response does not return the value of the pin, but: "For input modes, typically the state is zero. However, for digital inputs, the state is the status of the pull-up resistor which is 1 if enabled, 0 if disabled."

Updating the pin value in onPinStateReceive leads to incorrect values being reported by Firmata4j

kurbatov commented 2 years ago

Thanks for reporting that. I'll take a look at this during new year holidays.

oliviercoma commented 2 years ago

Thanks for your feedback. I'm currently working on a fix. I'll create a PR.

oliviercoma commented 2 years ago

Also, the if

if (pin.getMode() == null) {
    pin.initMode(Pin.Mode.resolve((Byte) event.getBodyItem(PIN_MODE)));
}

is also an a potential issue I think. The device should be the owner of the mode, and if it reports the mode to be something (e.g. INPUT), it should not be filtered. It should probably also triggers the onModeChange event.

This issue will probably only happen if some custom code is added to the physical device (i.e. not StandardFirmata) that changes the pin mode directly. Unlikely to happen, but it could lead to a difficult to investigate issue