Created
August 21, 2012 22:43
-
-
Save ma4a/3420078 to your computer and use it in GitHub Desktop.
make the Raspberry Pi connected LEDs etc blink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #GPIO-Port-Config: http://www.elinux.org/File:GPIOs.png | |
| import os, time | |
| def setGPIOValue(gpioNr, gpioValue): | |
| file = "/sys/class/gpio/gpio" + gpioNr + "/value" | |
| fileH = open(file, "w") | |
| fileH.write(gpioValue) | |
| fileH.close() | |
| print "Set value of GPIO" + gpioNr + " to " + gpioValue | |
| def getGPIOValue(gpioNr): | |
| file = "/sys/class/gpio/gpio" + gpioNr + "/value" | |
| fileH = open(file, "r") | |
| value = fileH.read() | |
| fileH.close() | |
| return value | |
| def toggleGPIOValue(gpioNr): | |
| newValue = ("1" if "0" in getGPIOValue(gpioNr) else "0") | |
| file = "/sys/class/gpio/gpio" + gpioNr + "/value" | |
| fileH = open(file, "w") | |
| fileH.write(newValue) | |
| fileH.close() | |
| print "Set value of GPIO" + gpioNr + " to " + newValue | |
| def activateGPIO(gpioNr): | |
| file = "/sys/class/gpio/export" | |
| fileH = open(file, "w") | |
| fileH.write(gpioNr) | |
| fileH.close() | |
| file = "/sys/class/gpio/gpio" + gpioNr + "/direction" | |
| fileH = open(file, "w") | |
| fileH.write("out") | |
| fileH.close() | |
| print "Activated GPIO" + gpioNr | |
| def deactivateGPIO(gpioNr): | |
| file = "/sys/class/gpio/unexport" | |
| fileH = open(file, "w") | |
| fileH.write(gpioNr) | |
| fileH.close() | |
| print "Deactivated GPIO" + gpioNr | |
| gpioList = ["0", "1", "4"] | |
| waitTime = 0.2 | |
| for x in gpioList: | |
| if not os.path.exists("/sys/class/gpio/gpio" + x): | |
| activateGPIO(x) | |
| else: | |
| print "GPIO" + x + " already activated" | |
| file = "/sys/class/gpio/gpio" + x + "/direction" | |
| fileH = open(file, "w") | |
| fileH.write("out") | |
| fileH.close() | |
| time.sleep(waitTime) | |
| for repeat in range(11): | |
| for x in gpioList: | |
| toggleGPIOValue(x) | |
| time.sleep(waitTime) | |
| for x in gpioList: | |
| deactivateGPIO(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment