epd2in9d.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. # *****************************************************************************
  4. # * | File : epd2in9d.py
  5. # * | Author : Waveshare team
  6. # * | Function : Electronic paper driver
  7. # * | Info :
  8. # *----------------
  9. # * | This version: V2.0
  10. # * | Date : 2019-06-20
  11. # # | Info : python demo
  12. # -----------------------------------------------------------------------------
  13. # Permission is hereby granted, free of charge, to any person obtaining a copy
  14. # of this software and associated documnetation files (the "Software"), to deal
  15. # in the Software without restriction, including without limitation the rights
  16. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. # copies of the Software, and to permit persons to whom the Software is
  18. # furished to do so, subject to the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included in
  21. # all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. # THE SOFTWARE.
  30. #
  31. import logging
  32. from . import epdconfig
  33. from PIL import Image
  34. import RPi.GPIO as GPIO
  35. # Display resolution
  36. EPD_WIDTH = 128
  37. EPD_HEIGHT = 296
  38. logger = logging.getLogger(__name__)
  39. class EPD:
  40. def __init__(self):
  41. self.reset_pin = epdconfig.RST_PIN
  42. self.dc_pin = epdconfig.DC_PIN
  43. self.busy_pin = epdconfig.BUSY_PIN
  44. self.cs_pin = epdconfig.CS_PIN
  45. self.width = EPD_WIDTH
  46. self.height = EPD_HEIGHT
  47. lut_vcom1 = [
  48. 0x00, 0x19, 0x01, 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, 0x00, 0x00, 0x00, 0x00,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55. 0x00, 0x00,
  56. ]
  57. lut_ww1 = [
  58. 0x00, 0x19, 0x01, 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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. ]
  66. lut_bw1 = [
  67. 0x80, 0x19, 0x01, 0x00, 0x00, 0x01,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. ]
  75. lut_wb1 = [
  76. 0x40, 0x19, 0x01, 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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  82. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83. ]
  84. lut_bb1 = [
  85. 0x00, 0x19, 0x01, 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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  90. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  92. ]
  93. # Hardware reset
  94. def reset(self):
  95. epdconfig.digital_write(self.reset_pin, 1)
  96. epdconfig.delay_ms(20)
  97. epdconfig.digital_write(self.reset_pin, 0)
  98. epdconfig.delay_ms(5)
  99. epdconfig.digital_write(self.reset_pin, 1)
  100. epdconfig.delay_ms(20)
  101. epdconfig.digital_write(self.reset_pin, 0)
  102. epdconfig.delay_ms(5)
  103. epdconfig.digital_write(self.reset_pin, 1)
  104. epdconfig.delay_ms(20)
  105. epdconfig.digital_write(self.reset_pin, 0)
  106. epdconfig.delay_ms(5)
  107. epdconfig.digital_write(self.reset_pin, 1)
  108. epdconfig.delay_ms(20)
  109. def send_command(self, command):
  110. epdconfig.digital_write(self.dc_pin, 0)
  111. epdconfig.digital_write(self.cs_pin, 0)
  112. epdconfig.spi_writebyte([command])
  113. epdconfig.digital_write(self.cs_pin, 1)
  114. def send_data(self, data):
  115. epdconfig.digital_write(self.dc_pin, 1)
  116. epdconfig.digital_write(self.cs_pin, 0)
  117. epdconfig.spi_writebyte([data])
  118. epdconfig.digital_write(self.cs_pin, 1)
  119. def ReadBusy(self):
  120. logger.debug("e-Paper busy")
  121. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  122. self.send_command(0x71)
  123. epdconfig.delay_ms(10)
  124. logger.debug("e-Paper busy release")
  125. def TurnOnDisplay(self):
  126. self.send_command(0x12)
  127. epdconfig.delay_ms(10)
  128. self.ReadBusy()
  129. def init(self):
  130. if (epdconfig.module_init() != 0):
  131. return -1
  132. # EPD hardware init start
  133. self.reset()
  134. self.send_command(0x04)
  135. self.ReadBusy() #waiting for the electronic paper IC to release the idle signal
  136. self.send_command(0x00) #panel setting
  137. self.send_data(0x1f) # LUT from OTP,KW-BF KWR-AF BWROTP 0f BWOTP 1f
  138. self.send_command(0x61) #resolution setting
  139. self.send_data (0x80)
  140. self.send_data (0x01)
  141. self.send_data (0x28)
  142. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  143. self.send_data(0x97) #WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  144. return 0
  145. def SetPartReg(self):
  146. self.send_command(0x01) #POWER SETTING
  147. self.send_data(0x03)
  148. self.send_data(0x00)
  149. self.send_data(0x2b)
  150. self.send_data(0x2b)
  151. self.send_data(0x03)
  152. self.send_command(0x06) #boost soft start
  153. self.send_data(0x17) #A
  154. self.send_data(0x17) #B
  155. self.send_data(0x17) #C
  156. self.send_command(0x04)
  157. self.ReadBusy()
  158. self.send_command(0x00) #panel setting
  159. self.send_data(0xbf) #LUT from OTP,128x296
  160. self.send_command(0x30) #PLL setting
  161. self.send_data(0x3a) # 3a 100HZ 29 150Hz 39 200HZ 31 171HZ
  162. self.send_command(0x61) #resolution setting
  163. self.send_data(self.width)
  164. self.send_data((self.height >> 8) & 0xff)
  165. self.send_data(self.height & 0xff)
  166. self.send_command(0x82) #vcom_DC setting
  167. self.send_data(0x12)
  168. self.send_command(0X50)
  169. self.send_data(0x97)
  170. self.send_command(0x20) # vcom
  171. for count in range(0, 44):
  172. self.send_data(self.lut_vcom1[count])
  173. self.send_command(0x21) # ww --
  174. for count in range(0, 42):
  175. self.send_data(self.lut_ww1[count])
  176. self.send_command(0x22) # bw r
  177. for count in range(0, 42):
  178. self.send_data(self.lut_bw1[count])
  179. self.send_command(0x23) # wb w
  180. for count in range(0, 42):
  181. self.send_data(self.lut_wb1[count])
  182. self.send_command(0x24) # bb b
  183. for count in range(0, 42):
  184. self.send_data(self.lut_bb1[count])
  185. def getbuffer(self, image):
  186. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  187. buf = [0xFF] * (int(self.width/8) * self.height)
  188. image_monocolor = image.convert('1')
  189. imwidth, imheight = image_monocolor.size
  190. pixels = image_monocolor.load()
  191. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  192. if(imwidth == self.width and imheight == self.height):
  193. logger.debug("Vertical")
  194. for y in range(imheight):
  195. for x in range(imwidth):
  196. # Set the bits for the column of pixels at the current position.
  197. if pixels[x, y] == 0:
  198. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  199. elif(imwidth == self.height and imheight == self.width):
  200. logger.debug("Horizontal")
  201. for y in range(imheight):
  202. for x in range(imwidth):
  203. newx = y
  204. newy = self.height - x - 1
  205. if pixels[x, y] == 0:
  206. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  207. return buf
  208. def display(self, image):
  209. self.send_command(0x10)
  210. for i in range(0, int(self.width * self.height / 8)):
  211. self.send_data(0x00)
  212. epdconfig.delay_ms(10)
  213. self.send_command(0x13)
  214. for i in range(0, int(self.width * self.height / 8)):
  215. self.send_data(image[i])
  216. epdconfig.delay_ms(10)
  217. self.TurnOnDisplay()
  218. def DisplayPartial(self, image):
  219. self.SetPartReg()
  220. self.send_command(0x91)
  221. self.send_command(0x90)
  222. self.send_data(0)
  223. self.send_data(self.width - 1)
  224. self.send_data(0)
  225. self.send_data(0)
  226. self.send_data(int(self.height / 256))
  227. self.send_data(self.height % 256 - 1)
  228. self.send_data(0x28)
  229. self.send_command(0x10)
  230. for i in range(0, int(self.width * self.height / 8)):
  231. self.send_data(image[i])
  232. epdconfig.delay_ms(10)
  233. self.send_command(0x13)
  234. for i in range(0, int(self.width * self.height / 8)):
  235. self.send_data(~image[i])
  236. epdconfig.delay_ms(10)
  237. self.TurnOnDisplay()
  238. def Clear(self, color):
  239. self.send_command(0x10)
  240. for i in range(0, int(self.width * self.height / 8)):
  241. self.send_data(0x00)
  242. epdconfig.delay_ms(10)
  243. self.send_command(0x13)
  244. for i in range(0, int(self.width * self.height / 8)):
  245. self.send_data(0xFF)
  246. epdconfig.delay_ms(10)
  247. self.TurnOnDisplay()
  248. def sleep(self):
  249. self.send_command(0X50)
  250. self.send_data(0xf7)
  251. self.send_command(0X02) #power off
  252. self.send_command(0X07) #deep sleep
  253. self.send_data(0xA5)
  254. epdconfig.delay_ms(2000)
  255. epdconfig.module_exit()
  256. ### END OF FILE ###