GeezerVison is showing github.com — IO: Speed up GPIO.pinMode() · crazyrex/processing@4cbf6bc
File tree
Expand file treeCollapse file tree
Open diff view settings
java/libraries/io/src/processing/io
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: java/libraries/io/src/processing/io/GPIO.java +11-9Lines changed: 11 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -356,14 +356,6 @@ public static void pinMode(int pin, int mode) { |
356 | 356 | } |
357 | 357 | } |
358 | 358 |
|
359 | | - // delay to give udev a chance to change the file permissions behind our back |
360 | | - // there should really be a cleaner way for this |
361 | | - try { |
362 | | - Thread.sleep(500); |
363 | | - } catch (InterruptedException e) { |
364 | | - Thread.currentThread().interrupt(); |
365 | | - } |
366 | | - |
367 | 359 | // set direction and default level for outputs |
368 | 360 | fn = String.format("/sys/class/gpio/gpio%d/direction", pin); |
369 | 361 | String out; |
@@ -395,7 +387,17 @@ public static void pinMode(int pin, int mode) { |
395 | 387 | } else { |
396 | 388 | throw new IllegalArgumentException("Unknown mode"); |
397 | 389 | } |
398 | | - ret = NativeInterface.writeFile(fn, out); |
| 390 | + |
| 391 | + // we need to give udev some time to change the file permissions behind our back |
| 392 | + // retry for 500ms when writing to the file fails with -EPERM |
| 393 | + long start = System.currentTimeMillis(); |
| 394 | + do { |
| 395 | + ret = NativeInterface.writeFile(fn, out); |
| 396 | + if (ret == -1) { |
| 397 | + Thread.yield(); |
| 398 | + } |
| 399 | + } while (ret == -1 && System.currentTimeMillis()-start < 500); |
| 400 | + |
399 | 401 | if (ret < 0) { |
400 | 402 | throw new RuntimeException(fn + ": " + NativeInterface.getError(ret)); |
401 | 403 | } |
|