epd1in54_V2.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. # *****************************************************************************
  2. # * | File : epd1in54_V2.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1
  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 = 200
  33. EPD_HEIGHT = 200
  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. # waveform full refresh
  44. WF_Full_1IN54 = [
  45. 0x80, 0x48, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  46. 0x40, 0x48, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  47. 0x80, 0x48, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  48. 0x40, 0x48, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  49. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  50. 0xA, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  51. 0x8, 0x1, 0x0, 0x8, 0x1, 0x0, 0x2,
  52. 0xA, 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. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  61. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  62. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x0, 0x0, 0x0,
  63. 0x22, 0x17, 0x41, 0x0, 0x32, 0x20
  64. ]
  65. # waveform partial refresh(fast)
  66. WF_PARTIAL_1IN54_0 = [
  67. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  68. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  69. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  70. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  71. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  72. 0xF,0x0,0x0,0x0,0x0,0x0,0x0,
  73. 0x1,0x1,0x0,0x0,0x0,0x0,0x0,
  74. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  75. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  76. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  77. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  78. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  79. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  80. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  81. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  82. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  83. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  84. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  85. 0x02,0x17,0x41,0xB0,0x32,0x28,
  86. ]
  87. # waveform partial refresh(quality)
  88. WF_PARTIAL_1IN54_1 = [
  89. 0x0,0x00,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  90. 0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  91. 0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  92. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  93. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  94. 0xA,0x0,0x0,0x0,0x0,0x0,0x1,
  95. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  96. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  97. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  98. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  99. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  100. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  101. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  102. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  103. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  104. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  105. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  106. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  107. 0x22,0x17,0x41,0x0,0x32,0x20,
  108. ]
  109. # Hardware reset
  110. def reset(self):
  111. epdconfig.digital_write(self.reset_pin, 1)
  112. epdconfig.delay_ms(200)
  113. epdconfig.digital_write(self.reset_pin, 0)
  114. epdconfig.delay_ms(5)
  115. epdconfig.digital_write(self.reset_pin, 1)
  116. epdconfig.delay_ms(200)
  117. def send_command(self, command):
  118. epdconfig.digital_write(self.dc_pin, 0)
  119. epdconfig.digital_write(self.cs_pin, 0)
  120. epdconfig.spi_writebyte([command])
  121. epdconfig.digital_write(self.cs_pin, 1)
  122. def send_data(self, data):
  123. epdconfig.digital_write(self.dc_pin, 1)
  124. epdconfig.digital_write(self.cs_pin, 0)
  125. epdconfig.spi_writebyte([data])
  126. epdconfig.digital_write(self.cs_pin, 1)
  127. def ReadBusy(self):
  128. logger.debug("e-Paper busy")
  129. while(epdconfig.digital_read(self.busy_pin) == 1):
  130. epdconfig.delay_ms(20)
  131. logger.debug("e-Paper busy release")
  132. def TurnOnDisplay(self):
  133. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  134. self.send_data(0xc7)
  135. self.send_command(0x20) # MASTER_ACTIVATION
  136. self.ReadBusy()
  137. def TurnOnDisplayPart(self):
  138. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  139. self.send_data(0xcF)
  140. self.send_command(0x20) # MASTER_ACTIVATION
  141. self.ReadBusy()
  142. def lut(self, lut):
  143. self.send_command(0x32) # WRITE_LUT_REGISTER
  144. for i in range(0, len(lut)):
  145. self.send_data(lut[i])
  146. def set_lut(self, lut):
  147. self.lut(lut)
  148. self.send_command(0x3f)
  149. self.send_data(lut[153])
  150. self.send_command(0x03)
  151. self.send_data(lut[154])
  152. self.send_command(0x04)
  153. self.send_data(lut[155])
  154. self.send_data(lut[156])
  155. self.send_data(lut[157])
  156. self.send_command(0x2c)
  157. self.send_data(lut[158])
  158. def SetWindows(self, Xstart, Ystart, Xend, Yend):
  159. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  160. self.send_data((Xstart>>3) & 0xFF)
  161. self.send_data((Xend>>3) & 0xFF)
  162. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  163. self.send_data(Ystart & 0xFF)
  164. self.send_data((Ystart >> 8) & 0xFF)
  165. self.send_data(Yend & 0xFF)
  166. self.send_data((Yend >> 8) & 0xFF)
  167. def SetCursor(self, Xstart, Ystart):
  168. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  169. self.send_data(Xstart & 0xFF)
  170. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  171. self.send_data(Ystart & 0xFF)
  172. self.send_data((Ystart >> 8) & 0xFF)
  173. def init(self, isPartial):
  174. if (epdconfig.module_init() != 0):
  175. return -1
  176. if(isPartial):
  177. logger.debug("partial refresh")
  178. self.reset()
  179. self.ReadBusy()
  180. self.set_lut(self.WF_PARTIAL_1IN54_0)
  181. self.send_command(0x37)
  182. self.send_data(0x00)
  183. self.send_data(0x00)
  184. self.send_data(0x00)
  185. self.send_data(0x00)
  186. self.send_data(0x00)
  187. self.send_data(0x40)
  188. self.send_data(0x00)
  189. self.send_data(0x00)
  190. self.send_data(0x00)
  191. self.send_data(0x00)
  192. self.send_command(0x3c) # BorderWavefrom
  193. self.send_data(0x80)
  194. self.send_command(0x22)
  195. self.send_data(0xc0)
  196. self.send_command(0x20)
  197. self.ReadBusy()
  198. else:
  199. logger.debug("full refresh")
  200. # EPD hardware init start
  201. self.reset()
  202. self.ReadBusy()
  203. self.send_command(0x12) # SWRESET (software reset)
  204. self.ReadBusy()
  205. self.send_command(0x01) # DRIVER_OUTPUT_CONTROL
  206. self.send_data(0xC7) # (EPD_HEIGHT - 1) & 0xFF
  207. self.send_data(0x00) # ((EPD_HEIGHT - 1) >> 8) & 0xFF
  208. self.send_data(0x01) # GD = 0 SM = 0 TB = 0
  209. self.send_command(0x11) # data entry mode
  210. self.send_data(0x01)
  211. self.SetWindows(0, self.height-1, self.width-1, 0) # Set Windows
  212. self.send_command(0x3C) # BorderWavefrom
  213. self.send_data(0x01)
  214. self.send_command(0x18)
  215. self.send_data(0x80)
  216. self.send_command(0x22) # #Load Temperature and waveform setting.
  217. self.send_data(0XB1)
  218. self.send_command(0x20)
  219. self.SetCursor(0, self.height-1) # Set Cursor
  220. self.ReadBusy()
  221. self.set_lut(self.WF_Full_1IN54) # Set lut
  222. def Clear(self, color):
  223. self.send_command(0x24)
  224. for j in range(0, self.height):
  225. for i in range(0, int(self.width / 8)):
  226. self.send_data(color)
  227. self.TurnOnDisplay()
  228. def getbuffer(self, image):
  229. buf = [0xFF] * (int(self.width/8) * self.height)
  230. image_monocolor = image.convert('1')
  231. imwidth, imheight = image_monocolor.size
  232. pixels = image_monocolor.load()
  233. if(imwidth == self.width and imheight == self.height):
  234. logger.debug("Horizontal")
  235. for y in range(imheight):
  236. for x in range(imwidth):
  237. # Set the bits for the column of pixels at the current position.
  238. if pixels[x, y] == 0:
  239. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  240. elif(imwidth == self.height and imheight == self.width):
  241. logger.debug("Vertical")
  242. for y in range(imheight):
  243. for x in range(imwidth):
  244. newx = y
  245. newy = self.height - x - 1
  246. if pixels[x, y] == 0:
  247. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  248. return buf
  249. def display(self, image):
  250. if (image == None):
  251. return
  252. self.send_command(0x24)
  253. for j in range(0, self.height):
  254. for i in range(0, int(self.width / 8)):
  255. self.send_data(image[i + j * int(self.width / 8)])
  256. self.TurnOnDisplay()
  257. def displayPartBaseImage(self, image):
  258. if (image == None):
  259. return
  260. self.send_command(0x24)
  261. for j in range(0, self.height):
  262. for i in range(0, int(self.width / 8)):
  263. self.send_data(image[i + j * int(self.width / 8)])
  264. self.send_command(0x26)
  265. for j in range(0, self.height):
  266. for i in range(0, int(self.width / 8)):
  267. self.send_data(image[i + j * int(self.width / 8)])
  268. self.TurnOnDisplayPart()
  269. def displayPart(self, image):
  270. if (image == None):
  271. return
  272. self.send_command(0x24)
  273. for j in range(0, self.height):
  274. for i in range(0, int(self.width / 8)):
  275. self.send_data(image[i + j * int(self.width / 8)])
  276. self.TurnOnDisplayPart()
  277. def sleep(self):
  278. self.send_command(0x10) # DEEP_SLEEP_MODE
  279. self.send_data(0x01)
  280. epdconfig.delay_ms(2000)
  281. epdconfig.module_exit()
  282. ### END OF FILE ###