epd2in13_V2.py 11 KB

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