-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifyBoard.py
executable file
·31 lines (25 loc) · 1.01 KB
/
NotifyBoard.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
#!/usr/bin/env python
import serial, re, time
class NotifyBoard(object):
def __init__(self, serialport):
try:
self.port = serial.Serial(serialport, 9600, timeout=2)
except serial.SerialException:
# errors went to stderr, which went to Harry Potter land.
# ideally these would go to irc or something.
raise RuntimeError('unable to open serial port?')
self.lastmsg = ''
# seems that we need a newline for the
# arduino to work??
self.eol = '\x0a'
def display(self, msg, permanent=True):
assert isinstance(msg, unicode)
msg = msg[:162] # Max tested size before it corrupts
msg = msg.replace(u'\xa3', '\x1f') # Fix up for pound
if not re.match('^[\x1f -~]*$', msg):
raise ValueError('Standard ASCII only, please')
self.port.write(str(msg + self.eol))
if permanent:
self.lastmsg = msg
def restore(self):
self.port.write(str(self.lastmsg) + self.eol)