-
Notifications
You must be signed in to change notification settings - Fork 11
/
display_refactored.py
289 lines (228 loc) · 8.88 KB
/
display_refactored.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/python
HEADER = '\x02'
FOOTER = '\x03'
class Output2Display(object):
"Output to the display over serial connection"
def __init__(self, port=None, baudrate=4800):
import serial
self.serial = serial.Serial(port, baudrate)
def write(self, data):
"Write data to serial port"
self.serial.write(data)
def chr_to_hex_to_4bit(character):
"Convert the character from hex to number to 4 bit binary string."
number = int(character, 16)
binary = bin(number)[2::].zfill(4)
return binary
def decode_2_bytes_to_number(one, two):
"Convert the havana encoded two bytes to a number."
one = chr_to_hex_to_4bit(one)
two = chr_to_hex_to_4bit(two)
number = int(one+two, 2)
return number
def encode_number_to_2_bytes(number):
"Convert number to havana encoded two bytes"
binary = bin(number)[2::].zfill(8)
one = hex(int(binary[:4], 2))[2:].upper()
two = hex(int(binary[4:], 2))[2:].upper()
return one, two
def calculate_checksum(data, header, head):
"Calculate checksum"
summed = 0
tmp = [ord(head)]
for entry in header:
tmp.append(ord(entry))
summed += sum(tmp)
tmp = list()
for number in data:
for value in encode_number_to_2_bytes(number):
tmp.append(ord(value))
summed += sum(tmp)
summed += 1
summed = summed & 0xFF # Chop of everything that falls outside a byte
summed = summed ^ 255 # XOR it
summed += 1
return summed
class OutputSerial(object):
"Hook into the serial protocol"
HEADER_LENGTH = 4
FOOTER_LENGTH = 2
ROWS = 16
COLUMNS = None
ADDRESS = None
def __init__(self):
self.header = list()
self.footer = list()
self.worker = self.fsm_data
self.buffer = None # 1 byte buffer
self.hindex = 0
self.vindex = 0
self.data = list()
def fsm_head(self, character):
# FSM, handle header
self.header.append(character)
if len(self.header) == self.HEADER_LENGTH:
# When we got the full header we can now calculate the size.
self.ADDRESS = decode_2_bytes_to_number('0', self.header[1])
constant = decode_2_bytes_to_number('0', self.header[0])
number = decode_2_bytes_to_number(self.header[2], self.header[3])
self.COLUMNS = (number * 8) / self.ROWS
self.worker = self.fsm_data
print('#'*self.COLUMNS)
print('# Header : ' + str(self.header))
print('#'*self.COLUMNS)
print('# Constant: ' + str(constant))
print('# Address : ' + str(self.ADDRESS))
print('# Rows : ' + str(self.ROWS))
print('# Columns : ' + str(self.COLUMNS))
print('#'*self.COLUMNS)
def fsm_foot(self, character):
# FSM, handle footer
self.footer.append(character)
if len(self.footer) == self.FOOTER_LENGTH:
print('# Footer : ' + str(self.footer))
print('# Checksum: ' + str(decode_2_bytes_to_number(*self.footer)))
calculated = calculate_checksum(self.data, self.header, HEADER)
print('# Rehashed: ' + str(calculated))
self.worker = self.fsm_done
self.worker('')
def fsm_work(self, character):
# Handle the character
self.data.append(decode_2_bytes_to_number(self.buffer, character))
def fsm_data(self, character):
# FSM, handle data
if self.buffer is None:
self.buffer = character
else:
self.fsm_work(character)
self.buffer = None
def fsm_done(self, character):
# FSM, handle done
print('# Data : ' + str(len(self.data)))
print('#'*self.COLUMNS)
tmp = list()
while len(tmp) < self.ROWS:
tmp.append([])
first = None
for entry in self.data:
if first is None:
first = entry
else:
one = bin(entry)[2::].zfill(8)
two = bin(first)[2::].zfill(8)
txt = one+two
txt = txt.replace('0', '-')
txt = txt.replace('1', '#')
txt = txt[::-1]
for index, character in enumerate(txt):
tmp[index].append(character)
first = None
for row in tmp:
pass
print(''.join(row))
print('#'*self.COLUMNS)
def write(self, character):
if character == HEADER:
self.worker = self.fsm_head
elif character == FOOTER:
self.worker = self.fsm_foot
else:
self.worker(character)
def encode(matrix_hash_dash, address=1):
"Encode the matix into Hanover display serial protocol"
matrix = matrix_hash_dash.split('\n')
constant = 1
rows = len(matrix)
columns = len(matrix[0])
for column in matrix:
if len(column) != columns:
raise ValueError('Matrix not well formed.')
header = [str(constant),
str(address)]
data_size = (rows*columns) / 8
for character in encode_number_to_2_bytes(data_size):
header.append(character)
data = list()
for index_column in range(columns):
column = list()
for index_row in range(rows):
column.append(matrix[index_row][index_column])
column = ''.join(column)
column = column.replace(' ', '0')
column = column.replace('-', '0')
column = column.replace('#', '1')
column = column[::-1]
nibble_c = column[:4]
nibble_d = column[4:8]
nibble_a = column[8:12]
nibble_b = column[12:]
for nibble in [nibble_a, nibble_b, nibble_c, nibble_d]:
number = int(nibble, 2)
string = hex(number)[2:].upper()
data.append(string)
checksum_data = list()
zipped = zip(data[::2], data[1::2])
for item in zipped:
number = decode_2_bytes_to_number(*item)
checksum_data.append(number)
footer = calculate_checksum(checksum_data, header, HEADER)
footer = encode_number_to_2_bytes(footer)
returns = [HEADER] + header[::] + data + [FOOTER] + list(footer)
returns = ''.join(returns)
return returns
def main(matrix, address=1, console_display=False):
if console_display:
output = OutputSerial()
else:
output = Output2Display('/dev/ttyUSB0')
encoded = encode(matrix, address)
for character in encoded:
output.write(character)
TMP = """
------------------------------------------------------------------------------------------------
--######---##------------------------------------###-###----------######---##-------------------
--##-------##------###---------------------------#######----------##-------##------###----------
--##-------##------------#######-#######-##---##-##-#-##-#######--##-------##------------#######
-#####-----##------##----##---##-##---##-###--##-##---##-##------#####----###------##----##---##
-###-------###-----###---##---##-##---##-###--##-###--##-###-----###------###------###---##---##
-###-------###-----###---#######-#######-#######-###--##-###-----###------###------###---#######
-###-------###-----###---###-----###----------##-###--##-#######-###------######---###---###----
-------------------------###-----###-----#######-----------------------------------------###----
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
"""
if __name__ == '__main__':
from glyphs import five, ten
from flip import *
import time
while True:
container = list()
for row in glify('Hey tuesday'.upper() + ' '*13 + str(time.strftime("%H:%M")), five):
tmp = list()
for item in row:
tmp.append(str(int(item)))
tmp = ''.join(tmp).ljust(96, '0')
tmp += '\n'
container.append(''.join(tmp))
container.append(('0'*96) + '\n')
for row in glify('Lets Dance!'.upper(), ten):
tmp = list()
for item in row:
tmp.append(str(int(item)))
tmp = ''.join(tmp).ljust(96, '0')
tmp += '\n'
container.append(''.join(tmp))
while len(container) < 16:
container.append(('0'*96) + '\n')
print(container)
container = ''.join(container)
container = container.replace('0', '-')
container = container.replace('1', '#')
data = container.strip()
main(data)
time.sleep(3)