epd2in13_V2.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. import numpy as np
  32. # Display resolution
  33. EPD_WIDTH = 122
  34. EPD_HEIGHT = 250
  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. def ReadBusy(self):
  94. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  95. epdconfig.delay_ms(100)
  96. def TurnOnDisplay(self):
  97. self.send_command(0x22)
  98. self.send_data(0xC7)
  99. self.send_command(0x20)
  100. self.ReadBusy()
  101. def TurnOnDisplayPart(self):
  102. self.send_command(0x22)
  103. self.send_data(0x0c)
  104. self.send_command(0x20)
  105. self.ReadBusy()
  106. def init(self, update):
  107. if (epdconfig.module_init() != 0):
  108. return -1
  109. # EPD hardware init start
  110. self.reset()
  111. if(update == self.FULL_UPDATE):
  112. self.ReadBusy()
  113. self.send_command(0x12) # soft reset
  114. self.ReadBusy()
  115. self.send_command(0x74) #set analog block control
  116. self.send_data(0x54)
  117. self.send_command(0x7E) #set digital block control
  118. self.send_data(0x3B)
  119. self.send_command(0x01) #Driver output control
  120. self.send_data(0xF9)
  121. self.send_data(0x00)
  122. self.send_data(0x00)
  123. self.send_command(0x11) #data entry mode
  124. self.send_data(0x01)
  125. self.send_command(0x44) #set Ram-X address start/end position
  126. self.send_data(0x00)
  127. self.send_data(0x0F) #0x0C-->(15+1)*8=128
  128. self.send_command(0x45) #set Ram-Y address start/end position
  129. self.send_data(0xF9) #0xF9-->(249+1)=250
  130. self.send_data(0x00)
  131. self.send_data(0x00)
  132. self.send_data(0x00)
  133. self.send_command(0x3C) #BorderWavefrom
  134. self.send_data(0x03)
  135. self.send_command(0x2C) #VCOM Voltage
  136. self.send_data(0x55) #
  137. self.send_command(0x03)
  138. self.send_data(self.lut_full_update[70])
  139. self.send_command(0x04) #
  140. self.send_data(self.lut_full_update[71])
  141. self.send_data(self.lut_full_update[72])
  142. self.send_data(self.lut_full_update[73])
  143. self.send_command(0x3A) #Dummy Line
  144. self.send_data(self.lut_full_update[74])
  145. self.send_command(0x3B) #Gate time
  146. self.send_data(self.lut_full_update[75])
  147. self.send_command(0x32)
  148. for count in range(70):
  149. self.send_data(self.lut_full_update[count])
  150. self.send_command(0x4E) # set RAM x address count to 0
  151. self.send_data(0x00)
  152. self.send_command(0x4F) # set RAM y address count to 0X127
  153. self.send_data(0xF9)
  154. self.send_data(0x00)
  155. self.ReadBusy()
  156. else:
  157. self.send_command(0x2C) #VCOM Voltage
  158. self.send_data(0x26)
  159. self.ReadBusy()
  160. self.send_command(0x32)
  161. for count in range(70):
  162. self.send_data(self.lut_partial_update[count])
  163. self.send_command(0x37)
  164. self.send_data(0x00)
  165. self.send_data(0x00)
  166. self.send_data(0x00)
  167. self.send_data(0x00)
  168. self.send_data(0x40)
  169. self.send_data(0x00)
  170. self.send_data(0x00)
  171. self.send_command(0x22)
  172. self.send_data(0xC0)
  173. self.send_command(0x20)
  174. self.ReadBusy()
  175. self.send_command(0x3C) #BorderWavefrom
  176. self.send_data(0x01)
  177. return 0
  178. def getbuffer(self, image):
  179. if self.width%8 == 0:
  180. linewidth = int(self.width/8)
  181. else:
  182. linewidth = int(self.width/8) + 1
  183. buf = [0xFF] * (linewidth * self.height)
  184. image_monocolor = image.convert('1')
  185. imwidth, imheight = image_monocolor.size
  186. pixels = image_monocolor.load()
  187. if(imwidth == self.width and imheight == self.height):
  188. logging.debug("Vertical")
  189. for y in range(imheight):
  190. for x in range(imwidth):
  191. if pixels[x, y] == 0:
  192. x = imwidth - x
  193. buf[int(x / 8) + y * linewidth] &= ~(0x80 >> (x % 8))
  194. elif(imwidth == self.height and imheight == self.width):
  195. logging.debug("Horizontal")
  196. for y in range(imheight):
  197. for x in range(imwidth):
  198. newx = y
  199. newy = self.height - x - 1
  200. if pixels[x, y] == 0:
  201. newy = imwidth - newy - 1
  202. buf[int(newx / 8) + newy*linewidth] &= ~(0x80 >> (y % 8))
  203. return buf
  204. def display(self, image):
  205. if self.width%8 == 0:
  206. linewidth = int(self.width/8)
  207. else:
  208. linewidth = int(self.width/8) + 1
  209. self.send_command(0x24)
  210. for j in range(0, self.height):
  211. for i in range(0, linewidth):
  212. self.send_data(image[i + j * linewidth])
  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. self.send_command(0x24)
  220. for j in range(0, self.height):
  221. for i in range(0, linewidth):
  222. self.send_data(image[i + j * linewidth])
  223. self.send_command(0x26)
  224. for j in range(0, self.height):
  225. for i in range(0, linewidth):
  226. self.send_data(~image[i + j * linewidth])
  227. self.TurnOnDisplayPart()
  228. def displayPartBaseImage(self, image):
  229. if self.width%8 == 0:
  230. linewidth = int(self.width/8)
  231. else:
  232. linewidth = int(self.width/8) + 1
  233. self.send_command(0x24)
  234. for j in range(0, self.height):
  235. for i in range(0, linewidth):
  236. self.send_data(image[i + j * linewidth])
  237. self.send_command(0x26)
  238. for j in range(0, self.height):
  239. for i in range(0, linewidth):
  240. self.send_data(image[i + j * linewidth])
  241. self.TurnOnDisplay()
  242. def Clear(self, color):
  243. if self.width%8 == 0:
  244. linewidth = int(self.width/8)
  245. else:
  246. linewidth = int(self.width/8) + 1
  247. # logging.debug(linewidth)
  248. self.send_command(0x24)
  249. for j in range(0, self.height):
  250. for i in range(0, linewidth):
  251. self.send_data(color)
  252. # self.send_command(0x26)
  253. # for j in range(0, self.height):
  254. # for i in range(0, linewidth):
  255. # self.send_data(color)
  256. self.TurnOnDisplay()
  257. def sleep(self):
  258. # self.send_command(0x22) #POWER OFF
  259. # self.send_data(0xC3)
  260. # self.send_command(0x20)
  261. self.send_command(0x10) #enter deep sleep
  262. self.send_data(0x03)
  263. epdconfig.delay_ms(2000)
  264. epdconfig.module_exit()
  265. ### END OF FILE ###