Skip to content

Instantly share code, notes, and snippets.

@Geofferey
Created August 11, 2018 04:23
Show Gist options
  • Save Geofferey/730b86981c2e353f68abf18399ac0d46 to your computer and use it in GitHub Desktop.
Save Geofferey/730b86981c2e353f68abf18399ac0d46 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#A multi-function switch for the Raspberry Pi
#This script was originally inspired by [email protected]/member/AndrewH7
#Modified by: Geofferey@XDA/Instagram/Github
#Use this script at your own risk
import RPi.GPIO as GPIO
import os
import time
pin_number=18
#Replace YOUR_CHOSEN_GPIO_NUMBER_HERE with the GPIO pin number you wish to use
#Make sure you know which rapsberry pi revision you are using first
#The line should look something like this e.g. "pin_number=7"
GPIO.setmode(GPIO.BCM)
#Enables BCM pin numbering (i.e. the GPIO number, not physical pin number)
#WARNING: this will change between Pi versions
GPIO.setup(pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#It's very important the pin is an input to avoid short-circuits
#The pull-up resistor means the pin is high by default
try:
while True:
GPIO.wait_for_edge(pin_number, GPIO.FALLING)
start = time.time()
time.sleep(0.02)
while GPIO.input(pin_number) == GPIO.LOW:
time.sleep(0.01)
duration = time.time() - start
if duration >= 6:
print "Start WPS Pairing"
os.system("wps_config")
continue
elif duration >= 3:
print "Enable Bluetooth Discoverability"
os.system("/etc/init.d/bluetooth-agent start &")
time.sleep(30)
os.system("/etc/init.d/bluetooth-agent stop &")
continue
else:
print "Modifer Mode"
os.system("modprobe ledtrig_heartbeat")
os.system("echo heartbeat >/sys/class/leds/led1/trigger")
GPIO.wait_for_edge(pin_number, GPIO.FALLING)
start = time.time()
time.sleep(0.02)
while GPIO.input(pin_number) == GPIO.LOW:
time.sleep(0.01)
duration = time.time() - start
if duration >= 6:
print "Reboot Pi"
os.system("reboot")
continue
elif duration >= 3:
print "PowerOff Pi"
os.system("paplay /home/pi/PowerOff.wav &")
os.system("sleep 4")
os.system("shutdown -h now")
else:
print "Exit Modifier Mode"
os.system("echo input >/sys/class/leds/led1/trigger")
continue
except KeyboardInterrupt:
GPIO.cleanup
except:
GPIO.cleanup
finally:
GPIO.cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment