2
0

epd_2in66_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 epd2in66
  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 Demo")
  17. epd = epd2in66.EPD()
  18. logging.info("init and Clear")
  19. epd.init(0)
  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. Himage = Image.new('1', (epd.height, epd.width), 0xFF) # 0xFF: clear the frame
  26. draw = ImageDraw.Draw(Himage)
  27. draw.text((10, 0), 'hello world', font = font24, fill = 0)
  28. draw.text((10, 20), '2.66inch e-Paper', font = font24, fill = 0)
  29. draw.text((10, 100), u'微雪电子', font = font24, fill = 0)
  30. draw.line((20, 50, 70, 100), fill = 0)
  31. draw.line((70, 50, 20, 100), fill = 0)
  32. draw.rectangle((20, 50, 70, 100), outline = 0)
  33. draw.line((165, 50, 165, 100), fill = 0)
  34. draw.line((140, 75, 190, 75), fill = 0)
  35. draw.arc((140, 50, 190, 100), 0, 360, fill = 0)
  36. draw.rectangle((80, 50, 130, 100), fill = 0)
  37. draw.chord((200, 50, 250, 100), 0, 360, fill = 0)
  38. epd.display(epd.getbuffer(Himage))
  39. time.sleep(5)
  40. logging.info("2.read bmp file")
  41. Himage = Image.open(os.path.join(picdir, '2.66inch-9.bmp'))
  42. epd.display(epd.getbuffer(Himage))
  43. time.sleep(5)
  44. logging.info("3.read bmp file on window")
  45. Himage2 = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  46. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  47. Himage2.paste(bmp, (50,20))
  48. epd.display(epd.getbuffer(Himage2))
  49. time.sleep(5)
  50. # Drawing on the Vertical image
  51. logging.info("4.Drawing on the Vertical image...")
  52. Limage = Image.new('1', (epd.width, epd.height), 0xFF) # 0xFF: clear the frame
  53. draw = ImageDraw.Draw(Limage)
  54. draw.text((2, 0), 'hello world', font = font18, fill = 0)
  55. draw.text((2, 20), '2.66inch epd', font = font18, fill = 0)
  56. draw.text((10, 40), u'微雪电子', font = font24, fill = 0)
  57. draw.line((10, 90, 60, 140), fill = 0)
  58. draw.line((60, 90, 10, 140), fill = 0)
  59. draw.rectangle((10, 90, 60, 140), outline = 0)
  60. draw.line((95, 90, 95, 140), fill = 0)
  61. draw.line((70, 115, 120, 115), fill = 0)
  62. draw.arc((70, 90, 120, 140), 0, 360, fill = 0)
  63. draw.rectangle((10, 150, 60, 200), fill = 0)
  64. draw.chord((70, 150, 120, 200), 0, 360, fill = 0)
  65. epd.display(epd.getbuffer(Limage))
  66. time.sleep(5)
  67. # partial update, mode 1
  68. logging.info("5.show time, partial update, just mode 1")
  69. epd.init(1) # partial mode
  70. epd.Clear()
  71. time_draw = ImageDraw.Draw(Limage)
  72. num = 0
  73. while (True):
  74. time_draw.rectangle((10, 210, 120, 250), fill = 255)
  75. time_draw.text((10, 210), time.strftime('%H:%M:%S'), font = font24, fill = 0)
  76. epd.display(epd.getbuffer(Limage))
  77. num = num + 1
  78. if(num == 10):
  79. break
  80. logging.info("Clear...")
  81. epd.init(0)
  82. epd.Clear()
  83. logging.info("Goto Sleep...")
  84. epd.sleep()
  85. except IOError as e:
  86. logging.info(e)
  87. except KeyboardInterrupt:
  88. logging.info("ctrl + c:")
  89. epd2in66.epdconfig.module_exit(cleanup=True)
  90. exit()