epd2in13_V2.py 11 KB

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