Skip to content

Instantly share code, notes, and snippets.

@sumeet
Forked from powerwlsl/screen.py
Created April 28, 2018 07:00
Show Gist options
  • Save sumeet/fb355abbe090917050f2fd8be5100a3b to your computer and use it in GitHub Desktop.
Save sumeet/fb355abbe090917050f2fd8be5100a3b to your computer and use it in GitHub Desktop.
# turns the display on and off using an external switch plugged into GPIO
# see http://razzpisampler.oreilly.com/ch07.html for more info
import RPi.GPIO as GPIO
import time
import os
# -- blacklight --
RPI_BLACKLIGHT_ON = '0'
RPI_BLACKLIGHT_OFF = '1'
def blacklight_is_on():
return open('/sys/class/backlight/rpi_backlight/bl_power').strip() == RPI_BLACKLIGHT_ON)
def turn_off_blacklight():
os.system("sudo bash -c 'echo 1 > /sys/class/backlight/rpi_backlight/bl_power'")
def turn_on_blacklight():
os.system("sudo bash -c 'echo 0 > /sys/class/backlight/rpi_backlight/bl_power'")
def toggle_blacklight():
if blacklight_is_on():
turn_off_blacklight()
else:
turn_on_blacklight()
# -- GPIO -- (hardware input)
# use the BCM pin number scheme. for more info, see
# https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/cheat_sheet/
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, # listen on pin 18
GPIO.IN, # type input (not output)
pull_up_down=GPIO.PUD_UP) # activate on switch release, instead of switch down
# -- main loop --
while True:
input_state = GPIO.input(18)
# in GPIO.PUD_UP mode, `input_state == False` indicates the switch was pressed
#
# see the link at the top of the page for more info
if input_state == False:
toggle_blacklight()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment