Connecting to serial port once, not maaaaany times.
parent
8089412c78
commit
6c626fad92
|
@ -6,21 +6,18 @@ Created on Nov 10, 2012
|
|||
|
||||
|
||||
import random, pygame, sys
|
||||
#import time
|
||||
from pygame.locals import *
|
||||
from math import fabs
|
||||
from math import ceil
|
||||
import serial
|
||||
import glob
|
||||
|
||||
#import and init pygame
|
||||
pygame.init()
|
||||
|
||||
def main():
|
||||
#create the screen
|
||||
window = pygame.display.set_mode((800, 600))
|
||||
|
||||
|
||||
GRAY = ( 182, 182, 182)
|
||||
VIOLET = (150, 100, 190)
|
||||
RED = (150, 0, 0)
|
||||
|
@ -46,9 +43,7 @@ def main():
|
|||
#input handling ( code):
|
||||
while True:
|
||||
screen.draw_surface(cannon)
|
||||
|
||||
#mouseClicked = False
|
||||
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
|
@ -194,7 +189,19 @@ class CannonController :
|
|||
self.position = [0, 0]
|
||||
self.fired = False
|
||||
self.laser = False
|
||||
ports = glob.glob("/dev/ttyACM*")
|
||||
|
||||
if(len(ports) == 1):
|
||||
self.ser = serial.Serial(ports[0], 115200, timeout=1)
|
||||
self.connected_to_serial = True
|
||||
else:
|
||||
self.ser = False
|
||||
self.connected_to_serial = False
|
||||
if len(ports) > 1 :
|
||||
raise "To many devices to handle"
|
||||
elif len(ports) == 0 :
|
||||
print "No device is connected"
|
||||
|
||||
def set_position(self, point):
|
||||
self.position = point
|
||||
|
||||
|
@ -210,7 +217,6 @@ class CannonController :
|
|||
def move_left(self, change):
|
||||
if(self.position[0] >= change):
|
||||
self.position[0] -= change
|
||||
|
||||
def move_right(self, change):
|
||||
if(self.position[0] < 255 - change):
|
||||
self.position[0] += change
|
||||
|
@ -250,24 +256,19 @@ class CannonController :
|
|||
return [chr(x) for x in data]
|
||||
|
||||
def send_data(self):
|
||||
ports = glob.glob("/dev/ttyACM*")
|
||||
if(len(ports) > 1):
|
||||
raise "To many devices to handle"
|
||||
if(len(ports) != 0):
|
||||
ser = serial.Serial("/dev/ttyACM0", 115200, timeout=1)
|
||||
if(self.connected_to_serial):
|
||||
data = self.get_data_to_send()
|
||||
ser.write('a')
|
||||
ser.write(data[0])
|
||||
self.ser.write('a')
|
||||
self.ser.write(data[0])
|
||||
print "wrote %i" % ord(data[0])
|
||||
ser.write('b')
|
||||
ser.write(data[1])
|
||||
ser.write('c')
|
||||
ser.write(data[2])
|
||||
ser.write('d')
|
||||
ser.write(data[3])
|
||||
self.ser.write('b')
|
||||
self.ser.write(data[1])
|
||||
self.ser.write('c')
|
||||
self.ser.write(data[2])
|
||||
self.ser.write('d')
|
||||
self.ser.write(data[3])
|
||||
else:
|
||||
print "No device is connected"
|
||||
|
||||
print "Cannot send data, not connected..."
|
||||
|
||||
class Gunpoint :
|
||||
def __init__(self, point1, point2, point3):
|
||||
|
|
Loading…
Reference in New Issue