Tobias Simetsreiter 4 lat temu
rodzic
commit
afa6d9667b
6 zmienionych plików z 58 dodań i 53 usunięć
  1. 1 0
      .gitignore
  2. 3 3
      Makefile
  3. 0 21
      bin/image.py
  4. 17 14
      simpilib/__init__.py
  5. 4 1
      simpilib/cli.py
  6. 33 14
      simpilib/eink.py

+ 1 - 0
.gitignore

@@ -57,4 +57,5 @@ docs/_build/
 
 # PyBuilder
 target/
+wavesharelib
 config.mk

+ 3 - 3
Makefile

@@ -34,7 +34,7 @@ bcmlib:
 	#For more details, please refer to http://www.airspayce.com/mikem/bcm2835/
 
 wiringpi:
-	sudo apt-get install wiringpi
+	sudo apt-get install -y wiringpi
 	#For Pi 4, you need to update it:
 	wget https://project-downloads.drogon.net/wiringpi-latest.deb
 	sudo dpkg -i wiringpi-latest.deb
@@ -45,8 +45,8 @@ wiringpi:
 pythonlib:
 	#python3
 	sudo apt-get update
-	sudo apt-get install python3-pip python3-pil python3-numpy
-	sudo pip3 install RPi.GPIO spidev
+	sudo apt-get install -y python3-pip python3-pil python3-numpy
+	sudo pip3 install RPi.GPIO spidev qrcode
 
 # demo-py/%:
 # 	cd e-Paper/RaspberryPi\&JetsonNano/python/examples && sudo python3 ${*}

+ 0 - 21
bin/image.py

@@ -1,21 +0,0 @@
-#!/usr/bin/env python3
-# -*- coding:utf-8 -*-
-import sys
-import os
-pydir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
-picdir = os.path.join(pydir, 'pic')
-libdir = os.path.join(pydir)
-libdir = os.path.join(pydir, 'wavesharelib')
-print(picdir)
-print(libdir)
-if os.path.exists(libdir):
-    sys.path.append(libdir)
-
-import logging
-from waveshare_epd import epd2in13_V2
-import time
-from PIL import Image,ImageDraw,ImageFont
-
-logging.info("2.read bmp file...")
-image = Image.open(os.path.join(picdir, '2in13.bmp'))
-epd.display(epd.getbuffer(image))

+ 17 - 14
simpilib/__init__.py

@@ -8,7 +8,7 @@ def loadFontSize(text, draw, image, fontpath):
     font = ImageFont.truetype(fontpath, fontsize)
     fsize = (0,0)
     imgsize = (0.9,0.9)
-    longline = getLongLine(text.splitlines())
+    longline = getLongestLine(text.splitlines())
     while True:
         font_last = font
         fontsize += 1
@@ -22,37 +22,40 @@ def loadFontSize(text, draw, image, fontpath):
     return font_last
 
 
-def wrapText(text):
+def wrapText(text, line_width_factor=5):
     import textwrap
     splittext = []
-    longline = ""
-    iter_width = 500
-    line_width_factor = 5
+    longestline = ""
+    iter_width = 400
     while len(splittext)*line_width_factor < iter_width:
         iter_width -= 1
         splittext = textwrap.wrap(text, width=iter_width)
-        longline = getLongLine(splittext)
+        longestline = getLongestLine(splittext)
         print("Wrap:", iter_width, len(splittext))
-    return "\n".join(splittext), longline
+    return "\n".join(splittext), longestline
 
 
-def getLongLine(split):
+def getLongestLine(split):
     longline = ""
     for l in split:
         if len(l) > len(longline):
             longline = l
     return longline
 
+def convert_qr(quote):
+    import qrcode
+    qr = qrcode.QRCode(version=1, box_size=3, border=1)
+    qr.add_data("https://css.tsimnet.eu")
+    qr.make()
+    return qr.make_image().get_image()
 
