epd13in3E.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. # /*****************************************************************************
  2. # * | File : epd12in48.py
  3. # * | Author : Waveshare electrices
  4. # * | Function : Hardware underlying interface
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2019-11-01
  9. # * | Info :
  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 time
  30. import epdconfig
  31. import PIL
  32. from PIL import Image
  33. import io
  34. EPD_WIDTH = 1200
  35. EPD_HEIGHT = 1600
  36. class EPD():
  37. def __init__(self):
  38. self.width = EPD_WIDTH
  39. self.height = EPD_HEIGHT
  40. self.BLACK = 0x000000 # 0000 BGR
  41. self.WHITE = 0xffffff # 0001
  42. self.YELLOW = 0x00ffff # 0010
  43. self.RED = 0x0000ff # 0011
  44. self.BLUE = 0xff0000 # 0101
  45. self.GREEN = 0x00ff00 # 0110
  46. self.EPD_CS_M_PIN = epdconfig.EPD_CS_M_PIN
  47. self.EPD_CS_S_PIN = epdconfig.EPD_CS_S_PIN
  48. self.EPD_DC_PIN = epdconfig.EPD_DC_PIN
  49. self.EPD_RST_PIN = epdconfig.EPD_RST_PIN
  50. self.EPD_BUSY_PIN = epdconfig.EPD_BUSY_PIN
  51. self.EPD_PWR_PIN = epdconfig.EPD_PWR_PIN
  52. def Reset(self):
  53. epdconfig.digital_write(self.EPD_RST_PIN, 1)
  54. time.sleep(0.03)
  55. epdconfig.digital_write(self.EPD_RST_PIN, 0)
  56. time.sleep(0.03)
  57. epdconfig.digital_write(self.EPD_RST_PIN, 1)
  58. time.sleep(0.03)
  59. epdconfig.digital_write(self.EPD_RST_PIN, 0)
  60. time.sleep(0.03)
  61. epdconfig.digital_write(self.EPD_RST_PIN, 1)
  62. time.sleep(0.03)
  63. def CS_ALL(self, Value):
  64. epdconfig.digital_write(self.EPD_CS_M_PIN, Value)
  65. epdconfig.digital_write(self.EPD_CS_S_PIN, Value)
  66. def SendCommand(self, Command):
  67. epdconfig.spi_writebyte(Command)
  68. def SendData(self, Data):
  69. epdconfig.spi_writebyte(Data)
  70. def SendData2(self, buf, Len):
  71. epdconfig.spi_writebyte2(buf, Len)
  72. def ReadBusyH(self):
  73. print("e-Paper busy H")
  74. while(epdconfig.digital_read(self.EPD_BUSY_PIN) == 0): # 0: busy, 1: idle
  75. epdconfig.delay_ms(5)
  76. print("e-Paper busy H release")
  77. def TurnOnDisplay(self):
  78. print("Write PON")
  79. self.CS_ALL(0)
  80. self.SendCommand(0x04)
  81. self.CS_ALL(1)
  82. self.ReadBusyH()
  83. epdconfig.delay_ms(50)
  84. print("Write DRF")
  85. self.CS_ALL(0)
  86. self.SendCommand(0x12)
  87. self.SendData(0x00)
  88. self.CS_ALL(1)
  89. self.ReadBusyH()
  90. print("Write POF")
  91. self.CS_ALL(0)
  92. self.SendCommand(0x02)
  93. self.SendData(0x00)
  94. self.CS_ALL(1)
  95. print("Display Done!!")
  96. def Init(self):
  97. print("EPD init...")
  98. epdconfig.module_init()
  99. self.Reset()
  100. self.ReadBusyH()
  101. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  102. self.SendCommand(0x74)
  103. self.SendData(0xC0)
  104. self.SendData(0x1C)
  105. self.SendData(0x1C)
  106. self.SendData(0xCC)
  107. self.SendData(0xCC)
  108. self.SendData(0xCC)
  109. self.SendData(0x15)
  110. self.SendData(0x15)
  111. self.SendData(0x55)
  112. self.CS_ALL(1)
  113. self.CS_ALL(0)
  114. self.SendCommand(0xF0)
  115. self.SendData(0x49)
  116. self.SendData(0x55)
  117. self.SendData(0x13)
  118. self.SendData(0x5D)
  119. self.SendData(0x05)
  120. self.SendData(0x10)
  121. self.CS_ALL(1)
  122. self.CS_ALL(0)
  123. self.SendCommand(0x00)
  124. self.SendData(0xDF)
  125. self.SendData(0x69)
  126. self.CS_ALL(1)
  127. self.CS_ALL(0)
  128. self.SendCommand(0x50)
  129. self.SendData(0xF7)
  130. self.CS_ALL(1)
  131. self.CS_ALL(0)
  132. self.SendCommand(0x60)
  133. self.SendData(0x03)
  134. self.SendData(0x03)
  135. self.CS_ALL(1)
  136. self.CS_ALL(0)
  137. self.SendCommand(0x86)
  138. self.SendData(0x10)
  139. self.CS_ALL(1)
  140. self.CS_ALL(0)
  141. self.SendCommand(0xE3)
  142. self.SendData(0x22)
  143. self.CS_ALL(1)
  144. self.CS_ALL(0)
  145. self.SendCommand(0xE0)
  146. self.SendData(0x01)
  147. self.CS_ALL(1)
  148. self.CS_ALL(0)
  149. self.SendCommand(0x61)
  150. self.SendData(0x04)
  151. self.SendData(0xB0)
  152. self.SendData(0x03)
  153. self.SendData(0x20)
  154. self.CS_ALL(1)
  155. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  156. self.SendCommand(0x01)
  157. self.SendData(0x0F)
  158. self.SendData(0x00)
  159. self.SendData(0x28)
  160. self.SendData(0x2C)
  161. self.SendData(0x28)
  162. self.SendData(0x38)
  163. self.CS_ALL(1)
  164. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  165. self.SendCommand(0xB6)
  166. self.SendData(0x07)
  167. self.CS_ALL(1)
  168. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  169. self.SendCommand(0x06)
  170. self.SendData(0xE8)
  171. self.SendData(0x28)
  172. self.CS_ALL(1)
  173. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  174. self.SendCommand(0xB7)
  175. self.SendData(0x01)
  176. self.CS_ALL(1)
  177. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  178. self.SendCommand(0x05)
  179. self.SendData(0xE8)
  180. self.SendData(0x28)
  181. self.CS_ALL(1)
  182. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  183. self.SendCommand(0xB0)
  184. self.SendData(0x01)
  185. self.CS_ALL(1)
  186. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  187. self.SendCommand(0xB1)
  188. self.SendData(0x02)
  189. self.CS_ALL(1)
  190. def getbuffer(self, image):
  191. # Create a pallette with the 7 colors supported by the panel
  192. pal_image = Image.new("P", (1,1))
  193. pal_image.putpalette( (0,0,0, 255,255,255, 255,255,0, 255,0,0, 0,0,0, 0,0,255, 0,255,0) + (0,0,0)*249)
  194. # pal_image.putpalette( (0,0,0, 255,255,255, 0,255,0, 0,0,255, 255,0,0, 255,255,0, 255,128,0) + (0,0,0)*249)
  195. # Check if we need to rotate the image
  196. imwidth, imheight = image.size
  197. if(imwidth == self.width and imheight == self.height):
  198. image_temp = image
  199. elif(imwidth == self.height and imheight == self.width):
  200. image_temp = image.rotate(90, expand=True)
  201. else:
  202. print("Invalid image dimensions: %d x %d, expected %d x %d" % (imwidth, imheight, self.width, self.height))
  203. # Convert the soruce image to the 7 colors, dithering if needed
  204. image_7color = image_temp.convert("RGB").quantize(palette=pal_image)
  205. buf_7color = bytearray(image_7color.tobytes('raw'))
  206. # PIL does not support 4 bit color, so pack the 4 bits of color
  207. # into a single byte to transfer to the panel
  208. buf = [0x00] * int(self.width * self.height / 2)
  209. idx = 0
  210. for i in range(0, len(buf_7color), 2):
  211. buf[idx] = (buf_7color[i] << 4) + buf_7color[i+1]
  212. idx += 1
  213. return buf
  214. def Clear(self, color=0x11):
  215. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  216. self.SendCommand(0x10)
  217. for i in range(self.height):
  218. self.SendData2([color]* int(self.width/2), int(self.width/2))
  219. self.CS_ALL(1)
  220. epdconfig.digital_write(self.EPD_CS_S_PIN, 0)
  221. self.SendCommand(0x10)
  222. for i in range(self.height):
  223. self.SendData2([color]* int(self.width/2), int(self.width/2))
  224. self.CS_ALL(1)
  225. self.TurnOnDisplay()
  226. def display(self, image):
  227. Width =int(self.width / 4)
  228. Width1 =int(self.width / 2)
  229. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  230. self.SendCommand(0x10)
  231. for i in range(self.height):
  232. self.SendData2(image[i * Width1 : i * Width1+Width], Width)
  233. self.CS_ALL(1)
  234. epdconfig.digital_write(self.EPD_CS_S_PIN, 0)
  235. self.SendCommand(0x10)
  236. for i in range(self.height):
  237. self.SendData2(image[i * Width1+Width : i * Width1+Width1], Width)
  238. self.CS_ALL(1)
  239. self.TurnOnDisplay()
  240. def sleep(self):
  241. self.CS_ALL(0)
  242. self.SendCommand(0x07)
  243. self.SendData(0XA5)
  244. self.CS_ALL(1)
  245. epdconfig.delay_ms(2000)
  246. epdconfig.module_exit()
  247. ### END OF FILE ###