epd7in5_V2.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. Voltage_Frame_7IN5_V2 = [
  44. 0x6, 0x3F, 0x3F, 0x11, 0x24, 0x7, 0x17,
  45. ]
  46. LUT_VCOM_7IN5_V2 = [
  47. 0x0, 0xF, 0xF, 0x0, 0x0, 0x1,
  48. 0x0, 0xF, 0x1, 0xF, 0x1, 0x2,
  49. 0x0, 0xF, 0xF, 0x0, 0x0, 0x1,
  50. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  51. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  52. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  53. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  54. ]
  55. LUT_WW_7IN5_V2 = [
  56. 0x10, 0xF, 0xF, 0x0, 0x0, 0x1,
  57. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  58. 0x20, 0xF, 0xF, 0x0, 0x0, 0x1,
  59. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  60. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  61. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  62. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  63. ]
  64. LUT_BW_7IN5_V2 = [
  65. 0x10, 0xF, 0xF, 0x0, 0x0, 0x1,
  66. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  67. 0x20, 0xF, 0xF, 0x0, 0x0, 0x1,
  68. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  69. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  70. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  71. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  72. ]
  73. LUT_WB_7IN5_V2 = [
  74. 0x80, 0xF, 0xF, 0x0, 0x0, 0x1,
  75. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  76. 0x40, 0xF, 0xF, 0x0, 0x0, 0x1,
  77. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  78. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  79. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  80. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  81. ]
  82. LUT_BB_7IN5_V2 = [
  83. 0x80, 0xF, 0xF, 0x0, 0x0, 0x1,
  84. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  85. 0x40, 0xF, 0xF, 0x0, 0x0, 0x1,
  86. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  87. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  88. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  89. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  90. ]
  91. # Hardware reset
  92. def reset(self):
  93. epdconfig.digital_write(self.reset_pin, 1)
  94. epdconfig.delay_ms(20)
  95. epdconfig.digital_write(self.reset_pin, 0)
  96. epdconfig.delay_ms(2)
  97. epdconfig.digital_write(self.reset_pin, 1)
  98. epdconfig.delay_ms(20)
  99. def send_command(self, command):
  100. epdconfig.digital_write(self.dc_pin, 0)
  101. epdconfig.digital_write(self.cs_pin, 0)
  102. epdconfig.spi_writebyte([command])
  103. epdconfig.digital_write(self.cs_pin, 1)
  104. def send_data(self, data):
  105. epdconfig.digital_write(self.dc_pin, 1)
  106. epdconfig.digital_write(self.cs_pin, 0)
  107. epdconfig.spi_writebyte([data])
  108. epdconfig.digital_write(self.cs_pin, 1)
  109. def send_data2(self, data):
  110. epdconfig.digital_write(self.dc_pin, 1)
  111. epdconfig.digital_write(self.cs_pin, 0)
  112. epdconfig.SPI.writebytes2(data)
  113. epdconfig.digital_write(self.cs_pin, 1)
  114. def ReadBusy(self):
  115. logger.debug("e-Paper busy")
  116. self.send_command(0x71)
  117. busy = epdconfig.digital_read(self.busy_pin)
  118. while(busy == 0):
  119. self.send_command(0x71)
  120. busy = epdconfig.digital_read(self.busy_pin)
  121. epdconfig.delay_ms(20)
  122. logger.debug("e-Paper busy release")
  123. def SetLut(self, lut_vcom, lut_ww, lut_bw, lut_wb, lut_bb):
  124. self.send_command(0x20)
  125. for count in range(0, 42):
  126. self.send_data(lut_vcom[count])
  127. self.send_command(0x21)
  128. for count in range(0, 42):
  129. self.send_data(lut_ww[count])
  130. self.send_command(0x22)
  131. for count in range(0, 42):
  132. self.send_data(lut_bw[count])
  133. self.send_command(0x23)
  134. for count in range(0, 42):
  135. self.send_data(lut_wb[count])
  136. self.send_command(0x24)
  137. for count in range(0, 42):
  138. self.send_data(lut_bb[count])
  139. def init(self):
  140. if (epdconfig.module_init() != 0):
  141. return -1
  142. # EPD hardware init start
  143. self.reset()
  144. # self.send_command(0x06) # btst
  145. # self.send_data(0x17)
  146. # self.send_data(0x17)
  147. # self.send_data(0x28) # If an exception is displayed, try using 0x38
  148. # self.send_data(0x17)
  149. # self.send_command(0x01) #POWER SETTING
  150. # self.send_data(0x07)
  151. # self.send_data(0x07) #VGH=20V,VGL=-20V
  152. # self.send_data(0x3f) #VDH=15V
  153. # self.send_data(0x3f) #VDL=-15V
  154. self.send_command(0x01); # power setting
  155. self.send_data(0x17); # 1-0=11: internal power
  156. self.send_data(self.Voltage_Frame_7IN5_V2[6]); # VGH&VGL
  157. self.send_data(self.Voltage_Frame_7IN5_V2[1]); # VSH
  158. self.send_data(self.Voltage_Frame_7IN5_V2[2]); # VSL
  159. self.send_data(self.Voltage_Frame_7IN5_V2[3]); # VSHR
  160. self.send_command(0x82); # VCOM DC Setting
  161. self.send_data(self.Voltage_Frame_7IN5_V2[4]); # VCOM
  162. self.send_command(0x06); # Booster Setting
  163. self.send_data(0x27);
  164. self.send_data(0x27);
  165. self.send_data(0x2F);
  166. self.send_data(0x17);
  167. self.send_command(0x30); # OSC Setting
  168. self.send_data(self.Voltage_Frame_7IN5_V2[0]); # 2-0=100: N=4 ; 5-3=111: M=7 ; 3C=50Hz 3A=100HZ
  169. self.send_command(0x04) #POWER ON
  170. epdconfig.delay_ms(100)
  171. self.ReadBusy()
  172. self.send_command(0X00) #PANNEL SETTING
  173. self.send_data(0x3F) #KW-3f KWR-2F BWROTP 0f BWOTP 1f
  174. self.send_command(0x61) #tres
  175. self.send_data(0x03) #source 800
  176. self.send_data(0x20)
  177. self.send_data(0x01) #gate 480
  178. self.send_data(0xE0)
  179. self.send_command(0X15)
  180. self.send_data(0x00)
  181. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  182. self.send_data(0x10)
  183. self.send_data(0x07)
  184. self.send_command(0X60) #TCON SETTING
  185. self.send_data(0x22)
  186. self.send_command(0x65); # Resolution setting
  187. self.send_data(0x00);
  188. self.send_data(0x00); # 800*480
  189. self.send_data(0x00);
  190. self.send_data(0x00);
  191. self.SetLut(self.LUT_VCOM_7IN5_V2, self.LUT_WW_7IN5_V2, self.LUT_BW_7IN5_V2, self.LUT_WB_7IN5_V2, self.LUT_BB_7IN5_V2)
  192. # EPD hardware init end
  193. return 0
  194. def getbuffer(self, image):
  195. img = image
  196. imwidth, imheight = img.size
  197. if(imwidth == self.width and imheight == self.height):
  198. img = img.convert('1')
  199. elif(imwidth == self.height and imheight == self.width):
  200. # image has correct dimensions, but needs to be rotated
  201. img = img.rotate(90, expand=True).convert('1')
  202. else:
  203. logger.warning("Wrong image dimensions: must be " + str(self.width) + "x" + str(self.height))
  204. # return a blank buffer
  205. return [0x00] * (int(self.width/8) * self.height)
  206. buf = bytearray(img.tobytes('raw'))
  207. # The bytes need to be inverted, because in the PIL world 0=black and 1=white, but
  208. # in the e-paper world 0=white and 1=black.
  209. for i in range(len(buf)):
  210. buf[i] ^= 0xFF
  211. return buf
  212. def display(self, image):
  213. self.send_command(0x13)
  214. self.send_data2(image)
  215. self.send_command(0x12)
  216. epdconfig.delay_ms(100)
  217. self.ReadBusy()
  218. def Clear(self):
  219. buf = [0x00] * (int(self.width/8) * self.height)
  220. self.send_command(0x10)
  221. self.send_data2(buf)
  222. self.send_command(0x13)
  223. self.send_data2(buf)
  224. self.send_command(0x12)
  225. epdconfig.delay_ms(100)
  226. self.ReadBusy()
  227. def sleep(self):
  228. self.send_command(0x02) # POWER_OFF
  229. self.ReadBusy()
  230. self.send_command(0x07) # DEEP_SLEEP
  231. self.send_data(0XA5)
  232. epdconfig.delay_ms(2000)
  233. epdconfig.module_exit()
  234. ### END OF FILE ###