Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait permissions to be set #15

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions OPi/sysfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,49 @@
# Copyright (c) 2017 Richard Hull
# See LICENSE.md for details.

import time
import os
from contextlib import contextmanager
from OPi.constants import HIGH, LOW, IN, OUT, \
NONE, RISING, FALLING, BOTH

# Allow to wait up to 1 second for the file have the correct permissions
WAIT_PERMISSION_TIMEOUT = 1.

@contextmanager
def value_descriptor(pin, mode="r"):
path = "/sys/class/gpio/gpio{0}/value".format(pin)
start_time = time.time()
while not os.access(path, os.W_OK) and time.time() - start_time < WAIT_PERMISSION_TIMEOUT:
time.sleep(0.1)
with open(path, mode) as fp:
yield fp


def export(pin):
path = "/sys/class/gpio/export"
start_time = time.time()
while not os.access(path, os.W_OK) and time.time() - start_time < WAIT_PERMISSION_TIMEOUT:
time.sleep(0.1)
with open(path, "w") as fp:
fp.write(str(pin))


def unexport(pin):
path = "/sys/class/gpio/unexport"
start_time = time.time()
while not os.access(path, os.W_OK) and time.time() - start_time < WAIT_PERMISSION_TIMEOUT:
time.sleep(0.1)
with open(path, "w") as fp:
fp.write(str(pin))


def direction(pin, dir):
assert dir in [IN, OUT]
path = "/sys/class/gpio/gpio{0}/direction".format(pin)
start_time = time.time()
while not os.access(path, os.W_OK) and time.time() - start_time < WAIT_PERMISSION_TIMEOUT:
time.sleep(0.1)
with open(path, "w") as fp:
if dir == IN:
fp.write("in")
Expand All @@ -54,6 +70,9 @@ def output(pin, value):
def edge(pin, trigger):
assert trigger in [NONE, RISING, FALLING, BOTH]
path = "/sys/class/gpio/gpio{0}/edge".format(pin)
start_time = time.time()
while not os.access(path, os.W_OK) and time.time() - start_time < WAIT_PERMISSION_TIMEOUT:
time.sleep(0.1)
opts = {
NONE: "none",
RISING: "rising",
Expand Down