-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_LCDd_2x40.py
163 lines (141 loc) · 5.84 KB
/
display_LCDd_2x40.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
from __future__ import unicode_literals
# !/usr/bin/env python
# -*- coding: utf-8 -*-
from lcdproc.server import Server
from threading import Thread, Event, Timer, Lock
import time
import logging
from unidecode import unidecode
import music_player
class LockableServer(Server):
"""
A subclass of lcdproc Server to make it thread-safe
"""
def __init__(self, hostname, port):
super(LockableServer, self).__init__(hostname=hostname, port=port)
self._lock = Lock()
def acquire(self):
self._lock.acquire()
def release(self):
self._lock.release()
def __enter__(self):
self.acquire()
# noinspection PyShadowingBuiltins,PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
def __exit__(self, type, value, traceback):
self.release()
class UpdateThread(Thread):
"""
A thread to update the display regularly
"""
def __init__(self, display, loaded_config):
Thread.__init__(self, name='UpdateThread')
self.alive = Event()
self.alive.set()
self.display = display
self.player = music_player.Player(loaded_config)
self.playing = ["",""]
self.loaded_config = loaded_config
def run(self):
logging.debug("Starting updating thread ")
while self.alive.isSet():
time.sleep(0.25)
self.display.set_queue(self.player.queue_count())
self.display.waiting_entry()
if self.player.is_playing():
if self.player.index() != self.playing:
self.display.playing_song(self.player.index(), self.player.title(), self.player.artist())
else:
self.display.waiting()
self.playing = ""
def join(self, timeout=None):
self.alive.clear()
return super(UpdateThread, self).join(timeout)
class DisplayLCDd2x40:
"""
A class to handle all the display functions of the jukebox and actually display them on a 40x2
display through the python lcdproc module
"""
def __init__(self, loaded_config):
self.loaded_config = loaded_config
self.lcd = LockableServer(hostname=self.loaded_config.lcd['lcdd_host'],
port=self.loaded_config.lcd['lcdd_port'])
with self.lcd:
self.lcd.start_session()
self.screen = self.lcd.add_screen(unidecode("jukebox"))
self.screen.set_heartbeat(unidecode("off"))
self.screen.set_priority(unidecode("foreground"))
self.entry_string = self.screen.add_scroller_widget(unidecode("entry"),
text=unidecode("Choose song"), left=1, top=1,
right=28, bottom=1, speed=4)
self.queue_string = self.screen.add_string_widget(unidecode("queue"),
text=unidecode("Queue : 0"), x=30, y=1)
self.icon = self.screen.add_icon_widget(unidecode("playIcon"), x=1, y=2, name=unidecode("STOP"))
self.playing_string = self.screen.add_scroller_widget(unidecode("playing"),
text=unidecode("Nothing in the playlist."
" Add a song ?"),
left=3, top=2, right=40, bottom=2, speed=4)
self.UT = UpdateThread(self, loaded_config)
self.UT.start()
self.timer = None
self.entryInProgress = False
self.lastAdded = time.time()
self.queue = 0
def set_queue(self, q):
"""
Change the length of the queue displayed on the LCD
"""
self.queue = q
with self.lcd:
self.queue_string.set_text(unidecode("Queue : %d" % q))
def waiting(self):
"""
Tell the display that no song is playing
"""
with self.lcd:
self.icon.set_name(unidecode("STOP"))
self.playing_string.set_text(unidecode("Nothing in the playlist. Add a song ?"))
def playing_song(self, index, title, artist):
#TODO
"""
Tell the display which song is playing
"""
with self.lcd:
self.icon.set_name(unidecode("PLAY"))
index = unicode(index[0])+unicode(index[1])
text = "%s - %s - %s" % (index, title, artist)
self.playing_string.set_text(unidecode(text))
def remove_entry(self):
"""
Tell the display that there is no entry
"""
self.entryInProgress = False
self.waiting_entry()
def waiting_entry(self):
"""
The display waits for an entry
"""
if self.entryInProgress is False:
if (self.queue < self.loaded_config.variables['nb_music']) \
or (time.time() - self.lastAdded > self.loaded_config.variables['add_timeout']):
with self.lcd:
self.entry_string.set_text(unidecode("Choose song"))
else:
text = "Wait %s seconds" % (
int(self.loaded_config.variables['add_timeout'] + 1 - time.time() + self.lastAdded))
with self.lcd:
self.entry_string.set_text(unidecode(text))
def entry(self, entry, song=None):
"""
The display shows the current entry
"""
self.entryInProgress = True
text = "Entry : %s" % entry
if self.timer is not None:
self.timer.cancel()
if song is not None:
text += " - %s - %s" % (song.name, song.artist)
self.lastAdded = time.time()
self.timer = Timer(5, self.remove_entry)
self.timer.start()
with self.lcd:
self.entry_string.set_text(unidecode(text))