epd_3in52_test.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 epd3in52
  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 Demo")
  17. epd = epd3in52.EPD()
  18. logging.info("init and Clear")
  19. epd.init()
  20. epd.display_NUM(epd.WHITE)
  21. epd.lut_GC()
  22. epd.refresh()
  23. epd.send_command(0x50)
  24. epd.send_data(0x17)
  25. time.sleep(2)
  26. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  27. font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
  28. font30 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 40)
  29. # Drawing on the Horizontal image
  30. logging.info("1.Drawing on the Horizontal image...")
  31. Himage = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  32. draw = ImageDraw.Draw(Himage)
  33. draw.text((10, 0), 'hello world', font = font24, fill = 0)
  34. draw.text((10, 20), '3.52inch e-Paper', font = font24, fill = 0)
  35. draw.text((150, 0), u'微雪电子', font = font24, fill = 0)
  36. draw.line((20, 50, 70, 100), fill = 0)
  37. draw.line((70, 50, 20, 100), fill = 0)
  38. draw.rectangle((20, 50, 70, 100), outline = 0)
  39. draw.line((165, 50, 165, 100), fill = 0)
  40. draw.line((140, 75, 190, 75), fill = 0)
  41. draw.arc((140, 50, 190, 100), 0, 360, fill = 0)
  42. draw.rectangle((80, 50, 130, 100), fill = 0)
  43. draw.chord((200, 50, 250, 100), 0, 360, fill = 0)
  44. epd.display(epd.getbuffer(Himage))
  45. epd.lut_GC()
  46. epd.refresh()
  47. time.sleep(2)
  48. # Drawing on the Vertical image
  49. logging.info("2.Drawing on the Vertical image...")
  50. Limage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  51. draw = ImageDraw.Draw(Limage)
  52. draw.text((2, 0), 'hello world', font = font18, fill = 0)
  53. draw.text((2, 20), '3.52inch e-Paper', font = font18, fill = 0)
  54. draw.text((20, 50), u'微雪电子', font = font18, fill = 0)
  55. draw.line((10, 90, 60, 140), fill = 0)
  56. draw.line((60, 90, 10, 140), fill = 0)
  57. draw.rectangle((10, 90, 60, 140), outline = 0)
  58. draw.line((95, 90, 95, 140), fill = 0)
  59. draw.line((70, 115, 120, 115), fill = 0)
  60. draw.arc((70, 90, 120, 140), 0, 360, fill = 0)
  61. draw.rectangle((10, 150, 60, 200), fill = 0)
  62. draw.chord((70, 150, 120, 200), 0, 360, fill = 0)
  63. epd.display(epd.getbuffer(Limage))
  64. epd.lut_GC()
  65. epd.refresh()
  66. time.sleep(2)
  67. logging.info("3.read bmp file")
  68. Himage = Image.open(os.path.join(picdir, '3in52-1.bmp'))
  69. epd.display(epd.getbuffer(Himage))
  70. epd.lut_GC()
  71. epd.refresh()
  72. time.sleep(2)
  73. logging.info("4.read bmp file on window")
  74. Himage2 = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  75. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  76. Himage2.paste(bmp, (50,10))
  77. epd.display(epd.getbuffer(Himage2))
  78. epd.lut_GC()
  79. epd.refresh()
  80. time.sleep(2)
  81. # print("Quick refresh is supported, but the refresh effect is not good, but it is not recommended")
  82. # Himage = Image.open(os.path.join(picdir, '3in52-2.bmp'))
  83. # epd.display(epd.getbuffer(Himage))
  84. # epd.lut_DU()
  85. # epd.refresh()
  86. # time.sleep(2)
  87. # Himage = Image.open(os.path.join(picdir, '3in52-3.bmp'))
  88. # epd.display(epd.getbuffer(Himage))
  89. # epd.lut_DU()
  90. # epd.refresh()
  91. # time.sleep(2)
  92. logging.info("Clear...")
  93. epd.Clear()
  94. logging.info("Goto Sleep...")
  95. epd.sleep()
  96. except IOError as e:
  97. logging.info(e)
  98. except KeyboardInterrupt:
  99. logging.info("ctrl + c:")
  100. epd3in52.epdconfig.module_exit()
  101. exit()