epd10in85g.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 = 1360//2
  35. EPD_HEIGHT = 480
  36. class EPD():
  37. def __init__(self):
  38. self.width = EPD_WIDTH
  39. self.height = EPD_HEIGHT
  40. self.BLACK = 0x000000 # 00 BGR
  41. self.WHITE = 0xffffff # 01
  42. self.YELLOW = 0x00ffff # 10
  43. self.RED = 0x0000ff # 11
  44. self.EPD_CS_M_PIN = epdconfig.EPD_CS_M_PIN
  45. self.EPD_CS_S_PIN = epdconfig.EPD_CS_S_PIN
  46. self.EPD_DC_PIN = epdconfig.EPD_DC_PIN
  47. self.EPD_RST_PIN = epdconfig.EPD_RST_PIN
  48. self.EPD_BUSY_PIN = epdconfig.EPD_BUSY_PIN
  49. self.EPD_PWR_PIN = epdconfig.EPD_PWR_PIN
  50. def Reset(self):
  51. epdconfig.digital_write(self.EPD_RST_PIN, 1)
  52. epdconfig.delay_ms(200)
  53. epdconfig.digital_write(self.EPD_RST_PIN, 0) # module reset
  54. epdconfig.delay_ms(2)
  55. epdconfig.digital_write(self.EPD_RST_PIN, 1)
  56. epdconfig.delay_ms(200)
  57. def CS_ALL(self, Value):
  58. epdconfig.digital_write(self.EPD_CS_M_PIN, Value)
  59. epdconfig.digital_write(self.EPD_CS_S_PIN, Value)
  60. def SendCommand(self, Command):
  61. epdconfig.digital_write(self.EPD_DC_PIN, 0)
  62. epdconfig.spi_writebyte(Command)
  63. def SendData(self, Data):
  64. epdconfig.digital_write(self.EPD_DC_PIN, 1)
  65. epdconfig.spi_writebyte(Data)
  66. def SendData2(self, buf, Len):
  67. epdconfig.digital_write(self.EPD_DC_PIN, 1)
  68. epdconfig.spi_writebyte2(buf, Len)
  69. def ReadBusyH(self):
  70. print("e-Paper busy H")
  71. epdconfig.delay_ms(100)
  72. while(epdconfig.digital_read(self.EPD_BUSY_PIN) == 0): # 0: idle, 1: busy
  73. epdconfig.delay_ms(20)
  74. print("e-Paper busy release")
  75. def TurnOnDisplay(self):
  76. self.CS_ALL(0)
  77. self.SendCommand(0x12)
  78. self.SendData(0x00)
  79. self.CS_ALL(1)
  80. self.ReadBusyH()
  81. def Init(self):
  82. print("EPD init...")
  83. epdconfig.module_init()
  84. self.Reset()
  85. self.ReadBusyH()
  86. self.CS_ALL(0)
  87. self.SendCommand(0x4D)
  88. self.SendData(0x78)
  89. self.SendCommand(0x00)
  90. self.SendData(0x2F)
  91. self.SendData(0x29)
  92. self.SendCommand(0x06)
  93. self.SendData(0x0d)
  94. self.SendData(0x12)
  95. self.SendData(0x30)
  96. self.SendData(0x20)
  97. self.SendData(0x19)
  98. self.SendData(0x3D)
  99. self.SendData(0x0C)
  100. self.SendCommand(0x06)
  101. self.SendData(0x0d)
  102. self.SendData(0x12)
  103. self.SendData(0x30)
  104. self.SendData(0x20)
  105. self.SendData(0x19)
  106. self.SendData(0x3D)
  107. self.SendData(0x0C)
  108. self.SendCommand(0x50)
  109. self.SendData(0x37)
  110. self.SendCommand(0x61)
  111. self.SendData(int(self.width/256))
  112. self.SendData(self.width%256)
  113. self.SendData(int(self.height/256))
  114. self.SendData(self.height%256)
  115. self.SendCommand(0x65)
  116. self.SendData(0x00)
  117. self.SendData(0x00)
  118. self.SendData(0x00)
  119. self.SendData(0x00)
  120. self.SendCommand(0xE0)
  121. self.SendData(0x01)
  122. self.SendCommand(0xE3)
  123. self.SendData(0x08)
  124. self.SendCommand(0xE5)
  125. self.SendData(0x08)
  126. self.SendCommand(0xE9)
  127. self.SendData(0x01)
  128. self.SendCommand(0x04)
  129. self.ReadBusyH()
  130. self.CS_ALL(1)
  131. def getbuffer(self, image):
  132. # Create a pallette with the 4 colors supported by the panel
  133. pal_image = Image.new("P", (1,1))
  134. pal_image.putpalette( (0,0,0, 255,255,255, 255,255,0, 255,0,0) + (0,0,0)*252)
  135. # Check if we need to rotate the image
  136. imwidth, imheight = image.size
  137. if(imwidth == self.width*2 and imheight == self.height):
  138. image_temp = image
  139. elif(imwidth == self.height and imheight == self.width*2):
  140. image_temp = image.rotate(90, expand=True)
  141. else:
  142. logger.warning("Invalid image dimensions: %d x %d, expected %d x %d" % (imwidth, imheight, self.width*2, self.height))
  143. # Convert the soruce image to the 4 colors, dithering if needed
  144. image_4color = image_temp.convert("RGB").quantize(palette=pal_image)
  145. buf_4color = bytearray(image_4color.tobytes('raw'))
  146. # into a single byte to transfer to the panel
  147. if (self.width*2) % 4 == 0 :
  148. Width = (self.width*2) // 4
  149. else :
  150. Width = (self.width*2) // 4 + 1
  151. Height = self.height
  152. buf = [0x00] * int(Width * Height)
  153. idx = 0
  154. for j in range(0, Height):
  155. for i in range(0, Width):
  156. buf[i + j * Width] = (buf_4color[idx] << 6) + (buf_4color[idx+1] << 4) + (buf_4color[idx+2] << 2) + buf_4color[idx+3]
  157. idx = idx + 4
  158. return buf
  159. def Clear(self, color=0x55):
  160. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  161. self.SendCommand(0x10)
  162. for i in range(self.height):
  163. self.SendData2([color]* int(self.width/2), int(self.width/2))
  164. self.CS_ALL(1)
  165. epdconfig.digital_write(self.EPD_CS_S_PIN, 0)
  166. self.SendCommand(0x10)
  167. for i in range(self.height):
  168. self.SendData2([color]* int(self.width/2), int(self.width/2))
  169. self.CS_ALL(1)
  170. self.TurnOnDisplay()
  171. def display(self, image):
  172. Width =int(self.width / 4)
  173. Width1 =int(self.width / 2)
  174. epdconfig.digital_write(self.EPD_CS_M_PIN, 0)
  175. self.SendCommand(0x10)
  176. for i in range(self.height):
  177. self.SendData2(image[i * Width1 : i * Width1+Width], Width)
  178. self.CS_ALL(1)
  179. epdconfig.digital_write(self.EPD_CS_S_PIN, 0)
  180. self.SendCommand(0x10)
  181. for i in range(self.height):
  182. self.SendData2(image[i * Width1+Width : i * Width1+Width1], Width)
  183. self.CS_ALL(1)
  184. self.TurnOnDisplay()
  185. def sleep(self):
  186. self.CS_ALL(0)
  187. self.SendCommand(0x02) # POWER_OFF
  188. self.SendData(0X00)
  189. epdconfig.delay_ms(100)
  190. self.SendCommand(0x07) # DEEP_SLEEP
  191. self.SendData(0XA5)
  192. self.CS_ALL(1)
  193. epdconfig.delay_ms(2000)
  194. epdconfig.module_exit()
  195. ### END OF FILE ###