Skip to content

Instantly share code, notes, and snippets.

@amcolash
Last active December 26, 2016 23:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amcolash/313cbb4ad83f962c77c5c4f751bd6c3f to your computer and use it in GitHub Desktop.
Save amcolash/313cbb4ad83f962c77c5c4f751bd6c3f to your computer and use it in GitHub Desktop.
Simple script to toggle display based off of PIR hooked up to sbc
#!/bin/bash
# /etc/init.d/wakeup
### BEGIN INIT INFO
# Provides: Wakeup for Display
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start Wakeup at boot
# Description: Start Wakeup at boot
### END INIT INFO
# If you want a command to always run, put it here
PATH=$PATH:/usr/local/bin
# Change this if necessary
MYPATH=/home/chip/MagicMirror
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting Wakeup"
# run application you want to start
kill -9 $(ps aux | grep 'MagicMirror/wakeup.py' | awk '{print $2}')
pushd $MYPATH/
python $MYPATH/wakeup.py &
popd
;;
restart)
echo "Restarting Wakeup"
# run application you want to start
kill -9 $(ps aux | grep 'MagicMirror/wakeup.py' | awk '{print $2}')
pushd $MYPATH/
python $MYPATH/wakeup.py &
popd
;;
stop)
echo "Stopping Wakeup"
# kill application you want to stop
kill -9 $(ps aux | grep 'MagicMirror/wakeup.py' | awk '{print $2}')
;;
*)
echo "Usage: /etc/init.d/wakeup {start|stop|restart}"
exit 1
;;
esac
exit 0
#!/usr/bin/env python
import CHIP_IO.GPIO as GPIO
import subprocess
from subprocess import Popen, PIPE, STDOUT
import time
def display_power(enabled):
command = ""
if (enabled):
print("Turning on display")
command = "su -c 'DISPLAY=:0 xset dpms force on' chip"
else:
print("Turning off display")
command = "su -c 'DISPLAY=:0 xset dpms force off' chip"
subprocess.call(command, shell=True)
def check():
enabled = GPIO.input("XIO-P1")
check_display = """su -c "DISPLAY=:0 xset q | grep 'Monitor is'" chip"""
p = Popen(check_display, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
if enabled and "Monitor is Off" in p.stdout.read():
display_power(True)
elif not enabled and "Monitor is On" in p.stdout.read():
display_power(False)
try:
GPIO.setup("XIO-P1", GPIO.IN)
time.sleep(3)
print("initial status: " + str(GPIO.input("XIO-P1")))
while True:
check()
time.sleep(1)
except Exception as e:
GPIO.cleanup() # clean up GPIO on CTRL+C exit, or hopefully SIGEXIT
print e
print "Cleaning up forecfully"
print "Cleaning up normally"
GPIO.cleanup() # clean up GPIO on normal exit
@amcolash
Copy link
Author

wakeup is the init.d (upstart) script and wakeup.py is the actual python script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment