Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

qr.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. self.text.text(self.heading, self.font, 0, True, -10)
  82. for x in range(0, self.image.width):
  83. for y in range(0, self.image.height):
  84. v = self.image.getpixel((x, y))
  85. if isinstance(v, int):
  86. v = (v, v, v)
  87. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  88. if __name__ == "__main__":
  89. import util
  90. import os
  91. t = util.getTarget()
  92. d = QRScreen(t, "Hello World", 10.0, "Drinks:", "tom-thumb", (255, 255, 255), (0, 0, 0))
  93. # dump generated QR image to console, for embedding in Pico script
  94. print("Dumping QR image to qr_tmp.txt")
  95. with open("qr_tmp.txt", "w") as f:
  96. f.write("# QR code image for \"" + d.data + "\"" + os.linesep)
  97. f.write("# size:" + str(d.image.width) + "x" + str(d.image.height) + os.linesep)
  98. f.write("qr_data = [" + os.linesep)
  99. for y in range(0, d.image.height):
  100. f.write(" [" + os.linesep)
  101. for x in range(0, d.image.width):
  102. s = str(d.image.getpixel((x, y)))
  103. f.write(" " + s + "," + os.linesep)
  104. f.write(" ]," + os.linesep)
  105. f.write("]" + os.linesep)
  106. t.loop(d.draw)