pygame_player.py
 1  # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  This example is for use on (Linux) computers that are using CPython with
 7  Adafruit Blinka to support CircuitPython libraries. CircuitPython does
 8  not support PIL/pillow (python imaging library)!
 9  
10  Author(s): Melissa LeBlanc-Williams for Adafruit Industries
11  """
12  import serial
13  import adafruit_board_toolkit.circuitpython_serial
14  from animatedgif import AnimatedGif
15  import pygame
16  
17  port = None
18  
19  
20  def detect_port():
21      """
22      Detect the port automatically
23      """
24      comports = adafruit_board_toolkit.circuitpython_serial.data_comports()
25      ports = [comport.device for comport in comports]
26      if len(ports) >= 1:
27          if len(ports) > 1:
28              print("Multiple devices detected, using the first detected port.")
29          return ports[0]
30      raise RuntimeError(
31          "Unable to find any CircuitPython Devices with the CDC data port enabled."
32      )
33  
34  
35  port = serial.Serial(
36      detect_port(),
37      9600,
38      parity="N",
39      rtscts=False,
40      xonxoff=False,
41      exclusive=True,
42  )
43  
44  
45  class PyGameAnimatedGif(AnimatedGif):
46      def __init__(self, display, include_delays=True, folder=None):
47          self._width, self._height = pygame.display.get_surface().get_size()
48          self._incoming_packet = b""
49          super().__init__(display, include_delays=include_delays, folder=folder)
50  
51      def get_next_value(self):
52          if not port:
53              return None
54          while port.in_waiting:
55              self._incoming_packet += port.read(port.in_waiting)
56          while (
57              self._running
58              and not len(self._incoming_packet)
59              and self._incoming_packet.decode().find(",")
60          ):
61              self._incoming_packet += port.read(port.in_waiting)
62              self.check_pygame_events()
63  
64          all_values = self._incoming_packet.decode().split(",")
65          value = all_values.pop(0)
66          self._incoming_packet = ",".join(all_values).encode("utf-8")
67          return value
68  
69      def check_pygame_events(self):
70          for event in pygame.event.get():
71              if event.type == pygame.QUIT:
72                  self._running = False
73              elif event.type == pygame.KEYDOWN:
74                  if event.key == pygame.K_ESCAPE:
75                      self._running = False
76  
77      def update_display(self, image):
78          pilImage = image
79          self.display.blit(
80              pygame.image.fromstring(pilImage.tobytes(), pilImage.size, pilImage.mode),
81              (0, 0),
82          )
83          pygame.display.flip()
84  
85  
86  pygame.init()
87  pygame.mouse.set_visible(False)
88  screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
89  gif_player = PyGameAnimatedGif(screen, include_delays=False, folder="images")
90  pygame.mouse.set_visible(True)
91  port.close()