-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathledsign.py
254 lines (223 loc) · 6.9 KB
/
ledsign.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# ledsign: Outputs a message to an M-500 led scrolly sign.
# Documentation on the protocol is sparse; but a few good resources are:
# - http://wls.wwco.com/ledsigns/m-500/
# (in particular, much of this code is based on the protocol descriptiojn at <http://wls.wwco.com/ledsigns/m-500/m-500-protocol.php>
# - http://www.avbrand.com/projects/carpc/ledsign/technew.asp
# - https://github.com/BrightLedSigns/LedSign
#
import serial, io, sys, threading, dateparser
write_lock = threading.Lock()
def openserial():
global ser
# For now, assume we're writing to /dev/ttyUSB0.
ser = serial.Serial("/dev/ttyUSB0")
def writeString(s):
with write_lock:
ser.write(s.encode("utf-8"))
# Source for the next section: http://wls.wwco.com/ledsigns/m-500/m-500-protocol.php
# The first part of any command is the file number. (A file is basically a slot in memory.)
FILE1 = "f01"
# The first file
FILE2 = "f02"
# The second file
# The next part of a command is the transition mode.
transitions = {
"cyclic": "A",
"immediate": "B",
"open right": "C",
"open left": "D",
"open out": "E",
"open in": "F",
"cover out": "G",
"cover right": "H",
"cover left": "I",
"cover in": "J",
"scroll up": "K",
"scroll down": "L",
"interlace 1": "M",
"interlace 2": "N",
"cover up": "O",
"cover down": "P",
"scan line": "Q",
"explode": "R",
"pacman": "S",
"stack": "T",
"shoot": "U",
"flash": "V",
"random": "W",
"slide in": "X",
}
# There are escape sequences for colors and fonts. These can be used anywhere in your message. You can change colors mid-message if you want.
# Note that it actually expects a literal backslash followed by a letter -- these aren't control characters.
colors = {
"red": "\\a",
"bright red": "\\b",
"orange": "\\c",
"bright orange": "\\d",
"yellow": "\\e",
"bright yellow": "\\f",
"green": "\\g",
"bright green": "\\h",
"layer mix": "\\i",
"bright layer mix": "\\j",
"vertical mix": "\\k",
"sawtooth mix": "\\l",
"green on red": "\\m", # Why would you ever...
"red on green": "\\n", # Oh god why
"orange on red": "\\o", # This isn't even readable!
"yellow on green": "\\p", # Just no.
}
fonts = {
"default": "\\s",
"short": "\\q",
"short and wide": "\\r",
"wide": "\\t",
"7x9": "\\u",
"extra wide": "\\v",
"small": "\\w",
}
# There are a few specials
graphics = {
"cityscape": "^Q", # These are big graphics: they fill the whole display
"traffic": "^R",
"tea party": "^S",
"telephone": "^T",
"sunset": "^U",
"cargo ship": "^V",
"swimmers": "^W",
"mouse": "^X",
"sun": "^66", # These are little graphics: they're each a few letters wide.
"cloudy": "^67",
"rain": "^68",
"clock": "^69",
"phone": "^70",
"specs": "^71",
"faucet": "^72",
"rocket": "^73",
"bug": "^74",
"key": "^75",
"shirt": "^76",
"chopper": "^77",
"car": "^78",
"tank": "^79",
"house": "^80",
"teapot": "^81",
"trees": "^82",
"swan": "^83",
"mbike": "^84",
"bike": "^85",
"crown": "^86",
"strawberry": "^87",
"arrow right": "^88",
"arrow left": "^89",
"arrow down": "^90",
"arrow up": "^91",
"cup": "^92",
"chair": "^93",
"shoe": "^94",
"glass": "^95",
}
specialchars = {
"^": r"\^",
"\\": r"\\",
"EOL": "\r", # A line ends with a carriage return. The next line must begin with a transition.
"newline": "\r"
+ transitions[
"open right"
], # This is a shortcut for adding a new line with a reasonable transition.
"EOM": "\r\r\r", # A command ends with three carriage returns.
}
# For convenience
EOM = specialchars["EOM"]
EOL = specialchars["EOL"]
newline = specialchars["newline"]
def demo():
# Demo mode
# This tells the sign to pay attention. 128 addresses all signs -- apparently you can address individual signs by using a device's ID number (whatever that is).
writeString("~128~")
writeString(
FILE1
+ transitions["pacman"]
+ colors["bright red"]
+ "SIPB "
+ colors["bright layer mix"]
+ "LED!"
+ newline
)
# Colors demo
writeString(
"Colors: "
+ "".join(sorted(colors[s] + " " + s + " " for s in colors))
+ newline
)
# Fonts demo
writeString(
"Fonts: "
+ "".join(
sorted(
fonts[s] + " " + s + ": Five hazards! Quickly, exit by jumping down. "
for s in fonts
)
)
+ newline
)
# Transitions demo
writeString(
"Transitions:"
+ "".join(sorted(EOL + transitions[s] + s for s in transitions))
+ newline
)
# Graphics demo
writeString(
"Graphics: "
+ "".join(sorted(s + " " + graphics[s] + " " for s in graphics))
+ EOM
)
# Format a string so that it can specify fonts, graphics, colors, etc.
def format(s):
for m in (colors, fonts, graphics, specialchars, transitions):
for k, v in list(m.items()):
s = s.replace("<" + k + ">", v)
# So that you can pass a "<" or a ">", we use "<lt>" and "<gt>"
# To avoid edge cases like "<lt>gt>", we want to do these replacements simultaneously.
# We didn't know an easy way to do this, so we'll use an out-of-band character (\0) instead to avoid issues.
s = s.replace("<lt>", "<\0")
s = s.replace("<gt>", ">")
s = s.replace("<\0", "<")
return s
import socketserver
class Server(socketserver.ThreadingMixIn, socketserver.TCPServer):
allow_reuse_address = True
class Handler(socketserver.StreamRequestHandler):
def handle(self):
try:
for line in self.rfile:
line = line.strip().decode("utf-8")
print(line)
# print("RECV", line, file=out)
if not line:
break
elif line == "[[demo]]":
demo()
else:
if line.startswith("<raw>"):
line = line[5:]
head = ""
elif line.startswith("<settime>"):
parsed = dateparser.parse(line[9:])
if not parsed:
self.wfile.write(b"Bad Date\n")
self.wfile.flush()
continue
self.wfile.write(b"Date OK\n")
self.wfile.flush()
head = "E"
line = parsed.strftime("%u1%y%m%d%H%M%S")
else:
head = FILE1 + transitions["open right"]
writeString("~128~" + head + format(line) + EOM)
except ValueError:
pass
if __name__ == "__main__":
openserial()
Server(("", 41337), Handler).serve_forever()