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.

qr.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. # Uses the Python QR code image generator:
  3. # https://github.com/lincolnloop/python-qrcode
  4. #
  5. # ----------------------------------------------------------------------------
  6. # "THE BEER-WARE LICENSE" (Revision 42):
  7. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  8. # you can do whatever you want with this stuff. If we meet some day, and you
  9. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  10. # ----------------------------------------------------------------------------
  11. import time
  12. import qrcode
  13. import util
  14. class QRScreen:
  15. def __init__(self, g, d, t = 10.0, h = None, f = None, c1 = (0, 0, 0), c2 = (255, 255, 255)):
  16. self.gui = g
  17. self.time = t
  18. self.heading = h
  19. self.font = f
  20. self.c1 = c1
  21. self.c2 = c2
  22. qr = qrcode.QRCode(
  23. box_size = 1,
  24. border = 0,
  25. )
  26. qr.add_data(d)
  27. qr.make(fit = True)
  28. if util.isPi():
  29. # work-around for weird bug in old qrcode lib?
  30. if (self.c1 == (0, 0, 0)) and (self.c2 == (255, 255, 255)):
  31. self.image = qr.make_image(fill_color = "black", back_color = "white")
  32. self.c1 = (0, 0, 0)
  33. self.c2 = (255, 255, 255)
  34. elif (self.c1 == (255, 255, 255)) and (self.c2 == (0, 0, 0)):
  35. self.image = qr.make_image(fill_color = "white", back_color = "black")
  36. self.c1 = (255, 255, 255)
  37. self.c2 = (0, 0, 0)
  38. else:
  39. raise RuntimeError("QR colors other than black/white not supported on Pi")
  40. else:
  41. self.image = qr.make_image(fill_color = self.c1, back_color = self.c2)
  42. if self.heading != None:
  43. DrawText = util.getTextDrawer()
  44. self.text = DrawText(self.gui, self.c1, self.c2)
  45. self.yOff = self.gui.height - self.image.height
  46. else:
  47. self.yOff = int((self.gui.height - self.image.height) / 2)
  48. self.xOff = int((self.gui.width - self.image.width) / 2)
  49. self.restart()
  50. def restart(self):
  51. self.start = time.time()
  52. def finished(self):
  53. return (time.time() - self.start) >= self.time
  54. def draw(self):
  55. # fill border, if needed
  56. if self.c2 != (0, 0, 0):
  57. for x in range(0, self.gui.width):
  58. for y in range(0, self.yOff):
  59. self.gui.set_pixel(x, y, self.c2)
  60. for y in range(0, self.gui.height - self.image.height - self.yOff):
  61. self.gui.set_pixel(x, y + self.yOff + self.image.height, self.c2)
  62. for y in range(0, self.image.height):
  63. for x in range(0, self.xOff):
  64. self.gui.set_pixel(x, y + self.yOff, self.c2)
  65. for x in range(0, self.gui.width - self.image.width - self.xOff):
  66. self.gui.set_pixel(x + self.xOff + self.image.width, y + self.yOff, self.c2)
  67. if self.heading != None:
  68. self.text.text(self.heading, self.font, 0, True, -10)
  69. for x in range(0, self.image.width):
  70. for y in range(0, self.image.height):
  71. v = self.image.getpixel((x, y))
  72. if isinstance(v, int):
  73. v = (v, v, v)
  74. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  75. if __name__ == "__main__":
  76. import util
  77. t = util.getTarget()
  78. d = QRScreen(t, "Hello World", 10.0, "Drinks:", "tom-thumb", (255, 255, 255), (0, 0, 0))
  79. t.loop(d.draw)