display.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!./bin/python3
  2. import yaml
  3. import pygame # type: ignore
  4. # Color constants
  5. BLACK = (0, 0, 0)
  6. GREEN = (0, 255, 0)
  7. class Display:
  8. '''
  9. Display object
  10. '''
  11. def __init__(self, config, pydsp):
  12. # Set config values
  13. self._dx = config.get("dimx", 128)
  14. self._dy = config.get("dimy", 160)
  15. self._playlistFile = config.get("playlist", "music.yml")
  16. self._pydsp = pydsp # Pygame display object
  17. self._fontsize = config.get("fontsize", 12)
  18. # Set initialisation values
  19. self._selection_pointer = 0 # Current selected value
  20. def update(self):
  21. '''
  22. Load the data from the given config file.
  23. '''
  24. playlist = yaml.safe_load(open(self._playlistFile))
  25. font = pygame.font.Font('freesansbold.ttf', self._fontsize)
  26. drawline = 0 # Line currently drawm
  27. for i, item in enumerate(playlist.get("playlist", [])):
  28. print(item["name"])
  29. if drawline == self._selection_pointer:
  30. text = font.render(item["name"], True, BLACK, GREEN)
  31. else:
  32. text = font.render(item["name"], True, GREEN, BLACK)
  33. self._pydsp.blit(text, (0, drawline * self._fontsize))
  34. drawline += 1