epd_4in01f_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 epd4in01f
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd4in01f Demo")
  17. epd = epd4in01f.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. font30 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 40)
  24. # Drawing on the Horizontal image
  25. logging.info("1.Drawing on the Horizontal image...")
  26. Himage = Image.new('RGB', (epd.width, epd.height), 0xffffff) # 255: clear the frame
  27. draw = ImageDraw.Draw(Himage)
  28. draw.text((10, 0), 'hello world', font = font24, fill = 0)
  29. draw.text((10, 20), '4.01inch e-Paper', font = font24, fill = 0)
  30. draw.text((10, 160), u'微雪电子', font = font30, fill = epd.BLACK)
  31. draw.text((10, 200), u'微雪电子', font = font30, fill = epd.ORANGE)
  32. draw.text((10, 240), u'微雪电子', font = font30, fill = epd.GREEN)
  33. draw.text((10, 280), u'微雪电子', font = font30, fill = epd.BLUE)
  34. draw.text((10, 320), u'微雪电子', font = font30, fill = epd.RED)
  35. draw.text((10, 360), u'微雪电子', font = font30, fill = epd.YELLOW)
  36. draw.line((20, 50, 70, 100), fill = 0)
  37. draw.line((70, 50, 20, 100), fill = 0)
  38. draw.rectangle((20, 50, 70, 100), outline = 0)
  39. draw.line((165, 50, 165, 100), fill = 0)
  40. draw.line((140, 75, 190, 75), fill = 0)
  41. draw.arc((140, 50, 190, 100), 0, 360, fill = 0)
  42. draw.rectangle((80, 50, 130, 100), fill = 0)
  43. draw.chord((200, 50, 250, 100), 0, 360, fill = 0)
  44. epd.display(epd.getbuffer(Himage))
  45. time.sleep(3)
  46. epd.Clear()
  47. logging.info("2.Drawing on the Vertical image...")
  48. Himage = Image.new('RGB', (epd.height, epd.width), 0xffffff) # 255: clear the frame
  49. draw = ImageDraw.Draw(Himage)
  50. draw.text((10, 0), 'hello world', font = font24, fill = 0)
  51. draw.text((10, 20), '4.01inch e-Paper', font = font24, fill = 0)
  52. draw.text((10, 160), u'微雪电子', font = font30, fill = epd.BLACK)
  53. draw.text((10, 200), u'微雪电子', font = font30, fill = epd.ORANGE)
  54. draw.text((10, 240), u'微雪电子', font = font30, fill = epd.GREEN)
  55. draw.text((10, 280), u'微雪电子', font = font30, fill = epd.BLUE)
  56. draw.text((10, 320), u'微雪电子', font = font30, fill = epd.RED)
  57. draw.text((10, 360), u'微雪电子', font = font30, fill = epd.YELLOW)
  58. draw.line((20, 50, 70, 100), fill = 0)
  59. draw.line((70, 50, 20, 100), fill = 0)
  60. draw.rectangle((20, 50, 70, 100), outline = 0)
  61. draw.line((165, 50, 165, 100), fill = 0)
  62. draw.line((140, 75, 190, 75), fill = 0)
  63. draw.arc((140, 50, 190, 100), 0, 360, fill = 0)
  64. draw.rectangle((80, 50, 130, 100), fill = 0)
  65. draw.chord((200, 50, 250, 100), 0, 360, fill = 0)
  66. epd.display(epd.getbuffer(Himage))
  67. time.sleep(3)
  68. epd.Clear()
  69. logging.info("3.read bmp file")
  70. Himage = Image.open(os.path.join(picdir, '4in01-1.bmp'))
  71. epd.display(epd.getbuffer(Himage))
  72. time.sleep(3)
  73. epd.Clear()
  74. Himage = Image.open(os.path.join(picdir, '4in01-2.bmp'))
  75. epd.display(epd.getbuffer(Himage))
  76. time.sleep(3)
  77. epd.Clear()
  78. Himage = Image.open(os.path.join(picdir, '4in01-3.bmp'))
  79. epd.display(epd.getbuffer(Himage))
  80. time.sleep(3)
  81. epd.Clear()
  82. Himage = Image.open(os.path.join(picdir, '4in01-4.bmp'))
  83. epd.display(epd.getbuffer(Himage))
  84. time.sleep(3)
  85. epd.Clear()
  86. logging.info("Goto Sleep...")
  87. epd.sleep()
  88. except IOError as e:
  89. logging.info(e)
  90. except KeyboardInterrupt:
  91. logging.info("ctrl + c:")
  92. epd4in01f.epdconfig.module_exit(cleanup=True)
  93. exit()