epd_1in54_test.py 2.9 KB

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