epd2in9d.py 12 KB

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