epd_2in7_V2_test.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 epd2in7_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("epd2in7 Demo")
  17. epd = epd2in7_V2.EPD()
  18. '''2Gray(Black and white) display'''
  19. logging.info("init and Clear")
  20. epd.init()
  21. epd.Clear()
  22. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  23. font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
  24. font35 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 35)
  25. # Quick refresh
  26. logging.info("Quick refresh demo")
  27. epd.init_Fast()
  28. # Drawing on the Vertical image
  29. logging.info("1.Drawing on the Vertical image...")
  30. Limage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  31. draw = ImageDraw.Draw(Limage)
  32. draw.text((2, 0), 'hello world', font = font18, fill = 0)
  33. draw.text((20, 50), u'微雪电子', font = font18, fill = 0)
  34. draw.line((10, 90, 60, 140), fill = 0)
  35. draw.line((60, 90, 10, 140), fill = 0)
  36. draw.rectangle((10, 90, 60, 140), outline = 0)
  37. draw.line((95, 90, 95, 140), fill = 0)
  38. draw.line((70, 115, 120, 115), fill = 0)
  39. draw.arc((70, 90, 120, 140), 0, 360, fill = 0)
  40. draw.rectangle((10, 150, 60, 200), fill = 0)
  41. draw.chord((70, 150, 120, 200), 0, 360, fill = 0)
  42. epd.display_Fast(epd.getbuffer(Limage))
  43. time.sleep(2)
  44. logging.info("2.read bmp file")
  45. Himage = Image.open(os.path.join(picdir, '2in7.bmp'))
  46. epd.display_Fast(epd.getbuffer(Himage))
  47. time.sleep(2)
  48. # Normal refresh
  49. logging.info("Normal refresh demo")
  50. epd.init()
  51. logging.info("3.read bmp file on window")
  52. Himage2 = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  53. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  54. Himage2.paste(bmp, (50,10))
  55. epd.display(epd.getbuffer(Himage2))
  56. time.sleep(2)
  57. # Drawing on the Horizontal image
  58. logging.info("4.Drawing on the Horizontal image...")
  59. Himage = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  60. draw = ImageDraw.Draw(Himage)
  61. draw.text((10, 0), 'hello world', font = font24, fill = 0)
  62. draw.text((150, 0), u'微雪电子', font = font24, fill = 0)
  63. draw.line((20, 50, 70, 100), fill = 0)
  64. draw.line((70, 50, 20, 100), fill = 0)
  65. draw.rectangle((20, 50, 70, 100), outline = 0)
  66. draw.line((165, 50, 165, 100), fill = 0)
  67. draw.line((140, 75, 190, 75), fill = 0)
  68. draw.arc((140, 50, 190, 100), 0, 360, fill = 0)
  69. draw.rectangle((80, 50, 130, 100), fill = 0)
  70. draw.chord((200, 50, 250, 100), 0, 360, fill = 0)
  71. epd.display_Base(epd.getbuffer(Himage))
  72. time.sleep(2)
  73. # partial update
  74. logging.info("5.show time")
  75. epd.init()
  76. '''
  77. # If you didn't use the EPD_2IN7_V2_Display_Base() function to refresh the image before,
  78. # use the EPD_2IN7_V2_Display_Base_color() function to refresh the background color,
  79. # otherwise the background color will be garbled
  80. '''
  81. # epd.display_Base_color(0xff)
  82. # Himage = Image.new('1', (epd.height ,epd.width), 0xff)
  83. # draw = ImageDraw.Draw(time_image)
  84. num = 0
  85. while (True):
  86. draw.rectangle((10, 110, 120, 150), fill = 255)
  87. draw.text((10, 110), time.strftime('%H:%M:%S'), font = font24, fill = 0)
  88. newimage = Himage.crop([10, 110, 120, 150])
  89. Himage.paste(newimage, (10,110))
  90. epd.display_Partial(epd.getbuffer(Himage),110, epd.height - 120, 150, epd.height - 10)
  91. num = num + 1
  92. if(num == 10):
  93. break
  94. # epd.init()
  95. # epd.clear()
  96. # epd.display_Base_color(0xff)
  97. # Himage = Image.new('1', (epd.width ,epd.height), 0xff)
  98. # draw = ImageDraw.Draw(Himage)
  99. # num = 0
  100. # while (True):
  101. # draw.rectangle((10, 10, 120, 50), fill = 255)
  102. # draw.text((10, 10), time.strftime('%H:%M:%S'), font = font24, fill = 0)
  103. # newimage = Himage.crop([10, 10, 120, 50])
  104. # Himage.paste(newimage, (10,10))
  105. # epd.display_Partial(epd.getbuffer(Himage),10, 10, 120, 50)
  106. # num = num + 1
  107. # if(num == 10):
  108. # break
  109. '''4Gray display'''
  110. logging.info("4Gray display--------------------------------")
  111. epd.Init_4Gray()
  112. Limage = Image.new('L', (epd.width, epd.height), 0) # 255: clear the frame
  113. draw = ImageDraw.Draw(Limage)
  114. draw.text((20, 0), u'微雪电子', font = font35, fill = epd.GRAY1)
  115. draw.text((20, 35), u'微雪电子', font = font35, fill = epd.GRAY2)
  116. draw.text((20, 70), u'微雪电子', font = font35, fill = epd.GRAY3)
  117. draw.text((40, 110), 'hello world', font = font18, fill = epd.GRAY1)
  118. draw.line((10, 140, 60, 190), fill = epd.GRAY1)
  119. draw.line((60, 140, 10, 190), fill = epd.GRAY1)
  120. draw.rectangle((10, 140, 60, 190), outline = epd.GRAY1)
  121. draw.line((95, 140, 95, 190), fill = epd.GRAY1)
  122. draw.line((70, 165, 120, 165), fill = epd.GRAY1)
  123. draw.arc((70, 140, 120, 190), 0, 360, fill = epd.GRAY1)
  124. draw.rectangle((10, 200, 60, 250), fill = epd.GRAY1)
  125. draw.chord((70, 200, 120, 250), 0, 360, fill = epd.GRAY1)
  126. epd.display_4Gray(epd.getbuffer_4Gray(Limage))
  127. time.sleep(2)
  128. #display 4Gra bmp
  129. Himage = Image.open(os.path.join(picdir, '2in7_Scale.bmp'))
  130. epd.display_4Gray(epd.getbuffer_4Gray(Himage))
  131. time.sleep(2)
  132. logging.info("Clear...")
  133. epd.init()
  134. epd.Clear()
  135. logging.info("Goto Sleep...")
  136. epd.sleep()
  137. except IOError as e:
  138. logging.info(e)
  139. except KeyboardInterrupt:
  140. logging.info("ctrl + c:")
  141. epd2in7_V2.epdconfig.module_exit(cleanup=True)
  142. exit()