-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmulcd.py
executable file
·472 lines (379 loc) · 10.9 KB
/
fmulcd.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/usr/bin/python
import random
import sys
from signal import alarm, signal, SIGALRM, SIGKILL
import os
import time
import subprocess
import logging
logger = logging.getLogger('fmu_logger')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
fh = logging.FileHandler('/var/log/fmulcd.log', 'a')
fh.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s_%(name)s_%(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
logger.addHandler(ch)
logger.addHandler(fh)
from lib.mpd_client import *
import fmuglobals
from fmutheme import Fmutheme
#from lib.gpio-buttons import AnalogButtons
import pygameui as ui
#from lib.ft5406 import Touchscreen, TS_PRESS, TS_RELEASE, TS_MOVE
if fmuglobals.USE_LIRCD:
from lib.lircpoll import Irw
from scenes import *
from web.server.server import *
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
class Fmulcd(object):
def __init__(self):
self.current = False
self.last = False
screen_width = fmuglobals.SCREEN_SIZE[0]
screen_height = fmuglobals.SCREEN_SIZE[1]
self.screen_dimensions = (screen_width, screen_height)
self.screen = False
self.ss_type = 'random'
self.ss_timer = 0
self.ss_timer_on = True
self.ss_delay = fmuglobals.SS_DELAY
self.web_server = None
self.init_pygame()
self.init_web()
fmutheme = Fmutheme()
ui.theme.use_theme(fmutheme)
rect = pygame.Rect((0,0), self.screen_dimensions)
self.scenes = {
'NowPlaying': NowPlayingScene(rect, 'NowPlaying'),
'Albums': AlbumListScene(rect, 'Albums'),
'Radio': RadioScene(rect, 'Radio'),
'Settings': SettingsScene(rect, 'Settings'),
#'Controls': ControlsScene(rect, 'Controls'),
'Screensaver': ScreensaverScene(rect, 'Screensaver'),
#'SpectrumScreensaver': SpectrumScreensaver(rect, 'SpectrumScreensaver', self.screen),
'WaveScreensaver': WaveScreensaver(rect, 'WaveScreensaver', self.screen),
#'OrigamiScreensaver': OrigamiScreensaver(rect, 'OrigamiScreensaver', self.screen),
}
for name, scene in self.scenes.items():
scene.layout()
self.screensavers = [
'Screensaver',
'WaveScreensaver',
#'LinesScreensaver',
#'SpectrumScreensaver',
#'OrigamiScreensaver',
]
if fmuglobals.RUN_ON_RASPBERRY_PI:
self.scenes['LinesScreensaver'] = LinesScreensaver(rect, 'LinesScreensaver', self.screen)
self.screensavers.append('LinesScreensaver')
self.dialogs = {
'Controls': ControlsDialog(rect),
#'Brightness': BrightnessDialog(rect)
}
for name,scene in self.scenes.items():
scene.on_state_changed.connect(self.scene_state_change)
scene.on_nav_change.connect(self.change_scene)
scene.open_dialog.connect(self.open_dialog)
for name,dialog in self.dialogs.items():
dialog.on_dismissed.connect(self.dialog_dismissed)
self.make_current_scene(self.scenes['NowPlaying'])
#self.ab = AnalogButtons()
def scene_state_change(self, scene):
logger.debug("FMULCD::scene_state_change::%s" % scene.name)
"""
init_pygame
"""
def init_pygame(self):
# this section is an unbelievable nasty hack - for some reason Pygame
# needs a keyboardinterrupt to initialise in some limited circs (second time running)
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
signal(SIGALRM, alarm_handler)
alarm(3)
try:
os.environ["DISPLAY"] = ":0" # JWR 20230516
pygame.display.init()
pygame.font.init()
if fmuglobals.RUN_ON_RASPBERRY_PI:
pygame.mouse.set_visible(fmuglobals.SHOW_MOUSE)
# custom cursor
# surf = pygame.Surface((10, 10)) # you could also load an image
# surf.fill((255, 255, 255, 0)) # and use that as your surface
# color = pygame.cursors.Cursor((0, 0), surf)
# pygame.mouse.set_cursor(color)
if fmuglobals.FULLSCREEN and fmuglobals.RUN_ON_RASPBERRY_PI:
self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
else:
self.screen = pygame.display.set_mode((self.screen_dimensions)) #, pygame.NOFRAME)
alarm(0)
except Alarm:
raise KeyboardInterrupt
logger.debug('pygame inited on rpi?: %s' % fmuglobals.RUN_ON_RASPBERRY_PI)
pygame.key.set_repeat(300,180)
pygame.display.set_caption('FmuLcd')
#pygame.event.set_allowed(None)
#pygame.event.set_allowed(( pygame.QUIT, pygame.KEYDOWN ))
"""
init_web
"""
def init_web(self):
#try:
# f = open(os.devnull, 'w')
# sys.stdout = sys.stderr = f
self.web_server = Server(self)
#except Exception as e:
# logger.debug(e)
"""
make_current_scene
"""
def make_current_scene(self, scene):
logger.debug('Fmulcd::make_current_scene \t' + scene.name)
#if self.current == scene:
# return
if self.current:
self.current.exited()
self.last = self.current
self.current = scene
self.current.entered()
self.current.refresh()
"""
open_dialog
add the requested dialog on top of the current scene
"""
def open_dialog(self, dialog_name):
#logger.debug('fmulcd::open_dialog %s' % dialog_name)
if self.current:
dialog = self.dialogs[dialog_name]
self.current.add_child(dialog)
self.current.dialog = dialog
dialog.entered()
dialog.focus()
"""
dialog_dismissed
"""
def dialog_dismissed(self):
logger.debug('fmulcd::dialog_dismissed')
if self.current:
self.current.dialog = None
self.current.layout()
"""
change_scene
called from a PiScene on_nav_change
push requested scene to ui and refresh it
"""
def change_scene(self, scene_name, refresh=False, from_screensaver=False):
logger.debug("Fmulcd::change_scene %s", self)
if from_screensaver:
logger.debug('exiting screensaver, going to %s' % self.last.name)
self.ss_timer_on = True
#if self.last:
# self.make_current_scene(self.last)
# return
if refresh == True:
self.scenes['Albums'].populate_artists_view()
self.make_current_scene(self.scenes[scene_name])
"""
set_screensaver_type
"""
def set_screensaver_type(self, type) :
self.ss_type = type
if fmu.current.name in fmu.screensavers and fmu.current.name != type:
#fmu.change_scene('NowPlaying', False, True)
self.load_screensaver()
def screensaver_tick(self, ms, activity):
if activity:
self.ss_timer = 0
else:
self.ss_timer += ms
if self.ss_timer >= self.ss_delay:
logger.debug('going to screensaver')
self.ss_timer_on = False
self.ss_timer = 0
self.load_screensaver()
def load_screensaver(self) :
if (self.ss_type == 'random') :
ss = self.screensavers[random.randrange(len(self.screensavers))]
else:
ss = self.ss_type
self.change_scene(ss)
def kill_app(self) :
logger.debug("fmu.kill_app")
fmu.current.exited()
time.sleep(.05)
# if fmuglobals.RUN_ON_RASPBERRY_PI:
# #GPIO.output(18, GPIO.LOW)
# pygame.quit()
# try:
# subprocess.Popen('sudo service fmulcd stop', shell=True, stdout=subprocess.PIPE)
# except:
# pass
pygame.quit()
#sys.exit(0)
os._exit(0)
"""
ts mousedown handler
"""
def ts_press_handler(event, touch):
_press_handler((touch.x, touch.y))
"""
ts mouseup handler
"""
def ts_release_handler(event, touch):
_release_handler((touch.x, touch.y))
"""
ts mousemove handler
"""
def ts_move_handler(event, touch):
lst = touch.last_position
delta = max(abs(lst[0] - touch.x), abs(lst[1] - touch.y))
move_handler((touch.x, touch.y), delta)
"""
mousedown handler
"""
def _press_handler(mousepoint):
#logger.debug("_press_handler %s", mousepoint)
global user_active
global down_in_view
user_active = True
hit_view = fmu.current.hit(mousepoint)
if (hit_view is not None and not isinstance(hit_view, ui.Scene)):
ui.focus.set(hit_view)
down_in_view = hit_view
pt = hit_view.from_window(mousepoint)
hit_view.mouse_down('FOO', pt)
else:
ui.focus.set(None)
"""
mouseup handler
"""
def _release_handler(mousepoint):
global user_active
global down_in_view
user_active = True
hit_view = fmu.current.hit(mousepoint)
if fmu.current.name in fmu.screensavers:
fmu.change_scene('NowPlaying', False, True)
return
if hit_view is not None:
if down_in_view and hit_view != down_in_view:
down_in_view.blurred()
ui.focus.set(None)
pt = hit_view.from_window(mousepoint)
hit_view.mouse_up('FOO', pt)
down_in_view = None
"""
mousemove handler
"""
def _move_handler(mousepoint):
global user_active
global down_in_view
user_active = True
if down_in_view and down_in_view.draggable:
pt = down_in_view.from_window(mousepoint)
down_in_view.mouse_drag(pt, e.rel)
else:
fmu.current.mouse_motion(mousepoint)
"""
translate lirc key to pygame key
"""
def lirc_key_translate(key):
if key == b'KEY_LEFT':
return pygame.K_LEFT
elif key == b'KEY_RIGHT':
return pygame.K_RIGHT
elif key == b'KEY_UP':
return pygame.K_UP
elif key == b'KEY_DOWN':
return pygame.K_DOWN
elif key == b'KEY_ENTER':
return pygame.K_RETURN
elif key == b'KEY_SELECT':
return pygame.K_RETURN
return key
"""
check lirc key for special case
"""
def lirc_special_case(key):
if key == b'KEY_VOLUMEUP':
mpd.set_volume_relative(5)
return True
elif key == b'KEY_VOLUMEDOWN':
mpd.set_volume_relative(-5)
return True
elif key == b'KEY_MUTE':
#mpd.toggle_muted()
mpd.set_volume(0)
return True
return False
"""
main
"""
if __name__ == '__main__':
logger.debug('fmulcd started')
time.sleep(1)
fmu = Fmulcd()
update = False
down_in_view = None
user_active = False
if fmuglobals.RUN_ON_RASPBERRY_PI:
#ts = Touchscreen()
if fmuglobals.USE_LIRCD:
irw = Irw(8)
irw.run()
clock = pygame.time.Clock()
#fps = 32 if fmuglobals.RUN_ON_RASPBERRY_PI else 30
ticks = 0
time.sleep(0.3)
try:
mpd.radio_station_start('http://stream0.wfmu.org/freeform-128k')
except:
pass
while True:
ticks = clock.tick() #(fps)
down_in_view = None
user_active = False
if fmuglobals.RUN_ON_RASPBERRY_PI:
#ts.poll()
# for touch in ts.touches:
# touch.on_press = ts_press_handler
# touch.on_release = ts_release_handler
# #touch.on_move = ts_move_handler
if fmuglobals.USE_LIRCD:
irwlast = irw.last()
if irwlast is not None:
user_active = True
if lirc_special_case(irwlast) is False:
irw_trans = lirc_key_translate(irwlast)
fmu.current.key_down(irw_trans, '')
for e in pygame.event.get():
if e.type == pygame.QUIT:
fmu.kill_app()
break
if e.type == pygame.KEYDOWN:
user_active = True
if (( e.key == pygame.K_ESCAPE )):
fmu.kill_app()
else:
fmu.current.key_down(e.key, e.unicode)
break
#if not fmuglobals.RUN_ON_RASPBERRY_PI:
if e.type == pygame.MOUSEBUTTONDOWN:
_press_handler(pygame.mouse.get_pos())
elif e.type == pygame.MOUSEBUTTONUP:
_release_handler(pygame.mouse.get_pos())
#elif e.type == pygame.MOUSEMOTION:
# _move_handler(mousepoint, e.rel)
if fmu.ss_timer_on:
fmu.screensaver_tick(ticks, user_active)
if fmu.current.update():
if fmu.web_server is not None:
fmu.web_server.update()
if fmu.current.draw(): # and fmu.current.is_screensaver != True:
fmu.screen.blit(fmu.current.surface, (0, 0))
pygame.display.flip()
#time.sleep(.033)