Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

qr.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 qrcode
  10. import util
  11. from draw import DrawText
  12. class QRScreen:
  13. def __init__(self, g, d, t = 10.0, h = None, c1 = (0, 0, 0), c2 = (255, 255, 255)):
  14. self.gui = g
  15. self.time = t
  16. self.heading = h
  17. self.c1 = c1
  18. self.c2 = c2
  19. qr = qrcode.QRCode(
  20. box_size = 1,
  21. border = 0,
  22. )
  23. qr.add_data(d)
  24. qr.make(fit = True)
  25. if util.isPi():
  26. # work-around for weird bug in old qrcode lib?
  27. self.image = qr.make_image(fill_color = "black", back_color = "white")
  28. self.c1 = (0, 0, 0)
  29. self.c2 = (255, 255, 255)
  30. else:
  31. self.image = qr.make_image(fill_color = self.c1, back_color = self.c2)
  32. if self.heading != None:
  33. self.text = DrawText(self.gui)
  34. self.xOff = int((self.gui.width - self.image.width) / 2)
  35. self.yOff = int((self.gui.height - self.image.height) / 2)
  36. self.restart()
  37. def restart(self):
  38. self.start = time.time()
  39. def finished(self):
  40. return (time.time() - self.start) >= self.time
  41. def draw(self):
  42. # fill border, if needed
  43. if self.c2 != (0, 0, 0):
  44. for x in range(0, self.gui.width):
  45. for y in range(0, self.yOff):
  46. self.gui.set_pixel(x, y, self.c2)
  47. for y in range(0, self.gui.height - self.image.height - self.yOff):
  48. self.gui.set_pixel(x, y + self.yOff + self.image.height, self.c2)
  49. for y in range(0, self.image.height):
  50. for x in range(0, self.xOff):
  51. self.gui.set_pixel(x, y + self.yOff, self.c2)
  52. for x in range(0, self.gui.width - self.image.width - self.xOff):
  53. self.gui.set_pixel(x + self.xOff + self.image.width, y + self.yOff, self.c2)
  54. for x in range(0, self.image.width):
  55. for y in range(0, self.image.height):
  56. v = self.image.getpixel((x, y))
  57. if isinstance(v, int):
  58. v = (v, v, v)
  59. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  60. if self.heading != None:
  61. self.text.text(self.heading, "ib8x8u", 0, True, -10)
  62. if __name__ == "__main__":
  63. import util
  64. t = util.getTarget()
  65. d = QRScreen(t, "Hello World", 10.0, "Test")
  66. t.debug_loop(d.draw)