Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix(pin): send correct message when changing digital output pin's value" #63

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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