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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. else:
  51. # Show pre-generated QR code image
  52. self.image = PicoImage()
  53. self.image.height = len(self.data)
  54. self.image.width = len(self.data[0])
  55. self.image.data = self.data
  56. if self.heading != None:
  57. DrawText = util.getTextDrawer()
  58. self.text = DrawText(self.gui, self.c1, self.c2)
  59. self.yOff = self.gui.height - self.image.height
  60. else:
  61. self.yOff = int((self.gui.height - self.image.height) / 2)
  62. self.xOff = int((self.gui.width - self.image.width) / 2)
  63. self.restart()
  64. def restart(self):
  65. self.start = time.time()
  66. def finished(self):
  67. return (time.time() - self.start) >= self.time
  68. def draw(self):
  69. # fill border, if needed
  70. if self.c2 != (0, 0, 0):
  71. for x in range(0, self.gui.width):
  72. for y in range(0, self.yOff):
  73. self.gui.set_pixel(x, y, self.c2)
  74. for y in range(0, self.gui.height - self.image.height - self.yOff):
  75. self.gui.set_pixel(x, y + self.yOff + self.image.height, self.c2)
  76. for y in range(0, self.image.height):
  77. for x in range(0, self.xOff):
  78. self.gui.set_pixel(x, y + self.yOff, self.c2)
  79. for x in range(0, self.gui.width - self.image.width - self.xOff):
  80. self.gui.set_pixel(x + self.xOff + self.image.width, y + self.yOff, self.c2)
  81. if self.heading != None:
  82. off = 0
  83. if self.font == "bitmap6":
  84. off = -14
  85. elif self.font == "tom-thumb":
  86. off = -11
  87. self.text.text(self.heading, self.font, 0, True, off)
  88. for x in range(0, self.image.width):
  89. for y in range(0, self.image.height):
  90. v = self.image.getpixel((x, y))
  91. if isinstance(v, int):
  92. v = (v, v, v)
  93. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  94. if __name__ == "__main__":
  95. import util
  96. import os
  97. import sys
  98. t = util.getTarget()
  99. d = QRScreen(t, "http://ubabot.frubar.net", 10.0, "Drinks:", "tom-thumb", (255, 255, 255), (0, 0, 0))
  100. try:
  101. # dump generated QR image to console, for embedding in Pico script
  102. print("Dumping QR image to qr_tmp.py")
  103. with open("qr_tmp.py", "w") as f:
  104. f.write("# QR code image for \"" + d.data + "\"" + os.linesep)
  105. f.write("# size:" + str(d.image.width) + "x" + str(d.image.height) + os.linesep)
  106. f.write("qr_data = [" + os.linesep)
  107. for y in range(0, d.image.height):
  108. f.write(" [" + os.linesep)
  109. for x in range(0, d.image.width):
  110. s = str(d.image.getpixel((x, y)))
  111. f.write(" " + s + "," + os.linesep)
  112. f.write(" ]," + os.linesep)
  113. f.write("]" + os.linesep)
  114. except Exception as e:
  115. print()
  116. if hasattr(sys, "print_exception"):
  117. sys.print_exception(e)
  118. else:
  119. print(e)
  120. print()
  121. util.loop(t, d.draw)