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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 PicoImage():
  15. def getpixel(self, xy):
  16. x, y = xy
  17. return self.data[y][x]
  18. class QRScreen:
  19. def __init__(self, g, d, t = 10.0, h = None, f = None, c1 = (0, 0, 0), c2 = (255, 255, 255)):
  20. self.gui = g
  21. self.data = d
  22. self.time = t
  23. self.heading = h
  24. self.font = f
  25. self.c1 = c1
  26. self.c2 = c2
  27. if isinstance(self.data, str):
  28. # Generate QR code with library
  29. qr = qrcode.QRCode(
  30. box_size = 1,
  31. border = 0,
  32. )
  33. qr.add_data(self.data)
  34. qr.make(fit = True)
  35. if util.isPi():
  36. # work-around for weird bug in old qrcode lib?
  37. if (self.c1 == (0, 0, 0)) and (self.c2 == (255, 255, 255)):
  38. self.image = qr.make_image(fill_color = "black", back_color = "white")
  39. self.c1 = (0, 0, 0)
  40. self.c2 = (255, 255, 255)
  41. elif (self.c1 == (255, 255, 255)) and (self.c2 == (0, 0, 0)):
  42. self.image = qr.make_image(fill_color = "white", back_color = "black")
  43. self.c1 = (255, 255, 255)
  44. self.c2 = (0, 0, 0)
  45. else:
  46. # Use default colors for QR code, and colors for text
  47. self.image = qr.make_image(fill_color = "white", back_color = "black")
  48. else:
  49. self.image = qr.make_image(fill_color = self.c1, back_color = self.c2)
  50. # enlarge small images
  51. if ((self.image.width * 2) <= self.gui.width) and ((self.image.height * 2) <= self.gui.height):
  52. from PIL import Image
  53. self.image = self.image.crop(self.image.getbbox())
  54. self.image = self.image.resize((self.image.width * 2, self.image.height * 2), Image.Resampling.NEAREST)
  55. else:
  56. # Show pre-generated QR code image
  57. self.image = PicoImage()
  58. self.image.height = len(self.data)
  59. self.image.width = len(self.data[0])
  60. self.image.data = self.data
  61. if self.heading != None:
  62. DrawText = util.getTextDrawer()
  63. self.text = DrawText(self.gui, self.c1, self.c2)
  64. self.text.setText(self.heading, self.font)
  65. self.yOff = self.gui.height - self.image.height
  66. else:
  67. self.yOff = int((self.gui.height - self.image.height) / 2)
  68. self.xOff = int((self.gui.width - self.image.width) / 2)
  69. self.restart()
  70. def restart(self):
  71. self.start = time.time()
  72. def finished(self):
  73. return (time.time() - self.start) >= self.time
  74. def draw(self):
  75. # fill border, if needed
  76. if self.c2 != (0, 0, 0):
  77. for x in range(0, self.gui.width):
  78. for y in range(0, self.yOff):
  79. self.gui.set_pixel(x, y, self.c2)
  80. for y in range(0, self.gui.height - self.image.height - self.yOff):
  81. self.gui.set_pixel(x, y + self.yOff + self.image.height, self.c2)
  82. for y in range(0, self.image.height):
  83. for x in range(0, self.xOff):
  84. self.gui.set_pixel(x, y + self.yOff, self.c2)
  85. for x in range(0, self.gui.width - self.image.width - self.xOff):
  86. self.gui.set_pixel(x + self.xOff + self.image.width, y + self.yOff, self.c2)
  87. if self.heading != None:
  88. off = 0
  89. if self.font == "bitmap6":
  90. if self.gui.height == 64:
  91. off = -14 - 16
  92. else:
  93. off = -14
  94. elif self.font == "tom-thumb":
  95. if self.gui.height == 64:
  96. off = -11 - 16
  97. else:
  98. off = -11
  99. self.text.draw(0, off)
  100. for x in range(0, self.image.width):
  101. for y in range(0, self.image.height):
  102. v = self.image.getpixel((x, y))
  103. if isinstance(v, int):
  104. v = (v, v, v)
  105. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  106. if __name__ == "__main__":
  107. import util
  108. import os
  109. import sys
  110. t = util.getTarget()
  111. d = QRScreen(t, "http://ubabot.frubar.net", 10.0, "Drinks:", "tom-thumb", (255, 255, 255), (0, 0, 0))
  112. try:
  113. # dump generated QR image to console, for embedding in Pico script
  114. print("Dumping QR image to qr_tmp.py")
  115. with open("qr_tmp.py", "w") as f:
  116. f.write("# QR code image for \"" + d.data + "\"" + os.linesep)
  117. f.write("# size:" + str(d.image.width) + "x" + str(d.image.height) + os.linesep)
  118. f.write("qr_data = [" + os.linesep)
  119. for y in range(0, d.image.height):
  120. f.write(" [" + os.linesep)
  121. for x in range(0, d.image.width):
  122. s = str(d.image.getpixel((x, y)))
  123. f.write(" " + s + "," + os.linesep)
  124. f.write(" ]," + os.linesep)
  125. f.write("]" + os.linesep)
  126. except Exception as e:
  127. print()
  128. if hasattr(sys, "print_exception"):
  129. sys.print_exception(e)
  130. else:
  131. print(e)
  132. print()
  133. util.loop(t, d.draw)