epd2in13_V3.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. # *****************************************************************************
  2. # * | File : epd2in13_V3.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.1
  8. # * | Date : 2021-10-30
  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. import numpy as np
  32. # Display resolution
  33. EPD_WIDTH = 122
  34. EPD_HEIGHT = 250
  35. logger = logging.getLogger(__name__)
  36. class EPD:
  37. def __init__(self):
  38. self.reset_pin = epdconfig.RST_PIN
  39. self.dc_pin = epdconfig.DC_PIN
  40. self.busy_pin = epdconfig.BUSY_PIN
  41. self.cs_pin = epdconfig.CS_PIN
  42. self.width = EPD_WIDTH
  43. self.height = EPD_HEIGHT
  44. lut_partial_update= [
  45. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  46. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  47. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  48. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  49. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  50. 0x14,0x0,0x0,0x0,0x0,0x0,0x0,
  51. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  52. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  53. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  54. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  55. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  56. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  57. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  58. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  59. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  60. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  61. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  62. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  63. 0x22,0x17,0x41,0x00,0x32,0x36,
  64. ]
  65. lut_full_update = [
  66. 0x80, 0x4A, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  67. 0x40, 0x4A, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  68. 0x80, 0x4A, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  69. 0x40, 0x4A, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  70. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  71. 0xF, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  72. 0xF, 0x0, 0x0, 0xF, 0x0, 0x0, 0x2,
  73. 0xF, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  74. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  75. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  76. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  77. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  78. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  79. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  80. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  81. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  82. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  83. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x0, 0x0, 0x0,
  84. 0x22, 0x17, 0x41, 0x0, 0x32, 0x36,
  85. ]
  86. # Hardware reset
  87. def reset(self):
  88. epdconfig.digital_write(self.reset_pin, 1)
  89. epdconfig.delay_ms(20)
  90. epdconfig.digital_write(self.reset_pin, 0)
  91. epdconfig.delay_ms(2)
  92. epdconfig.digital_write(self.reset_pin, 1)
  93. epdconfig.delay_ms(20)
  94. def send_command(self, command):
  95. epdconfig.digital_write(self.dc_pin, 0)
  96. epdconfig.digital_write(self.cs_pin, 0)
  97. epdconfig.spi_writebyte([command])
  98. epdconfig.digital_write(self.cs_pin, 1)
  99. def send_data(self, data):
  100. epdconfig.digital_write(self.dc_pin, 1)
  101. epdconfig.digital_write(self.cs_pin, 0)
  102. epdconfig.spi_writebyte([data])
  103. epdconfig.digital_write(self.cs_pin, 1)
  104. def ReadBusy(self):
  105. logger.debug("e-Paper busy")
  106. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  107. epdconfig.delay_ms(10)
  108. logger.debug("e-Paper busy release")
  109. def TurnOnDisplay(self):
  110. self.send_command(0x22) # Display Update Control
  111. self.send_data(0xC7)
  112. self.send_command(0x20) # Activate Display Update Sequence
  113. self.ReadBusy()
  114. def TurnOnDisplayPart(self):
  115. self.send_command(0x22) # Display Update Control
  116. self.send_data(0x0f) # fast:0x0c, quality:0x0f, 0xcf
  117. self.send_command(0x20) # Activate Display Update Sequence
  118. self.ReadBusy()
  119. def Lut(self, lut):
  120. self.send_command(0x32)
  121. for i in range(0, 153):
  122. self.send_data(lut[i])
  123. self.ReadBusy()
  124. def SetLut(self, lut):
  125. self.Lut(lut)
  126. self.send_command(0x3f)
  127. self.send_data(lut[153])
  128. self.send_command(0x03); # gate voltage
  129. self.send_data(lut[154])
  130. self.send_command(0x04); # source voltage
  131. self.send_data(lut[155]) # VSH
  132. self.send_data(lut[156]) # VSH2
  133. self.send_data(lut[157]) # VSL
  134. self.send_command(0x2c); # VCOM
  135. self.send_data(lut[158])
  136. def SetWindow(self, x_start, y_start, x_end, y_end):
  137. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  138. # x point must be the multiple of 8 or the last 3 bits will be ignored
  139. self.send_data((x_start>>3) & 0xFF)
  140. self.send_data((x_end>>3) & 0xFF)
  141. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  142. self.send_data(y_start & 0xFF)
  143. self.send_data((y_start >> 8) & 0xFF)
  144. self.send_data(y_end & 0xFF)
  145. self.send_data((y_end >> 8) & 0xFF)
  146. def SetCursor(self, x, y):
  147. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  148. # x point must be the multiple of 8 or the last 3 bits will be ignored
  149. self.send_data(x & 0xFF)
  150. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  151. self.send_data(y & 0xFF)
  152. self.send_data((y >> 8) & 0xFF)
  153. def init(self):
  154. if (epdconfig.module_init() != 0):
  155. return -1
  156. # EPD hardware init start
  157. self.reset()
  158. self.ReadBusy()
  159. self.send_command(0x12) #SWRESET
  160. self.ReadBusy()
  161. self.send_command(0x01) #Driver output control
  162. self.send_data(0xf9)
  163. self.send_data(0x00)
  164. self.send_data(0x00)
  165. self.send_command(0x11) #data entry mode
  166. self.send_data(0x03)
  167. self.SetWindow(0, 0, self.width-1, self.height-1)
  168. self.SetCursor(0, 0)
  169. self.send_command(0x3c)
  170. self.send_data(0x05)
  171. self.send_command(0x21) # Display update control
  172. self.send_data(0x00)
  173. self.send_data(0x80)
  174. self.send_command(0x18)
  175. self.send_data(0x80)
  176. self.ReadBusy()
  177. self.SetLut(self.lut_full_update)
  178. return 0
  179. def getbuffer(self, image):
  180. img = image
  181. imwidth, imheight = img.size
  182. if(imwidth == self.width and imheight == self.height):
  183. img = img.convert('1')
  184. elif(imwidth == self.height and imheight == self.width):
  185. # image has correct dimensions, but needs to be rotated
  186. img = img.rotate(90, expand=True).convert('1')
  187. else:
  188. logger.warning("Wrong image dimensions: must be " + str(self.width) + "x" + str(self.height))
  189. # return a blank buffer
  190. return [0x00] * (int(self.width/8) * self.height)
  191. buf = bytearray(img.tobytes('raw'))
  192. return buf
  193. def display(self, image):
  194. if self.width%8 == 0:
  195. linewidth = int(self.width/8)
  196. else:
  197. linewidth = int(self.width/8) + 1
  198. self.send_command(0x24)
  199. for j in range(0, self.height):
  200. for i in range(0, linewidth):
  201. self.send_data(image[i + j * linewidth])
  202. self.TurnOnDisplay()
  203. def displayPartial(self, image):
  204. if self.width%8 == 0:
  205. linewidth = int(self.width/8)
  206. else:
  207. linewidth = int(self.width/8) + 1
  208. epdconfig.digital_write(self.reset_pin, 0)
  209. epdconfig.delay_ms(1)
  210. epdconfig.digital_write(self.reset_pin, 1)
  211. self.SetLut(self.lut_partial_update)
  212. self.send_command(0x37)
  213. self.send_data(0x00)
  214. self.send_data(0x00)
  215. self.send_data(0x00)
  216. self.send_data(0x00)
  217. self.send_data(0x00)
  218. self.send_data(0x40)
  219. self.send_data(0x00)
  220. self.send_data(0x00)
  221. self.send_data(0x00)
  222. self.send_data(0x00)
  223. self.send_command(0x3C) #BorderWavefrom
  224. self.send_data(0x80)
  225. self.send_command(0x22)
  226. self.send_data(0xC0)
  227. self.send_command(0x20)
  228. self.ReadBusy()
  229. self.SetWindow(0, 0, self.width - 1, self.height - 1)
  230. self.SetCursor(0, 0)
  231. self.send_command(0x24) # WRITE_RAM
  232. for j in range(0, self.height):
  233. for i in range(0, linewidth):
  234. self.send_data(image[i + j * linewidth])
  235. self.TurnOnDisplayPart()
  236. def displayPartBaseImage(self, image):
  237. if self.width%8 == 0:
  238. linewidth = int(self.width/8)
  239. else:
  240. linewidth = int(self.width/8) + 1
  241. self.send_command(0x24)
  242. for j in range(0, self.height):
  243. for i in range(0, linewidth):
  244. self.send_data(image[i + j * linewidth])
  245. self.send_command(0x26)
  246. for j in range(0, self.height):
  247. for i in range(0, linewidth):
  248. self.send_data(image[i + j * linewidth])
  249. self.TurnOnDisplay()
  250. def Clear(self, color):
  251. if self.width%8 == 0:
  252. linewidth = int(self.width/8)
  253. else:
  254. linewidth = int(self.width/8) + 1
  255. # logger.debug(linewidth)
  256. self.send_command(0x24)
  257. for j in range(0, self.height):
  258. for i in range(0, linewidth):
  259. self.send_data(color)
  260. self.TurnOnDisplay()
  261. def sleep(self):
  262. self.send_command(0x10) #enter deep sleep
  263. self.send_data(0x01)
  264. epdconfig.delay_ms(2000)
  265. epdconfig.module_exit()
  266. ### END OF FILE ###