epd_2in7b_V2_test.py 4.3 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 epd2in7b_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("epd2in7b_V2 Demo")
  17. epd = epd2in7b_V2.EPD()
  18. while(1) :
  19. logging.info("init and Clear")
  20. epd.init()
  21. epd.Clear()
  22. time.sleep(1)
  23. # Drawing on the image
  24. logging.info("Drawing")
  25. blackimage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  26. redimage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  27. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  28. font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
  29. # Drawing on the Horizontal image
  30. logging.info("1.Drawing on the Horizontal image...")
  31. HBlackimage = Image.new('1', (epd.height, epd.width), 255) # 298*126
  32. HRedimage = Image.new('1', (epd.height, epd.width), 255) # 298*126
  33. drawblack = ImageDraw.Draw(HBlackimage)
  34. drawred = ImageDraw.Draw(HRedimage)
  35. drawblack.text((10, 0), 'hello world', font = font24, fill = 0)
  36. drawblack.text((10, 20), '2.7inch e-Paper', font = font24, fill = 0)
  37. drawblack.text((150, 0), u'微雪电子', font = font24, fill = 0)
  38. drawblack.line((20, 50, 70, 100), fill = 0)
  39. drawblack.line((70, 50, 20, 100), fill = 0)
  40. drawblack.rectangle((20, 50, 70, 100), outline = 0)
  41. drawred.line((165, 50, 165, 100), fill = 0)
  42. drawred.line((140, 75, 190, 75), fill = 0)
  43. drawred.arc((140, 50, 190, 100), 0, 360, fill = 0)
  44. drawred.rectangle((80, 50, 130, 100), fill = 0)
  45. drawred.chord((200, 50, 250, 100), 0, 360, fill = 0)
  46. epd.display(epd.getbuffer(HBlackimage), epd.getbuffer(HRedimage))
  47. time.sleep(20)
  48. # Drawing on the Vertical image
  49. logging.info("2.Drawing on the Vertical image...")
  50. LBlackimage = Image.new('1', (epd.width, epd.height), 255) # 126*298
  51. LRedimage = Image.new('1', (epd.width, epd.height), 255) # 126*298
  52. drawblack = ImageDraw.Draw(LBlackimage)
  53. drawred = ImageDraw.Draw(LRedimage)
  54. drawblack.text((2, 0), 'hello world', font = font18, fill = 0)
  55. drawblack.text((2, 20), '2.9inch epd', font = font18, fill = 0)
  56. drawblack.text((20, 50), u'微雪电子', font = font18, fill = 0)
  57. drawblack.line((10, 90, 60, 140), fill = 0)
  58. drawblack.line((60, 90, 10, 140), fill = 0)
  59. drawblack.rectangle((10, 90, 60, 140), outline = 0)
  60. drawred.line((95, 90, 95, 140), fill = 0)
  61. drawred.line((70, 115, 120, 115), fill = 0)
  62. drawred.arc((70, 90, 120, 140), 0, 360, fill = 0)
  63. drawred.rectangle((10, 150, 60, 200), fill = 0)
  64. drawred.chord((70, 150, 120, 200), 0, 360, fill = 0)
  65. epd.display(epd.getbuffer(LBlackimage), epd.getbuffer(LRedimage))
  66. time.sleep(2)
  67. logging.info("3.read bmp file")
  68. HBlackimage = Image.open(os.path.join(picdir, '2in7b-b.bmp'))
  69. HRedimage = Image.open(os.path.join(picdir, '2in7b-r.bmp'))
  70. epd.display(epd.getbuffer(HBlackimage), epd.getbuffer(HRedimage))
  71. time.sleep(2)
  72. logging.info("4.read bmp file on window")
  73. blackimage1 = Image.new('1', (epd.height, epd.width), 255) # 298*126
  74. redimage1 = Image.new('1', (epd.height, epd.width), 255) # 298*126
  75. newimage = Image.open(os.path.join(picdir, '100x100.bmp'))
  76. blackimage1.paste(newimage, (50,10))
  77. epd.display(epd.getbuffer(blackimage1), epd.getbuffer(redimage1))
  78. logging.info("Clear...")
  79. epd.init()
  80. epd.Clear()
  81. logging.info("Goto Sleep...")
  82. epd.sleep()
  83. time.sleep(30)
  84. epd.Dev_exit()
  85. except IOError as e:
  86. logging.info(e)
  87. except KeyboardInterrupt:
  88. logging.info("ctrl + c:")
  89. epd2in7b_V2.epdconfig.module_exit()
  90. exit()