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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. class CheckHTTP:
  11. def __init__(self, u, r = 600.0):
  12. self.url = u
  13. self.refresh = r
  14. self.successScreen = None
  15. self.failScreen = None
  16. self.get = util.getRequests()
  17. self.restart()
  18. def success(self, s):
  19. self.successScreen = s
  20. def fail(self, f):
  21. self.failScreen = f
  22. def restart(self):
  23. self.start = time.time()
  24. self.response = None
  25. self.request()
  26. if self.successScreen != None:
  27. self.successScreen.restart()
  28. if self.failScreen != None:
  29. self.failScreen.restart()
  30. def request(self):
  31. if self.get == None:
  32. return
  33. if (self.response == None) or ((time.time() - self.start) >= self.refresh):
  34. self.start = time.time()
  35. try:
  36. r = self.get(self.url)
  37. self.response = (r.status_code < 400)
  38. except:
  39. self.response = False
  40. def finished(self):
  41. if self.get == None:
  42. return True
  43. self.request()
  44. if self.response:
  45. return self.successScreen.finished()
  46. else:
  47. return self.failScreen.finished()
  48. def draw(self):
  49. if self.get == None:
  50. return
  51. self.request()
  52. if self.response:
  53. self.successScreen.draw()
  54. else:
  55. self.failScreen.draw()
  56. if __name__ == "__main__":
  57. from solid import Solid
  58. import util
  59. t = util.getTarget()
  60. # show splash screen while connecting to WiFi on Pico
  61. from splash import SplashScreen
  62. splash = SplashScreen(t)
  63. t.loop_start()
  64. splash.draw()
  65. t.loop_end()
  66. d = CheckHTTP("http://xythobuz.de")
  67. d.success(Solid(t, 1.0, (0, 255, 0)))
  68. d.fail(Solid(t, 1.0, (255, 0, 0)))
  69. t.loop(d.draw)