Demo entry 2161835

CODE

   

Submitted by anonymous on Jul 11, 2015 at 00:00
Language: Python 3. Code size: 833 Bytes.

class Gpio:
    def __init__(self, gpio, direction="out"):
        self.gpio = gpio
        self.path = "/sys/class/gpio/gpio{}/".format(self.gpio)

        open("/sys/class/gpio/export", "w").write(str(self.gpio))
        open(self.path + "direction", "w").write(direction)

    def set(self, value):
        open(self.path + "value", "w").write(str(value))

    def get(self):
        return int(open(self.path + "value", "r").read().replace("\n", ""))

    def toggle(self):
        if self.get() == 0:
            open(self.path + "value", "w").write("1")
        else:
            open(self.path + "value", "w").write("0")
    
    def get_direction(self):
        return open(self.path + "direction", "r").read().replace("\n", "")
    
    def __del__(self):
        open("/sys/class/gpio/unexport", "w").write(str(self.gpio))

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).