blob: 8d7c6d8baa9b8cbe3883b7946bfd98b84d890bc2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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"""
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.")
# 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()
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))
if self.ser.read(1) != "1":
raise SyncError
def close_connection(self):
"""Closes the connection to the Arduino."""
if self.ser is not None:
self.ser.close()
self.ser = None
|