-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from kurbatov/fix/62-reverting-digital-pin-update
Revert "fix(pin): send correct message when changing digital output pin's value"
- Loading branch information
Showing
2 changed files
with
22 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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)); | ||
} | ||
|