Skip to content

Commit 4cbf6bc

Browse files
committed
IO: Speed up GPIO.pinMode()
1 parent 2015448 commit 4cbf6bc

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

java/libraries/io/src/processing/io/GPIO.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,6 @@ public static void pinMode(int pin, int mode) {
356356
}
357357
}
358358

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-
367359
// set direction and default level for outputs
368360
fn = String.format("/sys/class/gpio/gpio%d/direction", pin);
369361
String out;
@@ -395,7 +387,17 @@ public static void pinMode(int pin, int mode) {
395387
} else {
396388
throw new IllegalArgumentException("Unknown mode");
397389
}
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+
399401
if (ret < 0) {
400402
throw new RuntimeException(fn + ": " + NativeInterface.getError(ret));
401403
}

0 commit comments

Comments
 (0)