epd_2in13_V4_test.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 epd2in13_V4
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd2in13_V4 Demo")
  17. epd = epd2in13_V4.EPD()
  18. logging.info("init and Clear")
  19. epd.init()
  20. epd.Clear(0xFF)
  21. # Drawing on the image
  22. font15 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 15)
  23. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  24. if 1:
  25. logging.info("E-paper refresh")
  26. epd.init()
  27. logging.info("1.Drawing on the image...")
  28. image = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  29. draw = ImageDraw.Draw(image)
  30. draw.rectangle([(0,0),(50,50)],outline = 0)
  31. draw.rectangle([(55,0),(100,50)],fill = 0)
  32. draw.line([(0,0),(50,50)], fill = 0,width = 1)
  33. draw.line([(0,50),(50,0)], fill = 0,width = 1)
  34. draw.chord((10, 60, 50, 100), 0, 360, fill = 0)
  35. draw.ellipse((55, 60, 95, 100), outline = 0)
  36. draw.pieslice((55, 60, 95, 100), 90, 180, outline = 0)
  37. draw.pieslice((55, 60, 95, 100), 270, 360, fill = 0)
  38. draw.polygon([(110,0),(110,50),(150,25)],outline = 0)
  39. draw.polygon([(190,0),(190,50),(150,25)],fill = 0)
  40. draw.text((120, 60), 'e-Paper demo', font = font15, fill = 0)
  41. draw.text((110, 90), u'微雪电子', font = font24, fill = 0)
  42. # image = image.rotate(180) # rotate
  43. epd.display(epd.getbuffer(image))
  44. time.sleep(2)
  45. # read bmp file
  46. logging.info("2.read bmp file...")
  47. image = Image.open(os.path.join(picdir, '2in13.bmp'))
  48. epd.display(epd.getbuffer(image))
  49. time.sleep(2)
  50. # read bmp file on window
  51. logging.info("3.read bmp file on window...")
  52. # epd.Clear(0xFF)
  53. image1 = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  54. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  55. image1.paste(bmp, (2,2))
  56. epd.display(epd.getbuffer(image1))
  57. time.sleep(2)
  58. else:
  59. logging.info("E-paper refreshes quickly")
  60. epd.init_fast()
  61. logging.info("1.Drawing on the image...")
  62. image = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  63. draw = ImageDraw.Draw(image)
  64. draw.rectangle([(0,0),(50,50)],outline = 0)
  65. draw.rectangle([(55,0),(100,50)],fill = 0)
  66. draw.line([(0,0),(50,50)], fill = 0,width = 1)
  67. draw.line([(0,50),(50,0)], fill = 0,width = 1)
  68. draw.chord((10, 60, 50, 100), 0, 360, fill = 0)
  69. draw.ellipse((55, 60, 95, 100), outline = 0)
  70. draw.pieslice((55, 60, 95, 100), 90, 180, outline = 0)
  71. draw.pieslice((55, 60, 95, 100), 270, 360, fill = 0)
  72. draw.polygon([(110,0),(110,50),(150,25)],outline = 0)
  73. draw.polygon([(190,0),(190,50),(150,25)],fill = 0)
  74. draw.text((120, 60), 'e-Paper demo', font = font15, fill = 0)
  75. draw.text((110, 90), u'微雪电子', font = font24, fill = 0)
  76. # image = image.rotate(180) # rotate
  77. epd.display_fast(epd.getbuffer(image))
  78. time.sleep(2)
  79. # read bmp file
  80. logging.info("2.read bmp file...")
  81. image = Image.open(os.path.join(picdir, '2in13.bmp'))
  82. epd.display_fast(epd.getbuffer(image))
  83. time.sleep(2)
  84. # read bmp file on window
  85. logging.info("3.read bmp file on window...")
  86. # epd.Clear(0xFF)
  87. image1 = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  88. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  89. image1.paste(bmp, (2,2))
  90. epd.display_fast(epd.getbuffer(image1))
  91. time.sleep(2)
  92. # # partial update
  93. logging.info("4.show time...")
  94. time_image = Image.new('1', (epd.height, epd.width), 255)
  95. time_draw = ImageDraw.Draw(time_image)
  96. epd.displayPartBaseImage(epd.getbuffer(time_image))
  97. num = 0
  98. while (True):
  99. time_draw.rectangle((120, 80, 220, 105), fill = 255)
  100. time_draw.text((120, 80), time.strftime('%H:%M:%S'), font = font24, fill = 0)
  101. epd.displayPartial(epd.getbuffer(time_image))
  102. num = num + 1
  103. if(num == 10):
  104. break
  105. logging.info("Clear...")
  106. epd.init()
  107. epd.Clear(0xFF)
  108. logging.info("Goto Sleep...")
  109. epd.sleep()
  110. except IOError as e:
  111. logging.info(e)
  112. except KeyboardInterrupt:
  113. logging.info("ctrl + c:")
  114. epd2in13_V4.epdconfig.module_exit(cleanup=True)
  115. exit()