From f19a5bcea831c9432eb8f973f4162ee3673f805e Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 13 Jun 2014 12:01:36 +0000 Subject: [PATCH] Added bugfix to prevent access errors on newly exported pin control files --- examples/bitmap.php | 8 ++++++++ lib/GpioPin.php | 15 +++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/examples/bitmap.php b/examples/bitmap.php index f2cfca5..a254757 100644 --- a/examples/bitmap.php +++ b/examples/bitmap.php @@ -33,4 +33,12 @@ for($n = 1; $n < 16; $n++) { usleep(500000); } +for($m = 0; $m < 20; $m++) { +foreach(array(0, 0, 12, 14, 15, 7, 3, 1, 0) as $n) { + $par->write($n); + usleep(100000); +} +} + + $par->write(0); diff --git a/lib/GpioPin.php b/lib/GpioPin.php index ea97252..7ac8d35 100644 --- a/lib/GpioPin.php +++ b/lib/GpioPin.php @@ -155,12 +155,19 @@ class GpioPin } else { $path = "/sys/class/gpio/{$file}"; } - if (!is_writable($path)) { - throw new HardwareException("Sysfs file {$path} not writable"); + if (!file_exists($path)) { + throw new HardwareException("Sysfs file {$path} does not exist"); } - $f = fopen($path,"w+"); + + $ts = microtime(true); + while ((!is_writable($path)) && (microtime(true) < $ts+1)) { + usleep(10000); + } + + $f = @fopen($path,"c"); if (!is_resource($f)) { - throw new HardwareException("Unable to write to sysfs file {$path}"); + $err = error_get_last(); + throw new HardwareException("Unable to write to sysfs file {$path}: ".$err['message']); } fwrite($f,$value); fclose($f);