Last active
January 8, 2016 10:46
-
-
Save jjfalling/8118225 to your computer and use it in GitHub Desktop.
backlight control for rpi tft screen
This file contains 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
#!/usr/bin/env python | |
import RPi.GPIO as GPIO | |
import time | |
import os | |
#adjust for where your switch is connected | |
buttonPin = 18 | |
sleepDelay = 1.0 | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(buttonPin,GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
#MAKE ALL OF THIS NATIVE | |
os.system("echo 252 > /sys/class/gpio/export") | |
os.system("echo 'out' > /sys/class/gpio/gpio252/direction") | |
os.system("echo '1' > /sys/class/gpio/gpio252/value") | |
while True: | |
#assuming the script to call is long enough we can ignore bouncing | |
if (GPIO.input(buttonPin) == 0): | |
#get current state of backlight | |
curValFile = open("/sys/class/gpio/gpio252/value", "r") | |
curVal = curValFile.read() | |
curVal = int(curVal) | |
curValFile.close() | |
#this is the script that will be called (as root) | |
if curVal == 0: | |
os.system("echo '1' > /sys/class/gpio/gpio252/value") | |
time.sleep(sleepDelay) | |
elif curVal == 1: | |
os.system("echo '0' > /sys/class/gpio/gpio252/value") | |
time.sleep(sleepDelay) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! I have been trying to get that button to work but without any luck, was almost giving up and then i found this, you really made my day.
Only thing i needed to change was that on my pi i use gpio508 instead of gpio252 :)
Great work!