From 78cbf012c12d26ea43038c3fa59e244bd15f467e Mon Sep 17 00:00:00 2001 From: gohai Date: Sun, 24 Jun 2018 19:48:37 -0700 Subject: [PATCH] IO: Speed up GPIO.pinMode() --- java/libraries/io/src/processing/io/GPIO.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/java/libraries/io/src/processing/io/GPIO.java b/java/libraries/io/src/processing/io/GPIO.java index 54e41bb1c..a0f6b9262 100644 --- a/java/libraries/io/src/processing/io/GPIO.java +++ b/java/libraries/io/src/processing/io/GPIO.java @@ -356,14 +356,6 @@ public class GPIO { } } - // delay to give udev a chance to change the file permissions behind our back - // there should really be a cleaner way for this - try { - Thread.sleep(500); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - // set direction and default level for outputs fn = String.format("/sys/class/gpio/gpio%d/direction", pin); String out; @@ -395,7 +387,17 @@ public class GPIO { } else { throw new IllegalArgumentException("Unknown mode"); } - ret = NativeInterface.writeFile(fn, out); + + // we need to give udev some time to change the file permissions behind our back + // retry for 500ms when writing to the file fails with -EPERM + long start = System.currentTimeMillis(); + do { + ret = NativeInterface.writeFile(fn, out); + if (ret == -1) { + Thread.yield(); + } + } while (ret == -1 && System.currentTimeMillis()-start < 500); + if (ret < 0) { throw new RuntimeException(fn + ": " + NativeInterface.getError(ret)); }