epd_2in13_V2_test.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 epd2in13_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("epd2in13_V2 Demo")
  17. epd = epd2in13_V2.EPD()
  18. logging.info("init and Clear")
  19. epd.init(epd.FULL_UPDATE)
  20. epd.Clear(0xFF)
  21. # Drawing on the image
  22. font15 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 15)
  23. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  24. logging.info("1.Drawing on the image...")
  25. image = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  26. draw = ImageDraw.Draw(image)
  27. draw.rectangle([(0,0),(50,50)],outline = 0)
  28. draw.rectangle([(55,0),(100,50)],fill = 0)
  29. draw.line([(0,0),(50,50)], fill = 0,width = 1)
  30. draw.line([(0,50),(50,0)], fill = 0,width = 1)
  31. draw.chord((10, 60, 50, 100), 0, 360, fill = 0)
  32. draw.ellipse((55, 60, 95, 100), outline = 0)
  33. draw.pieslice((55, 60, 95, 100), 90, 180, outline = 0)
  34. draw.pieslice((55, 60, 95, 100), 270, 360, fill = 0)
  35. draw.polygon([(110,0),(110,50),(150,25)],outline = 0)
  36. draw.polygon([(190,0),(190,50),(150,25)],fill = 0)
  37. draw.text((120, 60), 'e-Paper demo', font = font15, fill = 0)
  38. draw.text((110, 90), u'微雪电子', font = font24, fill = 0)
  39. epd.display(epd.getbuffer(image))
  40. time.sleep(2)
  41. # read bmp file
  42. logging.info("2.read bmp file...")
  43. image = Image.open(os.path.join(picdir, '2in13.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.height, epd.width), 255) # 255: clear the frame
  50. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  51. image1.paste(bmp, (2,2))
  52. epd.display(epd.getbuffer(image1))
  53. time.sleep(2)
  54. # # partial update
  55. logging.info("4.show time...")
  56. time_image = Image.new('1', (epd.height, epd.width), 255)
  57. time_draw = ImageDraw.Draw(time_image)
  58. epd.init(epd.FULL_UPDATE)
  59. epd.displayPartBaseImage(epd.getbuffer(time_image))
  60. epd.init(epd.PART_UPDATE)
  61. num = 0
  62. while (True):
  63. time_draw.rectangle((120, 80, 220, 105), fill = 255)
  64. time_draw.text((120, 80), time.strftime('%H:%M:%S'), font = font24, fill = 0)
  65. epd.displayPartial(epd.getbuffer(time_image))
  66. num = num + 1
  67. if(num == 10):
  68. break
  69. # epd.Clear(0xFF)
  70. logging.info("Clear...")
  71. epd.init(epd.FULL_UPDATE)
  72. epd.Clear(0xFF)
  73. logging.info("Goto Sleep...")
  74. epd.sleep()
  75. except IOError as e:
  76. logging.info(e)
  77. except KeyboardInterrupt:
  78. logging.info("ctrl + c:")
  79. epd2in13_V2.epdconfig.module_exit(cleanup=True)
  80. exit()