eink.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import os as _os
  2. fontpath = _os.path.realpath(_os.path.join(__file__,"..","..","wavesharelib","pic", 'Font.ttc'))
  3. def eink_qr(args):
  4. from waveshare_epd import epd2in13_V2
  5. from PIL import Image,ImageDraw
  6. import qrcode
  7. epd = epd2in13_V2.EPD()
  8. epd.init(epd.FULL_UPDATE)
  9. image = Image.new('1', (epd.height, epd.width), 0)
  10. qr = qrcode.QRCode(version=1, box_size=3, border=1)
  11. qr.add_data(args.text)
  12. qr.make()
  13. pic = qr.make_image().get_image()
  14. image.paste(pic, (int(epd.height-pic.width),0), pic)
  15. epd.display(epd.getbuffer(image))
  16. epd.sleep()
  17. epd.Dev_exit()
  18. def eink_qr_shorturl(args):
  19. import simpilib
  20. args.text = simpilib.short_url(args.text)
  21. eink_qr(args)
  22. def eink_quote_qr(args):
  23. import simpilib
  24. import random
  25. data = simpilib.getRedditJson("quotes","top","day")
  26. quotes = data["data"]["children"]
  27. q = random.choice(quotes)
  28. # args.text = q["data"]["title"]
  29. # eink_text(args)
  30. text = q["data"]["title"]
  31. author = None
  32. import re
  33. dashes = r"(.*)[~\u002D\u058A\u05BE\u1400\u1806\u2010-\u2015\u2E17\u2E1A\u2E3A\u2E3B\u2E40\u301C\u3030\u30A0\uFE31\uFE32\uFE58\uFE63\uFF0D](.*)"
  34. matches = re.match(dashes, text)
  35. if matches:
  36. groups = matches.groups()
  37. if len(groups[1]) < 30:
  38. text = groups[0]
  39. author = groups[1]
  40. if not author:
  41. qt = r"(.*)\"(.*)\"(.*)"
  42. matches = re.match(qt, text)
  43. if matches:
  44. groups = matches.groups()
  45. if len(groups[0]) < 10 and len(groups[1]) > 10:
  46. text = groups[1]
  47. author = groups[2]
  48. text = re.sub(r"[^a-zA-Z0-9, ]","", text).strip()
  49. if author:
  50. author = re.sub(r"[^a-zA-Z0-9 ]","", author).strip()
  51. from urllib.parse import urlencode
  52. args = urlencode({
  53. "search": author,
  54. "title": "Special:Search",
  55. })
  56. url = simpilib.short_url("https://wikipedia.org/w/index.php?" + args)
  57. else:
  58. url = simpilib.short_url(q["data"]["url"])
  59. print(text)
  60. print(author)
  61. eink_text_qr(text, url, author)
  62. def eink_text_qr(text, qrdata, smalltext=None):
  63. import simpilib
  64. from waveshare_epd import epd2in13_V2
  65. from PIL import Image,ImageDraw
  66. import qrcode
  67. epd = epd2in13_V2.EPD()
  68. base_image = Image.new('1', (epd.height, epd.width), 0)
  69. qr = qrcode.QRCode(version=1, box_size=3, border=1)
  70. qr.add_data(qrdata)
  71. qr.make()
  72. pic = qr.make_image().get_image()
  73. text_image = (base_image.width-pic.width, base_image.height)
  74. small_image = (pic.width, base_image.height-pic.height)
  75. base_image.paste(pic, (base_image.width-pic.width,0), pic)
  76. draw = ImageDraw.Draw(base_image)
  77. epd.init(epd.FULL_UPDATE)
  78. epd.displayPartBaseImage(epd.getbuffer(base_image))
  79. epd.init(epd.PART_UPDATE)
  80. text, font = simpilib.fitText(text_image, fontpath, text)
  81. w, h = draw.textsize(text, font=font)
  82. center = [
  83. int((text_image[0] - w)/2),
  84. int((text_image[1]-h)/2) - 3,
  85. ]
  86. draw.text(center, text, align="center", font = font, fill = 255, spacing=2)
  87. if smalltext != None and len(smalltext) > 2:
  88. smalltext, smallfont = simpilib.fitText(small_image, fontpath, smalltext)
  89. w, h = draw.textsize(smalltext, font=font)
  90. center = [
  91. base_image.width-pic.width+int((small_image[0] - w)/2),
  92. pic.height + int((small_image[1] - h)/2),
  93. ]
  94. draw.text(center, smalltext, align="center", font = font, fill = 255, spacing=2)
  95. epd.displayPartial(epd.getbuffer(base_image))
  96. epd.sleep()
  97. epd.Dev_exit()
  98. def eink_quote(args):
  99. import simpilib
  100. quote = simpilib.getQuote()
  101. if not quote:
  102. return
  103. args.text = quote["content"] + ' - ' + quote["author"]
  104. eink_text(args)
  105. def eink_text(args):
  106. from waveshare_epd import epd2in13_V2
  107. from PIL import Image,ImageDraw
  108. import simpilib
  109. epd = epd2in13_V2.EPD()
  110. print("init and Clear")
  111. epd.init(epd.FULL_UPDATE)
  112. # epd.Clear(0xFF)
  113. image = Image.new('1', (epd.height, epd.width), 0)
  114. draw = ImageDraw.Draw(image)
  115. text, font = simpilib.fitText(image.size, fontpath, args.text)
  116. w, h = draw.textsize(text, font=font)
  117. # epd parameters are rotated 90°
  118. center = [
  119. int((epd.height - w)/2),
  120. int((epd.width-h)/2) - 3,
  121. ]
  122. # -3 px to leave some space for low characters
  123. print("Drawing:", (w, h), (epd.height, epd.width), center)
  124. draw.text(center, text, align="center", font = font, fill = 255, spacing=2)
  125. # epd.display(epd.getbuffer(image.rotate(180)))
  126. epd.display(epd.getbuffer(image))
  127. epd.sleep()
  128. epd.Dev_exit()