From fd57b0b3751b875f4fed91ebf401cc1ed0281ce8 Mon Sep 17 00:00:00 2001 From: Oleg Kurbatov Date: Tue, 23 May 2023 23:58:59 +0300 Subject: [PATCH] Revert "fix(pin): send correct message when changing digital output pin's value" This reverts commit 7d89ad9d5eb75e8e9fbf1c3f3acde98a621042ed. --- .../firmata/FirmataMessageFactory.java | 13 +--------- .../org/firmata4j/firmata/FirmataPin.java | 25 ++++++++++++++++--- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/firmata4j/firmata/FirmataMessageFactory.java b/src/main/java/org/firmata4j/firmata/FirmataMessageFactory.java index 6effc35..20beabd 100644 --- a/src/main/java/org/firmata4j/firmata/FirmataMessageFactory.java +++ b/src/main/java/org/firmata4j/firmata/FirmataMessageFactory.java @@ -222,20 +222,9 @@ public static byte[] setMode(byte pinId, Pin.Mode mode) { * @param value values of port's pins * @return Firmata message to set digital output */ - public static byte[] setDigitalPortValue(byte portId, byte value) { + public static byte[] setDigitalPinValue(byte portId, byte value) { return new byte[]{(byte) (DIGITAL_MESSAGE | (portId & 0x0F)), (byte) (value & 0x7F), (byte) ((value >>> 7) & 0x7F)}; } - - /** - * Creates Firmata message to set digital values of a pin. - * - * @param pinId index of a pin - * @param value value of the pin (0 or 1) - * @return Firmata message to set the value of a digital output pin - */ - public static byte[] setDigitalPinValue(byte pinId, byte value) { - return new byte[]{SET_DIGITAL_PIN_VALUE, pinId, value}; - } /** * Creates Firmata message to set value of an output pin in PWM mode.
diff --git a/src/main/java/org/firmata4j/firmata/FirmataPin.java b/src/main/java/org/firmata4j/firmata/FirmataPin.java index 2bfeac0..4bf2f9b 100644 --- a/src/main/java/org/firmata4j/firmata/FirmataPin.java +++ b/src/main/java/org/firmata4j/firmata/FirmataPin.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2014-2021 Oleg Kurbatov (o.v.kurbatov@gmail.com) + * Copyright (c) 2014 Oleg Kurbatov (o.v.kurbatov@gmail.com) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -132,11 +132,28 @@ public synchronized void setValue(long value) throws IOException, IllegalStateEx byte[] message; long newValue; if (currentMode == Mode.OUTPUT) { - newValue = value > 0 ? 1 : 0; - message = FirmataMessageFactory.setDigitalPortValue(pinId, (byte) newValue); + //have to calculate the value of whole port (8-pin set) the pin sits in + byte portId = (byte) (pinId / 8); + byte pinInPort = (byte) (pinId % 8); + byte portValue = 0; + for (int i = 0; i < 8; i++) { + Pin p = device.getPin(portId * 8 + i); + if (p.getMode() == Mode.OUTPUT && p.getValue() > 0) { + portValue |= 1 << i; + } + } + byte bitmask = (byte) (1 << pinInPort); + boolean val = value > 0; + if (val) { + portValue |= bitmask; + } else { + portValue &= ((byte) ~bitmask); + } + message = FirmataMessageFactory.setDigitalPinValue(portId, portValue); + newValue = val ? 1 : 0; } else if (currentMode == Mode.ANALOG || currentMode == Mode.PWM || currentMode == Mode.SERVO) { + message = FirmataMessageFactory.setAnalogPinValue(pinId, value); newValue = value; - message = FirmataMessageFactory.setAnalogPinValue(pinId, newValue); } else { throw new IllegalStateException(String.format("Port %d is in %s mode and its value cannot be set.", pinId, currentMode)); }