diff options
author | Kevin Chabowski <kevin@kch42.de> | 2011-03-22 22:15:22 +0100 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2011-03-22 22:15:22 +0100 |
commit | 4cdf144c41c7b2d3100251e5a47e35f98cb09c30 (patch) | |
tree | 1e32cf43f05be71bea151ee9f4713cc6a7d61053 /Python code/serialrgb.py | |
parent | aedc42f9ff57eb7fb456ef6fd9e94c099ff84076 (diff) | |
download | SerialRGB-LED-Arduino-4cdf144c41c7b2d3100251e5a47e35f98cb09c30.tar.gz SerialRGB-LED-Arduino-4cdf144c41c7b2d3100251e5a47e35f98cb09c30.tar.bz2 SerialRGB-LED-Arduino-4cdf144c41c7b2d3100251e5a47e35f98cb09c30.zip |
Added synchronisation with Arduino.
Diffstat (limited to 'Python code/serialrgb.py')
-rw-r--r-- | Python code/serialrgb.py | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/Python code/serialrgb.py b/Python code/serialrgb.py index 7296e22..8d7c6d8 100644 --- a/Python code/serialrgb.py +++ b/Python code/serialrgb.py @@ -1,7 +1,15 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import serial +import serial, time + +class SyncError(Exception): + """Synchronisation error""" + def __init__(self, msg=""): + self._msg = str(msg) + + def __str__(self): + return msg class SerialRGB(object): """Easy controlling of the RGB-LED / Arduino""" @@ -16,6 +24,12 @@ class SerialRGB(object): self.ser = serial.Serial(addr, baud) except: raise IOError("Could not connect to Arduino via serial port.") + # Sync... + while self.ser.inWaiting() < 1: + self.ser.write("\x00") + time.sleep(.01) + if self.ser.read(1) != "1": + raise SyncError def __del__(self): self.close_connection() @@ -28,6 +42,8 @@ class SerialRGB(object): """ r, g, b = color self.ser.write(chr(r) + chr(g) + chr(b)) + if self.ser.read(1) != "1": + raise SyncError def close_connection(self): """Closes the connection to the Arduino.""" |