-
Notifications
You must be signed in to change notification settings - Fork 2
/
mca66.py
494 lines (397 loc) · 11.9 KB
/
mca66.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import serial
import time
import collections
import json
import copy
class ByteUtils:
""" helper class to create byte arrays """
@staticmethod
def ba2hex(a):
""" converts a byte array to hex """
return ":".join("%0.2x" % x for x in a)
@staticmethod
def s2hex(s):
""" converts a string to hex values """
return ":".join(x.encode('hex') for x in s)
@staticmethod
def diff(s1, s2, name1, name2):
h1 = ByteUtils.s2hex(s1)
h2 = ByteUtils.s2hex(s2)
print "1 ("+name1+") "
print h1
print "2 ("+name2+") "
print h2
d = ""
a1 = h1.split(":")
a2 = h2.split(":")
for i in range(0, len(a1)):
if len(d) > 0:
d += ":"
if a1[i] != a2[i]:
try:
#d += ByteUtils.b2h(ByteUtils.h2b(a1[i]) - ByteUtils.h2b(a2[i]))
d += a1[i]
except:
print a1, a2
else:
d += "--"
print "d "
print d
@staticmethod
def h2b(v):
""" hex to byte """
return int(v, 16)
@staticmethod
def b2h(v):
""" byte to hex """
return "%0.2x" % v
@staticmethod
def to_byte_array(hex_tuple):
""" converts an array of hex string to a byte array """
command = bytearray([0 for number in xrange(len(hex_tuple))])
index = 0
for h in hex_tuple:
command[index] = ByteUtils.h2b(h)
index += 1
return ByteUtils._checksum(command)
@staticmethod
def _checksum(command):
""" calculates the checksum """
checksum = 0
for char in command:
checksum += char
command[len(command)-1] = checksum
return command
class ZoneState(object):
def __init__(self, bindata):
data = []
for i in bindata:
data.append(ord(i))
if data[0] != 0x02:
raise Exception("bad data input: header "+str(data[0])+" "+ByteUtils.s2hex(data))
if data[1] != 0x00:
raise "bad data input: reserved"
self.zone = data[2]
self.command = data[3]
self.state = collections.OrderedDict()
if data[3] == 5:
#Data1 - general state
self.state["power"] = True if data[4] & 0b10000000 else False
self.state["mute"] = True if data[4] & 0b01000000 else False
self.state["mode"] = "attributes" if data[4] & 0b00100000 else "volume"
#self.state["power2"] = True if data[4] & 0b00010000 else False
self.state["party"] = True if data[4] & 0b00001000 else False
self.state["party_input"] = data[4] & 0b00000111
#Data2 - keypad led indicator
self.state["mode_led"] = data[5] & 0b01111111
#Data3 - inputs?
#self.state["led_input"] = data[6] & 0b00111111
#self.state["power3"] = True if data[6] & 0b10000000 else False
#self.state["input1"] = True if data[6] & 0b00100000 else False
#self.state["input2"] = True if data[6] & 0b00010000 else False
#self.state["input3"] = True if data[6] & 0b00001000 else False
#self.state["input4"] = True if data[6] & 0b00000100 else False
#self.state["input5"] = True if data[6] & 0b00000010 else False
#self.state["input6"] = True if data[6] & 0b00000001 else False
#Data4 - reserved
#Data5 - input port
self.state["input"] = data[8] + 1
#Data6 - volume (range 195-255; 0 == max)
volume = data[9]
if volume == 0:
volume = 256
volume = int((volume - 195.0) / 61.0 * 100.0)
self.state["volume"] = volume
#Data7 - treble
self.state["treble"] = data[10]
#Data8 - bass
self.state["bass"] = data[11]
#Data9 - balance
self.state["balance"] = data[12]
#checksum
self.state["checksum"] = data[13]
def clone_state(self):
return copy.copy(self.state)
def pretty(self):
#return "zone: "+str(self.zone)+"\n"+json.dumps(self.state, indent=2, separators=(',', ': '))
return "zone: "+str(self.zone)+" "+json.dumps(self.state)
class MCA66Result(object):
def __init__(self, zone_states):
self.data = {}
for z in zone_states:
if z.zone != 0:
self.data[z.zone] = z.clone_state()
self.data[z.zone]['zone'] = z.zone
def json(self):
return json.dumps(self.data)
def json_data(self):
return self.data
class MCA66Command(object):
def __init__(self, command_hex, rx_bytes, name, wait_time = 0):
self.command = ByteUtils.to_byte_array(command_hex)
self.rx_bytes = rx_bytes
self.result = None
self.name = name
self.zone_states = []
self.wait_time = wait_time
def execute(self, ser):
""" execute the command and returns the result """
ser.write(self.command)
self.result = ser.read(self.rx_bytes)
self._parse()
if self.wait_time > 0:
time.sleep(self.wait_time)
return MCA66Result(self.zone_states)
def _parse(self):
#breakup the result into chunks of 14 characters and create a ZoneState for each one
i = 0
while (i+14 <= len(self.result)):
self.zone_states.append(ZoneState(self.result[i:i+14]))
i += 14
def debug(self):
print "Command: %s (tx_bytes %d)" % (self.name, self.rx_bytes)
print(ByteUtils.ba2hex(self.command))
if self.result:
print "result: (len: "+str(len(self.result))+") "+str(ByteUtils.s2hex(self.result))+" "+str(self.result)
for i in self.zone_states:
print i.pretty()
print
def diff(self, label, command):
""" prints out the difference between the 2 command results """
print "diff> "+label
ByteUtils.diff(self.result, command.result, self.name, command.name)
print
@staticmethod
def get_model():
command = (
"02", #head
"00", #reserved
"00", #zone
"08", #command
"00", #data
"00", #checksum
)
return MCA66Command(command, 13, "model")
@staticmethod
def get_zone_state():
command = (
"02", #head
"00", #reserved
"00", #zone
"06", #command
"00", #data
"00", #checksum
)
return MCA66Command(command, 98, "state")
@staticmethod
def set_power(zone, power):
if power:
data = "20"
else:
data = "21"
command = (
"02", #head
"00", #reserved
ByteUtils.b2h(zone), #zone
"04", #command
data, #data
"00", #checksum
)
sleep_time = 0.25
if power:
sleep_time = 2
return MCA66Command(command, 28, "z"+str(zone)+" power "+str(power), sleep_time)
@staticmethod
def vol_up(zone):
command = (
"02", #head
"00", #reserved
ByteUtils.b2h(zone), #zone
"04", #command
"09", #data
"00", #checksum
)
return MCA66Command(command, 28, "z"+str(zone)+" vol_up")
@staticmethod
def vol_down(zone):
command = (
"02", #head
"00", #reserved
ByteUtils.b2h(zone), #zone
"04", #command
"0a", #data
"00", #checksum
)
return MCA66Command(command, 28, "z"+str(zone)+" vol_down")
@staticmethod
def mute(zone):
command = (
"02", #head
"00", #reserved
ByteUtils.b2h(zone), #zone
"04", #command
"22", #data
"00", #checksum
)
return MCA66Command(command, 28, "z"+str(zone)+" vol_down")
@staticmethod
def set_input(zone, input_num):
""" sets the zone's input to to the input number. input number is 1 based """
input_num = int(input_num)
command = (
"02", #head
"00", #reserved
ByteUtils.b2h(zone), #zone
"04", #command
ByteUtils.b2h(input_num+2), #data, input 1 is 0x03, 2 is 0x04
"00", #checksum
)
return MCA66Command(command, 28, "z"+str(zone)+" input"+str(input_num))
if __name__ == '__main__':
# this was run as a main script
state = []
def track_state(ser, label):
s1 = command = MCA66Command.get_zone_state(5);
data = command.execute(ser)
state.append(s1)
s1.name += " "+label
s1.debug()
port = "/dev/ttyUSB0"
ser = serial.Serial(port, 38400, timeout=2)
track_state(ser, "init")
c1 = command = MCA66Command.set_power(3, True);
data = command.execute(ser)
#command.debug()
time.sleep(3)
#===============================================
track_state(ser, "after on")
c2 = command = MCA66Command.set_input(3, 2);
data = command.execute(ser)
command.debug()
time.sleep(2)
#===============================================
track_state(ser, "after input 2")
c2 = command = MCA66Command.set_input(3, 3);
data = command.execute(ser)
command.debug()
time.sleep(2)
#===============================================
track_state(ser, "after input 3")
c2 = command = MCA66Command.set_input(3, 4);
data = command.execute(ser)
#command.debug()
time.sleep(2)
#===============================================
track_state(ser, "after input 4")
c2 = command = MCA66Command.set_input(3, 1);
data = command.execute(ser)
command.debug()
time.sleep(2)
#===============================================
track_state(ser, "after input 1")
c2 = command = MCA66Command.vol_up(3);
data = command.execute(ser)
c2.debug()
c2 = command = MCA66Command.vol_up(3);
data = command.execute(ser)
c2.debug()
c2 = command = MCA66Command.vol_up(3);
data = command.execute(ser)
c2.debug()
track_state(ser, "after volup 1")
c2 = command = MCA66Command.vol_down(3);
data = command.execute(ser)
c2.debug()
c2 = command = MCA66Command.vol_down(3);
data = command.execute(ser)
c2.debug()
c2 = command = MCA66Command.vol_down(3);
data = command.execute(ser)
c2.debug()
#command.debug()
time.sleep(2)
#===============================================
track_state(ser, "after input vol")
c4 = command = MCA66Command.mute(3);
data = command.execute(ser)
#command.debug()
time.sleep(1)
#===============================================
track_state(ser, "after mute1")
c4 = command = MCA66Command.mute(3);
data = command.execute(ser)
#command.debug()
time.sleep(1)
#===============================================
track_state(ser, "after mute2")
c4 = command = MCA66Command.set_power(3, False);
data = command.execute(ser)
#command.debug()
time.sleep(1)
#===============================================
track_state(ser, "end")
#for i in range(0, len(state) - 1):
# state[i].diff("s%d, s%d" % (i, i+1), state[i + 1])
#state[0].diff("s%d, s%d" % (0, len(state) - 1), state[len(state) - 1])
"""
s1 = command = MCA66Command.get_zone_state(3);
data = command.execute(ser)
c1 = command = MCA66Command.set_power(3, True);
data = command.execute(ser)
command.debug()
time.sleep(3)
s2 = command = MCA66Command.get_zone_state(3);
data = command.execute(ser)
c2 = command = MCA66Command.vol_up(3);
data = command.execute(ser)
command.debug()
time.sleep(2)
s3 = command = MCA66Command.get_zone_state(3);
data = command.execute(ser)
c3 = command = MCA66Command.vol_down(3);
data = command.execute(ser)
command.debug()
time.sleep(2)
s4 = command = MCA66Command.get_zone_state(3);
data = command.execute(ser)
c4 = command = MCA66Command.set_power(3, False);
data = command.execute(ser)
command.debug()
time.sleep(1)
s5 = command = MCA66Command.get_zone_state(3);
data = command.execute(ser)
s1.diff("s1, s2", s2)
s2.diff("s2, s3", s3)
s3.diff("s3, s4", s3)
s4.diff("s4, s5", s5)
s1.diff("s1, s5", s5)
"""
"""
c2 = command = MCA66Command.get_zone_state(5);
data = command.execute(ser)
command.debug()
c3 = command = MCA66Command.set_power(5, True);
data = command.execute(ser)
command.debug()
c4 = command = MCA66Command.set_power(6, True);
data = command.execute(ser)
command.debug()
time.sleep(2)
c4_1 = command = MCA66Command.get_zone_state(5);
data = command.execute(ser)
command.debug()
c5 = command = MCA66Command.set_power(5, False);
data = command.execute(ser)
command.debug()
c6 = command = MCA66Command.set_power(6, False);
data = command.execute(ser)
command.debug()
c7 = command = MCA66Command.get_zone_state(5);
data = command.execute(ser)
command.debug()
c2.diff("turned on state", c4_1)
c3.diff("zone 5", c5)
c4.diff("zone 6", c6)
c2.diff("final state", c7)
"""
ser.close()