Last active
December 26, 2016 23:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wakeup is the init.d (upstart) script and wakeup.py is the actual python script