# -*- coding: utf-8 -*- """ Created on Sat Aug 10 18:35:46 2013 @author: Aaron O'Toole """ ############################################################################## #Import libraries import pygame, sys, serial, time ############################################################################## #Function definitions def messageArduino(text): '''text - (string) The message to send to the Arduino\n This function sends a message to the arduino after adding \0 (null terminator, the arduino code I'm using is made to look for this). Then it reads the message back from the arduino and prints it out to make sure everything is working. ''' Arduino.write(text + '\0') line = Arduino.readline() print line def downkeys(key): '''key - a pygame key\n This is the dictionary that is searched when a key is pressed down. Depending on which key is pressed, a certain function is called. If a key is pressed that doens't have a function, the default function is called which doesn't do anything. ''' {pygame.K_LEFT : left, pygame.K_RIGHT : right, pygame.K_UP : forward, pygame.K_DOWN : backward, pygame.K_RETURN : Quit}.get(key, default)() def upkeys(key): '''key - a pygame key\n This is the dictionary that is searched when a key is released. Depending on which key is released, a certain function is called. If a key is released that doens't have a function, the default function is called which doesn't do anything. ''' {pygame.K_LEFT : center, pygame.K_RIGHT : center, pygame.K_UP : stop, pygame.K_DOWN : stop}.get(key, default)() def left(): messageArduino('left') def right(): messageArduino('right') def center(): messageArduino('center') def forward(): messageArduino('forward') def backward(): messageArduino('backward') def stop(): messageArduino('stop') def default(): '''The key is not recognized, don't do anything''' pass def Quit(): '''Stop pygame, disconnect the Arduino, and quit the program''' print '\nExiting program' pygame.quit() Arduino.close() print 'Goodbye' sys.exit() def connectArduino(port = 'COM3'): '''port - (string) The com port that the Arduino is connected to.\n This sets up a connection to the Arduino and checks that it is open. If it doesn't open, it stops everything and outputs a message. The port varible is the only thing that might need to change. Check your device manager to see what port the arduino is using. ''' serArduino = serial.Serial( port, baudrate = 9600, parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE, bytesize = serial.EIGHTBITS) #Make sure the Arduino port is open (try closing first just in case) serArduino.close() serArduino.open() if not serArduino.isOpen(): print 'ERROR: Could not connect to Arduino' sys.exit() else: #You have to wait an amount of time between when you open port and when #you start to use it, otherwise there could be a crash. time.sleep(1) print 'Arduino is connected.' return serArduino ############################################################################## #Connect to the Arduino Arduino = connectArduino() #Setup pygame window pygame.init() pygame.display.set_caption('Enter keystrokes in this window') size = [300, 200] screen = pygame.display.set_mode(size) ############################################################################## #Start forever loop print 'Use the arrow keys to command the arduino' print 'Hit Enter to exit...' print '___________________________________________\n' while True: for event in pygame.event.get(): if event.type == pygame.QUIT:#if the window is closed quit program pygame.quit() Arduino.close() sys.exit() # check if key is pressed if event.type == pygame.KEYDOWN: downkeys(event.key) # also check if a key is released elif event.type == pygame.KEYUP: upkeys(event.key)