Skip to content

Instantly share code, notes, and snippets.

@amcolash

amcolash/wakeup

Last active Dec 26, 2016
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@amcolash amcolash commented Oct 25, 2016

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
You can’t perform that action at this time.