-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyaquos.py
412 lines (377 loc) · 12.6 KB
/
pyaquos.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
import serial
class controller:
"""
This is a class for controlling SHARP Aquos LC-32D59U, LC-42D59U, and
possibly other model LCD televisions via their RS-232 serial interface.
"""
def __init__(self, ttyname):
"""
Generate a new pyaquos interface object
@type ttyname: string
@param ttyname: the dev file for the serial interface (often
/dev/ttyUSB0 for USB serial adapters)
"""
self.tty = serial.Serial(ttyname, 9600)
self.ok = True
def lock_power(self, state):
"""
Enable/disable the use of the power() command.
@type state: bool
@param state: True if the power() command is to be disabled.
@return: A boolean set to True if the command ran succesfully.
"""
if state:
return self.send_command('RSPW0 \r\n')
else:
return self.send_command('POWR1 \r\n')
def lock_state(self):
"""
Power cycle the TV.
@return: Boolean set to True if the power() command is disabled.
"""
if self.query_state('RSPW? \r\n')[0] == '1':
return True
else:
return False
def power(self, state):
"""
Power cycle the TV.
@type state: bool
@param state: The power state for the TV. True is ON, False is OFF.
@return: A boolean set to True if the command ran succesfully.
"""
if state:
return self.send_command('POWR1 \r\n')
else:
return self.send_command('POWR0 \r\n')
def power_state(self):
"""
Power cycle the TV.
@return: A boolean set to True if the TV is currently powered on.
"""
if self.query_state('POWR? \r\n')[0] == '1':
return True
else:
return False
def volume(self, level):
"""
Change the volume on the TV.
@type level: int
@param level: The volume level for the TV, from 1 to 99 (low to high)
@return: A boolean set to True if the command ran succesfully.
"""
if level < 100 and level > 0:#The volume can range from 1 to 99
return self.send_command('VOLM%02d \r\n' % level)
else:
raise ValueError('Volume must be between 1 and 99')
def volume_state(self):
"""
Query the current volume level of the TV.
@return: An integer storing the current volume level of the TV.
"""
return int(self.query_state('VOLM?? \r\n'))
def screen_position(self, horizontal, vertical, clock, phase):
"""
Change the VGA screen position, clock, and phase (this may be dangerous)
I honestly don't know what clock and phase really do.
@type horizontal: int
@param horizontal: Horizontal offset (0-100)
@type vertical: int
@param vertical: Vertical offset (0-100)
@type clock: int
@param clock: Clock frequency (0-180)
@type phase: int
@param phase: Phase offset (0-40)
@return: A boolean set to True if the command ran succesfully.
"""
if horizontal < 0 or horizontal > 100:
raise ValueError('Horizontal offset must be between 0 and 100')
if vertical < 0 or vertical > 100:
raise ValueError('Vertical offset must be between 0 and 100')
if clock < 0 or clock > 180:
raise ValueError('Clock Frequency must be between 0 and 180')
if phase < 0 or phase > 40:
raise ValueError('Phase offset must be between 0 and 40')
hreturn = self.send_command('HPOS%03d \r\n' % horizontal)
vreturn = self.send_command('VPOS%03d \r\n' % vertical)
clckreturn = self.send_command('CLCK%03d \r\n' % clock)
phsreturn = self.send_command('PHSE%03d \r\n' % phase)
return hreturn and vreturn and clckreturn and phase
def screen_state(self):
"""
Return the current VGA screen settings.
@return: (int, int, int, int) of the horizontal offset, vertical
offset, clock frequency and phase.
"""
horizontal = int(self.query_state('HPOS??? \r\n'))
vertical = int(self.query_state('VPOS??? \r\n'))
clock = int(self.query_state('CLCK??? \r\n'))
phase = int(self.query_state('PHSE??? \r\n'))
return (horizontal, vertical, clock, phase)
def mute(self, state):
"""
Mute the TV.
@type state: bool
@param state: The muting state. True is muted, False is unmuted.
@return: A boolean set to True if the command ran succesfully.
"""
if state:
return self.send_command('MUTE1 \r\n')
else:
return self.send_command('MUTE2 \r\n')
def mute_state(self):
"""
Query if the TV is muted
@return: A boolean set to True if TV is muted.
"""
if self.query_state('MUTE? \r\n')[0] == '1':
return True
else:
return False
def input_toggle(self):
"""
Toggle your way through the inputs. This moves down through the
inputs, and loops back to INPUT1 after being called on INPUT8.
@return: A boolean set to True if the command ran succesfully.
"""
return self.send_command('ITGD1 \r\n')
def input_tv(self):
"""
Switch to the TV tuner.
@return: A boolean set to True if the command ran succesfully.
"""
return self.send_command('ITVD0 \r\n')
def input_num(self, number):
"""
Change the A/V Source for the TV
@type number: int
@param number: The input number for the TV. (See below comments)
@return: A boolean set to True if the command ran succesfully.
"""
if number < 9 and number > 0:#There are 8 inputs
#For the LC-32D59U, the inputs are:
#INPUT0: TV Tuner (use input_tv() to access this)
#INPUT1: HDMI1 Top Side HDMI
#INPUT2: HDMI2 Bottom Side HDMI
#INPUT3: HDMI3 Top Rear HDMI
#INPUT4: HDMI4 Bottom Rear HDMI
#INPUT5: COMP1 Top Rear Component
#INPUT6: COMP2 Bottom Rear Component
#INPUT7: AV Rear Composite
#INPUT8: PC IN VGA PC input
return self.send_command('IAVD%1d \r\n' % number)
else:
raise ValueError('Input must be between 1 and 8')
def input_state(self):
"""
Return the current input number of the TV.
@return: The integer number corresponding to the current input.
"""
return int(self.query_state('IAVD? \r\n'))
def cc_toggle(self):
"""
Toggle on/off the closed captioning.
@return: A boolean set to True if the command ran succesfully
"""
return self.send_command('CLCP0 \r\n')
def cc_state(self):
"""
Return the current state of the closed captioning
@return: A boolean set to True if the closed captioning is enabled.
"""
if self.query_state('CLCP? \r\n')[0] == '1':
return True
else:
return False
def av_mode(self, modenum):
"""
Change the AV mode (I believe this changes up the contrast, brightness,
and gamma, possibly other things).
@type modenum: int
@param modenum: the number of the mode you want to switch into
(see comments below)
@return: A boolean set to True if the command ran succesfully.
"""
if modenum < 6 and modenum > -1:# There are 6 possible options
#For the LCD-32D59U, the inputs are:
#MODE0: Toggle to the next mode
#MODE1: Standard
#MODE2: Dynamic
#MODE3: Movie
#MODE4: Power Saver
#MODE5: User
return self.send_command('AVMD%1d \r\n' % modenum)
else:
raise ValueError('AV mode number must be between 0 and 5')
def query_av_mode(self):
"""
Return the current AV mode.
@return: An integer storing the current AV mode.
"""
return int(self.query_state('AVMD? \r\n'))
def view_mode(self, modenum):
"""
Change the widescreen mode.
@type modenum: int
@param modenum: The widescreen mode you want to switch into (see
comments below)
@return: A boolean set to True if the command ran succesfully.
"""
if modenum < 8 and modenum > -1:#There are 8 possible options
#For the LCD-32D59U
#MODE0: Toggle to the next mode
#MODE1: Normal
#MODE2: S. Stretch
#MODE3: Stretch
#MODE4: Zoom
#MODE5: Full Screen
#MODE6: Dot by Dot
#MODE7: Cinema
return self.send_command('WIDE%1d \r\n' % modenum)
else:
raise ValueError('Widescreen mode number must be between 0 and 7')
def query_view_mode(self):
"""
Return the current widescreen mode.
@return: An integer storing the current widescreen mode.
"""
return int(self.query_state('WIDE? \r\n'))
def surround(self, state):
"""
Turn on/off the surround sound.
@type state: bool
@param state: The surround state: True is on, False is off.
@return: A boolean set to True if the command ran succesfully.
"""
if state:
return self.send_command('ACSU2 \r\n')
else:
return self.send_command('ACSU1 \r\n')
def query_surround(self):
"""
Return the current surround sound setting.
@return: A boolean set to True if surround sound is set to ON.
"""
if self.query_state('ACSU? \r\n') == '2':
return True
else:
return False
def sleep_timer(self, time):
"""
Set the TV's sleep timer. Unfortunately, there are only a few fixed
times that can be used, rather than arbitrary times.
@type time: int
@param time: the number of minutes for the sleep timer (0 is off)
@return: A boolean set to True if the command run successfully.
"""
#The only acceptable sleep times are 0, 30, 60, 90, and 120 minutes
if time == 0:
return self.send_command('OFTM0 \r\n')
elif time == 30:
return self.send_command('OFTM1 \r\n')
elif time == 60:
return self.send_command('OFTM2 \r\n')
elif time == 90:
return self.send_command('OFTM3 \r\n')
elif time == 120:
return self.send_command('OFTM4 \r\n')
else:
raise ValueError("The sleep time must be 0, 30, 60, 90, or 120 min")
def query_sleep_timer(self):
"""
Return the current sleep timer settings. I don't know if there is
any way to determine the amount of time remaining on the timer.
@return: An integer number of minutes the sleep timer is set to.
"""
return 30*int(self.query_state('OFTM? \r\n'))
def analog_channel(self, channel):
"""
Change the analog tuner channel.
@type channel: int
@param channel: the channel number to switch to
@return: A boolean set to True if the command run successfully.
"""
if channel < 1 or channel > 135:
raise ValueError("The channel must be between 1 and 135")
return self.send_command('DCCH%03d \r\n' % channel)
def query_analog_channel(self):
"""
Return the current channel setting for the analog tuner.
@return: an integer storing the current channel for the analog channel.
"""
return int(self.query_state('DCCH??? \r\n'))
def channel_up(self):
"""
Increment the channel.
@return: A boolean set to True if the command run successfully.
"""
return self.send_command('CHUP0 \r\n')
def channel_down(self):
"""
Decrement the channel.
@return: A boolean set to True if the command run successfully.
"""
return self.send_command('CHDW0 \r\n')
def digital_air_channel(self, channel):
"""
Change the digital air tuner channel
@type channel: int
@param channel: The digital air channel
@return: A boolean set to True if the command run successfully.
"""
if channel < 100 or channel > 9999:
raise ValueError("The channel must be between 100 and 9999")
return self.send_command('DA2P%04d\r\n' % channel)
def query_digital_air_channel(self):
"""
Return the current digital air channel
@return: an integer storing the current digital air tuner value.
"""
return int(self.query_state('DA2P????\r\n'))
#I am unsure how I could query the current digital cable channel, because of
#the different methods of actually setting the channel.
def digital_cable_channel(self, channel):
"""
Change the digital cable tuner channel
@type channel: int or (int, int)
@param channel: The digital channel (either single digit or (major, minor) channel number)
@return: A boolean set to True if the command run successfully.
"""
if type(channel) == tuple:
if channel[0] < 1 or channel[1] < 0 or channel[0] > 999 or \\
channel[1] > 999:
return ValueError("Digital Channel Major Number must be
between 1 and 999 and Minor Number must be between 0 and 999")
major = self.send_command('DC2U%03d \r\n' % channel[0])
minor = self.send_command('DC2L%03d \r\n' % channel[1])
return minor and major
else:
if channel < 0 or channel > 16383:
return ValueError("Digital channel number must be between 0 and 16383")
elif channel < 10000:
return self.send_command('DC10%04d\r\n' % channel)
else:
return self.send_command('DC11%04d\r\n' % channel)
def send_command(self, commandstring):
"""
Internal method for sending the commands and checking the return code.
@type commandstring: string
@param commandstring: The command to be passed to the TV over RS-232.
@return: A boolean set to True if the command ran succesfully
"""
self.tty.write(commandstring)
response = self.tty.readline()
if response == 'OK\r\n':
return True
else:
return False
def query_state(self, commandstring):
"""
Internal method for checking the state of some TV parameter.
@type commandstring: string
@param commandstring: The command to be passed to the TV over RS-232.
@return: The returned value.
"""
self.tty.write(commandstring)
response = self.tty.readline()
return response[:-2]