Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

apod.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env python3
  2. # https://apod.nasa.gov/apod/
  3. #
  4. # ----------------------------------------------------------------------------
  5. # "THE BEER-WARE LICENSE" (Revision 42):
  6. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  7. # you can do whatever you want with this stuff. If we meet some day, and you
  8. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  9. # ----------------------------------------------------------------------------
  10. import time
  11. import sys
  12. import os
  13. import util
  14. from image import ImageScreen
  15. class APOD:
  16. def __init__(self, g, i, to = 10.0, r = 6 * 60 * 60, ito = 30.0):
  17. self.gui = g
  18. self.input = i
  19. self.timeout = to
  20. self.refresh = r
  21. self.input_timeout = ito
  22. self.get = None
  23. self.path = "https://apod.nasa.gov/apod"
  24. self.img_url = None
  25. self.img_path = None
  26. self.image = None
  27. self.last = None
  28. self.restart()
  29. def reloadImage(self):
  30. if (self.last == None) or ((time.time() - self.last) >= self.refresh):
  31. try:
  32. print("APOD refresh")
  33. self.img_url, self.img_desc = self.get_image_metadata()
  34. self.img_path = self.download_image(self.img_url)
  35. self.image = ImageScreen(self.gui, self.img_path, 0.2, 1, 5.0, None, None, False)
  36. self.last = time.time()
  37. except Exception as e:
  38. print()
  39. if hasattr(sys, "print_exception"):
  40. sys.print_exception(e)
  41. else:
  42. print(e)
  43. print()
  44. def restart(self):
  45. self.reloadImage()
  46. self.old_keys = self.input.empty() # TODO support missing input
  47. self.lastInput = None
  48. self.drawingText = False
  49. self.show = time.time()
  50. def finished(self):
  51. if self.lastInput == None:
  52. # skip when no image could be loaded
  53. if self.image == None:
  54. return True
  55. # 10s show image timeout when no buttons have been pressed
  56. if (time.time() - self.show) >= self.timeout:
  57. return True
  58. else:
  59. # 30s button input timeout after last button press
  60. if (time.time() - self.lastInput) >= self.input_timeout:
  61. return True
  62. return False
  63. def fetch(self, url):
  64. # lazily initialize WiFi
  65. if self.get == None:
  66. self.get, post = util.getRequests()
  67. if self.get == None:
  68. return None
  69. try:
  70. #print("GET " + url)
  71. r = self.get(url)
  72. # explitic close on Response object not needed,
  73. # handled internally by r.content / r.text / r.json()
  74. # to avoid this automatic behaviour, first access r.content
  75. # to trigger caching it in response object, then close
  76. # socket.
  77. tmp = r.content
  78. if hasattr(r, "raw"):
  79. if r.raw != None:
  80. r.raw.close()
  81. r.raw = None
  82. return r
  83. except Exception as e:
  84. print()
  85. print(url)
  86. if hasattr(sys, "print_exception"):
  87. sys.print_exception(e)
  88. else:
  89. print(e)
  90. print()
  91. return None
  92. def get_image_metadata(self, path = ""):
  93. if len(path) == 0:
  94. print("Checking for new APOD")
  95. else:
  96. print("Checking out APOD '{}'".format(path))
  97. imgPath = None
  98. imgDesc = None
  99. r = self.fetch(self.path + "/" + path).text
  100. for line in r.splitlines():
  101. start = line.find('IMG SRC="')
  102. if start >= 0:
  103. start += 9
  104. end = line.find('"', start)
  105. img = line[start : end]
  106. imgPath = self.path + "/" + img
  107. break # TODO also check for description
  108. return imgPath, imgDesc
  109. def download_image(self, path):
  110. print("Loading " + path)
  111. r = self.fetch(path).content
  112. scriptDir = os.path.dirname(os.path.realpath(__file__))
  113. imageDir = os.path.join(scriptDir, "images")
  114. imagePath = os.path.join(imageDir, "apod_" + os.path.basename(path))
  115. if os.path.isfile(imagePath):
  116. print("Image already loaded. Skip.")
  117. return imagePath
  118. print("Storing at " + imagePath)
  119. with open(imagePath, 'wb') as f:
  120. f.write(r)
  121. return imagePath
  122. def buttons(self):
  123. keys = self.input.get()
  124. if keys["up"] and (not self.old_keys["up"]) and (not self.old_keys["select"]):
  125. pass # TODO next picture
  126. elif keys["down"] and (not self.old_keys["down"]) and (not self.old_keys["select"]):
  127. pass # TODO previous picture
  128. elif keys["a"] and (not self.old_keys["a"]):
  129. self.drawingText = True
  130. elif (keys["select"] and keys["start"] and (not self.old_keys["start"])) or (keys["start"] and keys["select"] and (not self.old_keys["select"])):
  131. self.restart()
  132. self.old_keys = keys.copy()
  133. def draw(self):
  134. if self.input != None:
  135. self.buttons()
  136. if self.drawingText:
  137. pass # TODO
  138. else:
  139. if self.image != None:
  140. self.image.draw()
  141. if __name__ == "__main__":
  142. i = util.getInput()
  143. t = util.getTarget(i)
  144. s = APOD(t, i)
  145. def helper():
  146. s.draw()
  147. if s.finished():
  148. s.restart()
  149. util.loop(t, helper)