Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

qr.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. raise RuntimeError("QR colors other than black/white not supported on Pi")
  47. else:
  48. self.image = qr.make_image(fill_color = self.c1, back_color = self.c2)
  49. else:
  50. # Show pre-generated QR code image
  51. self.image = PicoImage()
  52. self.image.height = len(self.data)
  53. self.image.width = len(self.data[0])
  54. self.image.data = self.data
  55. if self.heading != None:
  56. DrawText = util.getTextDrawer()
  57. self.text = DrawText(self.gui, self.c1, self.c2)
  58. self.yOff = self.gui.height - self.image.height
  59. else:
  60. self.yOff = int((self.gui.height - self.image.height) / 2)
  61. self.xOff = int((self.gui.width - self.image.width) / 2)
  62. self.restart()
  63. def restart(self):
  64. self.start = time.time()
  65. def finished(self):
  66. return (time.time() - self.start) >= self.time
  67. def draw(self):
  68. # fill border, if needed
  69. if self.c2 != (0, 0, 0):
  70. for x in range(0, self.gui.width):
  71. for y in range(0, self.yOff):
  72. self.gui.set_pixel(x, y, self.c2)
  73. for y in range(0, self.gui.height - self.image.height - self.yOff):
  74. self.gui.set_pixel(x, y + self.yOff + self.image.height, self.c2)
  75. for y in range(0, self.image.height):
  76. for x in range(0, self.xOff):
  77. self.gui.set_pixel(x, y + self.yOff, self.c2)
  78. for x in range(0, self.gui.width - self.image.width - self.xOff):
  79. self.gui.set_pixel(x + self.xOff + self.image.width, y + self.yOff, self.c2)
  80. if self.heading != None:
  81. off = -10
  82. if self.font == "bitmap6":
  83. off -= 3
  84. self.text.text(self.heading, self.font, 0, True, off)
  85. for x in range(0, self.image.width):
  86. for y in range(0, self.image.height):
  87. v = self.image.getpixel((x, y))
  88. if isinstance(v, int):
  89. v = (v, v, v)
  90. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  91. if __name__ == "__main__":
  92. import util
  93. import os
  94. t = util.getTarget()
  95. d = QRScreen(t, "http://ubabot.frubar.net", 10.0, "Drinks:", "tom-thumb", (255, 255, 255), (0, 0, 0))
  96. # dump generated QR image to console, for embedding in Pico script
  97. print("Dumping QR image to qr_tmp.txt")
  98. with open("qr_tmp.txt", "w") as f:
  99. f.write("# QR code image for \"" + d.data + "\"" + os.linesep)
  100. f.write("# size:" + str(d.image.width) + "x" + str(d.image.height) + os.linesep)
  101. f.write("qr_data = [" + os.linesep)
  102. for y in range(0, d.image.height):
  103. f.write(" [" + os.linesep)
  104. for x in range(0, d.image.width):
  105. s = str(d.image.getpixel((x, y)))
  106. f.write(" " + s + "," + os.linesep)
  107. f.write(" ]," + os.linesep)
  108. f.write("]" + os.linesep)
  109. t.loop(d.draw)