Skip to content

Commit 6cba967

Browse files
committed
Wait until GPIO file appears on the /sys filesystem (issue #36)
1 parent fc3d1d5 commit 6cba967

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

source/py_gpio.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,28 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar
106106
if (get_gpio_number(channel, &gpio))
107107
return NULL;
108108

109-
gpio_export(gpio);
110-
gpio_set_direction(gpio, direction);
111-
if (direction == OUTPUT) {
112-
gpio_set_value(gpio, initial);
113-
} else {
114-
gpio_set_value(gpio, pud);
115-
}
109+
unsigned int count = 100000;
110+
int res = -1;
111+
do
112+
{ // wait until gpio file appears on the filesystem
113+
res = gpio_export(gpio);
114+
} while(res != 0 && count-- > 0);
115+
if(count == 0)
116+
return NULL;
117+
118+
count = 100000;
119+
do {
120+
res = gpio_set_direction(gpio, direction);
121+
} while(res != 0 && count-- > 0);
122+
if(count == 0)
123+
return NULL;
124+
125+
count = 100000;
126+
do {
127+
res = gpio_set_value(gpio, direction == OUTPUT ? initial : pud);
128+
} while(res != 0 && count-- > 0);
129+
if(count == 0)
130+
return NULL;
116131

117132
gpio_direction[gpio] = direction;
118133

0 commit comments

Comments
 (0)