epd_3in7_test.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 epd3in7
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd3in7 Demo")
  17. epd = epd3in7.EPD()
  18. logging.info("init and Clear")
  19. epd.init(0)
  20. epd.Clear(0xFF, 0)
  21. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  22. font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
  23. # Drawing on the Horizontal image
  24. logging.info("1.Drawing on the Horizontal image...")
  25. Himage = Image.new('L', (epd.height, epd.width), 0xFF) # 0xFF: clear the frame
  26. draw = ImageDraw.Draw(Himage)
  27. draw.text((10, 0), 'hello world', font = font24, fill = 0)
  28. draw.text((10, 20), '3.7inch e-Paper', font = font24, fill = 0)
  29. draw.text((10, 100), u'微雪电子', font = font24, fill = epd.GRAY1)
  30. draw.text((10, 130), u'微雪电子', font = font24, fill = epd.GRAY2)
  31. draw.text((10, 160), u'微雪电子', font = font24, fill = epd.GRAY3)
  32. draw.text((10, 190), u'微雪电子', font = font24, fill = epd.GRAY4)
  33. draw.line((20, 50, 70, 100), fill = 0)
  34. draw.line((70, 50, 20, 100), fill = 0)
  35. draw.rectangle((20, 50, 70, 100), outline = 0)
  36. draw.line((165, 50, 165, 100), fill = 0)
  37. draw.line((140, 75, 190, 75), fill = 0)
  38. draw.arc((140, 50, 190, 100), 0, 360, fill = 0)
  39. draw.rectangle((80, 50, 130, 100), fill = 0)
  40. draw.chord((200, 50, 250, 100), 0, 360, fill = 0)
  41. epd.display_4Gray(epd.getbuffer_4Gray(Himage))
  42. time.sleep(5)
  43. logging.info("2.read 4 Gray bmp file")
  44. Himage = Image.open(os.path.join(picdir, '3in7_Scale.bmp'))
  45. epd.display_4Gray(epd.getbuffer_4Gray(Himage))
  46. time.sleep(5)
  47. logging.info("3.read bmp file on window")
  48. Himage2 = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  49. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  50. Himage2.paste(bmp, (200,50))
  51. epd.display_4Gray(epd.getbuffer_4Gray(Himage2))
  52. time.sleep(5)
  53. # Drawing on the Vertical image
  54. logging.info("4.Drawing on the Vertical image...")
  55. Limage = Image.new('L', (epd.width, epd.height), 0xFF) # 0xFF: clear the frame
  56. draw = ImageDraw.Draw(Limage)
  57. draw.text((2, 0), 'hello world', font = font18, fill = 0)
  58. draw.text((2, 20), '3.7inch epd', font = font18, fill = 0)
  59. draw.rectangle((150, 20, 246, 48), 'black', 'black')
  60. draw.text((150, 20), u'微雪电子', font = font24, fill = epd.GRAY1)
  61. draw.text((150, 50), u'微雪电子', font = font24, fill = epd.GRAY2)
  62. draw.text((150, 80), u'微雪电子', font = font24, fill = epd.GRAY3)
  63. draw.text((150, 110), u'微雪电子', font = font24, fill = epd.GRAY4)
  64. draw.line((10, 90, 60, 140), fill = 0)
  65. draw.line((60, 90, 10, 140), fill = 0)
  66. draw.rectangle((10, 90, 60, 140), outline = 0)
  67. draw.line((95, 90, 95, 140), fill = 0)
  68. draw.line((70, 115, 120, 115), fill = 0)
  69. draw.arc((70, 90, 120, 140), 0, 360, fill = 0)
  70. draw.rectangle((10, 150, 60, 200), fill = 0)
  71. draw.chord((70, 150, 120, 200), 0, 360, fill = 0)
  72. epd.display_4Gray(epd.getbuffer_4Gray(Limage))
  73. time.sleep(5)
  74. # partial update, just 1 Gary mode
  75. logging.info("5.show time, partial update, just 1 Gary mode")
  76. epd.init(1) # 1 Gary mode
  77. epd.Clear(0xFF, 1)
  78. time_image = Image.new('1', (epd.height, epd.width), 255)
  79. time_draw = ImageDraw.Draw(time_image)
  80. num = 0
  81. while (True):
  82. time_draw.rectangle((10, 10, 120, 50), fill = 255)
  83. time_draw.text((10, 10), time.strftime('%H:%M:%S'), font = font24, fill = 0)
  84. epd.display_1Gray(epd.getbuffer(time_image))
  85. num = num + 1
  86. if(num == 10):
  87. break
  88. logging.info("Clear...")
  89. epd.init(0)
  90. epd.Clear(0xFF, 0)
  91. logging.info("Goto Sleep...")
  92. epd.sleep()
  93. epd.Dev_exit()
  94. except IOError as e:
  95. logging.info(e)
  96. except KeyboardInterrupt:
  97. logging.info("ctrl + c:")
  98. epd3in7.epdconfig.module_exit()
  99. exit()