From a3f12aa96b86a44684f2afe43128e426384c8b86 Mon Sep 17 00:00:00 2001 From: Markus Koch Date: Sun, 5 Jul 2020 19:31:07 +0200 Subject: [PATCH] Prerender text and blit to correct position Fixes #1. --- software/webradio.py | 45 +++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/software/webradio.py b/software/webradio.py index a5bec78..b48e8b7 100644 --- a/software/webradio.py +++ b/software/webradio.py @@ -16,6 +16,7 @@ class display(object): self.font_info = ImageFont.truetype('/usr/share/fonts/TTF/DejaVuSans.ttf', 10) self.img = Image.new('1', (self.width, self.height), color = not self.fg) self.d = ImageDraw.Draw(self.img) + self.bar_redraw = True self.dbg = 0 def render(self): @@ -28,9 +29,8 @@ class display(object): print("animate NYI") def get_image(self): - #self.img.save('pil_text_font.png') - #self.img.save("/tmp/wr/frame%d.png" % self.dbg) - print(str(len(self.img.tobytes()))) + self.img.save("/tmp/wr/frame%d.png" % self.dbg) + self.img.tobytes() self.dbg += 1 class display_playback(display): @@ -41,6 +41,7 @@ class display_playback(display): self.title_scroll_max = -1 self.info = "" self.syms = []; + self.title_img = Image.new('1', (0, 0), color = not self.fg) def update_text(self, title): self.title = title @@ -51,45 +52,55 @@ class display_playback(display): self.title_scroll_max,h = self.d.textsize(self.title, font=self.font_title) self.title_scroll = self.title_scroll_max - 10 self.title += self.title # Wraparound for scrolling (pt2) + # Prerender + w,h = self.d.textsize(self.title, font=self.font_title) + self.title_img = Image.new('1', (w, h), color = not self.fg) + font_shift = 5 + ImageDraw.Draw(self.title_img).text((0,-font_shift), self.title, font=self.font_title, fill=self.fg) + self.title_img = self.title_img.crop((0,0,w,h-font_shift)) else: self.title_scroll = 0 self.title_scroll_max = -1 def update_info(self, info): self.info = info + self.bar_redraw = True def update_syms(self, syms): - self.syms = syms; + self.syms = syms + self.bar_redraw = True def scroll_title(self, amount = 1): - if (self.title_scroll >=0): + if (self.title_scroll >= 0): self.title_scroll = (self.title_scroll + amount) % self.title_scroll_max def animate(self, ticks): self.scroll_title(ticks) - def render(self): + def _render_bar(self): # Clear framebuffer - self.d.rectangle((0,0,self.width,self.height), fill = not self.fg) - - # Title - self.d.text((-self.title_scroll,-5), self.title, font=self.font_title, fill=self.fg) + self.d.rectangle((0,self.height - 1 - 10,self.width,self.height), fill = self.fg) # Playback info w,h = self.d.textsize(self.info, font=self.font_info) - self.d.text((self.width-w, self.height - 1 - 10 - 1), self.info, font=self.font_info, fill=self.fg) + self.d.text((self.width-w, self.height - 1 - 10 - 1), self.info, font=self.font_info, fill=not self.fg) # Status icons - status = "" - status = status.join([" " + icon + " " for icon in self.syms]) - self.d.text((-1, self.height - 1 - 10 - 1), status, font=self.font_info, fill=self.fg) + status = "".join([" " + icon + " " for icon in self.syms]) + self.d.text((-1, self.height - 1 - 10 - 1), status, font=self.font_info, fill=not self.fg) + + def render(self): + # Title (prerendered bitmap to shift) + self.img.paste(self.title_img, (-self.title_scroll,0)) + + # Other things were pre-rendered to the framebuffer directly + if (self.bar_redraw): + self.bar_redraw = False + self._render_bar() # TODO: Maybe a progress bar? #self.d.line((0,15, 127,15), fill=self.fg) - # Serialize - # TODO: Serialize - class display_manager(object): def __init__(self): self.tick = 0