def cleanup(channel=None): """ At the end any program, it is good practice to clean up any resources you might have used. This is no different with OPi.GPIO. By returning all channels you have used back to inputs with no pull up/down, you can avoid accidental damage to your Orange Pi by shorting out the pins. Note that this will only clean up GPIO channels that your script has used. Note that GPIO.cleanup() also clears the pin numbering system in use. To clean up at the end of your script: .. code:: python GPIO.cleanup() It is possible that don't want to clean up every channel leaving some set up when your program exits. You can clean up individual channels, a list or a tuple of channels: .. code:: python GPIO.cleanup(channel) GPIO.cleanup( (channel1, channel2) ) GPIO.cleanup( [channel1, channel2] ) """ if channel is None: cleanup(list(_exports.keys())) setwarnings(True) global _mode _mode = None elif isinstance(channel, list): for ch in channel: cleanup(ch) else: _check_configured(channel) pin = get_gpio_pin(_mode, channel) event.cleanup(pin) sysfs.unexport(pin) del _exports[channel]
def test_unexport(fs): fs.CreateFile("/sys/class/gpio/unexport") unexport(26) with open("/sys/class/gpio/unexport") as fp: assert fp.read() == "26"
def setup(channel, direction, initial=None): """ You need to set up every channel you are using as an input or an output. :param channel: the channel based on the numbering system you have specified (:py:attr:`GPIO.BOARD`, :py:attr:`GPIO.BCM` or :py:attr:`GPIO.SUNXI`). :param direction: whether to treat the GPIO pin as input or output (use only :py:attr:`GPIO.IN` or :py:attr:`GPIO.OUT`). :param initial: When supplied and setting up an output pin, resets the pin to the value given (can be :py:attr:`0` / :py:attr:`GPIO.LOW` / :py:attr:`False` or :py:attr:`1` / :py:attr:`GPIO.HIGH` / :py:attr:`True`). To configure a channel as an input: .. code:: python GPIO.setup(channel, GPIO.IN) To set up a channel as an output: .. code:: python GPIO.setup(channel, GPIO.OUT) You can also specify an initial value for your output channel: .. code:: python GPIO.setup(channel, GPIO.OUT, initial=GPIO.HIGH) **Setup more than one channel:** You can set up more than one channel per call. For example: .. code:: python chan_list = [11,12] # add as many channels as you want! # you can tuples instead i.e.: # chan_list = (11,12) GPIO.setup(chan_list, GPIO.OUT) """ if _mode is None: raise RuntimeError("Mode has not been set") if isinstance(channel, list): for ch in channel: setup(ch, direction, initial) else: if channel in _exports: raise RuntimeError("Channel {0} is already configured".format(channel)) pin = get_gpio_pin(_mode, channel) try: sysfs.export(pin) except (OSError, IOError) as e: if e.errno == 16: # Device or resource busy if _gpio_warnings: warnings.warn("This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.", stacklevel=2) sysfs.unexport(pin) sysfs.export(pin) else: raise e sysfs.direction(pin, direction) _exports[channel] = direction if direction == OUT and initial is not None: sysfs.output(pin, initial)