|
1 | 1 | /*
|
2 | 2 | * The MIT License (MIT)
|
3 | 3 | *
|
4 |
| - * Copyright (c) 2014-2021 Oleg Kurbatov ([email protected]) |
| 4 | + * Copyright (c) 2014 Oleg Kurbatov ([email protected]) |
5 | 5 | *
|
6 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 7 | * of this software and associated documentation files (the "Software"), to deal
|
@@ -132,11 +132,28 @@ public synchronized void setValue(long value) throws IOException, IllegalStateEx
|
132 | 132 | byte[] message;
|
133 | 133 | long newValue;
|
134 | 134 | 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; |
137 | 154 | } else if (currentMode == Mode.ANALOG || currentMode == Mode.PWM || currentMode == Mode.SERVO) {
|
| 155 | + message = FirmataMessageFactory.setAnalogPinValue(pinId, value); |
138 | 156 | newValue = value;
|
139 |
| - message = FirmataMessageFactory.setAnalogPinValue(pinId, newValue); |
140 | 157 | } else {
|
141 | 158 | throw new IllegalStateException(String.format("Port %d is in %s mode and its value cannot be set.", pinId, currentMode));
|
142 | 159 | }
|
|
0 commit comments