Created
August 31, 2016 05:29
-
-
Save rxseger/f7562c70166ece54f91e27fc0c0f0d57 to your computer and use it in GitHub Desktop.
eject CD/DVD tray using GPIO
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/python | |
# eject CD/DVD tray using GPIO to turn on motor until switch closes | |
import RPi.GPIO as GPIO | |
import signal | |
import sys | |
import time | |
GPIO.setmode(GPIO.BOARD) | |
TM = 18 # G24 | |
OPSW = 22 # G25 | |
MAX_SECONDS = 5 | |
GPIO.setwarnings(False) | |
GPIO.setup([OPSW], GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(TM, GPIO.OUT, initial=GPIO.LOW) | |
def motor(on): | |
GPIO.output(TM, on) | |
# TODO: allow driving each direction | |
if on: | |
# safety just in case switch doesn't fire | |
def off(): | |
print "turning motor off after %s seconds" % (MAX_SECONDS,) | |
motor(False) | |
sys.exit(2) | |
signal.signal(signal.SIGALRM, off) | |
signal.alarm(MAX_SECONDS) | |
def handle(pin): | |
value = GPIO.input(pin) | |
if value == False: | |
# OPSW low = fully open, turn off motor | |
motor(False) | |
print "ejected successfully" | |
sys.exit(0) | |
GPIO.add_event_detect(OPSW, GPIO.BOTH, handle) | |
if GPIO.input(OPSW) == False: | |
print "tray already open, not turning on motor" | |
motor(False) # in fact, turn it off in case already on | |
sys.exit(1) | |
motor(True) | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment