Sets an output pin to be either high or low @param pin GPIO pin @param value GPIO.HIGH (1) or GPIO.LOW (0) @see pinMode @see digitalRead @webref
(int pin, int value)
| 210 | * @webref |
| 211 | */ |
| 212 | public static void digitalWrite(int pin, int value) { |
| 213 | checkValidPin(pin); |
| 214 | |
| 215 | String out; |
| 216 | if (value == LOW) { |
| 217 | // values are also stored in a bitmap to make it possible to set a |
| 218 | // default level per pin before enabling the output |
| 219 | values.clear(pin); |
| 220 | out = "0"; |
| 221 | } else if (value == HIGH) { |
| 222 | values.set(pin); |
| 223 | out = "1"; |
| 224 | } else { |
| 225 | System.err.println("Only GPIO.LOW and GPIO.HIGH, 0 and 1, or true and false, can be used."); |
| 226 | throw new IllegalArgumentException("Illegal value"); |
| 227 | } |
| 228 | |
| 229 | if (NativeInterface.isSimulated()) { |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | String fn = String.format("/sys/class/gpio/gpio%d/value", pin); |
| 234 | int ret = NativeInterface.writeFile(fn, out); |
| 235 | if (ret < 0) { |
| 236 | if (ret != -2) { // ENOENT, pin might not yet be exported |
| 237 | throw new RuntimeException(NativeInterface.getError(ret)); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | |
| 243 | /** |
nothing calls this directly
no test coverage detected