2
0

epd_2in9_V2_test.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 epd2in9_V2
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd2in9 V2 Demo")
  17. epd = epd2in9_V2.EPD()
  18. logging.info("init and Clear")
  19. epd.init()
  20. epd.Clear(0xFF)
  21. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  22. font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
  23. font35 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 35)
  24. # Drawing on the Horizontal image
  25. logging.info("1.Drawing on the Horizontal image...")
  26. Himage = Image.new('1', (epd.height, epd.width), 255) # 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), '2.9inch e-Paper', font = font24, fill = 0)
  30. draw.text((150, 0), u'微雪电子', font = font24, fill = 0)
  31. draw.line((20, 50, 70, 100), fill = 0)
  32. draw.line((70, 50, 20, 100), fill = 0)
  33. draw.rectangle((20, 50, 70, 100), outline = 0)
  34. draw.line((165, 50, 165, 100), fill = 0)
  35. draw.line((140, 75, 190, 75), fill = 0)
  36. draw.arc((140, 50, 190, 100), 0, 360, fill = 0)
  37. draw.rectangle((80, 50, 130, 100), fill = 0)
  38. draw.chord((200, 50, 250, 100), 0, 360, fill = 0)
  39. # Himage = Himage.transpose(method=Image.ROTATE_180)
  40. epd.display(epd.getbuffer(Himage))
  41. time.sleep(2)
  42. # Drawing on the Vertical image
  43. logging.info("2.Drawing on the Vertical image...")
  44. Limage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  45. draw = ImageDraw.Draw(Limage)
  46. draw.text((2, 0), 'hello world', font = font18, fill = 0)
  47. draw.text((2, 20), '2.9inch epd', font = font18, fill = 0)
  48. draw.text((20, 50), u'微雪电子', font = font18, fill = 0)
  49. draw.line((10, 90, 60, 140), fill = 0)
  50. draw.line((60, 90, 10, 140), fill = 0)
  51. draw.rectangle((10, 90, 60, 140), outline = 0)
  52. draw.line((95, 90, 95, 140), fill = 0)
  53. draw.line((70, 115, 120, 115), fill = 0)
  54. draw.arc((70, 90, 120, 140), 0, 360, fill = 0)
  55. draw.rectangle((10, 150, 60, 200), fill = 0)
  56. draw.chord((70, 150, 120, 200), 0, 360, fill = 0)
  57. epd.display(epd.getbuffer(Limage))
  58. time.sleep(2)
  59. logging.info("3.read bmp file")
  60. Himage = Image.open(os.path.join(picdir, '2in9.bmp'))
  61. epd.display(epd.getbuffer(Himage))
  62. time.sleep(2)
  63. logging.info("4.read bmp file on window,Quick brush demo")
  64. epd.init_Fast()
  65. Himage2 = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  66. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  67. Himage2.paste(bmp, (50,10))
  68. epd.display(epd.getbuffer(Himage2))
  69. time.sleep(2)
  70. # partial update
  71. logging.info("5.show time")
  72. time_image = Image.new('1', (epd.height, epd.width), 255)
  73. time_draw = ImageDraw.Draw(time_image)
  74. epd.display_Base(epd.getbuffer(time_image))
  75. num = 0
  76. while (True):
  77. time_draw.rectangle((10, 10, 120, 50), fill = 255)
  78. time_draw.text((10, 10), time.strftime('%H:%M:%S'), font = font24, fill = 0)
  79. newimage = time_image.crop([10, 10, 120, 50])
  80. time_image.paste(newimage, (10,10))
  81. epd.display_Partial(epd.getbuffer(time_image))
  82. num = num + 1
  83. if(num == 10):
  84. break
  85. '''4Gray display'''
  86. logging.info("4Gray display--------------------------------")
  87. epd.Init_4Gray()
  88. Limage = Image.new('L', (epd.height, epd.width), 0) # 255: clear the frame
  89. draw = ImageDraw.Draw(Limage)
  90. draw.text((0, 0), u'微雪电子', font = font35, fill = epd.GRAY1)
  91. draw.text((0, 35), u'微雪电子', font = font35, fill = epd.GRAY2)
  92. draw.text((0, 70), u'微雪电子', font = font35, fill = epd.GRAY3)
  93. draw.text((20, 105), 'hello world', font = font18, fill = epd.GRAY1)
  94. draw.line((160, 10, 210, 60), fill = epd.GRAY1)
  95. draw.line((160, 60, 210, 10), fill = epd.GRAY1)
  96. draw.rectangle((160, 10, 210, 60), outline = epd.GRAY1)
  97. draw.line((160, 95, 210, 95), fill = epd.GRAY1)
  98. draw.line((185, 70, 185, 120), fill = epd.GRAY1)
  99. draw.arc((160, 70, 210, 120), 0, 360, fill = epd.GRAY1)
  100. draw.rectangle((220, 10, 270, 60), fill = epd.GRAY1)
  101. draw.chord((220, 70, 270, 120), 0, 360, fill = epd.GRAY1)
  102. epd.display_4Gray(epd.getbuffer_4Gray(Limage))
  103. time.sleep(2)
  104. #display 4Gra bmp
  105. Himage = Image.open(os.path.join(picdir, '2in9_Scale.bmp'))
  106. epd.display_4Gray(epd.getbuffer_4Gray(Himage))
  107. time.sleep(2)
  108. logging.info("Clear...")
  109. epd.init()
  110. epd.Clear(0xFF)
  111. logging.info("Goto Sleep...")
  112. epd.sleep()
  113. except IOError as e:
  114. logging.info(e)
  115. except KeyboardInterrupt:
  116. logging.info("ctrl + c:")
  117. epd2in9_V2.epdconfig.module_exit(cleanup=True)
  118. exit()