def __init__(self, position, size, font, empty_items_text): super(TextList, self).__init__(position, size) self._font = font self._emtpy_items_text = empty_items_text width = size[0] height = size[1] assert width > 0 assert height > 0 text_height = font.getsize( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")[1] assert text_height > 0 num_lines = int( math.ceil(float(height) / (text_height + self._text_margin))) self._lines = [] for i in range(0, num_lines): line_y = i * (text_height + self._text_margin) self._lines.append( ScrollingText((self._text_margin, line_y), (width - 2 * self._text_margin, text_height), font, "")) self._items = [] self._selected_item = None self._on_empty_items()
class WidgetsTestScreen(Screen): def __init__(self, screen_manager, keyboard_manager, contrast_screen): super(WidgetsTestScreen, self).__init__(screen_manager, keyboard_manager) font = fonts.DEFAULT_FONT_12 self._val = 0 self._pbar = ProgressBar((0, 30), (40, 10), 100) self._tlist = TextList((45, 5), (80, 55), font, "no playlists") self._tlist.set_draw_border(True) self._tlist.set_items([ "one one one one", "two two two two", "three three three three", "four", "five" ]) self._stext = ScrollingText((0, 0), (40, 20), font, u'Hello::Привет!') self._stext.set_invert(True) self._status = PlayingWidget((5, 45), (15, 15)) # self._status.set_draw_border(True) self._contrast_screen = contrast_screen def on_keyboard_event(self, buttons_pressed): if buttons_pressed == [KeyboardManager.UP]: self._tlist.select_previous() elif buttons_pressed == [KeyboardManager.DOWN]: self._tlist.select_next() elif buttons_pressed == [KeyboardManager.LEFT]: self._val = max(0, self._val - 5) self._pbar.set_value(self._val) elif buttons_pressed == [KeyboardManager.RIGHT]: self._val = min(100, self._val + 5) self._pbar.set_value(self._val) elif buttons_pressed == [KeyboardManager.CENTER]: logging.debug("selected: %s" % str(self._tlist.selected)) self._status.set_status(PlayingWidget.PLAYING) elif buttons_pressed == [KeyboardManager.A]: logging.debug("Dimming") self._screen_manager.dim() self._status.set_status(PlayingWidget.STOPPED) elif buttons_pressed == [KeyboardManager.B]: logging.debug("Undimming") self._screen_manager.undim() self._status.set_status(PlayingWidget.PAUSED) elif buttons_pressed == [KeyboardManager.A, KeyboardManager.B]: self._screen_manager.set_screen(self._contrast_screen) def widgets(self): return [self._pbar, self._tlist, self._stext, self._status]
def __init__(self, screen_manager, keyboard_manager, contrast_screen): super(WidgetsTestScreen, self).__init__(screen_manager, keyboard_manager) font = fonts.DEFAULT_FONT_12 self._val = 0 self._pbar = ProgressBar((0, 30), (40, 10), 100) self._tlist = TextList((45, 5), (80, 55), font, "no playlists") self._tlist.set_draw_border(True) self._tlist.set_items([ "one one one one", "two two two two", "three three three three", "four", "five" ]) self._stext = ScrollingText((0, 0), (40, 20), font, u'Hello::Привет!') self._stext.set_invert(True) self._status = PlayingWidget((5, 45), (15, 15)) # self._status.set_draw_border(True) self._contrast_screen = contrast_screen
def __init__(self, screen_manager, keyboard_manager, disp): super(ContrastScreen, self).__init__(screen_manager, keyboard_manager) self._disp = disp self._contrast = 0xCF # initial value from SSD1306.py self._pbar = ProgressBar((0, 25), (128, 14), 255) self._pbar.set_value(0xCF) font = fonts.DEFAULT_FONT_12 self._label = ScrollingText((25, 5), (100, 15), font, 'Contrast')
def __init__(self, screen_manager, keyboard_manager, client): super(StatusScreen, self).__init__(screen_manager, keyboard_manager) self._client = client font = fonts.DEFAULT_FONT_12 self._label = ScrollingText((0, 5), (125, 15), font, u'Status') self._status = ScrollingText((0, 25), (125, 15), font, u'') self._failure = ScrollingText((0, 45), (125, 15), font, u'') self._active_mutex = threading.Condition() self._thread_started_cond = threading.Condition() thread = threading.Thread(target=self._poll) thread.setDaemon(True) self._thread_started_cond.acquire() thread.start() self._thread_started_cond.wait() self._thread_started_cond.release()
def __init__(self, screen_manager, keyboard_manager, client, status_screen, volmgr): super(MainScreen, self).__init__(screen_manager, keyboard_manager) self._client = client self._status_screen = status_screen self._volmgr = volmgr font = fonts.DEFAULT_FONT_14 self._status = PlayingWidget((0, 0), (9, 9)) self._seekbar = ProgressBar((24, 1), (128 - 24, 7), 100) self._artist = ScrollingText((0, 12), (128, 16), font, u'') self._title = ScrollingText((0, 32), (128, 16), font, u'') self._volume = ProgressBar((0, 54), (128, 7), 100) self._play_list_screen = PlayListsScreen(screen_manager, keyboard_manager, client) self._last_update = time.time()
class StatusScreen(Screen): def __init__(self, screen_manager, keyboard_manager, client): super(StatusScreen, self).__init__(screen_manager, keyboard_manager) self._client = client font = fonts.DEFAULT_FONT_12 self._label = ScrollingText((0, 5), (125, 15), font, u'Status') self._status = ScrollingText((0, 25), (125, 15), font, u'') self._failure = ScrollingText((0, 45), (125, 15), font, u'') self._active_mutex = threading.Condition() self._thread_started_cond = threading.Condition() thread = threading.Thread(target=self._poll) thread.setDaemon(True) self._thread_started_cond.acquire() thread.start() self._thread_started_cond.wait() self._thread_started_cond.release() def activate(self): Screen.activate(self) self._active_mutex.acquire() self._active_mutex.notifyAll() self._active_mutex.release() def _poll(self): while True: self._active_mutex.acquire() self._thread_started_cond.acquire() self._thread_started_cond.notifyAll() self._thread_started_cond.release() self._active_mutex.wait() self._active_mutex.release() while self._active: self._status.set_text(self._client.connection_status) lf = self._client.last_connection_failure if lf is None: self._failure.set_text(u'') else: self._failure.set_text(lf) if self._client.connected: self._screen_manager.pop_screen() break time.sleep(2) def widgets(self): return [self._label, self._status, self._failure]
class MainScreen(Screen): # refresh on updates from server or every <REFRESH_RATE> seconds REFRESH_RATE = 5 # playlist to start on pressing 'A' button A_PLAYLIST = u'Дождь' # playlist to start on pressing 'A' and 'B' buttons together # (doesn't work, have to debug) AB_PLAYLIST = u'шум со звуком в конце' # volume adjustment step VOLUME_STEP = 3 def __init__(self, screen_manager, keyboard_manager, client, status_screen, volmgr): super(MainScreen, self).__init__(screen_manager, keyboard_manager) self._client = client self._status_screen = status_screen self._volmgr = volmgr font = fonts.DEFAULT_FONT_14 self._status = PlayingWidget((0, 0), (9, 9)) self._seekbar = ProgressBar((24, 1), (128 - 24, 7), 100) self._artist = ScrollingText((0, 12), (128, 16), font, u'') self._title = ScrollingText((0, 32), (128, 16), font, u'') self._volume = ProgressBar((0, 54), (128, 7), 100) self._play_list_screen = PlayListsScreen(screen_manager, keyboard_manager, client) self._last_update = time.time() def widgets(self): return [ self._status, self._seekbar, self._artist, self._title, # self._volume_label, self._volume] self._volume ] def activate(self): super(MainScreen, self).activate() # this will immediately trigger _connected if already connected self._client.add_connected_callback(self._connected) def deactivate(self): super(MainScreen, self).deactivate() self._client.remove_connected_callback(self._connected) # print("deactivate: noidle") self._stop_idle() def _connected(self): logging.info("Main screen connected, focing update") self._force_update() def _idle_update_status(self): try: self._client.fetch_idle() except mpd.base.PendingCommandError: pass self._update_status() # print("_idle_update_status: idle") self._client.send_idle() # continue idling def _update_status(self): st = self._client.status() cs = self._client.currentsong() state = st.get('state', 'stop') # play/stop/pause if state == 'play': self._status.set_status(PlayingWidget.PLAYING) elif state == 'stop': self._status.set_status(PlayingWidget.STOPPED) elif state == 'pause': self._status.set_status(PlayingWidget.PAUSED) if state == 'stop': self._artist.set_text(u'<stopped>') self._title.set_text(u'') elif isinstance(cs, basestring): # got this once, not sure what it means self._artist.set_text(cs) self._title.set_text(cs) else: artist = cs.get('artist') name = cs.get('name') if artist is not None: self._artist.set_text(artist) elif name is not None: self._artist.set_text(name) else: self._artist.set_text(u'Unknown Artist') title = cs.get('title', None) file = cs.get('file', None) if title is not None: self._title.set_text(title) elif file is not None: self._title.set_text(file) else: self._title.set_text(u'Unknown Title') elapsed = float(st.get('elapsed', 0.0)) duration = float(st.get('duration', 0.0)) if elapsed == 0 or duration == 0: _time = st.get('time', '0:0').split(':') elapsed = float(_time[0]) duration = float(_time[1]) if duration > 0: self._seekbar.set_value(elapsed * 100 / duration) else: self._seekbar.set_value(0) volume = self._volmgr.volume self._volume.set_value(volume) self._last_update = time.time() def _stop_idle(self): self._client.safe_noidle() def _force_update(self): # print("_force_update: noidle") self._stop_idle() self._update_status() # print("_force_update: idle") self._client.send_idle() def tick(self): try: if self._client.connected: force_update = time.time( ) - self._last_update > MainScreen.REFRESH_RATE if force_update and not self._screen_manager.is_screen_off(): self._force_update() elif select([self._client], [], [], 0)[0]: self._idle_update_status() else: self._screen_manager.set_screen(self._status_screen) except (socket.timeout, mpd.ConnectionError): self._screen_manager.set_screen(self._status_screen) def on_keyboard_event(self, buttons_pressed): # print("on_kbd_event: noidle") self._stop_idle() try: resume_idle = True if buttons_pressed == [KeyboardManager.UP] or buttons_pressed == [ KeyboardManager.DOWN ]: self._screen_manager.set_screen(self._play_list_screen) resume_idle = False elif buttons_pressed == [KeyboardManager.LEFT]: volume = self._volmgr.volume volume = max(0, volume - MainScreen.VOLUME_STEP) self._volmgr.set_volume(volume) self._volume.set_value(volume) elif buttons_pressed == [KeyboardManager.RIGHT]: volume = self._volmgr.volume volume = min(100, volume + MainScreen.VOLUME_STEP) self._volmgr.set_volume(volume) self._volume.set_value(volume) elif buttons_pressed == [KeyboardManager.CENTER]: status = self._status.status if status == PlayingWidget.PLAYING: self._client.pause(1) self._status.set_status(PlayingWidget.PAUSED) elif status == PlayingWidget.PAUSED: self._client.pause(0) self._status.set_status(PlayingWidget.PLAYING) elif buttons_pressed == [KeyboardManager.A]: self._client.play_playlist(MainScreen.A_PLAYLIST) elif buttons_pressed == [KeyboardManager.A, KeyboardManager.B]: self._client.play_playlist(MainScreen.AB_PLAYLIST) elif buttons_pressed == [KeyboardManager.B]: status = self._status.status if status == PlayingWidget.PLAYING: self._client.next() if resume_idle: # print("on_kbd_event: idle") self._client.send_idle() except (socket.timeout, mpd.ConnectionError): self._screen_manager.set_screen(self._status_screen)