forked from ornotermes/Arduino-LED-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialLedLib.py
52 lines (47 loc) · 1.67 KB
/
serialLedLib.py
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
#
#+ Copyright (c) 2014 Rikard Lindstrom <ornotermes@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import serial
import io
from time import sleep
class serialLed:
width = 1
height = 1
def connect(self, port, identifier):
self.serial = serial.Serial(port, 9600, timeout = 1)
self.sio = io.TextIOWrapper(io.BufferedRWPair(self.serial, self.serial))
self.serial.write("?")
sleep(0.5)
response = self.sio.readline().split(",");
if ( response[0] != identifier ):
raise Exception('Serial device did not give expected response!')
for i in range(1, len(response)):
operand = response[i][0]
if(operand == "W"):
self.width = int(response[i][1:])
if(operand == "H"):
self.height = int(response[i][1:])
def send(self, cmd, data = 0):
if cmd in ("C", "c", "D", "d"):
self.serial.write(cmd)
self.sio.flush()
self.serial.read(1)
if cmd in ("G", "g"):
self.serial.write("%s%s%s" % (cmd, chr(data[0]), chr(data[1])))
if cmd in ("S", "s"):
self.serial.write("%s%s%s%s" % (cmd, chr(data[0]), chr(data[1]), chr(data[2])))
#sleep(0.001)