diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Arduino sketch/serial_rgb.pde | 1 | ||||
| -rwxr-xr-x | Python code/rgbled.py | 17 | ||||
| -rw-r--r-- | Python code/serialrgb.py | 18 | 
4 files changed, 32 insertions, 5 deletions
| diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/Arduino sketch/serial_rgb.pde b/Arduino sketch/serial_rgb.pde index 84d331b..f6121a2 100644 --- a/Arduino sketch/serial_rgb.pde +++ b/Arduino sketch/serial_rgb.pde @@ -21,6 +21,7 @@ void loop()      r = Serial.read();      g = Serial.read();      b = Serial.read(); +    Serial.write('1'); // Sync    }    analogWrite(LEDR, r);    analogWrite(LEDG, g); diff --git a/Python code/rgbled.py b/Python code/rgbled.py index 8d699b4..8378577 100755 --- a/Python code/rgbled.py +++ b/Python code/rgbled.py @@ -4,7 +4,7 @@  from __future__ import division  import wx, math  from wx.lib.colourchooser.pycolourchooser import PyColourChooser -from serialrgb import SerialRGB +from serialrgb import SerialRGB, SyncError  def hsv2rgb(h, s, v):  	h *= 360.0 @@ -63,14 +63,23 @@ class rgbled_frame(wx.Frame):  		g = colour.Green()  		b = colour.Blue()  		if isinstance(self.rgbled, SerialRGB): -			self.rgbled.change_color((r, g, b)) +			try: +				self.rgbled.change_color((r, g, b)) +			except SyncError: +				wx.MessageDialog(None, "Could not synchronize with Arduino!", "Sync Error", wx.ICON_ERROR | wx.OK).ShowModal() +				self.Close()  class rgbled_app(wx.App):  	def OnInit(self):  		self.rgbled = None  		portdlg = wx.TextEntryDialog(None, "Serial port:")  		if portdlg.ShowModal() == wx.ID_OK: -			self.rgbled = SerialRGB(portdlg.GetValue()) +			try: +				self.rgbled = SerialRGB(portdlg.GetValue()) +			except SyncError: +				pass +			#	wx.MessageDialog(None, "Could not synchronize with Arduino!", "Sync Error", wx.ICON_ERROR | wx.OK).ShowModal() +			#	return False  			frame = rgbled_frame(self.rgbled)  			portdlg.Destroy()  			frame.Show() @@ -78,7 +87,7 @@ class rgbled_app(wx.App):  		return True  	def OnExit(self):  		if isinstance(self.rgbled,SerialRGB): -			self.rgb2hsv.close_connection() +			self.rgbled.close_connection()  if __name__ == '__main__':  	myapp = rgbled_app() 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.""" | 
