display.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!./bin/python3
  2. import os
  3. import yaml
  4. import pygame # type: ignore
  5. from typing import Dict
  6. import random
  7. import eyed3
  8. import tempfile
  9. # Color constants
  10. BLACK = (0, 0, 0)
  11. GREEN = (0, 255, 0)
  12. WHITE = (255, 255, 255)
  13. class Display:
  14. '''
  15. Display object which displays a list of strings. It can optionally highlight
  16. a string in the list and display a picture.
  17. '''
  18. def __init__(self, config, pydsp):
  19. # Set config values
  20. self._dx = config.get("dimx", 128) # Drawing dimensions
  21. self._dy = config.get("dimy", 160)
  22. self._pydsp = pydsp # Pygame display object
  23. self._fontsize = config.get("fontsize", 12) # Fontsize to use
  24. self.drawlines = int(self._dy / 12) - 5 # Max number of drawn lines
  25. def update(self, text=None, highlight=-1, title=None, img=None):
  26. '''
  27. Update the display.
  28. text - List of max self.drawlines items to display as list.
  29. highlight - Text item to highlight.
  30. title - List of max 4 items for the title.
  31. img - Image to display in upper left corner.
  32. '''
  33. self._pydsp.fill((0,0,0))
  34. font = pygame.font.Font('freesansbold.ttf', self._fontsize)
  35. if not img:
  36. img = os.path.join("img", "c64tetris.png")
  37. i = pygame.transform.scale(pygame.image.load(img), (50, 50))
  38. self._pydsp.blit(i, (0,0), (0, 0, self._dx, 50))
  39. if not title:
  40. title = ["", "Hacker", " Radio"]
  41. for i, item in enumerate(title):
  42. rendertext = font.render(item, True, WHITE, BLACK)
  43. self._pydsp.blit(rendertext, (55, 2 + i*12))
  44. if i == 3:
  45. break
  46. if not text:
  47. text = []
  48. for i, item in enumerate(text):
  49. if i == highlight:
  50. rendertext = font.render(item, True, BLACK, GREEN)
  51. else:
  52. rendertext = font.render(item, True, GREEN, BLACK)
  53. self._pydsp.blit(rendertext, (0, i * self._fontsize+50))
  54. if i == self.drawlines:
  55. break