epd_3in0g_test.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 epd3in0g
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd3in0g Demo")
  17. BLACK = 0x00
  18. WHITE = 0x55
  19. YELLOW = 0xAA
  20. RED = 0xFF
  21. epd = epd3in0g.EPD()
  22. logging.info("init and Clear")
  23. epd.init()
  24. epd.Clear(WHITE)
  25. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  26. font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
  27. font30 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 40)
  28. # Drawing on the image
  29. epd.init()
  30. logging.info("1.Drawing on the image...")
  31. Himage = Image.new('RGB', (epd.width, epd.height), 0xffffff)
  32. draw = ImageDraw.Draw(Himage)
  33. draw.text((5, 0), 'hello world', font = font18, fill = epd.RED)
  34. draw.text((5, 20), '3inch e-Paper', font = font24, fill = epd.YELLOW)
  35. draw.text((5, 45), u'微雪电子', font = font30, fill = epd.BLACK)
  36. draw.text((5, 85), u'微雪电子', font = font30, fill = epd.YELLOW)
  37. draw.text((5, 125), u'微雪电子', font = font30, fill = epd.RED)
  38. draw.line((5, 170, 80, 245), fill = epd.RED)
  39. draw.line((80, 170, 5, 245), fill = epd.YELLOW)
  40. draw.rectangle((5, 170, 80, 245), outline = epd.BLACK)
  41. draw.rectangle((90, 170, 165, 245), fill = epd.YELLOW)
  42. draw.arc((5, 250, 80, 325), 0, 360, fill = epd.BLACK)
  43. draw.chord((90, 250, 165, 325), 0, 360, fill = epd.RED)
  44. epd.display(epd.getbuffer(Himage))
  45. time.sleep(3)
  46. # Switch width and height for landscape display
  47. epd.init()
  48. logging.info("1.Drawing on the image...")
  49. Himage = Image.new('RGB', (epd.height, epd.width), 0xffffff)
  50. draw = ImageDraw.Draw(Himage)
  51. draw.text((5, 0), 'hello world', font = font18, fill = epd.RED)
  52. draw.text((5, 20), '3inch e-Paper', font = font24, fill = epd.YELLOW)
  53. draw.text((5, 45), u'微雪电子', font = font30, fill = epd.BLACK)
  54. draw.text((5, 85), u'微雪电子', font = font30, fill = epd.YELLOW)
  55. draw.text((5, 125), u'微雪电子', font = font30, fill = epd.RED)
  56. draw.line((205, 5, 295, 65), fill = epd.RED)
  57. draw.line((295, 5, 205, 65), fill = epd.YELLOW)
  58. draw.rectangle((205, 5, 295, 65), outline = epd.BLACK)
  59. draw.rectangle((305, 5, 395, 65), fill = epd.RED)
  60. draw.arc((205, 75, 295, 165), 0, 360, fill = epd.BLACK)
  61. draw.chord((305, 75, 395, 165), 0, 360, fill = epd.YELLOW)
  62. epd.display(epd.getbuffer(Himage))
  63. time.sleep(3)
  64. # read bmp file
  65. epd.init()
  66. logging.info("3.read bmp file")
  67. Himage = Image.open(os.path.join(picdir, '3inch-1.bmp'))
  68. epd.display(epd.getbuffer(Himage))
  69. time.sleep(3)
  70. epd.init()
  71. logging.info("3.read bmp file")
  72. Himage = Image.open(os.path.join(picdir, '3inch-2.bmp'))
  73. epd.display(epd.getbuffer(Himage))
  74. time.sleep(3)
  75. epd.init()
  76. logging.info("3.read bmp file")
  77. Himage = Image.open(os.path.join(picdir, '3inch-3.bmp'))
  78. epd.display(epd.getbuffer(Himage))
  79. time.sleep(3)
  80. epd.init()
  81. logging.info("Clear...")
  82. epd.Clear(WHITE)
  83. logging.info("Goto Sleep...")
  84. epd.sleep()
  85. except IOError as e:
  86. logging.info(e)
  87. except KeyboardInterrupt:
  88. logging.info("ctrl + c:")
  89. epd3in0g.epdconfig.module_exit()
  90. exit()