epd_1in54_V2_test.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 epd1in54_V2
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd1in54_V2 Demo")
  17. epd = epd1in54_V2.EPD()
  18. logging.info("init and Clear")
  19. epd.init(0)
  20. epd.Clear(0xFF)
  21. time.sleep(1)
  22. # Drawing on the image
  23. logging.info("1.Drawing on the image...")
  24. image = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  25. draw = ImageDraw.Draw(image)
  26. font = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  27. draw.rectangle((0, 10, 200, 34), fill = 0)
  28. draw.text((8, 12), 'hello world', font = font, fill = 255)
  29. draw.text((8, 36), u'微雪电子', font = font, fill = 0)
  30. draw.line((16, 60, 56, 60), fill = 0)
  31. draw.line((56, 60, 56, 110), fill = 0)
  32. draw.line((16, 110, 56, 110), fill = 0)
  33. draw.line((16, 110, 16, 60), fill = 0)
  34. draw.line((16, 60, 56, 110), fill = 0)
  35. draw.line((56, 60, 16, 110), fill = 0)
  36. draw.arc((90, 60, 150, 120), 0, 360, fill = 0)
  37. draw.rectangle((16, 130, 56, 180), fill = 0)
  38. draw.chord((90, 130, 150, 190), 0, 360, fill = 0)
  39. epd.display(epd.getbuffer(image.rotate(90)))
  40. time.sleep(2)
  41. # read bmp file
  42. logging.info("2.read bmp file...")
  43. image = Image.open(os.path.join(picdir, '1in54.bmp'))
  44. epd.display(epd.getbuffer(image))
  45. time.sleep(2)
  46. # read bmp file on window
  47. logging.info("3.read bmp file on window...")
  48. epd.Clear(0xFF)
  49. image1 = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  50. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  51. image1.paste(bmp, (50,50))
  52. epd.display(epd.getbuffer(image1))
  53. time.sleep(2)
  54. # partial update
  55. logging.info("4.show time...")
  56. time_image = image1
  57. # Image.new('1', (epd.width, epd.height), 255)
  58. epd.displayPartBaseImage(epd.getbuffer(time_image))
  59. epd.init(1) # into partial refresh mode
  60. time_draw = ImageDraw.Draw(time_image)
  61. num = 0
  62. while (True):
  63. time_draw.rectangle((10, 10, 120, 50), fill = 255)
  64. time_draw.text((10, 10), time.strftime('%H:%M:%S'), font = font, fill = 0)
  65. newimage = time_image.crop([10, 10, 120, 50])
  66. time_image.paste(newimage, (10,10))
  67. epd.displayPart(epd.getbuffer(time_image))
  68. num = num + 1
  69. if(num == 20):
  70. break
  71. logging.info("Clear...")
  72. epd.init(0)
  73. epd.Clear(0xFF)
  74. logging.info("Goto Sleep...")
  75. epd.sleep()
  76. except IOError as e:
  77. logging.info(e)
  78. except KeyboardInterrupt:
  79. logging.info("ctrl + c:")
  80. epd1in54_V2.epdconfig.module_exit(cleanup=True)
  81. exit()