epd_3in7_test.py 4.4 KB

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