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

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