epd2in13_V2.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. # *****************************************************************************
  2. # * | File : epd2in13_V2.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V4.0
  8. # * | Date : 2019-06-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 = 122
  33. EPD_HEIGHT = 250
  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. FULL_UPDATE = 0
  44. PART_UPDATE = 1
  45. lut_full_update= [
  46. 0x80,0x60,0x40,0x00,0x00,0x00,0x00, #LUT0: BB: VS 0 ~7
  47. 0x10,0x60,0x20,0x00,0x00,0x00,0x00, #LUT1: BW: VS 0 ~7
  48. 0x80,0x60,0x40,0x00,0x00,0x00,0x00, #LUT2: WB: VS 0 ~7
  49. 0x10,0x60,0x20,0x00,0x00,0x00,0x00, #LUT3: WW: VS 0 ~7
  50. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, #LUT4: VCOM: VS 0 ~7
  51. 0x03,0x03,0x00,0x00,0x02, # TP0 A~D RP0
  52. 0x09,0x09,0x00,0x00,0x02, # TP1 A~D RP1
  53. 0x03,0x03,0x00,0x00,0x02, # TP2 A~D RP2
  54. 0x00,0x00,0x00,0x00,0x00, # TP3 A~D RP3
  55. 0x00,0x00,0x00,0x00,0x00, # TP4 A~D RP4
  56. 0x00,0x00,0x00,0x00,0x00, # TP5 A~D RP5
  57. 0x00,0x00,0x00,0x00,0x00, # TP6 A~D RP6
  58. 0x15,0x41,0xA8,0x32,0x30,0x0A,
  59. ]
  60. lut_partial_update = [ #20 bytes
  61. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, #LUT0: BB: VS 0 ~7
  62. 0x80,0x00,0x00,0x00,0x00,0x00,0x00, #LUT1: BW: VS 0 ~7
  63. 0x40,0x00,0x00,0x00,0x00,0x00,0x00, #LUT2: WB: VS 0 ~7
  64. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, #LUT3: WW: VS 0 ~7
  65. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, #LUT4: VCOM: VS 0 ~7
  66. 0x0A,0x00,0x00,0x00,0x00, # TP0 A~D RP0
  67. 0x00,0x00,0x00,0x00,0x00, # TP1 A~D RP1
  68. 0x00,0x00,0x00,0x00,0x00, # TP2 A~D RP2
  69. 0x00,0x00,0x00,0x00,0x00, # TP3 A~D RP3
  70. 0x00,0x00,0x00,0x00,0x00, # TP4 A~D RP4
  71. 0x00,0x00,0x00,0x00,0x00, # TP5 A~D RP5
  72. 0x00,0x00,0x00,0x00,0x00, # TP6 A~D RP6
  73. 0x15,0x41,0xA8,0x32,0x30,0x0A,
  74. ]
  75. # Hardware reset
  76. def reset(self):
  77. epdconfig.digital_write(self.reset_pin, 1)
  78. epdconfig.delay_ms(200)
  79. epdconfig.digital_write(self.reset_pin, 0)
  80. epdconfig.delay_ms(5)
  81. epdconfig.digital_write(self.reset_pin, 1)
  82. epdconfig.delay_ms(200)
  83. def send_command(self, command):
  84. epdconfig.digital_write(self.dc_pin, 0)
  85. epdconfig.digital_write(self.cs_pin, 0)
  86. epdconfig.spi_writebyte([command])
  87. epdconfig.digital_write(self.cs_pin, 1)
  88. def send_data(self, data):
  89. epdconfig.digital_write(self.dc_pin, 1)
  90. epdconfig.digital_write(self.cs_pin, 0)
  91. epdconfig.spi_writebyte([data])
  92. epdconfig.digital_write(self.cs_pin, 1)
  93. # send a lot of data
  94. def send_data2(self, data):
  95. epdconfig.digital_write(self.dc_pin, 1)
  96. epdconfig.digital_write(self.cs_pin, 0)
  97. epdconfig.spi_writebyte2(data)
  98. epdconfig.digital_write(self.cs_pin, 1)
  99. def ReadBusy(self):
  100. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  101. epdconfig.delay_ms(100)
  102. def TurnOnDisplay(self):
  103. self.send_command(0x22)
  104. self.send_data(0xC7)
  105. self.send_command(0x20)
  106. self.ReadBusy()
  107. def TurnOnDisplayPart(self):
  108. self.send_command(0x22)
  109. self.send_data(0x0c)
  110. self.send_command(0x20)
  111. self.ReadBusy()
  112. def init(self, update):
  113. if (epdconfig.module_init() != 0):
  114. return -1
  115. # EPD hardware init start
  116. self.reset()
  117. if(update == self.FULL_UPDATE):
  118. self.ReadBusy()
  119. self.send_command(0x12) # soft reset
  120. self.ReadBusy()
  121. self.send_command(0x74) #set analog block control
  122. self.send_data(0x54)
  123. self.send_command(0x7E) #set digital block control
  124. self.send_data(0x3B)
  125. self.send_command(0x01) #Driver output control
  126. self.send_data(0xF9)
  127. self.send_data(0x00)
  128. self.send_data(0x00)
  129. self.send_command(0x11) #data entry mode
  130. self.send_data(0x01)
  131. self.send_command(0x44) #set Ram-X address start/end position
  132. self.send_data(0x00)
  133. self.send_data(0x0F) #0x0C-->(15+1)*8=128
  134. self.send_command(0x45) #set Ram-Y address start/end position
  135. self.send_data(0xF9) #0xF9-->(249+1)=250
  136. self.send_data(0x00)
  137. self.send_data(0x00)
  138. self.send_data(0x00)
  139. self.send_command(0x3C) #BorderWavefrom
  140. self.send_data(0x03)
  141. self.send_command(0x2C) #VCOM Voltage
  142. self.send_data(0x55) #
  143. self.send_command(0x03)
  144. self.send_data(self.lut_full_update[70])
  145. self.send_command(0x04) #
  146. self.send_data(self.lut_full_update[71])
  147. self.send_data(self.lut_full_update[72])
  148. self.send_data(self.lut_full_update[73])
  149. self.send_command(0x3A) #Dummy Line
  150. self.send_data(self.lut_full_update[74])
  151. self.send_command(0x3B) #Gate time
  152. self.send_data(self.lut_full_update[75])
  153. self.send_command(0x32)
  154. for count in range(70):
  155. self.send_data(self.lut_full_update[count])
  156. self.send_command(0x4E) # set RAM x address count to 0
  157. self.send_data(0x00)
  158. self.send_command(0x4F) # set RAM y address count to 0X127
  159. self.send_data(0xF9)
  160. self.send_data(0x00)
  161. self.ReadBusy()
  162. else:
  163. self.send_command(0x2C) #VCOM Voltage
  164. self.send_data(0x26)
  165. self.ReadBusy()
  166. self.send_command(0x32)
  167. for count in range(70):
  168. self.send_data(self.lut_partial_update[count])
  169. self.send_command(0x37)
  170. self.send_data(0x00)
  171. self.send_data(0x00)
  172. self.send_data(0x00)
  173. self.send_data(0x00)
  174. self.send_data(0x40)
  175. self.send_data(0x00)
  176. self.send_data(0x00)
  177. self.send_command(0x22)
  178. self.send_data(0xC0)
  179. self.send_command(0x20)
  180. self.ReadBusy()
  181. self.send_command(0x3C) #BorderWavefrom
  182. self.send_data(0x01)
  183. return 0
  184. def getbuffer(self, image):
  185. if self.width%8 == 0:
  186. linewidth = int(self.width/8)
  187. else:
  188. linewidth = int(self.width/8) + 1
  189. buf = [0xFF] * (linewidth * self.height)
  190. image_monocolor = image.convert('1')
  191. imwidth, imheight = image_monocolor.size
  192. pixels = image_monocolor.load()
  193. if(imwidth == self.width and imheight == self.height):
  194. logger.debug("Vertical")
  195. for y in range(imheight):
  196. for x in range(imwidth):
  197. if pixels[x, y] == 0:
  198. x = imwidth - x
  199. buf[int(x / 8) + y * linewidth] &= ~(0x80 >> (x % 8))
  200. elif(imwidth == self.height and imheight == self.width):
  201. logger.debug("Horizontal")
  202. for y in range(imheight):
  203. for x in range(imwidth):
  204. newx = y
  205. newy = self.height - x - 1
  206. if pixels[x, y] == 0:
  207. newy = imwidth - newy - 1
  208. buf[int(newx / 8) + newy*linewidth] &= ~(0x80 >> (y % 8))
  209. return buf
  210. def display(self, image):
  211. self.send_command(0x24)
  212. self.send_data2(image)
  213. self.TurnOnDisplay()
  214. def displayPartial(self, image):
  215. if self.width%8 == 0:
  216. linewidth = int(self.width/8)
  217. else:
  218. linewidth = int(self.width/8) + 1
  219. buf = [0x00] * self.height * linewidth
  220. for j in range(0, self.height):
  221. for i in range(0, linewidth):
  222. buf[i + j * linewidth] = ~image[i + j * linewidth]
  223. self.send_command(0x24)
  224. self.send_data2(image)
  225. self.send_command(0x26)
  226. self.send_data2(buf)
  227. self.TurnOnDisplayPart()
  228. def displayPartBaseImage(self, image):
  229. self.send_command(0x24)
  230. self.send_data2(image)
  231. self.send_command(0x26)
  232. self.send_data2(image)
  233. self.TurnOnDisplay()
  234. def Clear(self, color=0xFF):
  235. if self.width%8 == 0:
  236. linewidth = int(self.width/8)
  237. else:
  238. linewidth = int(self.width/8) + 1
  239. # logger.debug(linewidth)
  240. buf = [0x00] * self.height * linewidth
  241. for j in range(0, self.height):
  242. for i in range(0, linewidth):
  243. buf[i + j * linewidth] = color
  244. self.send_command(0x24)
  245. self.send_data2(buf)
  246. # self.send_command(0x26)
  247. # for j in range(0, self.height):
  248. # for i in range(0, linewidth):
  249. # self.send_data(color)
  250. self.TurnOnDisplay()
  251. def sleep(self):
  252. # self.send_command(0x22) #POWER OFF
  253. # self.send_data(0xC3)
  254. # self.send_command(0x20)
  255. self.send_command(0x10) #enter deep sleep
  256. self.send_data(0x03)
  257. epdconfig.delay_ms(2000)
  258. epdconfig.module_exit()
  259. ### END OF FILE ###