diff options
author | Kevin Chabowski <kevin@kch42.de> | 2011-01-29 23:19:05 +0100 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2011-01-29 23:19:05 +0100 |
commit | aedc42f9ff57eb7fb456ef6fd9e94c099ff84076 (patch) | |
tree | cff85e6903990a6bfd87b5b047722f693352b623 /Python code/serialrgb.py | |
download | SerialRGB-LED-Arduino-aedc42f9ff57eb7fb456ef6fd9e94c099ff84076.tar.gz SerialRGB-LED-Arduino-aedc42f9ff57eb7fb456ef6fd9e94c099ff84076.tar.bz2 SerialRGB-LED-Arduino-aedc42f9ff57eb7fb456ef6fd9e94c099ff84076.zip |
Initial commit
Diffstat (limited to 'Python code/serialrgb.py')
-rw-r--r-- | Python code/serialrgb.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Python code/serialrgb.py b/Python code/serialrgb.py new file mode 100644 index 0000000..7296e22 --- /dev/null +++ b/Python code/serialrgb.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import serial + +class SerialRGB(object): + """Easy controlling of the RGB-LED / Arduino""" + def __init__(self, addr, baud=9600): + """ + Creating a new SerialRGB object. + + addr -- The address of the serial port. + baud -- The baudrate (default: 9600) + """ + try: + self.ser = serial.Serial(addr, baud) + except: + raise IOError("Could not connect to Arduino via serial port.") + + def __del__(self): + self.close_connection() + + def change_color(self, color): + """ + Send a colot to the Arduino. + + color - 3-Tuple representing an RGB-Color (color components must be in range 0 - 255). + """ + r, g, b = color + self.ser.write(chr(r) + chr(g) + chr(b)) + + def close_connection(self): + """Closes the connection to the Arduino.""" + if self.ser is not None: + self.ser.close() + self.ser = None |