-def getQuote():
+def getQuote( maxLength=100 ):
     try:
         import requests
         # r = requests.get('https://type.fit/api/quotes')
-        r = requests.get('http://api.quotable.io/random?maxLength=200')
+        r = requests.get('http://api.quotable.io/random?maxLength='+str(maxLength))
         r = r.json()
-        text = r["content"] + " - " + r["author"]
-
+        text = r
     except Exception as ex:
-        text = str(ex)
-
+        return
     return text

+ 4 - 1
simpilib/cli.py

@@ -13,11 +13,14 @@ def main():
 def parser():
     from argparse import ArgumentParser
     from simpilib.install import install
-    from simpilib.eink import update_quote
+    from simpilib.eink import update_quote, update_qr
     p = ArgumentParser()
     sub = p.add_subparsers()
     install_parser = sub.add_parser("update_quote")
     install_parser.set_defaults(func=update_quote)
+    install_parser = sub.add_parser("qr")
+    install_parser.set_defaults(func=update_qr)
+    install_parser.add_argument("text")
     install_parser = sub.add_parser("install")
     install_parser.set_defaults(func=install)
     return p

+ 33 - 14
simpilib/eink.py

@@ -1,39 +1,58 @@
 
+import os as _os
+fontpath = _os.path.realpath(_os.path.join(__file__,"..","..","wavesharelib","pic", 'Font.ttc'))
+
+def update_qr(args):
+    from waveshare_epd import epd2in13_V2
+    from PIL import Image,ImageDraw
+    import qrcode
+    epd = epd2in13_V2.EPD()
+    epd.init(epd.FULL_UPDATE)
+    image = Image.new('1', (epd.height, epd.width), 0)
+    qr = qrcode.QRCode(version=1, box_size=3, border=1)
+    qr.add_data(args.text)
+    qr.make()
+    pic = qr.make_image().get_image()
+    image.paste(pic, (int(epd.height-pic.width),0), pic)
+    epd.display(epd.getbuffer(image))
+    epd.sleep()
+    epd.Dev_exit()
+
 def update_quote(args):
     import logging
     from waveshare_epd import epd2in13_V2
-    import time
     from PIL import Image,ImageDraw
     import simpilib
 
-    import os
-    fontpath = os.path.realpath(os.path.join(__file__,"..","..","wavesharelib","pic", 'Font.ttc'))
-
-
-    text = simpilib.getQuote()
+    quote = simpilib.getQuote()
+    if not quote:
+        return
+    pic = simpilib.convert_qr(quote)
+    text = quote["content"]
 
     epd = epd2in13_V2.EPD()
     logging.info("init and Clear")
     epd.init(epd.FULL_UPDATE)
-    epd.Clear(0xFF)
-    image = Image.new('1', (epd.height, epd.width), 255)
+    # epd.Clear(0xFF)
+    image = Image.new('1', (epd.height, epd.width), 0)
+    image.paste(pic, (int(epd.height-pic.width),0), pic)
     draw = ImageDraw.Draw(image)
 
-    text, longline = simpilib.wrapText(text)
+    text, longline = simpilib.wrapText(text, line_width_factor=3)
     font = simpilib.loadFontSize(text, draw, image, fontpath)
 
     w, h = draw.textsize(text, font=font)
 
     # epd parameters are rotated 90°
     center = [
-        int((epd.height-w)/2),
-        int((epd.width-h)/2)
+        int((epd.height - pic.width - w)/2),
+        int((epd.width-h)/2),
     ]
     # leave some space for low characters
-    center[1] = center[1] - 5
+    center[1] = center[1] - 3
 
-    print("Drawing:", (w, h), (epd.height, epd.width))
-    draw.text(center, text, align="center", font = font, fill = 0)
+    print("Drawing:", (w, h), (epd.height, epd.width), center)
+    draw.text(center, text, align="center", font = font, fill = 255)
 
     # epd.display(epd.getbuffer(image.rotate(180)))
     epd.display(epd.getbuffer(image))