-
Notifications
You must be signed in to change notification settings - Fork 2
/
lms_testcom.py
425 lines (368 loc) · 16 KB
/
lms_testcom.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
from pylms.server import Server
from pylms.player import Player
from time import sleep
import smbus
import time
import sys, getopt
myLCD = None
lcd_w = None
class LCD:
"""LCD Class build from the work of Math Hawkins"""
def __init__(self, lcd_address, lcd_width):
# Define some device parameters
self.I2C_ADDR = lcd_address # 0x3f I2C device address
# To detect use sudo i2cdetect -y 0
# or for RPi 2 sudo i2cdetect -y 1
self.LCD_WIDTH = lcd_width # 16 or 20 Maximum characters per line
print ("class LCD " + str(lcd_width))
#LCD_WIDTH = 20 OR 16 # Maximum characters per line
# Define some device constants
self.LCD_CHR = 1 # Mode - Sending data
self.LCD_CMD = 0 # Mode - Sending command
self.LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
self.LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
self.LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
self.LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
self.LCD_BACKLIGHT = 0x08 # On
#LCD_BACKLIGHT = 0x00 # Off
self.ENABLE = 0b00000100 # Enable bit
# Timing constants
self.E_PULSE = 0.0005
self.E_DELAY = 0.0005
#Open I2C interface
self.bus = smbus.SMBus(0) # Rev 1 Pi uses 0
#bus = smbus.SMBus(1) # Rev 2 Pi uses 1
#def lcd_init():
# Initialise display
self.lcd_byte(0x33,self.LCD_CMD) # 110011 Initialise
self.lcd_byte(0x32,self.LCD_CMD) # 110010 Initialise
self.lcd_byte(0x06,self.LCD_CMD) # 000110 Cursor move direction
self.lcd_byte(0x0C,self.LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
self.lcd_byte(0x28,self.LCD_CMD) # 101000 Data length, number of lines, font size
self.lcd_byte(0x01,self.LCD_CMD) # 000001 Clear display
time.sleep(self.E_DELAY)
def lcd_byte(self, bits, mode):
# Send byte to data pins
# bits = the data
# mode = 1 for data
# 0 for command
self.bits_high = mode | (bits & 0xF0) | self.LCD_BACKLIGHT
self.bits_low = mode | ((bits<<4) & 0xF0) | self.LCD_BACKLIGHT
# High bits
try:
self.bus.write_byte(self.I2C_ADDR, self.bits_high)
except:
print ("------------------------------------------")
print ("ERROR - Cannot write on the LCD @ Address " + self.I2C_ADDR )
print ("------------------------------------------")
printhelp()
quit()
self.bus.write_byte(self.I2C_ADDR, self.bits_high)
self.lcd_toggle_enable(self.bits_high)
# Low bits
self.bus.write_byte(self.I2C_ADDR, self.bits_low)
self.lcd_toggle_enable(self.bits_low)
def lcd_toggle_enable(self, bits):
# Toggle enable
time.sleep(self.E_DELAY)
self.bus.write_byte(self.I2C_ADDR, (bits | self.ENABLE))
time.sleep(self.E_PULSE)
self.bus.write_byte(self.I2C_ADDR,(bits & ~self.ENABLE))
time.sleep(self.E_DELAY)
def lcd_string(self, message, line):
# Send string to display
message = message.ljust(self.LCD_WIDTH," ")
if line == 1:
self.lcd_byte(self.LCD_LINE_1, self.LCD_CMD)
elif line == 2:
self.lcd_byte(self.LCD_LINE_2, self.LCD_CMD)
elif line == 3:
self.lcd_byte(self.LCD_LINE_3, self.LCD_CMD)
elif line == 4:
self.lcd_byte(self.LCD_LINE_4, self.LCD_CMD)
for i in range(self.LCD_WIDTH):
self.lcd_byte(ord(message[i]),self.LCD_CHR)
def lms_time_to_string(lms_time ):
"""convert a time from LMS into a mm:ss time"""
seconds = int(lms_time)
minutes = int(seconds / 60)
seconds = seconds - minutes * 60
if seconds < 10 :
seconds = "0" + str(seconds)
if minutes < 10 :
minutes = "0" + str(minutes)
return "%s:%s" %(minutes, seconds)
def printhelp():
"""Print help to explain parameters"""
print ("------------ USAGE ---------------------------------------------")
print ("lms_testcom.py ")
print ("-s <ipserver>")
print ("-p <ipPlayer>")
print ("-w <lcd_width>")
print ("-l <lcd_address>")
print ("-c for clock mode on line 4")
print ("ipserver like 192.168.1.102")
print ("player like 192.168.1.115 / default = auto detect")
print ("lcd with is 16 or 20 / 16 means 16x2, 20 means 20x4 / default = 20")
print ("lcd_address is the i2C LCD address like 0x3f (default). Use sudo i2cdetect -y 0")
print ("----------------------------------------------------------------")
return
def LCDTime(myLCD, lcd_w):
"""PRINT Clock on LCD"""
if lcd_w ==16:
myLCD.lcd_string(" ** Clock **",1)
myLCD.lcd_string(time.strftime('%Y-%m-%d %H:%M'),2)
sleep(5)
else:
myLCD.lcd_string(" ** Clock **",1)
myLCD.lcd_string(" ",2)
myLCD.lcd_string(" " + time.strftime('%Y-%m-%d %H:%M'),3)
myLCD.lcd_string(" ",4) #sq.rescanprogress(),4)
sleep(5)
return
def playerAutodetect(sc, myLCD):
"""Discover the player in PLAY mode"""
print ("autodetect")
while True:
while sc.get_player_count() == 0:
LCDTime(myLCD, lcd_w)
sleep(1)
players = []
players = sc.get_players(True)
for player in players:
sq = player
modePlayer = sq.get_mode()
if modePlayer == "play":
return sq
LCDTime(myLCD, lcd_w)
sleep(1)
def main(argv):
"""LCD MANAGER APP FOR LMS"""
lmsserver = "127.0.0.1"
lmsplayer = ""
lcd_address = "0x3f"
lcd_w = 20
verbose = True
clock_mode = False
try:
opts, args = getopt.getopt(argv,"hs:p:w:l:c",["server=","player=","lcd_width=","lcd_address=","clock"])
except getopt.GetoptError:
printhelp()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
printhelp()
sys.exit()
elif opt in ("-s", "--server"):
lmsserver = arg
elif opt in ("-p", "--player"):
lmsplayer = arg
elif opt in("-w","--lcd_width"):
lcd_w = int(arg)
elif opt in("-l","--lcd_address"):
lcd_address = arg
elif opt in("-c","--clock"):
clock_mode = True
myLCD = LCD(int(lcd_address,16), lcd_w)
#myLCD.lcd_string"1234567890123456",1)
myLCD.lcd_string(" TVC Audio ",1)
myLCD.lcd_string(" LMS LCD INFO ",2)
sleep(2)
myLCD.lcd_string("(C)2017 Renaud ",1)
myLCD.lcd_string("Coustellier ",2)
sleep(1)
sc = Server(hostname=lmsserver, port=9090, username="user", password="password")
# Server connection
# Clock mode waiting connection
connected = False
while connected <> True:
try:
sc.connect()
sleep(1)
connected = True
except Exception as e:
LCDTime(myLCD, lcd_w)
#myLCD.lcd_string(str(e),1)
myLCD.lcd_string("LMS SERVER",1)
myLCD.lcd_string("LMS v.: %s" % sc.get_version(),2)
sleep(2)
while sc.get_player_count() == 0:
LCDTime(myLCD, lcd_w)
sleep(2)
if lmsplayer <> "":
while True:
LCDTime(myLCD, lcd_w)
sq = playerAutodetect(sc, myLCD)
ipPlayer = str(sq.get_ip_address())
ipPlayer = ipPlayer[0:ipPlayer.find(":")]
if ipPlayer == lmsplayer:
break
sleep(3)
else:
myLCD.lcd_string("autodetect player",3)
myLCD.lcd_string("in play mode",3)
sq = playerAutodetect(sc, myLCD)
playerName = sq.get_name()
playerModel = sq.get_model()
if lcd_w == 20:
myLCD.lcd_string(playerName,3)
myLCD.lcd_string(playerModel,4)
sleep(2)
if lcd_w == 16:
# 16x2 LCD Code
while True:
try:
modePlayer = sq.get_mode()
if modePlayer == "pause":
myLCD.lcd_string("mode = pause",1)
myLCD.lcd_string(time.strftime('%Y-%m-%d %H:%M:%S'),2)
sleep(2)
elif modePlayer == "stop":
LCDTime(myLCD, lcd_w)
sleep(2)
# when "stop", looking for a running player except if player defined by user...
if lmsplayer == "":
sq = playerAutodetect(sc, myLCD)
elif modePlayer == "play":
trackAlbum = sq.get_track_album()
currentTrack = sq.get_track_current_title()
trackArtist = sq.get_track_artist()
currentVolume = sq.get_volume()
print ("")
print ("album:" + trackAlbum)
print ("artist:" + trackArtist)
print ("title:" + currentTrack)
myLCD.lcd_string("Alb." + trackAlbum,1)
myLCD.lcd_string("Art." + trackArtist,2)
sleep(2)
myLCD.lcd_string(trackAlbum,1)
myLCD.lcd_string(trackArtist,2)
td = "/" + lms_time_to_string(sq.get_track_duration())
ptc = str(sq.playlist_track_count())
linestatus = 0
charOffset = 0
while True:
linestatus = linestatus + 1
volume = (" - Volume %" + str(sq.get_volume()) )
#te = "time " + lms_time_to_string(sq.get_time_elapsed())
te = lms_time_to_string(sq.get_time_elapsed())
te = te + td
cti = str(sq.playlist_current_track_index())
if len(cti) > 1 and len(ptc) > 1:
if linestatus % 4 == 0:
te = te + " /" + ptc
else:
te = te + " " + cti + "/"
else:
te = te + " " + cti + "/" + ptc
while currentVolume != sq.get_volume():
# Volume
currentVolume = sq.get_volume()
myLCD.lcd_string("Volume %" + str(currentVolume), 1)
sleep(0.3)
if linestatus < 2:
myLCD.lcd_string("tle:" + currentTrack, 1)
myLCD.lcd_string(te, 2)
else:
# Track Name
if len(currentTrack) <= lcd_w:
# LENGHT is < LCD LCD_WIDTH
myLCD.lcd_string(currentTrack, 1)
else:
# LENGHT is > LCD_WIDTH
charOffset = linestatus - 2
myLCD.lcd_string(currentTrack[charOffset:], 1)
if linestatus + lcd_w > len(currentTrack):
linestatus = 0
myLCD.lcd_string(te, 2)
if sq.get_track_current_title() != currentTrack or sq.get_mode() !="play" :
# change detected
myLCD.lcd_string("Track/mode chang", 1)
myLCD.lcd_string("pls wait... ", 2)
linestatus = 0
break
sleep(0.65)
except:
lcd(lcd_w)
else:
# 20x4 LCD Code
while True:
try:
#if True == True:
modePlayer = sq.get_mode()
if modePlayer == "pause":
myLCD.lcd_string(sq.get_name(),1)
myLCD.lcd_string("Mode = Pause",2)
line3 = "RJ45"
ipPlayer = sq.get_ip_address()
ipPlayer = ipPlayer[0:ipPlayer.find(":")]
if int(sq.get_wifi_signal_strength()) > 1:
line3 = "wifi" + str(sq.get_wifi_signal_strength())
line3 = line3 + " " + ipPlayer
myLCD.lcd_string(line3,3)
myLCD.lcd_string(time.strftime('%Y-%m-%d %H:%M:%S'),4)
sleep(0.5)
elif modePlayer == "stop":
LCDTime(myLCD, lcd_w)
sleep(2)
if lmsplayer == "":
# when player mode is stop, looking for another running player except if...
sq = playerAutodetect(sc, myLCD)
elif modePlayer == "play" and lmsplayer == "":
trackAlbum = sq.get_track_album()
currentTrack = sq.get_track_current_title()
trackArtist = sq.get_track_artist()
currentVolume = sq.get_volume()
if verbose == True:
print ("")
print ("album:" + trackAlbum)
print ("artist:" + trackArtist)
print ("title:" + currentTrack)
td = "/" + lms_time_to_string(sq.get_track_duration())
linestatus = 0
charOffset = 0
while True:
if modePlayer <> "play":
break
linestatus = linestatus + 1
volume = (" - Volume %" + str(sq.get_volume()) )
te = lms_time_to_string(sq.get_time_elapsed())
te = te + td
te = te + " " + str(sq.playlist_current_track_index()) + "/" + str(sq.playlist_track_count())
while currentVolume != sq.get_volume():
# Volume
currentVolume = sq.get_volume()
myLCD.lcd_string("Volume %" + str(currentVolume), 1)
sleep(.25)
if sq.get_track_current_title() != currentTrack or sq.get_mode() !="play" :
# change detected
myLCD.lcd_string("Track/mode chang", 1)
#myLCD.lcd_string("pls wait... ", 2)
break
# Track Name
myLCD.lcd_string(trackArtist, 1)
myLCD.lcd_string(trackAlbum, 2)
#----------------------------
if len(currentTrack) <= lcd_w:
# LENGHT is < LCD LCD_WIDTH
myLCD.lcd_string(currentTrack, 3)
else:
# LENGHT is > LCD_WIDTH
charOffset = linestatus - 1
myLCD.lcd_string(currentTrack[charOffset:], 3)
if linestatus + lcd_w > len(currentTrack):
linestatus = 0
#myLCD.lcd_string(currentTrack, 3)
if clock_mode != True:
myLCD.lcd_string(te, 4)
sleep(0.5)
else:
myLCD.lcd_string(time.strftime('%Y-%m-%d %H:%M:%S'),4)
sleep(0.5)
except: #Exception as e:
#else:
#myLCD.lcd_string(str(e), 3)
sq = playerAutodetect(sc, myLCD)
sleep(2)
if __name__ == "__main__":
main(sys.argv[1:])