epd7in5_V2.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # *****************************************************************************
  2. # * | File : epd7in5.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V4.0
  8. # * | Date : 2019-06-20
  9. # # | Info : python demo
  10. #
  11. # Modifications by Dmitry Brant:
  12. # - Oct 2020: New routines for faster SPI data transfer.
  13. #
  14. # -----------------------------------------------------------------------------
  15. # Permission is hereby granted, free of charge, to any person obtaining a copy
  16. # of this software and associated documnetation files (the "Software"), to deal
  17. # in the Software without restriction, including without limitation the rights
  18. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  19. # copies of the Software, and to permit persons to whom the Software is
  20. # furished to do so, subject to the following conditions:
  21. #
  22. # The above copyright notice and this permission notice shall be included in
  23. # all copies or substantial portions of the Software.
  24. #
  25. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  30. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  31. # THE SOFTWARE.
  32. #
  33. import logging
  34. from . import epdconfig
  35. # Display resolution
  36. EPD_WIDTH = 800
  37. EPD_HEIGHT = 480
  38. class EPD:
  39. def __init__(self):
  40. self.reset_pin = epdconfig.RST_PIN
  41. self.dc_pin = epdconfig.DC_PIN
  42. self.busy_pin = epdconfig.BUSY_PIN
  43. self.cs_pin = epdconfig.CS_PIN
  44. self.width = EPD_WIDTH
  45. self.height = EPD_HEIGHT
  46. # Hardware reset
  47. def reset(self):
  48. epdconfig.digital_write(self.reset_pin, 1)
  49. epdconfig.delay_ms(200)
  50. epdconfig.digital_write(self.reset_pin, 0)
  51. epdconfig.delay_ms(2)
  52. epdconfig.digital_write(self.reset_pin, 1)
  53. epdconfig.delay_ms(200)
  54. def send_command(self, command):
  55. epdconfig.digital_write(self.dc_pin, 0)
  56. epdconfig.digital_write(self.cs_pin, 0)
  57. epdconfig.spi_writebyte([command])
  58. epdconfig.digital_write(self.cs_pin, 1)
  59. def send_data(self, data):
  60. epdconfig.digital_write(self.dc_pin, 1)
  61. epdconfig.digital_write(self.cs_pin, 0)
  62. epdconfig.spi_writebyte([data])
  63. epdconfig.digital_write(self.cs_pin, 1)
  64. def send_data2(self, data):
  65. epdconfig.digital_write(self.dc_pin, 1)
  66. epdconfig.digital_write(self.cs_pin, 0)
  67. epdconfig.SPI.writebytes2(data)
  68. epdconfig.digital_write(self.cs_pin, 1)
  69. def ReadBusy(self):
  70. logging.debug("e-Paper busy")
  71. self.send_command(0x71)
  72. busy = epdconfig.digital_read(self.busy_pin)
  73. while(busy == 0):
  74. self.send_command(0x71)
  75. busy = epdconfig.digital_read(self.busy_pin)
  76. epdconfig.delay_ms(200)
  77. def init(self):
  78. if (epdconfig.module_init() != 0):
  79. return -1
  80. # EPD hardware init start
  81. self.reset()
  82. self.send_command(0x01) #POWER SETTING
  83. self.send_data(0x07)
  84. self.send_data(0x07) #VGH=20V,VGL=-20V
  85. self.send_data(0x3f) #VDH=15V
  86. self.send_data(0x3f) #VDL=-15V
  87. self.send_command(0x04) #POWER ON
  88. epdconfig.delay_ms(100)
  89. self.ReadBusy()
  90. self.send_command(0X00) #PANNEL SETTING
  91. self.send_data(0x1F) #KW-3f KWR-2F BWROTP 0f BWOTP 1f
  92. self.send_command(0x61) #tres
  93. self.send_data(0x03) #source 800
  94. self.send_data(0x20)
  95. self.send_data(0x01) #gate 480
  96. self.send_data(0xE0)
  97. self.send_command(0X15)
  98. self.send_data(0x00)
  99. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  100. self.send_data(0x10)
  101. self.send_data(0x07)
  102. self.send_command(0X60) #TCON SETTING
  103. self.send_data(0x22)
  104. # EPD hardware init end
  105. return 0
  106. def getbuffer(self, image):
  107. img = image
  108. imwidth, imheight = img.size
  109. if(imwidth == self.width and imheight == self.height):
  110. img = img.convert('1')
  111. elif(imwidth == self.height and imheight == self.width):
  112. img = img.rotate(90, expand=True).convert('1')
  113. else:
  114. logging.warning("Wrong image dimensions: must be " + str(self.width) + "x" + str(self.height))
  115. # return a blank buffer
  116. return [0x00] * (int(self.width/8) * self.height)
  117. buf = bytearray(img.tobytes('raw'))
  118. # The bytes need to be inverted, because in the PIL world 0=black and 1=white, but
  119. # in the e-paper world 0=white and 1=black.
  120. for i in range(len(buf)):
  121. buf[i] ^= 0xFF
  122. return buf
  123. def display(self, image):
  124. self.send_command(0x13)
  125. self.send_data2(image)
  126. self.send_command(0x12)
  127. epdconfig.delay_ms(100)
  128. self.ReadBusy()
  129. def Clear(self):
  130. buf = [0x00] * (int(self.width/8) * self.height)
  131. self.send_command(0x10)
  132. self.send_data2(buf)
  133. self.send_command(0x13)
  134. self.send_data2(buf)
  135. self.send_command(0x12)
  136. epdconfig.delay_ms(100)
  137. self.ReadBusy()
  138. def sleep(self):
  139. self.send_command(0x02) # POWER_OFF
  140. self.ReadBusy()
  141. self.send_command(0x07) # DEEP_SLEEP
  142. self.send_data(0XA5)
  143. def Dev_exit(self):
  144. epdconfig.module_exit()
  145. ### END OF FILE ###