epd_1in54c_test.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 epd1in54c
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd1in54c Demo")
  17. epd = epd1in54c.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("1.Drawing on the image...")
  24. blackimage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  25. yellowimage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  26. font = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  27. font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
  28. drawblack = ImageDraw.Draw(blackimage)
  29. drawyellow = ImageDraw.Draw(yellowimage)
  30. drawblack.rectangle((0, 10, 200, 34), fill = 0)
  31. drawblack.text((8, 12), 'hello world', font = font, fill = 255)
  32. drawblack.text((8, 36), u'微雪电子', font = font, fill = 0)
  33. drawblack.line((10, 100, 70, 100), fill = 0)
  34. drawblack.line((40, 70, 40, 130), fill = 0)
  35. drawyellow.rectangle((10, 70, 70, 130), outline = 0)
  36. drawyellow.arc((80, 70, 140, 130), 0, 360, fill = 0)
  37. drawyellow.chord((90, 80, 130, 120), 0, 360, fill = 0)
  38. epd.display(epd.getbuffer(blackimage),epd.getbuffer(yellowimage))
  39. time.sleep(1)
  40. # read bmp file
  41. logging.info("2.read bmp file...")
  42. blackimage = Image.open(os.path.join(picdir, '1in54c-b.bmp'))
  43. yellowimage = Image.open(os.path.join(picdir, '1in54c-y.bmp'))
  44. epd.display(epd.getbuffer(blackimage),epd.getbuffer(yellowimage))
  45. time.sleep(1)
  46. # read bmp file on window
  47. logging.info("3.read bmp file on window...")
  48. blackimage1 = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  49. yellowimage2 = Image.new('1', (epd.width, epd.height), 255)
  50. newimage = Image.open(os.path.join(picdir, '100x100.bmp'))
  51. blackimage1.paste(newimage, (50,50))
  52. epd.display(epd.getbuffer(blackimage1), epd.getbuffer(yellowimage2))
  53. logging.info("Clear...")
  54. epd.init()
  55. epd.Clear()
  56. logging.info("Goto Sleep...")
  57. epd.sleep()
  58. except IOError as e:
  59. logging.info(e)
  60. except KeyboardInterrupt:
  61. logging.info("ctrl + c:")
  62. epd1in54c.epdconfig.module_exit(cleanup=True)
  63. exit()