-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispcon
executable file
·437 lines (399 loc) · 14.9 KB
/
dispcon
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
#!/usr/bin/env python3
from functools import reduce
import serial
import sys
from enum import Enum
import os
class m(Enum):
normal = 0
brightness = 1
contrast = 2
sharpness = 3
source = 4
sub = 5
subsrc = 6
sub1src = 7
sub2src = 8
sub3src = 9
subtype = 10
power = 11
brightnessRelative = 12
ignore = 13
class Monitor:
def __init__(self, port):
try:
self.ser = serial.Serial(port=port,
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1,
rtscts=False,
dsrdtr=False,
xonxoff=False)
except serial.SerialException:
print("Could not connect! Have you set the right serial port in the environment variable?")
sys.exit(1)
def __del__(self):
try:
self.ser.close()
except:
pass
def setPower(self, state):
command = [0x18, 1+state]
return self.sendCommand(command)
def setBrightness(self, value):
settings = self.getSettings()
return self.sendCommand([0x32, value, 0x37, settings["contrast"], settings["sharpness"], 0x37])
def setContrast(self, value):
settings = self.getSettings()
return self.sendCommand([0x32, settings["brightness"], 0x37, value, settings["sharpness"], 0x37])
def setSharpness(self, value):
settings = self.getSettings()
return self.sendCommand([0x32, settings["brightness"], 0x37, settings["contrast"], value, 0x37])
def getSettings(self):
settings = self.sendCommand([0x33])
return {"brightness": settings[1],"contrast":settings[3],"sharpness":settings[4]}
def getPlatform(self):
return self.sendCommand([0xA2])
def getSerial(self):
return str(self.sendCommand([0x15])[1:]).split("'")[1]
def getSource(self, sink):
if sink == "pip" or "subwin" in sink:
sources = self.sendCommand([0x85])[2:]
returndict = {}
for i, source in enumerate(sources):
for key, value in self.source_ports.items():
if value == source:
returndict["subwin"+str(i+1)] = key
if sink == "pip" or sink == "subwin1":
return returndict["subwin1"]
elif sink == "subwin2":
return returndict["subwin2"]
elif sink == "subwin3":
return returndict["subwin3"]
elif sink == "subwins":
return returndict
else:
print("Unrecognised sink.")
return
elif sink == "main":
source = self.sendCommand([0xAD])[2]
for key, value in self.source_ports.items():
if value == source:
return {"main":key}
def setSource(self, source, sink):
if sink == "main":
if source == "vga":
sType = self.source_types["vga"]
sPort = self.source_ports["vga"]
elif source == "hdmi":
sType = self.source_types["hdmi"]
sPort = self.source_ports["hdmi"]
elif source == "hdmi2":
sType = self.source_types["hdmi"]
sPort = self.source_ports["hdmi2"]
elif source == "dp":
sType = self.source_types["displayport"]
sPort = self.source_ports["dp"]
elif source == "mdp":
sType = self.source_types["displayport"]
sPort = self.source_ports["mdp"]
else:
print("Unrecognised source.")
return
command = [0xAC, sType, sPort, 0x01, 0x00]
elif sink == "pip" or "subwin" in sink:
try:
currentsources = self.getSource("subwins")
if sink == "pip" or sink == "subwin1":
command = [0x84, 0xFD, self.source_ports[source], self.source_ports[currentsources["subwin2"]], self.source_ports[currentsources["subwin3"]]]
elif sink == "subwin2":
command = [0x84, 0xFD, self.source_ports[currentsources["subwin1"]], self.source_ports[source], self.source_ports[currentsources["subwin3"]]]
elif sink == "subwin3":
command = [0x84, 0xFD, self.source_ports[currentsources["subwin1"]], self.source_ports[currentsources["subwin2"]], self.source_ports[source]]
else:
print("Unrecognised sink.")
return
except KeyError:
print("Unrecognised source.")
return
return self.sendCommand(command)
def setSubwin(self, style):
# No subwindow
if not style or style == "off" or style == "1":
command = [0x3C, 0x00, 0x00, 0x00, 0x00]
# PiP type
elif style == "bottomleft":
command = [0x3C, 0x01, 0x00, 0x00, 0x00]
elif style == "topleft":
command = [0x3C, 0x01, 0x01, 0x00, 0x00]
elif style == "topright":
command = [0x3C, 0x01, 0x02, 0x00, 0x00]
elif style == "bottomright" or style == "pip":
command = [0x3C, 0x01, 0x03, 0x00, 0x00]
# PbP type
elif style == "2":
command = [0x3C, 0x02, 0x03, 0x00, 0x00]
elif style == "3":
command = [0x3C, 0x03, 0x03, 0x00, 0x00]
elif style == "4":
command = [0x3C, 0x04, 0x03, 0x00, 0x00]
else:
print("Unrecognised subwindow type.")
return
return self.sendCommand(command)
def sendCommand(self, command):
if command is not None:
packet = bytearray(command)
packet.insert(0, self.HEADER)
packet.insert(1, self.MONITOR_ID)
packet.insert(2, self.CATEGORY)
packet.insert(3, self.PAGE)
packet.insert(4, self.FUNCTION)
packet.insert(5, len(packet)-3)
packet.insert(6, self.CONTROL)
packet.append(reduce(lambda a, b: a ^ b, packet)) # Checksum
self.ser.write(packet)
rep = self.ser.read(30)
result = rep[6:-1]
return result
# Some constants relating to the protocol
MONITOR_ID = 1
HEADER = 0xa6
CONTROL = 0x01
CATEGORY = 0x00
PAGE = 0x00
FUNCTION = 0x00
source_types = {
"video": 0x01,
"s-video": 0x02,
"dvd/hd": 0x03,
"rgbhv": 0x04,
"vga": 0x05,
"hdmi": 0x06,
"dvi": 0x07,
"ops": 0x08,
"displayport": 0x09}
source_ports = {
"vga": 0x00,
"dvi": 0x01,
"hdmi": 0x02,
"hdmi2": 0x03, #mhl
"dp": 0x04,
"mdp": 0x05}
mode = m.normal
arguments = sys.argv
portSet = False
try:
port = os.environ["PHILIPS_MONITOR"]
portSet = True
except KeyError:
pass
portarg = False
for i, arg in enumerate(arguments):
if not portarg:
if arg == "--port":
portarg = True
else:
port = arg
portSet = True
if portSet:
monitor = Monitor(port = port)
else:
print("No serial port set. Try running 'PHILIPS_MONITOR=" + '"/dev/ttyX"' + " dispcon COMMAND', or export a variable.")
print("Alternatively, pass a port using --port PORT")
sys.exit(1)
for i, arg in enumerate(arguments):
# Get mode / no mode set
if mode == m.normal:
# Get info
if arg == "settings":
params = monitor.getSettings()
print("Brightness: " + str(params["brightness"]) + "%\nContrast: " + str(params["contrast"]) + "%\nSharpness: " + str(params["sharpness"]) + "%")
elif arg == "serial":
print("Serial number: " + monitor.getSerial())
elif arg == "platform":
params = monitor.getPlatform()
print(params)
elif arg == "sources":
sources = monitor.getSource("subwins")
sources["main"] = monitor.getSource("main")["main"]
for key, value in sources.items():
print(key + ": " + value)
elif arg == "help" or arg == "?" or arg == "h":
print("Philips Display Serial Interface Communication Protocol (SICP).")
print("This program looks in the environment variable 'PHILIPS_MONITOR' for the serial port.")
print("This can be overridden using the --port command line option.")
print("All commands below may be combined.")
print(" ")
print("Getters:")
print("'dispcon settings' - Show current display settings")
print("'dispcon serial' - Show monitor serial number")
print("'dispcon platform' - Show SICP platform revision")
print("'dispcon sources' - Show current video input sources")
print("'dispcon help' - Show the current help menu")
print(" ")
print("General settings:")
print("'dispcon b X' - set (b)rightness to X %")
print("'dispcon b i X' - set (i)ncrease (b)rightness with X %point")
print("'dispcon c X' - set (c)ontrast to X %")
print("'dispcon s X' - set (s)harpness to X %")
print("'dispcon source I' - set the video input to I")
print(" Supported inputs are 'vga', 'hdmi', 'hdmi2', 'dvi', 'dp' and 'mdp'.")
print(" ")
print("Subwindow configuration:")
print("'dispcon sub type T' - set Subwindow type to T.")
print(" Supported types are 'pip', '1'='off', '2', '3' and '4'.")
print(" Type 2, 3 and 4 display inputs side-by-side.")
print("'discpcon sub off' - disable subwindow")
print("'dispcon sub source O I' - set source of Subwindow O to I.")
print(" Supported Subwindows are '1'='pip', '2' and '3'.")
print(" Supported inputs are 'vga', 'hdmi', 'hdmi2', 'dvi', 'dp' and 'mdp'.")
print("'dispcon sub type T source O1 I1 source O2 I2 ...' - combination to quickly set types and sources")
# Set command mode
elif arg == "b":
mode = m.brightness
elif arg == "c":
mode = m.contrast
elif arg == "s":
mode = m.sharpness
elif arg == "sub":
mode = m.sub
elif arg == "power":
mode = m.power
elif arg == "source":
mode = m.source
elif arg == "--port":
mode = m.ignore
else:
if arg is not arguments[0]:
print("Command not recognised.")
break
# Set Subwindow command mode
elif mode == m.sub:
if arg == "type":
mode = m.subtype
elif arg == "source":
mode = m.subsrc
elif arg == "off":
monitor.setSubwin(False)
mode = m.normal
else:
#End of PIP commands, re-insert current argument
mode = m.normal
arguments.insert(i,arg)
continue
# Set Subwindow type position
elif mode == m.subtype:
monitor.setSubwin(arg)
if i == len(arguments)-1:
mode = m.normal
else:
mode = m.sub
# Set Subwindow source
elif mode == m.subsrc:
if arg == "1" or arg == "pip":
mode = m.sub1src
elif arg == "2":
mode = m.sub2src
elif arg == "3":
mode = m.sub3src
else:
monitor.setSource(arg, sink="main")
mode = m.normal
elif mode == m.sub1src:
monitor.setSource(source=arg, sink="subwin1")
if i == len(arguments)-1:
mode = m.normal
else:
mode = m.sub
elif mode == m.sub2src:
monitor.setSource(source=arg, sink="subwin2")
if i == len(arguments)-1:
mode = m.normal
else:
mode = m.sub
elif mode == m.sub3src:
monitor.setSource(source=arg, sink="subwin3")
if i == len(arguments)-1:
mode = m.normal
else:
mode = m.sub
# Set brightness
elif mode == m.brightness:
try:
val = int(arg)
if val > 100 or val < 0:
print("Invalid value")
else:
monitor.setBrightness(val)
mode = m.normal
except ValueError:
if arg == "i":
mode = m.brightnessRelative
else:
print("Invalid value")
mode = m.normal
break
elif mode == m.brightnessRelative:
try:
val = int(arg)
if val > 100 or val < -100:
print("Invalid value")
else:
params = monitor.getSettings()
curbr = int(params["brightness"])
newbr = curbr + val
if newbr < 0:
newbr = 0
elif newbr > 100:
newbr = 100
monitor.setBrightness(newbr)
except ValueError:
print("Invalid value")
break
mode = m.normal
# Set contrast
elif mode == m.contrast:
try:
val = int(arg)
if val > 100 or val < 0:
print("Invalid value")
else:
monitor.setContrast(val)
except ValueError:
print("Invalid value")
break
mode = m.normal
# Set sharpness
elif mode == m.sharpness:
try:
val = int(arg)
if val > 100 or val < 0:
print("Invalid value")
else:
monitor.setSharpness(val)
except ValueError:
print("Invalid value")
break
mode = m.normal
# Set source
elif mode == m.source:
monitor.setSource(source=arg, sink="main")
mode = m.normal
# Set power
elif mode == m.power:
if arg == "on":
monitor.setPower(True)
elif arg =="off":
monitor.setPower(False)
else:
print("Invalid value")
break
mode = m.normal
elif mode == m.ignore:
mode = m.normal
if mode is not m.normal:
print("Missing argument.")
del monitor