epd7in5_V2.py 5.9 KB

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