forked from ahixon/jvc-remote
-
Notifications
You must be signed in to change notification settings - Fork 1
/
projector.py
457 lines (371 loc) · 10.9 KB
/
projector.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
from abc import abstractmethod
from datetime import datetime
import serial
class Button(object):
# TODO: generate strings based on class introspection?
UP = "up"
DOWN = "down"
BACK = "back"
ON = "on"
STANDBY = "standby"
INPUT = "input"
BRIGHTNESS = "brightness"
CONTRAST = "contrast"
SHARPNESS = "sharpness"
COLOR = "color"
TINT = "tint"
NR = "noise_reduction"
HIDE = "hide"
LENS_APERTURE = "lens_aperture"
MENU = "menu"
OK = "ok"
LENS = "lens"
RIGHT = "right"
LEFT = "left"
TEST = "test"
STAGE = "stage"
CINEMA2 = "cinema2"
CINEMA1 = "cinema1"
NATURAL = "natural"
DYNAMIC = "dynamic"
USER1 = "user1"
USER2 = "user2"
USER3 = "user3"
INFO = "info"
GAMMA = "gamma"
COLOR_TEMP = "color_temp"
ASPECT = "aspect"
CODES = {
UP: b"\x30\x31",
DOWN: b"\x30\x32",
BACK: b"\x30\x33",
ON: b"\x30\x35",
STANDBY: b"\x30\x36",
INPUT: b"\x30\x38",
BRIGHTNESS: b"\x30\x39",
CONTRAST: b"\x30\x41",
SHARPNESS: b"\x31\x34",
COLOR: b"\x31\x35",
TINT: b"\x31\x36",
NR: b"\x31\x38",
HIDE: b"\x31\x44",
LENS_APERTURE: b"\x32\x30",
MENU: b"\x32\x45",
OK: b"\x32\x46",
LENS: b"\x33\x30",
RIGHT: b"\x33\x34",
LEFT: b"\x33\x36",
TEST: b"\x35\x39",
STAGE: b"\x36\x37",
CINEMA2: b"\x36\x38",
CINEMA1: b"\x36\x39",
NATURAL: b"\x36\x41",
DYNAMIC: b"\x36\x42",
USER1: b"\x36\x44",
USER2: b"\x36\x43",
USER3: b"\x36\x45",
INFO: b"\x37\x34",
GAMMA: b"\x37\x35",
COLOR_TEMP: b"\x37\x36",
ASPECT: b"\x37\x37",
}
class InputSource(object):
S_VIDEO = "s-video"
VIDEO = "video"
COMPONENT = "component"
COMPUTER = "computer"
HDMI_1 = "hdmi1"
HDMI_2 = "hdmi2"
DISPLAY_NAMES = {
S_VIDEO: "S-Video",
VIDEO: "Video",
COMPONENT: "Component",
COMPUTER: "Computer",
HDMI_1: "HDMI 1",
HDMI_2: "HDMI 2",
}
@classmethod
def name(cls, name):
return cls.DISPLAY_NAMES[name]
class ProjectorCommunicationError(Exception):
pass
def newLogger(enable):
if enable:
return print
else:
def noop(*args, **kwargs):
return
return noop
project_mode_update_interval = 1 # 1s
class Projector(object):
def __init__(self, port_url, unit_id=b"\x89\x01", timeout=1):
self.url = port_url
self.port = serial.serial_for_url(
port_url, 19200, parity="N", stopbits=1, timeout=timeout
)
self.unit_id = unit_id
self.mode_last_updated = datetime(1970, 1, 1)
self.mode_last_known = None
self.log = newLogger(enable=True)
def send_operating(self, cmd, data=None, response_cmd=None):
return self.send(b"\x21", cmd, data, response_cmd)
def send_reference(self, cmd, data=None):
was_ok = self.send(b"\x3f", cmd, data)
# we don't get any data back
if was_ok == True:
return (was_ok, self.recv(cmd))
else:
return (was_ok, None)
def send(self, header, cmd, data=None, response_cmd=None):
footer = b"\x0A"
if data:
pkt = [header, self.unit_id, cmd, data, footer]
else:
pkt = [header, self.unit_id, cmd, footer]
self.port.write(b"".join(pkt))
hexdata = ''
if data:
hexdata = data.hex()
self.log('sending command:', header.hex(), self.unit_id.hex(), cmd.hex(), hexdata)
# check for the command we sent back out, unless another was specified
if not response_cmd:
response_cmd = cmd
return self.recv(response_cmd)
def recv(self, cmd):
# 0x40 = response
# 0x06 = ACK
resp = self.port.readline()
if not resp:
raise ProjectorCommunicationError("no response from projector")
result_code = resp[0]
if result_code == 0x06:
msgtype = "ack"
elif result_code == 0x040:
msgtype = "data"
else:
raise ProjectorCommunicationError(
"device returned unknown result code %r" % result_code
)
# ensure the unit ID matched
response_unit_id = resp[1:3]
if response_unit_id != self.unit_id:
raise ProjectorCommunicationError(
"device returned unknown unit id %r" % response_unit_id
)
self.log('received:', hex(result_code), resp[3:].hex())
response_cmd = resp[3 : 3 + len(cmd)]
if response_cmd != cmd:
raise ProjectorCommunicationError(
"device returned response command response %r for command %r"
% (response_cmd, cmd)
)
data = resp[3 + len(cmd) : -1] # don't include trailing \n
if msgtype == "data":
return data
elif msgtype == "ack":
return True
elif msgtype == "fail":
return False
else:
raise ValueError("invalid internal msgtype")
@property
def ready(self):
return self.send_operating(b"\x00\x00")
@property
def mode(self):
if (datetime.utcnow() - self.mode_last_updated).total_seconds() < project_mode_update_interval:
return self.mode_last_known
success = None
try:
success, state = self.send_reference(b"\x50\x57")
except Exception as ex:
print(ex)
if not success:
return None
# note these are strings
modes = {
b"\x30": "standby",
b"\x31": "power-on",
b"\x32": "cool-down",
b"\x34": "warning",
}
if state not in modes:
raise ValueError("unknown power state " + repr(state))
new_mode = modes[state]
self.mode_last_updated = datetime.utcnow()
self.mode_last_known = new_mode
return new_mode
def turn_on(self):
return self.send_operating(b"\x50\x57", b"\x31")
def turn_off(self):
return self.send_operating(b"\x50\x57", b"\x30")
def set_input(self, source):
# this command is only valid when powered on
if self.mode != "power-on":
return False
try:
code = self.source_to_code(source)
except KeyError:
return ValueError("invalid input " + repr(source))
return self.send_operating(b"\x49\x50", code)
def press_button(self, btn):
if btn not in self.valid_buttons:
raise ValueError("unsupported button " + repr(btn))
# this command is not in standby
# if self.mode == 'standby':
# return None
return self.send_operating(
b"\x52\x43\x37\x33", Button.CODES[btn], response_cmd=b"\x52\x43"
)
@property
def input(self):
# this command is only valid when powered on
if self.mode != "power-on":
return None
success, state = self.send_reference(b"\x49\x50")
if not success:
return None
# note these are strings
try:
source = self.code_to_source(state)
except KeyError:
raise ValueError("unknown video state " + repr(state))
return source
@property
def model(self):
return self.send_operating(b"\x4d\x44")
@abstractmethod
def source_to_code(self, source):
pass
@abstractmethod
def code_to_source(self, code):
pass
@property
@abstractmethod
def valid_sources(self):
pass
@property
@abstractmethod
def valid_buttons(self):
pass
class HD250(Projector):
VALID_BUTTONS = set([
Button.UP,
Button.DOWN,
Button.BACK,
Button.ON,
Button.STANDBY,
Button.INPUT,
Button.BRIGHTNESS,
Button.CONTRAST,
Button.SHARPNESS,
Button.COLOR,
Button.TINT,
Button.NR,
Button.HIDE,
Button.LENS_APERTURE,
Button.MENU,
Button.OK,
Button.LENS,
Button.RIGHT,
Button.LEFT,
Button.TEST,
Button.STAGE,
Button.CINEMA2,
Button.CINEMA1,
Button.NATURAL,
Button.DYNAMIC,
Button.USER1,
Button.USER2,
Button.USER3,
Button.INFO,
Button.GAMMA,
Button.COLOR_TEMP,
Button.ASPECT,
])
INPUT_SOURCES = {
InputSource.S_VIDEO: b"\x30",
InputSource.VIDEO: b"\x31",
InputSource.COMPUTER: b"\x32",
InputSource.HDMI_1: b"\x36",
InputSource.HDMI_2: b"\x37",
}
SOURCE_CODES = {code: name for name, code in INPUT_SOURCES.items()}
VALID_SOURCES = {name for name in INPUT_SOURCES.keys()}
@property
def valid_sources(self):
result = {}
for src in self.VALID_SOURCES:
result[src] = InputSource.name(src)
return result
@property
def valid_buttons(self):
return self.VALID_BUTTONS
def source_to_code(self, source):
return self.INPUT_SOURCES[source]
def code_to_source(self, code):
return self.SOURCE_CODES[code]
@property
def model(self):
return "DLA-HD250"
class RS40(Projector):
VALID_BUTTONS = set([
Button.UP,
Button.DOWN,
Button.BACK,
Button.ON,
Button.STANDBY,
Button.HIDE,
Button.LENS_APERTURE,
Button.MENU,
Button.OK,
Button.LENS,
Button.RIGHT,
Button.LEFT,
Button.STAGE,
Button.CINEMA2,
Button.CINEMA1,
Button.NATURAL,
Button.DYNAMIC,
Button.USER1,
Button.USER2,
Button.USER3,
Button.INFO,
Button.GAMMA,
Button.COLOR_TEMP,
Button.ASPECT,
])
INPUT_SOURCES = {
InputSource.COMPONENT: b"\x32",
InputSource.HDMI_1: b"\x36",
InputSource.HDMI_2: b"\x37",
}
SOURCE_CODES = {code: name for name, code in INPUT_SOURCES.items()}
VALID_SOURCES = {name for name in INPUT_SOURCES.keys()}
@property
def valid_sources(self):
result = {}
for src in self.VALID_SOURCES:
result[src] = InputSource.name(src)
return result
@property
def valid_buttons(self):
return self.VALID_BUTTONS
def source_to_code(self, source):
return self.INPUT_SOURCES[source]
def code_to_source(self, code):
return self.SOURCE_CODES[code]
@property
def model(self):
return "DLA-RS40"
if __name__ == "__main__":
p = HD250("/dev/ttyUSB0")
if p.ready:
print("currently in", p.mode, "mode")
print("viewing input", p.input)
print("model", p.model)
p.press_button(Button.BACK) # dismiss lamp warning
# p.turn_on()
# time.sleep (35) # let the projector warm up
else:
print("device not ready yet")