import thread import time, os try: #Need GPIO library import RPi.GPIO as GPIO #Incase we can't get the GPIO library except RuntimeError: print ("Unable to import GPIOLibrary. Make sure that this is being run") #copied from cow, Pins will be refrenced by the processor's numbering scheme GPIO.setmode(GPIO.BCM) #Pin 17 is the output (LED) #create the list of pins @@@@@@@ Will - I like this a lot! Good work :O) #all acceptable keyboard inputs letters = ["f", "i", "r", "e", " ", "w", "a", "s", "d", "c", "q"] # w = 18 = up # s = 16 = down # a = 12 = left # d = 21 = right # c = camera toggle # q = quit # fire & " " = fire sequence #all pins pins = [6, 12, 13, 16, 17, 18, 19, 21, 22, 23, 24, 25] #KBI associated with FIRE command fires = ["f", "i", "r", "e", " "] #pins associated with the FIRE command (in order, see above) fire_pins = [6, 25, 24, 23, 22] #no need for this code. These were an LED sequence in testing. I might leave the LEDs on the breadboard for initial testing #KBI associated with movement moves = ["w", "a", "s", "d"] # up, left, down, right #pins associated with movement (in order, see above) move_pins = [18, 12, 16, 21] #KBI associated with camera and break utilities = ["c", "q"] # c = camera toggle q = quit # ??????????????????????? DAN ??????????????????????????????? # ??????????????????????? DAN ??????????????????????????????? #pins associated with camera (cam1, cam2) cam_pins = [13, 19] # This needs to be changed to serial (usb) inputs for video, right? for pin in pins: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, False) # ??????????????????????? DAN ??????????????????????????????? GPIO.output(19,True)#turn on cam1 # ??????????????????????? DAN ??????????????????????????????? GPIO.output(17,True) # ready light # This is a "Get Character string" that enables this functionality. Don't understand or care how it works. try: from msvcrt import getch except ImportError: def getch(): import sys, tty, termios fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch char = None #function to await keyboard input def keypress(): global char char = None char = getch() #camera function and definition def swap_camera(): if GPIO.input(19): GPIO.output(13,True) GPIO.output(19,False) else: GPIO.output(19,True) GPIO.output(13,False) #function to turn on the active firing sequence pin def fire_light(dog): GPIO.output(dog,True) time.sleep(2) fire_loop = 0 # function to execute the fire sequence def fire_sequence(): GPIO.output(19,True) GPIO.output(13,False) for pin in fire_pins: GPIO.output(pin,False) time.sleep(.1) for pin in fire_pins: GPIO.output(pin,True) time.sleep(.5) for pin in fire_pins: GPIO.output(pin,False) time.sleep(.1) for pin in fire_pins: GPIO.output(pin,True) time.sleep(.5) for pin in fire_pins: GPIO.output(pin,False) #function to turn on movement light for camera 0 def drive_light_slow(dog): GPIO.output(dog,True) time.sleep(1) GPIO.output(dog,False) #function to turn on movement light for camera 1 def drive_light_fast(dog): #check active camera if GPIO.input(19): #slow for camera 1 #set blink speed and count bs = .05 bc = 1 else: bs = .2 bc = 1 #sequence lights for i in range(bc): GPIO.output(dog,True) time.sleep(bs) GPIO.output(dog,False) time.sleep(bs) #function to abort fire sequence and reset it def misfire(): for pin in fire_pins: GPIO.output(pin,False) print("Misfire") #function to analize keypress input? def waitcheck(): fire_loop = 1 loop=1 print ("press any key. . . ") while (loop): GPIO.output(17,False) if char in fires: for i in range(len(fires)): if fires[i] == char and GPIO.input(fire_pins[i]) == 0: if fire_loop == 1: if char <> "f" and GPIO.input(fire_pins[i-1]) == 0: fire_loop = 0 misfire() break else: fire_light(fire_pins[i]) if char == " ": fire_sequence() break else: fire_loop = 0 misfire() break else: if GPIO.input(6): misfire() if char == "q": for pin in pins: GPIO.output(pin,False) GPIO.cleanup() break elif char == "c": swap_camera() elif char == "w": drive_light_fast(18) elif char == "a": drive_light_fast(12) elif char == "s": drive_light_fast(16) elif char == "d": drive_light_fast(21) elif char == None: noaction = 1 else: print("You typed,", char) GPIO.output(17,True) fire_loop = 1 keypress() #allows multi taking to wait for keyboard input and work with other functions #seems like we may not need this... #thread.start_new_thread(keypress, ()) try: print("Beginning of execution") waitcheck() except KeyboardInterrupt: for pin in pins: GPIO.output(pin,False) GPIO.cleanup()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question