epd2in9_V2.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # *****************************************************************************
  2. # * | File : epd2in9_V2.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2020-10-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 = 128
  33. EPD_HEIGHT = 296
  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. WF_PARTIAL_2IN9 = [
  44. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  45. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  46. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  47. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  48. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  49. 0x0A,0x0,0x0,0x0,0x0,0x0,0x1,
  50. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  51. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  52. 0x0,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. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  62. 0x22,0x17,0x41,0xB0,0x32,0x36,
  63. ]
  64. WS_20_30 = [
  65. 0x80, 0x66, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0,
  66. 0x10, 0x66, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0,
  67. 0x80, 0x66, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0,
  68. 0x10, 0x66, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0,
  69. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  70. 0x14, 0x8, 0x0, 0x0, 0x0, 0x0, 0x2,
  71. 0xA, 0xA, 0x0, 0xA, 0xA, 0x0, 0x1,
  72. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  73. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  74. 0x0, 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. 0x14, 0x8, 0x0, 0x1, 0x0, 0x0, 0x1,
  79. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1,
  80. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  81. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  82. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x0, 0x0, 0x0,
  83. 0x22, 0x17, 0x41, 0x0, 0x32, 0x36
  84. ]
  85. # Hardware reset
  86. def reset(self):
  87. epdconfig.digital_write(self.reset_pin, 1)
  88. epdconfig.delay_ms(50)
  89. epdconfig.digital_write(self.reset_pin, 0)
  90. epdconfig.delay_ms(2)
  91. epdconfig.digital_write(self.reset_pin, 1)
  92. epdconfig.delay_ms(50)
  93. def send_command(self, command):
  94. epdconfig.digital_write(self.dc_pin, 0)
  95. epdconfig.digital_write(self.cs_pin, 0)
  96. epdconfig.spi_writebyte([command])
  97. epdconfig.digital_write(self.cs_pin, 1)
  98. def send_data(self, data):
  99. epdconfig.digital_write(self.dc_pin, 1)
  100. epdconfig.digital_write(self.cs_pin, 0)
  101. epdconfig.spi_writebyte([data])
  102. epdconfig.digital_write(self.cs_pin, 1)
  103. def ReadBusy(self):
  104. logger.debug("e-Paper busy")
  105. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  106. epdconfig.delay_ms(10)
  107. logger.debug("e-Paper busy release")
  108. def TurnOnDisplay(self):
  109. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  110. self.send_data(0xc7)
  111. self.send_command(0x20) # MASTER_ACTIVATION
  112. self.ReadBusy()
  113. def TurnOnDisplay_Partial(self):
  114. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  115. self.send_data(0x0F)
  116. self.send_command(0x20) # MASTER_ACTIVATION
  117. self.ReadBusy()
  118. def lut(self, lut):
  119. self.send_command(0x32)
  120. for i in range(0, 153):
  121. self.send_data(lut[i])
  122. self.ReadBusy()
  123. def SetLut(self, lut):
  124. self.lut(lut)
  125. self.send_command(0x3f)
  126. self.send_data(lut[153])
  127. self.send_command(0x03); # gate voltage
  128. self.send_data(lut[154])
  129. self.send_command(0x04); # source voltage
  130. self.send_data(lut[155]) # VSH
  131. self.send_data(lut[156]) # VSH2
  132. self.send_data(lut[157]) # VSL
  133. self.send_command(0x2c); # VCOM
  134. self.send_data(lut[158])
  135. def SetWindow(self, x_start, y_start, x_end, y_end):
  136. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  137. # x point must be the multiple of 8 or the last 3 bits will be ignored
  138. self.send_data((x_start>>3) & 0xFF)
  139. self.send_data((x_end>>3) & 0xFF)
  140. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  141. self.send_data(y_start & 0xFF)
  142. self.send_data((y_start >> 8) & 0xFF)
  143. self.send_data(y_end & 0xFF)
  144. self.send_data((y_end >> 8) & 0xFF)
  145. def SetCursor(self, x, y):
  146. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  147. # x point must be the multiple of 8 or the last 3 bits will be ignored
  148. self.send_data(x & 0xFF)
  149. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  150. self.send_data(y & 0xFF)
  151. self.send_data((y >> 8) & 0xFF)
  152. def init(self):
  153. if (epdconfig.module_init() != 0):
  154. return -1
  155. # EPD hardware init start
  156. self.reset()
  157. self.ReadBusy()
  158. self.send_command(0x12) #SWRESET
  159. self.ReadBusy()
  160. self.send_command(0x01) #Driver output control
  161. self.send_data(0x27)
  162. self.send_data(0x01)
  163. self.send_data(0x00)
  164. self.send_command(0x11) #data entry mode
  165. self.send_data(0x03)
  166. self.SetWindow(0, 0, self.width-1, self.height-1)
  167. self.send_command(0x21) # Display update control
  168. self.send_data(0x00)
  169. self.send_data(0x80)
  170. self.SetCursor(0, 0)
  171. self.ReadBusy()
  172. self.SetLut(self.WS_20_30)
  173. # EPD hardware init end
  174. return 0
  175. def getbuffer(self, image):
  176. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  177. buf = [0xFF] * (int(self.width/8) * self.height)
  178. image_monocolor = image.convert('1')
  179. imwidth, imheight = image_monocolor.size
  180. pixels = image_monocolor.load()
  181. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  182. if(imwidth == self.width and imheight == self.height):
  183. logger.debug("Vertical")
  184. for y in range(imheight):
  185. for x in range(imwidth):
  186. # Set the bits for the column of pixels at the current position.
  187. if pixels[x, y] == 0:
  188. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  189. elif(imwidth == self.height and imheight == self.width):
  190. logger.debug("Horizontal")
  191. for y in range(imheight):
  192. for x in range(imwidth):
  193. newx = y
  194. newy = self.height - x - 1
  195. if pixels[x, y] == 0:
  196. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  197. return buf
  198. def display(self, image):
  199. if (image == None):
  200. return
  201. self.send_command(0x24) # WRITE_RAM
  202. for j in range(0, self.height):
  203. for i in range(0, int(self.width / 8)):
  204. self.send_data(image[i + j * int(self.width / 8)])
  205. self.TurnOnDisplay()
  206. def display_Base(self, image):
  207. if (image == None):
  208. return
  209. self.send_command(0x24) # WRITE_RAM
  210. for j in range(0, self.height):
  211. for i in range(0, int(self.width / 8)):
  212. self.send_data(image[i + j * int(self.width / 8)])
  213. self.send_command(0x26) # WRITE_RAM
  214. for j in range(0, self.height):
  215. for i in range(0, int(self.width / 8)):
  216. self.send_data(image[i + j * int(self.width / 8)])
  217. self.TurnOnDisplay()
  218. def display_Partial(self, image):
  219. if (image == None):
  220. return
  221. epdconfig.digital_write(self.reset_pin, 0)
  222. epdconfig.delay_ms(2)
  223. epdconfig.digital_write(self.reset_pin, 1)
  224. epdconfig.delay_ms(2)
  225. self.SetLut(self.WF_PARTIAL_2IN9)
  226. self.send_command(0x37)
  227. self.send_data(0x00)
  228. self.send_data(0x00)
  229. self.send_data(0x00)
  230. self.send_data(0x00)
  231. self.send_data(0x00)
  232. self.send_data(0x40)
  233. self.send_data(0x00)
  234. self.send_data(0x00)
  235. self.send_data(0x00)
  236. self.send_data(0x00)
  237. self.send_command(0x3C) #BorderWavefrom
  238. self.send_data(0x80)
  239. self.send_command(0x22)
  240. self.send_data(0xC0)
  241. self.send_command(0x20)
  242. self.ReadBusy()
  243. self.SetWindow(0, 0, self.width - 1, self.height - 1)
  244. self.SetCursor(0, 0)
  245. self.send_command(0x24) # WRITE_RAM
  246. for j in range(0, self.height):
  247. for i in range(0, int(self.width / 8)):
  248. self.send_data(image[i + j * int(self.width / 8)])
  249. self.TurnOnDisplay_Partial()
  250. def Clear(self, color):
  251. self.send_command(0x24) # WRITE_RAM
  252. for j in range(0, self.height):
  253. for i in range(0, int(self.width / 8)):
  254. self.send_data(color)
  255. self.TurnOnDisplay()
  256. def sleep(self):
  257. self.send_command(0x10) # DEEP_SLEEP_MODE
  258. self.send_data(0x01)
  259. epdconfig.delay_ms(2000)
  260. epdconfig.module_exit()
  261. ### END OF FILE ###