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. from draw import DrawText
  15. class QRScreen:
  16. def __init__(self, g, d, t = 10.0, h = None, f = None, c1 = (0, 0, 0), c2 = (255, 255, 255)):
  17. self.gui = g
  18. self.time = t
  19. self.heading = h
  20. self.font = f
  21. self.c1 = c1
  22. self.c2 = c2
  23. qr = qrcode.QRCode(
  24. box_size = 1,
  25. border = 0,
  26. )
  27. qr.add_data(d)
  28. qr.make(fit = True)
  29. if util.isPi():
  30. # work-around for weird bug in old qrcode lib?
  31. if (self.c1 == (0, 0, 0)) and (self.c2 == (255, 255, 255)):
  32. self.image = qr.make_image(fill_color = "black", back_color = "white")
  33. self.c1 = (0, 0, 0)
  34. self.c2 = (255, 255, 255)
  35. elif (self.c1 == (255, 255, 255)) and (self.c2 == (0, 0, 0)):
  36. self.image = qr.make_image(fill_color = "white", back_color = "black")
  37. self.c1 = (255, 255, 255)
  38. self.c2 = (0, 0, 0)
  39. else:
  40. raise RuntimeError("QR colors other than black/white not supported on Pi")
  41. else:
  42. self.image = qr.make_image(fill_color = self.c1, back_color = self.c2)
  43. if self.heading != None:
  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.debug_loop(d.draw)