Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

net.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python3
  2. # ----------------------------------------------------------------------------
  3. # "THE BEER-WARE LICENSE" (Revision 42):
  4. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  5. # you can do whatever you want with this stuff. If we meet some day, and you
  6. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  7. # ----------------------------------------------------------------------------
  8. import time
  9. import util
  10. import sys
  11. class CheckHTTP:
  12. def __init__(self, u, r = 600.0):
  13. self.url = u
  14. self.refresh = r
  15. self.successScreen = None
  16. self.failScreen = None
  17. self.response = None
  18. self.get = util.getRequests()
  19. self.restart()
  20. def success(self, s):
  21. self.successScreen = s
  22. def fail(self, f):
  23. self.failScreen = f
  24. def restart(self):
  25. self.start = time.time()
  26. # when set to None here, manager will cause re-request every time
  27. # we don't do it, caching the response for the full refresh time.
  28. # this assumes the URL never changes, of course.
  29. #self.response = None
  30. self.request()
  31. if self.successScreen != None:
  32. self.successScreen.restart()
  33. if self.failScreen != None:
  34. self.failScreen.restart()
  35. def request(self):
  36. if self.get == None:
  37. return
  38. now = time.time()
  39. if (self.response == None) or ((now - self.start) >= self.refresh) or (now < self.start):
  40. self.start = now
  41. try:
  42. print("Refreshing " + self.url)
  43. r = self.get(self.url)
  44. r.close()
  45. print("Response: " + str(r.status_code))
  46. self.response = (r.status_code < 400)
  47. except Exception as e:
  48. print()
  49. sys.print_exception(e)
  50. print()
  51. self.response = False
  52. def finished(self):
  53. if self.get == None:
  54. return True
  55. self.request()
  56. if self.response:
  57. return self.successScreen.finished()
  58. else:
  59. return self.failScreen.finished()
  60. def draw(self):
  61. if self.get == None:
  62. return
  63. self.request()
  64. if self.response:
  65. self.successScreen.draw()
  66. else:
  67. self.failScreen.draw()
  68. if __name__ == "__main__":
  69. from solid import Solid
  70. import util
  71. t = util.getTarget()
  72. # show splash screen while connecting to WiFi on Pico
  73. from splash import SplashScreen
  74. splash = SplashScreen(t)
  75. t.loop_start()
  76. splash.draw()
  77. t.loop_end()
  78. d = CheckHTTP("http://xythobuz.de")
  79. d.success(Solid(t, 1.0, (0, 255, 0)))
  80. d.fail(Solid(t, 1.0, (255, 0, 0)))
  81. t.loop(d.draw)