epd_2in7b_test.py 3.9 KB

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