forked from fmntf/fiatcan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextMessage.py
206 lines (175 loc) · 5.83 KB
/
TextMessage.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
import math
import time
import unidecode
from can import Message
from textwrap import wrap
from FiatProtocol import *
class TextMessage:
string_end = "000000"
field_separator = "111111"
char_map = {
"0": "000001",
"1": "000010",
"2": "000011",
"3": "000100",
"4": "000101",
"5": "000110",
"6": "000111",
"7": "001000",
"8": "001001",
"9": "001010",
".": "001011",
"A": "001100",
"B": "001101",
"C": "001110",
"D": "001111",
"E": "010000",
"F": "010001",
"G": "010010",
"H": "010011",
"I": "010100",
"J": "010101",
"K": "010110",
"L": "010111",
"M": "011000",
"N": "011001",
"O": "011010",
"P": "011011",
"Q": "011100",
"R": "011101",
"S": "011110",
"T": "011111",
"U": "100000",
"V": "100001",
"W": "100010",
"X": "100011",
"Y": "100100",
"Z": "100101",
"ñ": "100110",
"ç": "100111",
" ": "101000",
"Ğ": "101001",
"i": "101010",
"j": "101011",
"§": "101100",
"À": "101101",
"Ä": "101110",
"ŭ": "101111",
"Ü": "110000",
"_": "110010",
"?": "110101",
"°": "110110",
"!": "110111",
"+": "111000",
"-": "111001",
":": "111010",
"/": "111011",
"#": "111100",
"*": "111101",
}
bitstrings_map = {}
instpanel_menu_prefix1 = "0001000000011010"
instpanel_menu_prefix2 = "0001000100011010"
instpanel_text_prefix1 = "0001000000010110"
instpanel_text_prefix2 = "0001000100010110"
bus = None
def __init__(self, bus):
self.bus = bus
for key, value in self.char_map.items():
self.bitstrings_map[value] = key
def encode_instpanel(self, message, is_menu):
bitstring = self.encode_string(message).ljust(96, '0')
if is_menu:
line1 = self.instpanel_menu_prefix1 + bitstring[:48]
line2 = self.instpanel_menu_prefix2 + bitstring[48:]
else:
line1 = self.instpanel_text_prefix1 + bitstring[:48]
line2 = self.instpanel_text_prefix2 + bitstring[48:]
can1 = Message(arbitration_id=CANID_BM_TEXT_MESSAGE, data=self.bitstring_to_bytes(line1))
can2 = Message(arbitration_id=CANID_BM_TEXT_MESSAGE, data=self.bitstring_to_bytes(line2))
return can1, can2
def encode_music(self, track, artist, folder=None):
bitstring = ""
if folder:
bitstring += self.encode_string(folder)
else:
bitstring += self.string_end
bitstring += self.field_separator
bitstring += self.encode_string(artist)
bitstring += self.field_separator
bitstring += self.encode_string(track, 18)
messages = []
nmessages = math.ceil(len(bitstring)/48) -1
i = 0
for bits in wrap(bitstring, 48):
if len(bits) < 48:
bits = bits.ljust(48, '0')
bits = ba(hex='0x{}{}2A'.format(nmessages, i)).bin + bits
messages.append(
Message(arbitration_id=CANID_BM_TEXT_MESSAGE, data=self.bitstring_to_bytes(bits))
)
i += 1
return messages
def normalize_string(self, string, maxlen=14):
string = string.rstrip().upper()
if len(string) > maxlen:
string = string[:maxlen]
chunks = []
for char in string:
if char in self.char_map:
chunks.append(char)
else:
decoded = unidecode.unidecode(char)
if len(decoded) == 1 and decoded in self.char_map:
chunks.append(decoded)
else:
chunks.append(' ')
return ''.join(chunks).replace(' ', ' ')
def encode_string(self, string, maxlen=14):
string = self.normalize_string(string, maxlen)
chunks = []
for char in string:
chunks.append(self.char_map[char])
return ''.join(chunks)
def decode(self, messages):
bitstring = ""
message_type = 'song' if messages[0][2] == '2' else 'instrument panel'
for message in messages:
bitstring += ba(hex='0x'+message[4:]).bin
return "{}: {}".format(message_type, self.decode_bitstring(bitstring))
def decode_radio(self, message):
bitstring = ba(hex='0x'+message).bin
return self.decode_bitstring(bitstring)
def decode_bitstring(self, bitstring):
decoded = ""
for bits in wrap(bitstring, 6):
if len(bits) < 6:
continue
if bits == self.field_separator:
decoded += "; "
elif bits == self.string_end:
pass
else:
if bits in self.bitstrings_map:
decoded += self.bitstrings_map[bits]
else:
decoded += "?"
return decoded
def bitstring_to_bytes(self, s):
return int(s, 2).to_bytes(len(s) // 8, byteorder='big')
def send_instpanel(self, string, is_menu=False):
print("Showing '{}' on instrument panel".format(string))
messages = self.encode_instpanel(string, is_menu)
self.bus.send(messages[0])
time.sleep(0.01)
self.bus.send(messages[1])
def send_music(self, track, artist, folder=None):
messages = self.encode_music(track, artist, folder)
for message in messages:
self.bus.send(message)
time.sleep(0.01)
def clear_instpanel(self):
print("Clearing instrument panel")
self.bus.send(
Message(arbitration_id=CANID_BM_TEXT_MESSAGE, data=bytearray(MESSAGE_INSTPANEL_CLEAR.bytes))
)