epd2in9_V2.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. class EPD:
  35. def __init__(self):
  36. self.reset_pin = epdconfig.RST_PIN
  37. self.dc_pin = epdconfig.DC_PIN
  38. self.busy_pin = epdconfig.BUSY_PIN
  39. self.cs_pin = epdconfig.CS_PIN
  40. self.width = EPD_WIDTH
  41. self.height = EPD_HEIGHT
  42. WF_PARTIAL_2IN9 = [
  43. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  44. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  45. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  46. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  47. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  48. 0x0A,0x0,0x0,0x0,0x0,0x0,0x2,
  49. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  50. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  51. 0x0,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. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  61. 0x22,0x17,0x41,0xB0,0x32,0x36,
  62. ]
  63. # Hardware reset
  64. def reset(self):
  65. epdconfig.digital_write(self.reset_pin, 1)
  66. epdconfig.delay_ms(50)
  67. epdconfig.digital_write(self.reset_pin, 0)
  68. epdconfig.delay_ms(2)
  69. epdconfig.digital_write(self.reset_pin, 1)
  70. epdconfig.delay_ms(50)
  71. def send_command(self, command):
  72. epdconfig.digital_write(self.dc_pin, 0)
  73. epdconfig.digital_write(self.cs_pin, 0)
  74. epdconfig.spi_writebyte([command])
  75. epdconfig.digital_write(self.cs_pin, 1)
  76. def send_data(self, data):
  77. epdconfig.digital_write(self.dc_pin, 1)
  78. epdconfig.digital_write(self.cs_pin, 0)
  79. epdconfig.spi_writebyte([data])
  80. epdconfig.digital_write(self.cs_pin, 1)
  81. def ReadBusy(self):
  82. logging.debug("e-Paper busy")
  83. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  84. epdconfig.delay_ms(10)
  85. logging.debug("e-Paper busy release")
  86. def TurnOnDisplay(self):
  87. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  88. self.send_data(0xF7)
  89. self.send_command(0x20) # MASTER_ACTIVATION
  90. self.ReadBusy()
  91. def TurnOnDisplay_Partial(self):
  92. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  93. self.send_data(0x0F)
  94. self.send_command(0x20) # MASTER_ACTIVATION
  95. self.ReadBusy()
  96. def SendLut(self):
  97. self.send_command(0x32)
  98. for i in range(0, 153):
  99. self.send_data(self.WF_PARTIAL_2IN9[i])
  100. self.ReadBusy()
  101. def SetWindow(self, x_start, y_start, x_end, y_end):
  102. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  103. # x point must be the multiple of 8 or the last 3 bits will be ignored
  104. self.send_data((x_start>>3) & 0xFF)
  105. self.send_data((x_end>>3) & 0xFF)
  106. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  107. self.send_data(y_start & 0xFF)
  108. self.send_data((y_start >> 8) & 0xFF)
  109. self.send_data(y_end & 0xFF)
  110. self.send_data((y_end >> 8) & 0xFF)
  111. def SetCursor(self, x, y):
  112. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  113. # x point must be the multiple of 8 or the last 3 bits will be ignored
  114. self.send_data(x & 0xFF)
  115. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  116. self.send_data(y & 0xFF)
  117. self.send_data((y >> 8) & 0xFF)
  118. self.ReadBusy()
  119. def init(self):
  120. if (epdconfig.module_init() != 0):
  121. return -1
  122. # EPD hardware init start
  123. self.reset()
  124. self.ReadBusy();
  125. self.send_command(0x12); #SWRESET
  126. self.ReadBusy();
  127. self.send_command(0x01); #Driver output control
  128. self.send_data(0x27);
  129. self.send_data(0x01);
  130. self.send_data(0x00);
  131. self.send_command(0x11); #data entry mode
  132. self.send_data(0x03);
  133. self.SetWindow(0, 0, self.width-1, self.height-1);
  134. self.send_command(0x21); # Display update control
  135. self.send_data(0x00);
  136. self.send_data(0x80);
  137. self.SetCursor(0, 0);
  138. self.ReadBusy();
  139. # EPD hardware init end
  140. return 0
  141. def getbuffer(self, image):
  142. # logging.debug("bufsiz = ",int(self.width/8) * self.height)
  143. buf = [0xFF] * (int(self.width/8) * self.height)
  144. image_monocolor = image.convert('1')
  145. imwidth, imheight = image_monocolor.size
  146. pixels = image_monocolor.load()
  147. # logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  148. if(imwidth == self.width and imheight == self.height):
  149. logging.debug("Vertical")
  150. for y in range(imheight):
  151. for x in range(imwidth):
  152. # Set the bits for the column of pixels at the current position.
  153. if pixels[x, y] == 0:
  154. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  155. elif(imwidth == self.height and imheight == self.width):
  156. logging.debug("Horizontal")
  157. for y in range(imheight):
  158. for x in range(imwidth):
  159. newx = y
  160. newy = self.height - x - 1
  161. if pixels[x, y] == 0:
  162. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  163. return buf
  164. def display(self, image):
  165. if (image == None):
  166. return
  167. self.send_command(0x24) # WRITE_RAM
  168. for j in range(0, self.height):
  169. for i in range(0, int(self.width / 8)):
  170. self.send_data(image[i + j * int(self.width / 8)])
  171. self.TurnOnDisplay()
  172. def display_Base(self, image):
  173. if (image == None):
  174. return
  175. self.send_command(0x24) # WRITE_RAM
  176. for j in range(0, self.height):
  177. for i in range(0, int(self.width / 8)):
  178. self.send_data(image[i + j * int(self.width / 8)])
  179. self.send_command(0x26) # WRITE_RAM
  180. for j in range(0, self.height):
  181. for i in range(0, int(self.width / 8)):
  182. self.send_data(image[i + j * int(self.width / 8)])
  183. self.TurnOnDisplay()
  184. def display_Partial(self, image):
  185. if (image == None):
  186. return
  187. epdconfig.digital_write(self.reset_pin, 0)
  188. epdconfig.delay_ms(2)
  189. epdconfig.digital_write(self.reset_pin, 1)
  190. epdconfig.delay_ms(2)
  191. self.SendLut();
  192. self.send_command(0x37);
  193. self.send_data(0x00);
  194. self.send_data(0x00);
  195. self.send_data(0x00);
  196. self.send_data(0x00);
  197. self.send_data(0x00);
  198. self.send_data(0x40);
  199. self.send_data(0x00);
  200. self.send_data(0x00);
  201. self.send_data(0x00);
  202. self.send_data(0x00);
  203. self.send_command(0x3C); #BorderWavefrom
  204. self.send_data(0x80);
  205. self.send_command(0x22);
  206. self.send_data(0xC0);
  207. self.send_command(0x20);
  208. self.ReadBusy();
  209. self.SetWindow(0, 0, self.width - 1, self.height - 1)
  210. self.SetCursor(0, 0)
  211. self.send_command(0x24) # WRITE_RAM
  212. for j in range(0, self.height):
  213. for i in range(0, int(self.width / 8)):
  214. self.send_data(image[i + j * int(self.width / 8)])
  215. self.TurnOnDisplay_Partial()
  216. def Clear(self, color):
  217. self.send_command(0x24) # WRITE_RAM
  218. for j in range(0, self.height):
  219. for i in range(0, int(self.width / 8)):
  220. self.send_data(color)
  221. self.TurnOnDisplay()
  222. def sleep(self):
  223. self.send_command(0x10) # DEEP_SLEEP_MODE
  224. self.send_data(0x01)
  225. def Dev_exit(self):
  226. epdconfig.module_exit()
  227. ### END OF FILE ###