Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

net.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. if hasattr(sys, "print_exception"):
  50. sys.print_exception(e)
  51. else:
  52. print(e)
  53. print()
  54. self.response = False
  55. def finished(self):
  56. if self.get == None:
  57. return True
  58. self.request()
  59. if self.response:
  60. return self.successScreen.finished()
  61. else:
  62. return self.failScreen.finished()
  63. def draw(self):
  64. if self.get == None:
  65. return
  66. self.request()
  67. if self.response:
  68. self.successScreen.draw()
  69. else:
  70. self.failScreen.draw()
  71. if __name__ == "__main__":
  72. from solid import Solid
  73. import util
  74. t = util.getTarget()
  75. # show splash screen while connecting to WiFi on Pico
  76. from splash import SplashScreen
  77. splash = SplashScreen(t)
  78. t.loop_start()
  79. splash.draw()
  80. t.loop_end()
  81. d = CheckHTTP("http://xythobuz.de")
  82. d.success(Solid(t, 1.0, (0, 255, 0)))
  83. d.fail(Solid(t, 1.0, (255, 0, 0)))
  84. t.loop(d.draw)