-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcx300.py
executable file
·304 lines (266 loc) · 11.3 KB
/
cx300.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hid, time, threading
#
# Timer class for periodic tasks
#
class Timer(threading.Thread):
def __init__(self, func, sec=30):
super(Timer, self).__init__()
self.func = func
self.sec = sec
self.running = True
def stop(self):
self.running = False
def run(self):
while self.running:
t = time.time()
self.func()
time_elapsed = time.time()-t
time.sleep(self.sec-time_elapsed if time_elapsed > 0 else 0)
class CX300:
"""Polycom CX300 USB phone"""
STATUS_AVAILABLE = [0x16, 0x01]
STATUS_BUSY = [0x16, 0x03]
STATUS_BE_RIGHT_BACK = [0x16, 0x05]
STATUS_AWAY = [0x16, 0x05]
STATUS_DO_NOT_DISTURB = [0x16, 0x06]
STATUS_OFF_WORK = [0x16, 0x07]
STATUS_LED_GREEN = [0x16, 0x01]
STATUS_LED_RED = [0x16, 0x03]
STATUS_LED_ORANGE_RED = [0x16, 0x04]
STATUS_LED_ORANGE = [0x16, 0x05]
STATUS_LED_OFF = [0x16, 0x07]
STATUS_LED_GREEN_ORANGE = [0x16, 0x08]
SPEAKER_LED_OFF = [0x02, 0x00]
SPEAKER_LED_ON = [0x02, 0x01]
DISPLAY_CLEAR = [0x13, 0x00]
TEXT_FOUR_CORNERS = [0x13, 0x0D]
TEXT_TOP_LEFT = [0x14, 0x01, 0x80]
TEXT_BOTTOM_LEFT = [0x14, 0x02, 0x80]
TEXT_TOP_RIGHT = [0x14, 0x03, 0x80]
TEXT_BOTTOM_RIGHT = [0x14, 0x04, 0x80]
TEXT_TWO_LINES = [0x13, 0x15]
TEXT_TOP_LINE = [0x14, 0x05, 0x80]
TEXT_BOTTOM_LINE = [0x14, 0x0A, 0x80]
def __init__(self, delegate=None):
self.last_command = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
self.time_last_key_down = None
if(delegate):
self.delegate = delegate
self.speaker_on = False
try:
self.device = hid.device()
self.device.open(0x095d, 0x9201)
print("Manufacturer: %s" % self.device.get_manufacturer_string())
print("Product: %s" % self.device.get_product_string())
print("Serial No: %s" % self.device.get_serial_number_string())
except IOError as ex:
print(ex)
self.device.set_nonblocking(1)
self.keepalive()
self.device.write(CX300.SPEAKER_LED_OFF)
self.timer = Timer(self.keepalive) # Need to periodically send the feature report
self.timer.start()
# print(dir(self.device))
def keepalive(self):
# Send feature report - without this the phone asks to upgrade Office Communicator
self.device.send_feature_report([0x17, 0x09, 0x04, 0x01, 0x02])
def close(self):
print("Closing the device")
self.device.close()
def display_clear(self):
self.device.write(CX300.DISPLAY_CLEAR)
def display_two_lines(self, first_line, second_line=""):
first_line = str(first_line).decode('utf-8') # Umlauts work this way
second_line = str(second_line).decode('utf-8') # Umlauts work this way
self.display_clear() # Needed, otherwise doesn't clear the screen in case of empty string
self.device.write(CX300.TEXT_TWO_LINES)
# text = text.ljust(16) # make it 16 characters long
# If the text in the first line is too long, then print the rest in the
# second line. TODO: Could split this on whitespace/word boundaries
if((len(first_line)>24) and (second_line=="")):
print("Putting text in second line")
second_line=first_line[23:48]
first_line=first_line[0:23]
l=1
for text in [first_line, second_line]:
if(l==1):
self.device.write(CX300.TEXT_TOP_LINE)
elif(l==2):
self.device.write(CX300.TEXT_BOTTOM_LINE)
filler = [0x00] * 8
longchunk = []
chunks = [text[i:i + 8] for i in range(0, len(text), 8)]
print(chunks)
i = 0
for chunk in chunks:
hexchunk = [ord(x) for x in chunk]
for j in range(8 - len(hexchunk)):
hexchunk.append(0x00)
longchunk = [item for sublist in zip(hexchunk, filler) for item in sublist]
longchunk.insert(0, 0x15)
if (i < len(chunks)-1):
longchunk.insert(1, 0x00)
else:
longchunk.insert(1, 0x80)
# print [hex(no) for no in longchunk]
self.device.write(longchunk)
i = i + 1
l = l + 1
def watch_keyboard(self):
print("Starting to watch the keyboard, press Ctrl-C to exit")
while(True):
msgs=[]
r = self.device.read(8)
if(r==[]):
continue
print('[{}]'.format(', '.join(hex(x) for x in r)))
# print(r)
# print(r[2])
try:
if((self.last_command[4] == 0x40) and (r[4] != 0x40)):
print("onHook")
if(self.delegate):
self.delegate.on_hook()
elif((self.last_command[4] != 0x40) and (r[4] == 0x40)):
print("offHook")
if(self.delegate):
self.delegate.off_hook()
elif((self.last_command[4] == 0x50) and (r[4] != 0x50)):
print("offSpeaker")
if(self.delegate):
self.delegate.off_speaker()
elif((self.last_command[4] != 0x50) and (r[4] == 0x50)):
print("onSpeaker")
if(self.delegate):
self.delegate.on_speaker()
# For whatever reason, sometimes I get 0x52 rather than 0x50
# for speaker
elif((self.last_command[4] == 0x52) and (r[4] != 0x52)):
print("offSpeaker")
if(self.delegate):
self.delegate.off_speaker()
elif((self.last_command[4] != 0x52) and (r[4] == 0x52)):
print("onSpeaker")
if(self.delegate):
self.delegate.on_speaker()
elif((self.last_command[4] == 0x60) and (r[4] != 0x60)):
print("offHeadphones")
if(self.delegate):
self.delegate.off_headphones()
elif((self.last_command[4] != 0x60) and (r[4] == 0x60)):
print("onHeadphones")
if(self.delegate):
self.delegate.on_headphones()
elif((self.last_command[7] == 0x01) and (r[7] != 0x01)):
print("offMute")
if(self.delegate):
self.delegate.off_mute()
elif((self.last_command[7] != 0x01) and (r[7] == 0x01)):
print("onMute")
if(self.delegate):
self.delegate.on_mute()
elif((self.last_command[2] == 0x00) and (r[2] != 0x00)):
print("keyDown")
self.time_last_key_down = time.time()
key = r[2]-1
print(key)
if(self.delegate):
self.delegate.key_down(key)
elif((self.last_command[2] != 0x00) and (r[2] == 0x00)):
print("keyUp")
time_key_was_pressed = time.time() - self.time_last_key_down
print(time_key_was_pressed)
key = self.last_command[2]-1
print(key)
if(self.delegate):
self.delegate.key_up(int(key), time_key_was_pressed)
elif(r[1] == 0x8):
print("longPress")
key = r[2]-1
print(key)
if(self.delegate):
self.delegate.long_press(key)
elif((self.last_command[1] == 0x00) and (r[1] != 0x00)):
key = r[1]
if(key==16): # Ignore Mute key, is handled elsewhere
continue
print("keyDown")
self.time_last_key_down = time.time()
if(key==4): # Redial
key=12
if(key==2): # Hold
key=13
if(key==32): # Backspace
key=14
print(key)
if(self.delegate):
self.delegate.key_down(key)
elif((self.last_command[1] != 0x00) and (r[1] == 0x00)):
key = self.last_command[1]
if(key==16): # Ignore Mute key, is handled elsewhere
continue
print("keyUp")
time_key_was_pressed = time.time() - self.time_last_key_down
print(time_key_was_pressed)
if(key==4): # Redial
key=12
if(key==2): # Hold
key=13
if(key==32): # Backspace
key=14
print(key)
if(self.delegate):
self.delegate.key_up(int(key), time_key_was_pressed)
except:
pass
print("")
self.last_command = r
class Delegate:
"""Instead of this class, a consumer of this file could implement
its functions and hence receive and act on the callbacks"""
def on_hook(self):
print("Delegate on_hook acting...")
def off_hook(self):
print("Delegate off_hook acting...")
def on_speaker(self):
print("Delegate on_speaker acting...")
def off_speaker(self):
print("Delegate off_speaker acting...")
def on_headphones(self):
print("Delegate on_headphones acting...")
def off_headphones(self):
print("Delegate off_headphones acting...")
def on_mute(self):
print("Delegate on_mute acting...")
def off_mute(self):
print("Delegate off_mute acting...")
def key_down(self, key):
print("Delegate key_down(%i) acting..." % (key))
def key_up(self, key, time_key_was_pressed):
print("Delegate key_up(%i, %f) acting..." % (key, time_key_was_pressed))
def long_press(self, key):
print("Delegate long_press(%i) acting..." % (key))
def main():
d = Delegate()
c = CX300(d)
c.display_two_lines("Welcome to CX300", "from Python")
# Cycle through LED colors
stati = [CX300.STATUS_LED_GREEN, CX300.STATUS_LED_RED,
CX300.STATUS_LED_ORANGE_RED, CX300.STATUS_LED_ORANGE,
CX300.STATUS_LED_GREEN_ORANGE, CX300.STATUS_LED_OFF]
for status in stati:
time.sleep(0.2)
c.device.write(status)
# c.display_clear()
c.display_two_lines("Press some keys", "on the CX300 öäüß") # Umlauts test
try:
c.watch_keyboard()
except (KeyboardInterrupt, SystemExit):
print '\n! Received keyboard interrupt, quitting\n'
c.timer.stop()
c.close()
c.close()
if (__name__ == "__main__"):
main()