2
0

epd_1in02_test.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 epd1in02
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd1in02 Demo")
  17. epd = epd1in02.EPD()
  18. logging.info("init and Clear")
  19. epd.Init()
  20. epd.Clear()
  21. # Drawing on the image
  22. logging.info("1.Drawing on the image...")
  23. image = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  24. draw = ImageDraw.Draw(image)
  25. font = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  26. draw.text((5, 0), 'hello world', font = font, fill = 0)
  27. draw.text((15, 40), u'微雪电子', font = font, fill = 0)
  28. epd.Display(epd.getbuffer(image))
  29. time.sleep(2)
  30. image = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  31. draw = ImageDraw.Draw(image)
  32. draw.rectangle((20, 0, 100, 80), fill = 0)
  33. draw.rectangle((22, 2, 98, 78), fill = 255)
  34. draw.chord((22, 2, 98, 78), 0, 360, fill = 0)
  35. draw.chord((24, 4, 96, 76), 0, 360, fill = 255)
  36. draw.line((20, 0, 100, 80), fill = 0)
  37. draw.line((20, 80, 100, 0), fill = 0)
  38. epd.display(epd.getbuffer(image))
  39. time.sleep(2)
  40. # read bmp file
  41. logging.info("2.read bmp file...")
  42. image = Image.open(os.path.join(picdir, '1in02.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. image1 = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  48. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  49. image1.paste(bmp, (0,0))
  50. epd.display(epd.getbuffer(image1))
  51. time.sleep(2)
  52. # # partial update
  53. logging.info("4.show time...")
  54. epd.Clear()
  55. epd.Partial_Init()
  56. time_image = Image.new('1', (epd.height, epd.width), 255)
  57. time_draw = ImageDraw.Draw(time_image)
  58. image_old = epd.getbuffer(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.DisplayPartial(image_old, epd.getbuffer(time_image))
  66. image_old = epd.getbuffer(time_image)
  67. num = num + 1
  68. if(num == 10):
  69. break
  70. logging.info("Clear...")
  71. epd.Init()
  72. epd.Clear()
  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. epd1in02.epdconfig.module_exit(cleanup=True)
  80. exit()