Skip to content

Instantly share code, notes, and snippets.

@map7
Created May 2, 2020 07:02
Show Gist options
  • Save map7/3dfc7ef214a48cedeef94fbce76d9011 to your computer and use it in GitHub Desktop.
Save map7/3dfc7ef214a48cedeef94fbce76d9011 to your computer and use it in GitHub Desktop.
screen control
#!/usr/bin/env python
import RPi.GPIO as GPIO # Import RPi GPIO Lib
import time
import os
def button_callback(channel):
print("Button was pushed!")
time.sleep(0.2)
GPIO.setwarnings(False) # Ignore warnings for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
# Set pin 10 to be an input pin & set initial value to be pulled low (off)
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# GPIO.add_event_detect(10,GPIO.FALLING,callback=button_callback) # Setup event on pin 10 rising edge
# message = input("Press enter to quit\n\n") # Run until someone presses enter
# GPIO.cleanup() # Clean up
state=0
while True:
input_state=GPIO.input(10)
if (state == 0 and input_state == False):
print('screen off')
os.system("echo 1 > /sys/class/backlight/rpi_backlight/bl_power")
time.sleep(0.5)
state=1
elif (state == 1 and input_state == False):
print('screen on')
os.system("echo 0 > /sys/class/backlight/rpi_backlight/bl_power")
time.sleep(0.5)
state=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment