2
0

epd_3in0g_test.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. import sys
  4. import os
  5. picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
  6. libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
  7. if os.path.exists(libdir):
  8. sys.path.append(libdir)
  9. import logging
  10. from waveshare_epd import epd3in0g
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd3in0g Demo")
  17. epd = epd3in0g.EPD()
  18. logging.info("init and Clear")
  19. epd.init()
  20. epd.Clear()
  21. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  22. font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
  23. font40 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 40)
  24. # Drawing on the image
  25. epd.init()
  26. logging.info("1.Drawing on the image...")
  27. Himage = Image.new('RGB', (epd.width, epd.height), 0xffffff)
  28. draw = ImageDraw.Draw(Himage)
  29. draw.text((5, 0), 'hello world', font = font18, fill = epd.RED)
  30. draw.text((5, 20), '3inch e-Paper', font = font24, fill = epd.YELLOW)
  31. draw.text((5, 45), u'微雪电子', font = font40, fill = epd.BLACK)
  32. draw.text((5, 85), u'微雪电子', font = font40, fill = epd.YELLOW)
  33. draw.text((5, 125), u'微雪电子', font = font40, fill = epd.RED)
  34. draw.line((5, 170, 80, 245), fill = epd.RED)
  35. draw.line((80, 170, 5, 245), fill = epd.YELLOW)
  36. draw.rectangle((5, 170, 80, 245), outline = epd.BLACK)
  37. draw.rectangle((90, 170, 165, 245), fill = epd.YELLOW)
  38. draw.arc((5, 250, 80, 325), 0, 360, fill = epd.BLACK)
  39. draw.chord((90, 250, 165, 325), 0, 360, fill = epd.RED)
  40. epd.display(epd.getbuffer(Himage))
  41. time.sleep(3)
  42. # Switch width and height for landscape display
  43. epd.init()
  44. logging.info("2.Drawing on the image...")
  45. Himage = Image.new('RGB', (epd.height, epd.width), epd.WHITE)
  46. draw = ImageDraw.Draw(Himage)
  47. draw.text((5, 0), 'hello world', font = font18, fill = epd.RED)
  48. draw.text((5, 20), '3inch e-Paper', font = font24, fill = epd.YELLOW)
  49. draw.text((5, 45), u'微雪电子', font = font40, fill = epd.BLACK)
  50. draw.text((5, 85), u'微雪电子', font = font40, fill = epd.YELLOW)
  51. draw.text((5, 125), u'微雪电子', font = font40, fill = epd.RED)
  52. draw.line((205, 5, 295, 65), fill = epd.RED)
  53. draw.line((295, 5, 205, 65), fill = epd.YELLOW)
  54. draw.rectangle((205, 5, 295, 65), outline = epd.BLACK)
  55. draw.rectangle((305, 5, 395, 65), fill = epd.RED)
  56. draw.arc((205, 75, 295, 165), 0, 360, fill = epd.BLACK)
  57. draw.chord((305, 75, 395, 165), 0, 360, fill = epd.YELLOW)
  58. epd.display(epd.getbuffer(Himage))
  59. time.sleep(3)
  60. # read bmp file
  61. epd.init()
  62. logging.info("3.read bmp file")
  63. Himage = Image.open(os.path.join(picdir, '3inch-1.bmp'))
  64. epd.display(epd.getbuffer(Himage))
  65. time.sleep(3)
  66. epd.init()
  67. logging.info("4.read bmp file")
  68. Himage = Image.open(os.path.join(picdir, '3inch-2.bmp'))
  69. epd.display(epd.getbuffer(Himage))
  70. time.sleep(3)
  71. epd.init()
  72. logging.info("5.read bmp file")
  73. Himage = Image.open(os.path.join(picdir, '3inch-3.bmp'))
  74. epd.display(epd.getbuffer(Himage))
  75. time.sleep(3)
  76. epd.init()
  77. logging.info("Clear...")
  78. epd.Clear()
  79. logging.info("Goto Sleep...")
  80. epd.sleep()
  81. except IOError as e:
  82. logging.info(e)
  83. except KeyboardInterrupt:
  84. logging.info("ctrl + c:")
  85. epd3in0g.epdconfig.module_exit(cleanup=True)
  86. exit()