epd7in5_V2.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. logger = logging.getLogger(__name__)
  35. class EPD:
  36. def __init__(self):
  37. self.reset_pin = epdconfig.RST_PIN
  38. self.dc_pin = epdconfig.DC_PIN
  39. self.busy_pin = epdconfig.BUSY_PIN
  40. self.cs_pin = epdconfig.CS_PIN
  41. self.width = EPD_WIDTH
  42. self.height = EPD_HEIGHT
  43. # Hardware reset
  44. def reset(self):
  45. epdconfig.digital_write(self.reset_pin, 1)
  46. epdconfig.delay_ms(20)
  47. epdconfig.digital_write(self.reset_pin, 0)
  48. epdconfig.delay_ms(2)
  49. epdconfig.digital_write(self.reset_pin, 1)
  50. epdconfig.delay_ms(20)
  51. def send_command(self, command):
  52. epdconfig.digital_write(self.dc_pin, 0)
  53. epdconfig.digital_write(self.cs_pin, 0)
  54. epdconfig.spi_writebyte([command])
  55. epdconfig.digital_write(self.cs_pin, 1)
  56. def send_data(self, data):
  57. epdconfig.digital_write(self.dc_pin, 1)
  58. epdconfig.digital_write(self.cs_pin, 0)
  59. epdconfig.spi_writebyte([data])
  60. epdconfig.digital_write(self.cs_pin, 1)
  61. def send_data2(self, data):
  62. epdconfig.digital_write(self.dc_pin, 1)
  63. epdconfig.digital_write(self.cs_pin, 0)
  64. epdconfig.SPI.writebytes2(data)
  65. epdconfig.digital_write(self.cs_pin, 1)
  66. def ReadBusy(self):
  67. logger.debug("e-Paper busy")
  68. self.send_command(0x71)
  69. busy = epdconfig.digital_read(self.busy_pin)
  70. while(busy == 0):
  71. self.send_command(0x71)
  72. busy = epdconfig.digital_read(self.busy_pin)
  73. epdconfig.delay_ms(20)
  74. logger.debug("e-Paper busy release")
  75. def init(self):
  76. if (epdconfig.module_init() != 0):
  77. return -1
  78. # EPD hardware init start
  79. self.reset()
  80. self.send_command(0x06) # btst
  81. self.send_data(0x17)
  82. self.send_data(0x17)
  83. self.send_data(0x28) # If an exception is displayed, try using 0x38
  84. self.send_data(0x17)
  85. self.send_command(0x01) #POWER SETTING
  86. self.send_data(0x07)
  87. self.send_data(0x07) #VGH=20V,VGL=-20V
  88. self.send_data(0x3f) #VDH=15V
  89. self.send_data(0x3f) #VDL=-15V
  90. self.send_command(0x04) #POWER ON
  91. epdconfig.delay_ms(100)
  92. self.ReadBusy()
  93. self.send_command(0X00) #PANNEL SETTING
  94. self.send_data(0x1F) #KW-3f KWR-2F BWROTP 0f BWOTP 1f
  95. self.send_command(0x61) #tres
  96. self.send_data(0x03) #source 800
  97. self.send_data(0x20)
  98. self.send_data(0x01) #gate 480
  99. self.send_data(0xE0)
  100. self.send_command(0X15)
  101. self.send_data(0x00)
  102. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  103. self.send_data(0x10)
  104. self.send_data(0x07)
  105. self.send_command(0X60) #TCON SETTING
  106. self.send_data(0x22)
  107. # EPD hardware init end
  108. return 0
  109. def init_fast(self):
  110. if (epdconfig.module_init() != 0):
  111. return -1
  112. # EPD hardware init start
  113. self.reset()
  114. self.send_command(0X00) #PANNEL SETTING
  115. self.send_data(0x1F) #KW-3f KWR-2F BWROTP 0f BWOTP 1f
  116. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  117. self.send_data(0x10)
  118. self.send_data(0x07)
  119. self.send_command(0x04) #POWER ON
  120. epdconfig.delay_ms(100)
  121. self.ReadBusy() #waiting for the electronic paper IC to release the idle signal
  122. #Enhanced display drive(Add 0x06 command)
  123. self.send_command(0x06) #Booster Soft Start
  124. self.send_data (0x27)
  125. self.send_data (0x27)
  126. self.send_data (0x18)
  127. self.send_data (0x17)
  128. self.send_command(0xE0)
  129. self.send_data(0x02)
  130. self.send_command(0xE5)
  131. self.send_data(0x5A)
  132. # EPD hardware init end
  133. return 0
  134. def init_part(self):
  135. if (epdconfig.module_init() != 0):
  136. return -1
  137. # EPD hardware init start
  138. self.reset()
  139. self.send_command(0X00) #PANNEL SETTING
  140. self.send_data(0x1F) #KW-3f KWR-2F BWROTP 0f BWOTP 1f
  141. self.send_command(0x04) #POWER ON
  142. epdconfig.delay_ms(100)
  143. self.ReadBusy() #waiting for the electronic paper IC to release the idle signal
  144. self.send_command(0xE0)
  145. self.send_data(0x02)
  146. self.send_command(0xE5)
  147. self.send_data(0x6E)
  148. # EPD hardware init end
  149. return 0
  150. def getbuffer(self, image):
  151. img = image
  152. imwidth, imheight = img.size
  153. if(imwidth == self.width and imheight == self.height):
  154. img = img.convert('1')
  155. elif(imwidth == self.height and imheight == self.width):
  156. # image has correct dimensions, but needs to be rotated
  157. img = img.rotate(90, expand=True).convert('1')
  158. else:
  159. logger.warning("Wrong image dimensions: must be " + str(self.width) + "x" + str(self.height))
  160. # return a blank buffer
  161. return [0x00] * (int(self.width/8) * self.height)
  162. buf = bytearray(img.tobytes('raw'))
  163. # The bytes need to be inverted, because in the PIL world 0=black and 1=white, but
  164. # in the e-paper world 0=white and 1=black.
  165. for i in range(len(buf)):
  166. buf[i] ^= 0xFF
  167. return buf
  168. def display(self, image):
  169. if(self.width % 8 == 0):
  170. Width = self.width // 8
  171. else:
  172. Width = self.width // 8 +1
  173. Height = self.height
  174. image1 = [0xFF] * int(self.width * self.height / 8)
  175. for j in range(Height):
  176. for i in range(Width):
  177. image1[i + j * Width] = ~image[i + j * Width]
  178. self.send_command(0x10)
  179. self.send_data2(image1)
  180. self.send_command(0x13)
  181. self.send_data2(image)
  182. self.send_command(0x12)
  183. epdconfig.delay_ms(100)
  184. self.ReadBusy()
  185. def Clear(self):
  186. self.send_command(0x10)
  187. self.send_data2([0xFF] * int(self.width * self.height / 8))
  188. self.send_command(0x13)
  189. self.send_data2([0x00] * int(self.width * self.height / 8))
  190. self.send_command(0x12)
  191. epdconfig.delay_ms(100)
  192. self.ReadBusy()
  193. def display_Partial(self, Image, Xstart, Ystart, Xend, Yend):
  194. if((Xstart % 8 + Xend % 8 == 8 & Xstart % 8 > Xend % 8) | Xstart % 8 + Xend % 8 == 0 | (Xend - Xstart)%8 == 0):
  195. Xstart = Xstart // 8 * 8
  196. Xend = Xend // 8 * 8
  197. else:
  198. Xstart = Xstart // 8 * 8
  199. if Xend % 8 == 0:
  200. Xend = Xend // 8 * 8
  201. else:
  202. Xend = Xend // 8 * 8 + 1
  203. Width = (Xend - Xstart) // 8
  204. Height = Yend - Ystart
  205. self.send_command(0x50)
  206. self.send_data(0xA9)
  207. self.send_data(0x07)
  208. self.send_command(0x91) #This command makes the display enter partial mode
  209. self.send_command(0x90) #resolution setting
  210. self.send_data (Xstart//256)
  211. self.send_data (Xstart%256) #x-start
  212. self.send_data ((Xend-1)//256)
  213. self.send_data ((Xend-1)%256) #x-end
  214. self.send_data (Ystart//256) #
  215. self.send_data (Ystart%256) #y-start
  216. self.send_data ((Yend-1)//256)
  217. self.send_data ((Yend-1)%256) #y-end
  218. self.send_data (0x01)
  219. image1 = [0xFF] * int(self.width * self.height / 8)
  220. for j in range(Height):
  221. for i in range(Width):
  222. image1[i + j * Width] = ~Image[i + j * Width]
  223. self.send_command(0x13) #Write Black and White image to RAM
  224. self.send_data2(image1)
  225. self.send_command(0x12)
  226. epdconfig.delay_ms(100)
  227. self.ReadBusy()
  228. def sleep(self):
  229. self.send_command(0x02) # POWER_OFF
  230. self.ReadBusy()
  231. self.send_command(0x07) # DEEP_SLEEP
  232. self.send_data(0XA5)
  233. epdconfig.delay_ms(2000)
  234. epdconfig.module_exit()
  235. ### END OF FILE ###