Skip to content

Commit

Permalink
Merge pull request #63 from kurbatov/fix/62-reverting-digital-pin-update
Browse files Browse the repository at this point in the history
Revert "fix(pin): send correct message when changing digital output pin's value"
  • Loading branch information
kurbatov authored May 23, 2023
2 parents b722b83 + fd57b0b commit 9c8ab05
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
13 changes: 1 addition & 12 deletions src/main/java/org/firmata4j/firmata/FirmataMessageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/org/firmata4j/firmata/FirmataPin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2021 Oleg Kurbatov ([email protected])
* Copyright (c) 2014 Oleg Kurbatov ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 9c8ab05

Please sign in to comment.