epd_2in9b_V4_test.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 epd2in9b_V4
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd2in9b V4 Demo")
  17. epd = epd2in9b_V4.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. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  25. font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
  26. logging.info("read bmp file")
  27. HBlackimage = Image.open(os.path.join(picdir, '2in9bc-b.bmp'))
  28. HRYimage = Image.open(os.path.join(picdir, '2in9bc-ry.bmp'))
  29. epd.display(epd.getbuffer(HBlackimage), epd.getbuffer(HRYimage))
  30. time.sleep(2)
  31. # Drawing on the Horizontal image
  32. logging.info("Drawing on the Horizontal image...")
  33. epd.init_Fast()
  34. HBlackimage = Image.new('1', (epd.height, epd.width), 255) # 298*126
  35. HRYimage = Image.new('1', (epd.height, epd.width), 255) # 298*126 ryimage: red or yellow image
  36. drawblack = ImageDraw.Draw(HBlackimage)
  37. drawry = ImageDraw.Draw(HRYimage)
  38. drawblack.text((10, 0), 'hello world', font = font24, fill = 0)
  39. drawblack.text((10, 20), '2.9inch e-Paper b V4', font = font24, fill = 0)
  40. drawblack.text((150, 0), u'微雪电子', font = font24, fill = 0)
  41. drawblack.line((20, 50, 70, 100), fill = 0)
  42. drawblack.line((70, 50, 20, 100), fill = 0)
  43. drawblack.rectangle((20, 50, 70, 100), outline = 0)
  44. drawry.line((165, 50, 165, 100), fill = 0)
  45. drawry.line((140, 75, 190, 75), fill = 0)
  46. drawry.arc((140, 50, 190, 100), 0, 360, fill = 0)
  47. drawry.rectangle((80, 50, 130, 100), fill = 0)
  48. drawry.chord((200, 50, 250, 100), 0, 360, fill = 0)
  49. epd.display_Fast(epd.getbuffer(HBlackimage), epd.getbuffer(HRYimage))
  50. time.sleep(2)
  51. '''
  52. # If you didn't use the display_Base() function to refresh the image before,
  53. # use the display_Base_color() function to refresh the background color,
  54. # otherwise the background color will be garbled
  55. # epd.init()
  56. # epd.display_Base(epd.getbuffer(HBlackimage), epd.getbuffer(HRYimage))
  57. # epd.display_Base_color(0xFF)
  58. '''
  59. # partial update
  60. logging.info("5.show time")
  61. epd.init()
  62. epd.display_Base_color(0xFF)
  63. HBlackimage = Image.new('1', (epd.height, epd.width), 255)
  64. drawblack = ImageDraw.Draw(HBlackimage)
  65. num = 0
  66. while (True):
  67. drawblack.rectangle((10, 10, 120, 50), fill = 255)
  68. drawblack.text((10, 10), time.strftime('%H:%M:%S'), font = font24, fill = 0)
  69. newimage = HBlackimage.crop([10, 10, 120, 50])
  70. HBlackimage.paste(newimage, (10,10))
  71. epd.display_Partial(epd.getbuffer(HBlackimage),10, epd.height - 120, 50, epd.height - 10)
  72. num = num + 1
  73. if(num == 10):
  74. break
  75. # # Drawing on the Vertical image
  76. # logging.info("Drawing on the Vertical image...")
  77. # LBlackimage = Image.new('1', (epd.width, epd.height), 255) # 126*298
  78. # LRYimage = Image.new('1', (epd.width, epd.height), 255) # 126*298
  79. # drawblack = ImageDraw.Draw(LBlackimage)
  80. # drawry = ImageDraw.Draw(LRYimage)
  81. # drawblack.text((2, 0), 'hello world', font = font18, fill = 0)
  82. # drawblack.text((2, 20), '2.9inch epd b V4', font = font18, fill = 0)
  83. # drawblack.text((20, 50), u'微雪电子', font = font18, fill = 0)
  84. # drawblack.line((10, 90, 60, 140), fill = 0)
  85. # drawblack.line((60, 90, 10, 140), fill = 0)
  86. # drawblack.rectangle((10, 90, 60, 140), outline = 0)
  87. # drawry.line((95, 90, 95, 140), fill = 0)
  88. # drawry.line((70, 115, 120, 115), fill = 0)
  89. # drawry.arc((70, 90, 120, 140), 0, 360, fill = 0)
  90. # drawry.rectangle((10, 150, 60, 200), fill = 0)
  91. # drawry.chord((70, 150, 120, 200), 0, 360, fill = 0)
  92. # epd.display(epd.getbuffer(LBlackimage), epd.getbuffer(LRYimage))
  93. # time.sleep(2)
  94. logging.info("Clear...")
  95. epd.init()
  96. epd.Clear()
  97. logging.info("Goto Sleep...")
  98. epd.sleep()
  99. except IOError as e:
  100. logging.info(e)
  101. except KeyboardInterrupt:
  102. logging.info("ctrl + c:")
  103. epd2in9b_V4.epdconfig.module_exit(cleanup=True)
  104. exit()