epd2in9d.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. # *****************************************************************************
  2. # * | File : epd2in9d.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V2.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. from PIL import Image
  32. import RPi.GPIO as GPIO
  33. # Display resolution
  34. EPD_WIDTH = 128
  35. EPD_HEIGHT = 296
  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. lut_vcomDC = [
  45. 0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
  46. 0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
  47. 0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
  48. 0x00, 0x12, 0x12, 0x00, 0x00, 0x01,
  49. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00,
  53. ]
  54. lut_ww = [
  55. 0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
  56. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  57. 0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
  58. 0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. ]
  63. lut_bw = [
  64. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  65. 0x90, 0x0F, 0x0F, 0x00, 0x00, 0x03,
  66. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  67. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. ]
  72. lut_wb = [
  73. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  74. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  75. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  76. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80. ]
  81. lut_bb = [
  82. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  83. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  84. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  85. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. ]
  90. lut_vcom1 = [
  91. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  92. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  97. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  98. 0x00, 0x00,
  99. ]
  100. lut_ww1 = [
  101. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  102. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. ]
  109. lut_bw1 = [
  110. 0x80, 0x19, 0x01, 0x00, 0x00, 0x01,
  111. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  112. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  113. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  117. ]
  118. lut_wb1 = [
  119. 0x40, 0x19, 0x01, 0x00, 0x00, 0x01,
  120. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  121. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  123. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. ]
  127. lut_bb1 = [
  128. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  129. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  132. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  133. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. ]
  136. # Hardware reset
  137. def reset(self):
  138. epdconfig.digital_write(self.reset_pin, 1)
  139. epdconfig.delay_ms(200)
  140. epdconfig.digital_write(self.reset_pin, 0)
  141. epdconfig.delay_ms(5)
  142. epdconfig.digital_write(self.reset_pin, 1)
  143. epdconfig.delay_ms(200)
  144. def send_command(self, command):
  145. epdconfig.digital_write(self.dc_pin, 0)
  146. epdconfig.digital_write(self.cs_pin, 0)
  147. epdconfig.spi_writebyte([command])
  148. epdconfig.digital_write(self.cs_pin, 1)
  149. def send_data(self, data):
  150. epdconfig.digital_write(self.dc_pin, 1)
  151. epdconfig.digital_write(self.cs_pin, 0)
  152. epdconfig.spi_writebyte([data])
  153. epdconfig.digital_write(self.cs_pin, 1)
  154. def ReadBusy(self):
  155. logging.debug("e-Paper busy")
  156. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  157. self.send_command(0x71)
  158. epdconfig.delay_ms(100)
  159. logging.debug("e-Paper busy release")
  160. def TurnOnDisplay(self):
  161. self.send_command(0x12)
  162. epdconfig.delay_ms(10)
  163. self.ReadBusy()
  164. def init(self):
  165. if (epdconfig.module_init() != 0):
  166. return -1
  167. # EPD hardware init start
  168. self.reset()
  169. self.send_command(0x01) # POWER SETTING
  170. self.send_data(0x03)
  171. self.send_data(0x00)
  172. self.send_data(0x2b)
  173. self.send_data(0x2b)
  174. self.send_data(0x03)
  175. self.send_command(0x06) # boost soft start
  176. self.send_data(0x17) # A
  177. self.send_data(0x17) # B
  178. self.send_data(0x17) # C
  179. self.send_command(0x04)
  180. self.ReadBusy()
  181. self.send_command(0x00) # panel setting
  182. self.send_data(0xbf) # LUT from OTP,128x296
  183. self.send_data(0x0d) # VCOM to 0V fast
  184. self.send_command(0x30) #PLL setting
  185. self.send_data(0x3a) # 3a 100HZ 29 150Hz 39 200HZ 31 171HZ
  186. self.send_command(0x61) # resolution setting
  187. self.send_data(self.width)
  188. self.send_data((self.height >> 8) & 0xff)
  189. self.send_data(self.height& 0xff)
  190. self.send_command(0x82) # vcom_DC setting
  191. self.send_data(0x28)
  192. return 0
  193. def SetFullReg(self):
  194. self.send_command(0x82)
  195. self.send_data(0x00)
  196. self.send_command(0X50)
  197. self.send_data(0x97)
  198. self.send_command(0x20) # vcom
  199. for count in range(0, 44):
  200. self.send_data(self.lut_vcomDC[count])
  201. self.send_command(0x21) # ww --
  202. for count in range(0, 42):
  203. self.send_data(self.lut_ww[count])
  204. self.send_command(0x22) # bw r
  205. for count in range(0, 42):
  206. self.send_data(self.lut_bw[count])
  207. self.send_command(0x23) # wb w
  208. for count in range(0, 42):
  209. self.send_data(self.lut_wb[count])
  210. self.send_command(0x24) # bb b
  211. for count in range(0, 42):
  212. self.send_data(self.lut_bb[count])
  213. def SetPartReg(self):
  214. self.send_command(0x82)
  215. self.send_data(0x03)
  216. self.send_command(0X50)
  217. self.send_data(0x47)
  218. self.send_command(0x20) # vcom
  219. for count in range(0, 44):
  220. self.send_data(self.lut_vcom1[count])
  221. self.send_command(0x21) # ww --
  222. for count in range(0, 42):
  223. self.send_data(self.lut_ww1[count])
  224. self.send_command(0x22) # bw r
  225. for count in range(0, 42):
  226. self.send_data(self.lut_bw1[count])
  227. self.send_command(0x23) # wb w
  228. for count in range(0, 42):
  229. self.send_data(self.lut_wb1[count])
  230. self.send_command(0x24) # bb b
  231. for count in range(0, 42):
  232. self.send_data(self.lut_bb1[count])
  233. def getbuffer(self, image):
  234. # logging.debug("bufsiz = ",int(self.width/8) * self.height)
  235. buf = [0xFF] * (int(self.width/8) * self.height)
  236. image_monocolor = image.convert('1')
  237. imwidth, imheight = image_monocolor.size
  238. pixels = image_monocolor.load()
  239. # logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  240. if(imwidth == self.width and imheight == self.height):
  241. logging.debug("Vertical")
  242. for y in range(imheight):
  243. for x in range(imwidth):
  244. # Set the bits for the column of pixels at the current position.
  245. if pixels[x, y] == 0:
  246. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  247. elif(imwidth == self.height and imheight == self.width):
  248. logging.debug("Horizontal")
  249. for y in range(imheight):
  250. for x in range(imwidth):
  251. newx = y
  252. newy = self.height - x - 1
  253. if pixels[x, y] == 0:
  254. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  255. return buf
  256. def display(self, image):
  257. self.send_command(0x10)
  258. for i in range(0, int(self.width * self.height / 8)):
  259. self.send_data(0x00)
  260. epdconfig.delay_ms(10)
  261. self.send_command(0x13)
  262. for i in range(0, int(self.width * self.height / 8)):
  263. self.send_data(image[i])
  264. epdconfig.delay_ms(10)
  265. self.SetFullReg()
  266. self.TurnOnDisplay()
  267. def DisplayPartial(self, image):
  268. self.SetPartReg()
  269. self.send_command(0x91)
  270. self.send_command(0x90)
  271. self.send_data(0)
  272. self.send_data(self.width - 1)
  273. self.send_data(0)
  274. self.send_data(0)
  275. self.send_data(int(self.height / 256))
  276. self.send_data(self.height % 256 - 1)
  277. self.send_data(0x28)
  278. self.send_command(0x10)
  279. for i in range(0, int(self.width * self.height / 8)):
  280. self.send_data(image[i])
  281. epdconfig.delay_ms(10)
  282. self.send_command(0x13)
  283. for i in range(0, int(self.width * self.height / 8)):
  284. self.send_data(~image[i])
  285. epdconfig.delay_ms(10)
  286. self.TurnOnDisplay()
  287. def Clear(self, color):
  288. self.send_command(0x10)
  289. for i in range(0, int(self.width * self.height / 8)):
  290. self.send_data(0x00)
  291. epdconfig.delay_ms(10)
  292. self.send_command(0x13)
  293. for i in range(0, int(self.width * self.height / 8)):
  294. self.send_data(0xFF)
  295. epdconfig.delay_ms(10)
  296. self.SetFullReg()
  297. self.TurnOnDisplay()
  298. def sleep(self):
  299. self.send_command(0X50)
  300. self.send_data(0xf7)
  301. self.send_command(0X02) #power off
  302. self.send_command(0X07) #deep sleep
  303. self.send_data(0xA5)
  304. epdconfig.delay_ms(2000)
  305. epdconfig.module_exit()
  306. ### END OF FILE ###