Skip to content

Instantly share code, notes, and snippets.

@rlemon

rlemon/app.py Secret

Created May 20, 2016 15:59
Show Gist options
  • Save rlemon/42caccb50b606ba346fca82afdc42492 to your computer and use it in GitHub Desktop.
Save rlemon/42caccb50b606ba346fca82afdc42492 to your computer and use it in GitHub Desktop.
import RPi.GPIO as GPIO
import time
import subprocess
import threading
GPIO.setmode(GPIO.BCM);
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP);
GPIO.setup(2, GPIO.OUT);
# red led (top to bottom, 1-4 on physical device)
GPIO.setup(17, GPIO.OUT); # 4
GPIO.setup(27, GPIO.OUT); # 1
GPIO.setup(22, GPIO.OUT); # 2
GPIO.setup(23, GPIO.OUT); # 3
GPIO.output(2,GPIO.LOW)
blinking = False
ready = False
def blink():
if blinking == False:
GPIO.output(2,GPIO.LOW)
time.sleep(1)
return blink()
GPIO.output(2,GPIO.LOW)
time.sleep(1)
GPIO.output(2,GPIO.HIGH)
time.sleep(1)
return blink()
def setupCommand(arr, ledPosition):
print("setting up thread" + str(ledPosition))
cmdThread = threading.Thread(name='command thread ' + str(ledPosition), target=execCommand, args=[arr, ledPosition])
cmdThread.start()
return cmdThread
def execCommand(arr,ledPosition):
result = subprocess.check_output(arr)
ret = True
if result.strip() == "1" :
ret = False
GPIO.output(ledPosition, GPIO.HIGH)
time.sleep(0.22)
print("result is:" + result.strip())
return ret
led = threading.Thread(name='non-block', target=blink)
led.start()
while True:
input_state = GPIO.input(18)
blinking = False
if input_state == False:
if ready == True:
print("start")
ready = False
blinking = True
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.LOW)
GPIO.output(22,GPIO.LOW)
GPIO.output(23,GPIO.LOW)
# this just launches a shell script that handles the dd and checks. it was way easier to do it this way
# for me. In the future I'll learn the IO in python.
t1 = setupCommand(['sudo', 'sh', '/home/pi/Development/sd-programmer/edd.sh', '/dev/sdb'], 27)
t2 = setupCommand(['sudo', 'sh', '/home/pi/Development/sd-programmer/edd.sh', '/dev/sda'], 22)
t3 = setupCommand(['sudo', 'sh', '/home/pi/Development/sd-programmer/edd.sh', '/dev/sdd'], 23)
t4 = setupCommand(['sudo', 'sh', '/home/pi/Development/sd-programmer/edd.sh', '/dev/sdc'], 17)
t1.join()
t2.join()
t3.join()
t4.join()
time.sleep(1)
print("end of loop")
else:
print("reset")
ready = True
GPIO.output(2,GPIO.LOW)
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.HIGH)
GPIO.output(22,GPIO.HIGH)
GPIO.output(23,GPIO.HIGH)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment