2
0

epd13in3b.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. # *****************************************************************************
  2. # * | File : epd13in3b.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2024-04-08
  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 = 960
  33. EPD_HEIGHT = 680
  34. GRAY1 = 0xff #white
  35. GRAY2 = 0xC0
  36. GRAY3 = 0x80 #gray
  37. GRAY4 = 0x00 #Blackest
  38. logger = logging.getLogger(__name__)
  39. class EPD:
  40. def __init__(self):
  41. self.reset_pin = epdconfig.RST_PIN
  42. self.dc_pin = epdconfig.DC_PIN
  43. self.busy_pin = epdconfig.BUSY_PIN
  44. self.cs_pin = epdconfig.CS_PIN
  45. self.width = EPD_WIDTH
  46. self.height = EPD_HEIGHT
  47. if (epdconfig.module_init() != 0):
  48. return -1
  49. # Hardware reset
  50. def reset(self):
  51. epdconfig.digital_write(self.reset_pin, 1)
  52. epdconfig.delay_ms(20)
  53. epdconfig.digital_write(self.reset_pin, 0)
  54. epdconfig.delay_ms(2)
  55. epdconfig.digital_write(self.reset_pin, 1)
  56. epdconfig.delay_ms(20)
  57. def send_command(self, command):
  58. epdconfig.digital_write(self.dc_pin, 0)
  59. epdconfig.digital_write(self.cs_pin, 0)
  60. epdconfig.spi_writebyte([command])
  61. epdconfig.digital_write(self.cs_pin, 1)
  62. def send_data(self, data):
  63. epdconfig.digital_write(self.dc_pin, 1)
  64. epdconfig.digital_write(self.cs_pin, 0)
  65. epdconfig.spi_writebyte([data])
  66. epdconfig.digital_write(self.cs_pin, 1)
  67. def send_data2(self, data):
  68. epdconfig.digital_write(self.dc_pin, 1)
  69. epdconfig.digital_write(self.cs_pin, 0)
  70. epdconfig.SPI.writebytes2(data)
  71. epdconfig.digital_write(self.cs_pin, 1)
  72. def ReadBusy(self):
  73. logger.debug("e-Paper busy")
  74. busy = epdconfig.digital_read(self.busy_pin)
  75. while(busy == 1):
  76. busy = epdconfig.digital_read(self.busy_pin)
  77. epdconfig.delay_ms(20)
  78. epdconfig.delay_ms(20)
  79. logger.debug("e-Paper busy release")
  80. def TurnOnDisplay(self):
  81. self.send_command(0x22) #Display Update Control
  82. self.send_data(0xF7)
  83. self.send_command(0x20) #Activate Display Update Sequence
  84. self.ReadBusy()
  85. def TurnOnDisplay_Part(self):
  86. self.send_command(0x22) #Display Update Control
  87. self.send_data(0xFF)
  88. self.send_command(0x20) #Activate Display Update Sequence
  89. self.ReadBusy()
  90. def init(self):
  91. # EPD hardware init start
  92. self.reset()
  93. self.ReadBusy()
  94. self.send_command(0x12) #SWRESET
  95. self.ReadBusy()
  96. self.send_command(0x0C)
  97. self.send_data(0xAE)
  98. self.send_data(0xC7)
  99. self.send_data(0xC3)
  100. self.send_data(0xC0)
  101. self.send_data(0x80)
  102. self.send_command(0x01)
  103. self.send_data(0xA7)
  104. self.send_data(0x02)
  105. self.send_data(0x00)
  106. self.send_command(0x11)
  107. self.send_data(0x03)
  108. self.send_command(0x44)
  109. self.send_data(0x00)
  110. self.send_data(0x00)
  111. self.send_data(0xBF)
  112. self.send_data(0x03)
  113. self.send_command(0x45)
  114. self.send_data(0x00)
  115. self.send_data(0x00)
  116. self.send_data(0xA7)
  117. self.send_data(0x02)
  118. self.send_command(0x3C)
  119. self.send_data(0x01)
  120. self.send_command(0x18)
  121. self.send_data(0x80)
  122. self.send_command(0x4E)
  123. self.send_data(0x00)
  124. self.send_data(0x00)
  125. self.send_command(0x4F)
  126. self.send_data(0x00)
  127. self.send_data(0x00)
  128. self.ReadBusy()
  129. # EPD hardware init end
  130. return 0
  131. def getbuffer(self, image):
  132. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  133. buf = [0xFF] * (int(self.width / 8) * self.height)
  134. image_monocolor = image.convert('1')
  135. imwidth, imheight = image_monocolor.size
  136. pixels = image_monocolor.load()
  137. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  138. if imwidth == self.width and imheight == self.height:
  139. logger.debug("Horizontal")
  140. for y in range(imheight):
  141. for x in range(imwidth):
  142. # Set the bits for the column of pixels at the current position.
  143. if pixels[x, y] == 0:
  144. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  145. elif imwidth == self.height and imheight == self.width:
  146. logger.debug("Vertical")
  147. for y in range(imheight):
  148. for x in range(imwidth):
  149. newx = y
  150. newy = self.height - x - 1
  151. if pixels[x, y] == 0:
  152. buf[int((newx + newy * self.width) / 8)] &= ~(0x80 >> (y % 8))
  153. return buf
  154. def Clear(self):
  155. self.send_command(0x24)
  156. self.send_data2([0xFF] * (int(self.width/8) * self.height))
  157. self.send_command(0x26)
  158. self.send_data2([0x00] * (int(self.width/8) * self.height))
  159. self.TurnOnDisplay()
  160. def Clear_Base(self):
  161. self.send_command(0x24)
  162. self.send_data2([0xFF] * (int(self.width/8) * self.height))
  163. self.send_command(0x26)
  164. self.send_data2([0x00] * (int(self.width/8) * self.height))
  165. self.TurnOnDisplay()
  166. self.send_command(0x26)
  167. self.send_data2([0xFF] * (int(self.width/8) * self.height))
  168. def display(self, blackimage, ryimage):
  169. if(self.width % 8 == 0):
  170. Width = self.width // 8
  171. else:
  172. Width = self.width // 8 +1
  173. Height = self.height
  174. if (blackimage != None):
  175. self.send_command(0x24)
  176. self.send_data2(blackimage)
  177. if (ryimage != None):
  178. for j in range(Height):
  179. for i in range(Width):
  180. ryimage[i + j * Width] = ~ryimage[i + j * Width]
  181. self.send_command(0x26)
  182. self.send_data2(ryimage)
  183. self.TurnOnDisplay()
  184. def display_Base(self, blackimage, ryimage):
  185. if(self.width % 8 == 0):
  186. Width = self.width // 8
  187. else:
  188. Width = self.width // 8 +1
  189. Height = self.height
  190. if (blackimage != None):
  191. self.send_command(0x24)
  192. self.send_data2(blackimage)
  193. if (ryimage != None):
  194. for j in range(Height):
  195. for i in range(Width):
  196. ryimage[i + j * Width] = ~ryimage[i + j * Width]
  197. self.send_command(0x26)
  198. self.send_data2(ryimage)
  199. self.TurnOnDisplay()
  200. self.send_command(0x26)
  201. self.send_data2(blackimage)
  202. def display_Partial(self, Image, Xstart, Ystart, Xend, Yend):
  203. if((Xstart % 8 + Xend % 8 == 8 & Xstart % 8 > Xend % 8) | Xstart % 8 + Xend % 8 == 0 | (Xend - Xstart)%8 == 0):
  204. Xstart = Xstart // 8
  205. Xend = Xend // 8
  206. else:
  207. Xstart = Xstart // 8
  208. if Xend % 8 == 0:
  209. Xend = Xend // 8
  210. else:
  211. Xend = Xend // 8 + 1
  212. if(self.width % 8 == 0):
  213. Width = self.width // 8
  214. else:
  215. Width = self.width // 8 +1
  216. Height = self.height
  217. Xend -= 1
  218. Yend -= 1
  219. self.send_command(0x3C)
  220. self.send_data(0x80)
  221. self.send_command(0x44)
  222. self.send_data((Xstart*8) & 0xff)
  223. self.send_data((Xstart>>5) & 0x01)
  224. self.send_data((Xend*8) & 0xff)
  225. self.send_data((Xend>>5) & 0x01)
  226. self.send_command(0x45)
  227. self.send_data(Ystart & 0xff)
  228. self.send_data((Ystart>>8) & 0x01)
  229. self.send_data(Yend & 0xff)
  230. self.send_data((Yend>>8) & 0x01)
  231. self.send_command(0x4E)
  232. self.send_data((Xstart*8) & 0xff)
  233. self.send_data((Xstart>>5) & 0x01)
  234. self.send_command(0x4F)
  235. self.send_data(Ystart & 0xff)
  236. self.send_data((Ystart>>8) & 0x01)
  237. self.send_command(0x24)
  238. for j in range(Height):
  239. for i in range(Width):
  240. if((j > Ystart-1) & (j < (Yend + 1)) & (i > Xstart-1) & (i < (Xend + 1))):
  241. self.send_data(Image[i + j * Width])
  242. self.TurnOnDisplay_Part()
  243. self.send_command(0x26)
  244. for j in range(Height):
  245. for i in range(Width):
  246. if((j > Ystart-1) & (j < (Yend + 1)) & (i > Xstart-1) & (i < (Xend + 1))):
  247. self.send_data(Image[i + j * Width])
  248. def sleep(self):
  249. self.send_command(0x10) # DEEP_SLEEP
  250. self.send_data(0x03)
  251. epdconfig.delay_ms(2000)
  252. epdconfig.module_exit()
  253. ### END OF FILE ###