summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-11-11 11:53:32 +0100
committerJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-11-11 11:53:32 +0100
commit6c626fad924852c32ff0a1a25fd7aceb23a46f8e (patch)
tree85323e4f4f0f44732ff55e0dd50924552d807e30
parent8089412c7859fd6ce1d67aa1beef7a3ba338b302 (diff)
downloadlaserz_und_stuff-6c626fad924852c32ff0a1a25fd7aceb23a46f8e.tar.gz
laserz_und_stuff-6c626fad924852c32ff0a1a25fd7aceb23a46f8e.tar.bz2
laserz_und_stuff-6c626fad924852c32ff0a1a25fd7aceb23a46f8e.tar.xz
laserz_und_stuff-6c626fad924852c32ff0a1a25fd7aceb23a46f8e.zip
Connecting to serial port once, not maaaaany times.
-rw-r--r--Lasers_und_stuff/main.py43
1 files changed, 22 insertions, 21 deletions
diff --git a/Lasers_und_stuff/main.py b/Lasers_und_stuff/main.py
index a9f4072..f17e81f 100644
--- a/Lasers_und_stuff/main.py
+++ b/Lasers_und_stuff/main.py
@@ -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):