Skip to content

Instantly share code, notes, and snippets.

@amcolash
Last active September 7, 2018 07:28
Show Gist options
  • Save amcolash/a4b8b20e4d47d7b10f3dd8f0efe46341 to your computer and use it in GitHub Desktop.
Save amcolash/a4b8b20e4d47d7b10f3dd8f0efe46341 to your computer and use it in GitHub Desktop.
Wakeup script in python and upstart script for my magic mirror. This allows for the mirror to turn on from a motion sensor.
#!/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)
while True:
check()
time.sleep(3)
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