epd_2in66b_test.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 epd2in66b
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd2in66 b Demo")
  17. epd = epd2in66b.EPD()
  18. logging.info("init and Clear")
  19. epd.init()
  20. epd.Clear()
  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. HBlackimage = Image.new('1', (epd.height, epd.width), 255) # 296*152
  26. HRYimage = Image.new('1', (epd.height, epd.width), 255) # 296*152 ryimage: red or yellow image
  27. drawblack = ImageDraw.Draw(HBlackimage)
  28. drawry = ImageDraw.Draw(HRYimage)
  29. drawblack.text((10, 0), 'hello world', font = font24, fill = 0)
  30. drawblack.text((10, 20), '2.66inch e-Paper b', font = font24, fill = 0)
  31. drawblack.text((150, 0), u'微雪电子', font = font24, fill = 0)
  32. drawblack.line((20, 50, 70, 100), fill = 0)
  33. drawblack.line((70, 50, 20, 100), fill = 0)
  34. drawblack.rectangle((20, 50, 70, 100), outline = 0)
  35. drawry.line((165, 50, 165, 100), fill = 0)
  36. drawry.line((140, 75, 190, 75), fill = 0)
  37. drawry.arc((140, 50, 190, 100), 0, 360, fill = 0)
  38. drawry.rectangle((80, 50, 130, 100), fill = 0)
  39. drawry.chord((200, 50, 250, 100), 0, 360, fill = 0)
  40. epd.display(epd.getbuffer(HBlackimage), epd.getbuffer(HRYimage))
  41. time.sleep(3)
  42. logging.info("2.read bmp file")
  43. HBlackimage = Image.open(os.path.join(picdir, '2in66b_b.bmp'))
  44. HRYimage = Image.open(os.path.join(picdir, '2in66b_r.bmp'))
  45. epd.display(epd.getbuffer(HBlackimage), epd.getbuffer(HRYimage))
  46. time.sleep(3)
  47. logging.info("3.read bmp file on window")
  48. blackimage1 = Image.new('1', (epd.height, epd.width), 255)
  49. redimage1 = Image.new('1', (epd.height, epd.width), 255)
  50. newimage = Image.open(os.path.join(picdir, '100x100.bmp'))
  51. blackimage1.paste(newimage, (50,10))
  52. epd.display(epd.getbuffer(blackimage1), epd.getbuffer(redimage1))
  53. time.sleep(3)
  54. # Drawing on the Vertical image
  55. logging.info("4.Drawing on the Vertical image...")
  56. LBlackimage = Image.new('1', (epd.width, epd.height), 255) # 152*296
  57. LRYimage = Image.new('1', (epd.width, epd.height), 255) # 152*296
  58. drawblack = ImageDraw.Draw(LBlackimage)
  59. drawry = ImageDraw.Draw(LRYimage)
  60. time.sleep(3)
  61. drawblack.text((2, 0), 'hello world', font = font18, fill = 0)
  62. drawblack.text((2, 20), '2.9inch epd bc', font = font18, fill = 0)
  63. drawblack.text((20, 50), u'微雪电子', font = font18, fill = 0)
  64. drawblack.line((10, 90, 60, 140), fill = 0)
  65. drawblack.line((60, 90, 10, 140), fill = 0)
  66. drawblack.rectangle((10, 90, 60, 140), outline = 0)
  67. drawry.line((95, 90, 95, 140), fill = 0)
  68. drawry.line((70, 115, 120, 115), fill = 0)
  69. drawry.arc((70, 90, 120, 140), 0, 360, fill = 0)
  70. drawry.rectangle((10, 150, 60, 200), fill = 0)
  71. drawry.chord((70, 150, 120, 200), 0, 360, fill = 0)
  72. epd.display(epd.getbuffer(LBlackimage), epd.getbuffer(LRYimage))
  73. time.sleep(3)
  74. logging.info("Clear...")
  75. epd.Clear()
  76. logging.info("Goto Sleep...")
  77. epd.sleep()
  78. except IOError as e:
  79. logging.info(e)
  80. except KeyboardInterrupt:
  81. logging.info("ctrl + c:")
  82. epd2in66b.epdconfig.module_exit(cleanup=True)
  83. exit()