epd_4in2_V2_test.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 epd4in2_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("epd4in2 Demo")
  17. epd = epd4in2_V2.EPD()
  18. logging.info("init and Clear")
  19. epd.init()
  20. epd.Clear()
  21. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  22. font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
  23. font35 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 35)
  24. if 0:
  25. logging.info("E-paper refresh")
  26. epd.init()
  27. # Drawing on the Horizontal image
  28. logging.info("1.Drawing on the Horizontal image...")
  29. Himage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  30. draw = ImageDraw.Draw(Himage)
  31. draw.text((10, 0), 'hello world', font = font24, fill = 0)
  32. draw.text((10, 20), '4.2inch V2 e-Paper', font = font24, fill = 0)
  33. draw.text((150, 0), u'微雪电子', font = font24, fill = 0)
  34. draw.line((20, 50, 70, 100), fill = 0)
  35. draw.line((70, 50, 20, 100), fill = 0)
  36. draw.rectangle((20, 50, 70, 100), outline = 0)
  37. draw.line((165, 50, 165, 100), fill = 0)
  38. draw.line((140, 75, 190, 75), fill = 0)
  39. draw.arc((140, 50, 190, 100), 0, 360, fill = 0)
  40. draw.rectangle((80, 50, 130, 100), fill = 0)
  41. draw.chord((200, 50, 250, 100), 0, 360, fill = 0)
  42. epd.display(epd.getbuffer(Himage))
  43. time.sleep(2)
  44. logging.info("2.read bmp file")
  45. Himage = Image.open(os.path.join(picdir, '4in2.bmp'))
  46. epd.display(epd.getbuffer(Himage))
  47. time.sleep(2)
  48. # Drawing on the Vertical image
  49. logging.info("3.read bmp file on window")
  50. Himage2 = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  51. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  52. Himage2.paste(bmp, (50,10))
  53. epd.display(epd.getbuffer(Himage2))
  54. time.sleep(2)
  55. logging.info("4.Drawing on the Vertical image...")
  56. Limage = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  57. draw = ImageDraw.Draw(Limage)
  58. draw.text((2, 0), 'hello world', font = font18, fill = 0)
  59. draw.text((2, 20), '4.2inch V2 epd', font = font18, fill = 0)
  60. draw.text((20, 50), u'微雪电子', font = font18, fill = 0)
  61. draw.line((10, 90, 60, 140), fill = 0)
  62. draw.line((60, 90, 10, 140), fill = 0)
  63. draw.rectangle((10, 90, 60, 140), outline = 0)
  64. draw.line((95, 90, 95, 140), fill = 0)
  65. draw.line((70, 115, 120, 115), fill = 0)
  66. draw.arc((70, 90, 120, 140), 0, 360, fill = 0)
  67. draw.rectangle((10, 150, 60, 200), fill = 0)
  68. draw.chord((70, 150, 120, 200), 0, 360, fill = 0)
  69. epd.display(epd.getbuffer(Limage))
  70. time.sleep(1)
  71. else:
  72. logging.info("E-paper refreshes quickly")
  73. # Drawing on the Horizontal image
  74. epd.init_fast(epd.Seconds_1_5S)
  75. logging.info("1.Drawing on the Horizontal image...")
  76. Himage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
  77. draw = ImageDraw.Draw(Himage)
  78. draw.text((10, 0), 'hello world', font = font24, fill = 0)
  79. draw.text((10, 20), '4.2inch V2 e-Paper', font = font24, fill = 0)
  80. draw.text((150, 0), u'微雪电子', font = font24, fill = 0)
  81. draw.line((20, 50, 70, 100), fill = 0)
  82. draw.line((70, 50, 20, 100), fill = 0)
  83. draw.rectangle((20, 50, 70, 100), outline = 0)
  84. draw.line((165, 50, 165, 100), fill = 0)
  85. draw.line((140, 75, 190, 75), fill = 0)
  86. draw.arc((140, 50, 190, 100), 0, 360, fill = 0)
  87. draw.rectangle((80, 50, 130, 100), fill = 0)
  88. draw.chord((200, 50, 250, 100), 0, 360, fill = 0)
  89. epd.display_Fast(epd.getbuffer(Himage))
  90. time.sleep(2)
  91. logging.info("2.read bmp file")
  92. Himage = Image.open(os.path.join(picdir, '4in2.bmp'))
  93. epd.display_Fast(epd.getbuffer(Himage))
  94. time.sleep(2)
  95. # Drawing on the Vertical image
  96. logging.info("3.read bmp file on window")
  97. Himage2 = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  98. bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
  99. Himage2.paste(bmp, (50,10))
  100. epd.display_Fast(epd.getbuffer(Himage2))
  101. time.sleep(2)
  102. logging.info("4.Drawing on the Vertical image...")
  103. Limage = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
  104. draw = ImageDraw.Draw(Limage)
  105. draw.text((2, 0), 'hello world', font = font18, fill = 0)
  106. draw.text((2, 20), '4.2inch V2 epd', font = font18, fill = 0)
  107. draw.text((20, 50), u'微雪电子', font = font18, fill = 0)
  108. draw.line((10, 90, 60, 140), fill = 0)
  109. draw.line((60, 90, 10, 140), fill = 0)
  110. draw.rectangle((10, 90, 60, 140), outline = 0)
  111. draw.line((95, 90, 95, 140), fill = 0)
  112. draw.line((70, 115, 120, 115), fill = 0)
  113. draw.arc((70, 90, 120, 140), 0, 360, fill = 0)
  114. draw.rectangle((10, 150, 60, 200), fill = 0)
  115. draw.chord((70, 150, 120, 200), 0, 360, fill = 0)
  116. epd.display_Fast(epd.getbuffer(Limage))
  117. time.sleep(1)
  118. # partial update
  119. logging.info("5.show time...")
  120. num = 0
  121. while (True):
  122. draw.rectangle((140, 80, 240, 105), fill = 255)
  123. draw.text((140, 80), time.strftime('%H:%M:%S'), font = font24, fill = 0)
  124. epd.display_Partial(epd.getbuffer(Limage))
  125. num = num + 1
  126. time.sleep(0.5)
  127. if(num == 10):
  128. break
  129. '''4Gray display'''
  130. logging.info("6.4Gray display--------------------------------")
  131. epd.Init_4Gray()
  132. Limage = Image.new('L', (epd.width, epd.height), 0) # 255: clear the frame
  133. draw = ImageDraw.Draw(Limage)
  134. draw.text((20, 0), u'微雪电子', font = font35, fill = epd.GRAY1)
  135. draw.text((20, 35), u'微雪电子', font = font35, fill = epd.GRAY2)
  136. draw.text((20, 70), u'微雪电子', font = font35, fill = epd.GRAY3)
  137. draw.text((40, 110), 'hello world', font = font18, fill = epd.GRAY1)
  138. draw.line((10, 140, 60, 190), fill = epd.GRAY1)
  139. draw.line((60, 140, 10, 190), fill = epd.GRAY1)
  140. draw.rectangle((10, 140, 60, 190), outline = epd.GRAY1)
  141. draw.line((95, 140, 95, 190), fill = epd.GRAY1)
  142. draw.line((70, 165, 120, 165), fill = epd.GRAY1)
  143. draw.arc((70, 140, 120, 190), 0, 360, fill = epd.GRAY1)
  144. draw.rectangle((10, 200, 60, 250), fill = epd.GRAY1)
  145. draw.chord((70, 200, 120, 250), 0, 360, fill = epd.GRAY1)
  146. epd.display_4Gray(epd.getbuffer_4Gray(Limage))
  147. time.sleep(3)
  148. #display 4Gra bmp
  149. Himage = Image.open(os.path.join(picdir, '4in2_Scale_1.bmp'))
  150. epd.display_4Gray(epd.getbuffer_4Gray(Himage))
  151. time.sleep(4)
  152. logging.info("Clear...")
  153. epd.init()
  154. epd.Clear()
  155. logging.info("Goto Sleep...")
  156. epd.sleep()
  157. except IOError as e:
  158. logging.info(e)
  159. except KeyboardInterrupt:
  160. logging.info("ctrl + c:")
  161. epd4in2_V2.epdconfig.module_exit(cleanup=True)
  162. exit()