2
0

epd2in9d.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.1
  10. # * | Date : 2022-08-10
  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. from distutils.command.build_scripts import build_scripts
  32. import logging
  33. from . import epdconfig
  34. from PIL import Image
  35. import RPi.GPIO as GPIO
  36. # Display resolution
  37. EPD_WIDTH = 128
  38. EPD_HEIGHT = 296
  39. logger = logging.getLogger(__name__)
  40. class EPD:
  41. def __init__(self):
  42. self.reset_pin = epdconfig.RST_PIN
  43. self.dc_pin = epdconfig.DC_PIN
  44. self.busy_pin = epdconfig.BUSY_PIN
  45. self.cs_pin = epdconfig.CS_PIN
  46. self.width = EPD_WIDTH
  47. self.height = EPD_HEIGHT
  48. lut_vcom1 = [
  49. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  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, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00,
  57. ]
  58. lut_ww1 = [
  59. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. ]
  67. lut_bw1 = [
  68. 0x80, 0x19, 0x01, 0x00, 0x00, 0x01,
  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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. ]
  76. lut_wb1 = [
  77. 0x40, 0x19, 0x01, 0x00, 0x00, 0x01,
  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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84. ]
  85. lut_bb1 = [
  86. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  93. ]
  94. # Hardware reset
  95. def reset(self):
  96. epdconfig.digital_write(self.reset_pin, 1)
  97. epdconfig.delay_ms(20)
  98. epdconfig.digital_write(self.reset_pin, 0)
  99. epdconfig.delay_ms(5)
  100. epdconfig.digital_write(self.reset_pin, 1)
  101. epdconfig.delay_ms(20)
  102. epdconfig.digital_write(self.reset_pin, 0)
  103. epdconfig.delay_ms(5)
  104. epdconfig.digital_write(self.reset_pin, 1)
  105. epdconfig.delay_ms(20)
  106. epdconfig.digital_write(self.reset_pin, 0)
  107. epdconfig.delay_ms(5)
  108. epdconfig.digital_write(self.reset_pin, 1)
  109. epdconfig.delay_ms(20)
  110. def send_command(self, command):
  111. epdconfig.digital_write(self.dc_pin, 0)
  112. epdconfig.digital_write(self.cs_pin, 0)
  113. epdconfig.spi_writebyte([command])
  114. epdconfig.digital_write(self.cs_pin, 1)
  115. def send_data(self, data):
  116. epdconfig.digital_write(self.dc_pin, 1)
  117. epdconfig.digital_write(self.cs_pin, 0)
  118. epdconfig.spi_writebyte([data])
  119. epdconfig.digital_write(self.cs_pin, 1)
  120. # send a lot of data
  121. def send_data2(self, data):
  122. epdconfig.digital_write(self.dc_pin, 1)
  123. epdconfig.digital_write(self.cs_pin, 0)
  124. epdconfig.spi_writebyte2(data)
  125. epdconfig.digital_write(self.cs_pin, 1)
  126. def ReadBusy(self):
  127. logger.debug("e-Paper busy")
  128. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  129. self.send_command(0x71)
  130. epdconfig.delay_ms(10)
  131. logger.debug("e-Paper busy release")
  132. def TurnOnDisplay(self):
  133. self.send_command(0x12)
  134. epdconfig.delay_ms(10)
  135. self.ReadBusy()
  136. def init(self):
  137. if (epdconfig.module_init() != 0):
  138. return -1
  139. # EPD hardware init start
  140. self.reset()
  141. self.send_command(0x04)
  142. self.ReadBusy() #waiting for the electronic paper IC to release the idle signal
  143. self.send_command(0x00) #panel setting
  144. self.send_data(0x1f) # LUT from OTP,KW-BF KWR-AF BWROTP 0f BWOTP 1f
  145. self.send_command(0x61) #resolution setting
  146. self.send_data (0x80)
  147. self.send_data (0x01)
  148. self.send_data (0x28)
  149. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  150. self.send_data(0x97) #WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  151. return 0
  152. def SetPartReg(self):
  153. self.send_command(0x01) #POWER SETTING
  154. self.send_data(0x03)
  155. self.send_data(0x00)
  156. self.send_data(0x2b)
  157. self.send_data(0x2b)
  158. self.send_data(0x03)
  159. self.send_command(0x06) #boost soft start
  160. self.send_data(0x17) #A
  161. self.send_data(0x17) #B
  162. self.send_data(0x17) #C
  163. self.send_command(0x04)
  164. self.ReadBusy()
  165. self.send_command(0x00) #panel setting
  166. self.send_data(0xbf) #LUT from OTP,128x296
  167. self.send_command(0x30) #PLL setting
  168. self.send_data(0x3a) # 3a 100HZ 29 150Hz 39 200HZ 31 171HZ
  169. self.send_command(0x61) #resolution setting
  170. self.send_data(self.width)
  171. self.send_data((self.height >> 8) & 0xff)
  172. self.send_data(self.height & 0xff)
  173. self.send_command(0x82) #vcom_DC setting
  174. self.send_data(0x12)
  175. self.send_command(0X50)
  176. self.send_data(0x97)
  177. self.send_command(0x20) # vcom
  178. self.send_data2(self.lut_vcom1)
  179. self.send_command(0x21) # ww --
  180. self.send_data2(self.lut_ww1)
  181. self.send_command(0x22) # bw r
  182. self.send_data2(self.lut_bw1)
  183. self.send_command(0x23) # wb w
  184. self.send_data2(self.lut_wb1)
  185. self.send_command(0x24) # bb b
  186. self.send_data2(self.lut_bb1)
  187. def getbuffer(self, image):
  188. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  189. buf = [0xFF] * (int(self.width/8) * self.height)
  190. image_monocolor = image.convert('1')
  191. imwidth, imheight = image_monocolor.size
  192. pixels = image_monocolor.load()
  193. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  194. if(imwidth == self.width and imheight == self.height):
  195. logger.debug("Vertical")
  196. for y in range(imheight):
  197. for x in range(imwidth):
  198. # Set the bits for the column of pixels at the current position.
  199. if pixels[x, y] == 0:
  200. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  201. elif(imwidth == self.height and imheight == self.width):
  202. logger.debug("Horizontal")
  203. for y in range(imheight):
  204. for x in range(imwidth):
  205. newx = y
  206. newy = self.height - x - 1
  207. if pixels[x, y] == 0:
  208. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  209. return buf
  210. def display(self, image):
  211. self.send_command(0x10)
  212. self.send_data2([0x00] * int(self.width * self.height / 8))
  213. epdconfig.delay_ms(10)
  214. self.send_command(0x13)
  215. self.send_data2(image)
  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. buf = [0x00] * int(self.width * self.height / 8)
  230. for i in range(0, int(self.width * self.height / 8)):
  231. buf[i] = ~image[i]
  232. self.send_command(0x10)
  233. self.send_data2(image)
  234. epdconfig.delay_ms(10)
  235. self.send_command(0x13)
  236. self.send_data2(buf)
  237. epdconfig.delay_ms(10)
  238. self.TurnOnDisplay()
  239. def Clear(self):
  240. self.send_command(0x10)
  241. self.send_data2([0x00] * int(self.width * self.height / 8))
  242. epdconfig.delay_ms(10)
  243. self.send_command(0x13)
  244. self.send_data2([0xFF] * int(self.width * self.height / 8))
  245. epdconfig.delay_ms(10)
  246. self.TurnOnDisplay()
  247. def sleep(self):
  248. self.send_command(0X50)
  249. self.send_data(0xf7)
  250. self.send_command(0X02) #power off
  251. self.send_command(0X07) #deep sleep
  252. self.send_data(0xA5)
  253. epdconfig.delay_ms(2000)
  254. epdconfig.module_exit()
  255. ### END OF FILE ###