Skip to content

Commit fd57b0b

Browse files
committed
Revert "fix(pin): send correct message when changing digital output pin's value"
This reverts commit 7d89ad9.
1 parent b722b83 commit fd57b0b

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/main/java/org/firmata4j/firmata/FirmataMessageFactory.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,9 @@ public static byte[] setMode(byte pinId, Pin.Mode mode) {
222222
* @param value values of port's pins
223223
* @return Firmata message to set digital output
224224
*/
225-
public static byte[] setDigitalPortValue(byte portId, byte value) {
225+
public static byte[] setDigitalPinValue(byte portId, byte value) {
226226
return new byte[]{(byte) (DIGITAL_MESSAGE | (portId & 0x0F)), (byte) (value & 0x7F), (byte) ((value >>> 7) & 0x7F)};
227227
}
228-
229-
/**
230-
* Creates Firmata message to set digital values of a pin.
231-
*
232-
* @param pinId index of a pin
233-
* @param value value of the pin (0 or 1)
234-
* @return Firmata message to set the value of a digital output pin
235-
*/
236-
public static byte[] setDigitalPinValue(byte pinId, byte value) {
237-
return new byte[]{SET_DIGITAL_PIN_VALUE, pinId, value};
238-
}
239228

240229
/**
241230
* Creates Firmata message to set value of an output pin in PWM mode.<br>

src/main/java/org/firmata4j/firmata/FirmataPin.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2014-2021 Oleg Kurbatov ([email protected])
4+
* Copyright (c) 2014 Oleg Kurbatov ([email protected])
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -132,11 +132,28 @@ public synchronized void setValue(long value) throws IOException, IllegalStateEx
132132
byte[] message;
133133
long newValue;
134134
if (currentMode == Mode.OUTPUT) {
135-
newValue = value > 0 ? 1 : 0;
136-
message = FirmataMessageFactory.setDigitalPortValue(pinId, (byte) newValue);
135+
//have to calculate the value of whole port (8-pin set) the pin sits in
136+
byte portId = (byte) (pinId / 8);
137+
byte pinInPort = (byte) (pinId % 8);
138+
byte portValue = 0;
139+
for (int i = 0; i < 8; i++) {
140+
Pin p = device.getPin(portId * 8 + i);
141+
if (p.getMode() == Mode.OUTPUT && p.getValue() > 0) {
142+
portValue |= 1 << i;
143+
}
144+
}
145+
byte bitmask = (byte) (1 << pinInPort);
146+
boolean val = value > 0;
147+
if (val) {
148+
portValue |= bitmask;
149+
} else {
150+
portValue &= ((byte) ~bitmask);
151+
}
152+
message = FirmataMessageFactory.setDigitalPinValue(portId, portValue);
153+
newValue = val ? 1 : 0;
137154
} else if (currentMode == Mode.ANALOG || currentMode == Mode.PWM || currentMode == Mode.SERVO) {
155+
message = FirmataMessageFactory.setAnalogPinValue(pinId, value);
138156
newValue = value;
139-
message = FirmataMessageFactory.setAnalogPinValue(pinId, newValue);
140157
} else {
141158
throw new IllegalStateException(String.format("Port %d is in %s mode and its value cannot be set.", pinId, currentMode));
142159
}

0 commit comments

Comments
 (0)